mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
3ccd3601b3
refs https://github.com/TryGhost/Team/issues/1071 We switched to using tiers pivot table that stores list of tiers with access to post when visibility is set to `tiers`. For themes using v3 API while having posts restricted to specific tiers visibility, the post data will not include the list of tiers, which caused an unexpected error while trying to determine post access from tiers list. This change blocks access to post if specific tiers visibility is enabled without data available for list of tiers on post.
65 lines
1.4 KiB
JavaScript
65 lines
1.4 KiB
JavaScript
const nql = require('@nexes/nql');
|
|
|
|
// @ts-check
|
|
/** @typedef { boolean } AccessFlag */
|
|
|
|
const PERMIT_ACCESS = true;
|
|
const BLOCK_ACCESS = false;
|
|
|
|
// TODO: better place to store this?
|
|
const MEMBER_NQL_EXPANSIONS = [{
|
|
key: 'labels',
|
|
replacement: 'labels.slug'
|
|
}, {
|
|
key: 'label',
|
|
replacement: 'labels.slug'
|
|
}, {
|
|
key: 'products',
|
|
replacement: 'products.slug'
|
|
}, {
|
|
key: 'product',
|
|
replacement: 'products.slug'
|
|
}];
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
|
|
let visibility = post.visibility === 'paid' ? 'status:-free' : post.visibility;
|
|
if (visibility === 'tiers') {
|
|
if (!post.tiers) {
|
|
return BLOCK_ACCESS;
|
|
}
|
|
visibility = post.tiers.map((product) => {
|
|
return `product:${product.slug}`;
|
|
}).join(',');
|
|
}
|
|
|
|
if (visibility && member.status && nql(visibility, {expansions: MEMBER_NQL_EXPANSIONS}).queryJSON(member)) {
|
|
return PERMIT_ACCESS;
|
|
}
|
|
|
|
return BLOCK_ACCESS;
|
|
}
|
|
|
|
module.exports = {
|
|
checkPostAccess,
|
|
PERMIT_ACCESS,
|
|
BLOCK_ACCESS
|
|
};
|