Ghost/core/server/services/members/content-gating.js
Fabien 'egg' O'Carroll 18b87d9734
Included all subscriptions in stripeSubscriptions (#12414)
refs https://github.com/TryGhost/Ghost/issues/12256

We no longer want to filter out cancelled subscriptions, so we are able
to remove the whereIn clause of the relation.

* Fixed paid flag on member
* Fixed content gating for members

Now that the subscriptions for a member include all of them, we must
explicitly check that the member has an active subscription in order to
consider them "paid"
2020-11-25 10:39:07 +00:00

43 lines
1.0 KiB
JavaScript

// @ts-check
/** @typedef { boolean } AccessFlag */
const PERMIT_ACCESS = true;
const BLOCK_ACCESS = false;
/**
* @param {object} post - A post object to check access to
* @param {object} member - The member whos access should be checked
*
* @returns {AccessFlag}
*/
function checkPostAccess(post, member) {
if (post.visibility === 'public') {
return PERMIT_ACCESS;
}
if (!member) {
return BLOCK_ACCESS;
}
if (post.visibility === 'members') {
return PERMIT_ACCESS;
}
const activeSubscriptions = member.stripe && member.stripe.subscriptions && member.stripe.subscriptions.filter((subscription) => {
return ['active', 'trialing', 'unpaid', 'past_due'].includes(subscription.status);
});
const memberHasPlan = activeSubscriptions && activeSubscriptions.length;
if (post.visibility === 'paid' && memberHasPlan) {
return PERMIT_ACCESS;
}
return BLOCK_ACCESS;
}
module.exports = {
checkPostAccess,
PERMIT_ACCESS,
BLOCK_ACCESS
};