mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-29 22:01:49 +03:00
c36e749820
refs https://github.com/TryGhost/Team/issues/581 closes https://github.com/TryGhost/Team/issues/582 Emails can now be sent to members with specific associated labels or products by specifying an NQL string. We want to bring the same members segment feature to content by allowing `visibility` to be an NQL filter string on top of the `public/members/paid` special-case strings. As an example it's possible to set `posts.visibility` to `label:vip` to make a post available only to those members with the `vip` label. - removed enum validations for `visibility` so it now accepts any string or `null` - bumped `@tryghost/admin-api-schema` for API-level validation changes - added nql validation to API input validators by running the visibility query against the members model - added transform of NQL to special-case visibility values when saving post model - ensures there's a single way of representing "members" and "paid" where NQL gives multiple ways of representing the same segment - useful for keeping theme-level checks such as `{{#has visibility="paid"}}` working as expected - updated content-gating to parse nql from post's visibility and use it to query the currently logged in member to see if there's a match - bumped @tryghost/members-api to include label and product data when loading member
57 lines
1.2 KiB
JavaScript
57 lines
1.2 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;
|
|
}
|
|
|
|
const visibility = post.visibility === 'paid' ? 'status:-free' : post.visibility;
|
|
|
|
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
|
|
};
|