diff --git a/perms/constants.js b/perms/constants.js deleted file mode 100644 index 4c85f9c26..000000000 --- a/perms/constants.js +++ /dev/null @@ -1,44 +0,0 @@ -module.exports = { - - // mutations - CREATE_COMMENT: 'CREATE_COMMENT', - CREATE_ACTION: 'CREATE_ACTION', - DELETE_ACTION: 'DELETE_ACTION', - EDIT_NAME: 'EDIT_NAME', - EDIT_COMMENT: 'EDIT_COMMENT', - REJECT_USERNAME: 'REJECT_USERNAME', - SET_USER_STATUS: 'SET_USER_STATUS', - SUSPEND_USER: 'SUSPEND_USER', - SET_COMMENT_STATUS: 'SET_COMMENT_STATUS', - ADD_COMMENT_TAG: 'ADD_COMMENT_TAG', - REMOVE_COMMENT_TAG: 'REMOVE_COMMENT_TAG', - UPDATE_USER_ROLES: 'UPDATE_USER_ROLES', - UPDATE_CONFIG: 'UPDATE_CONFIG', - CREATE_TOKEN: 'CREATE_TOKEN', - REVOKE_TOKEN: 'REVOKE_TOKEN', - UPDATE_ASSET_SETTINGS: 'UPDATE_ASSET_SETTINGS', - UPDATE_ASSET_STATUS: 'UPDATE_ASSET_STATUS', - UPDATE_SETTINGS: 'UPDATE_SETTINGS', - - // queries - SEARCH_ASSETS: 'SEARCH_ASSETS', - SEARCH_OTHER_USERS: 'SEARCH_OTHER_USERS', - SEARCH_ACTIONS: 'SEARCH_ACTIONS', - SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS: 'SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS', - SEARCH_OTHERS_COMMENTS: 'SEARCH_OTHERS_COMMENTS', - SEARCH_COMMENT_METRICS: 'SEARCH_COMMENT_METRICS', - LIST_OWN_TOKENS: 'LIST_OWN_TOKENS', - SEARCH_COMMENT_STATUS_HISTORY: 'SEARCH_COMMENT_STATUS_HISTORY', - VIEW_SUSPENSION_INFO: 'VIEW_SUSPENSION_INFO', - VIEW_PROTECTED_SETTINGS: 'VIEW_PROTECTED_SETTINGS', - - // subscriptions - SUBSCRIBE_COMMENT_ACCEPTED: 'SUBSCRIBE_COMMENT_ACCEPTED', - SUBSCRIBE_COMMENT_REJECTED: 'SUBSCRIBE_COMMENT_REJECTED', - SUBSCRIBE_COMMENT_FLAGGED: 'SUBSCRIBE_COMMENT_FLAGGED', - SUBSCRIBE_ALL_COMMENT_ADDED: 'SUBSCRIBE_ALL_COMMENT_ADDED', - SUBSCRIBE_ALL_COMMENT_EDITED: 'SUBSCRIBE_ALL_COMMENT_EDITED', - SUBSCRIBE_ALL_USER_SUSPENDED: 'SUBSCRIBE_ALL_USER_SUSPENDED', - SUBSCRIBE_ALL_USER_BANNED: 'SUBSCRIBE_ALL_USER_BANNED', - SUBSCRIBE_ALL_USERNAME_REJECTED: 'SUBSCRIBE_ALL_USERNAME_REJECTED', -}; diff --git a/perms/constants/index.js b/perms/constants/index.js new file mode 100644 index 000000000..8f43f8330 --- /dev/null +++ b/perms/constants/index.js @@ -0,0 +1,11 @@ +const merge = require('lodash/merge'); + +const mutation = require('./mutation'); +const query = require('./query'); +const subscription = require('./subscription'); + +module.exports = merge(...[ + mutation, + query, + subscription, +]); diff --git a/perms/constants/mutation.js b/perms/constants/mutation.js new file mode 100644 index 000000000..ce48681f0 --- /dev/null +++ b/perms/constants/mutation.js @@ -0,0 +1,20 @@ +module.exports = { + CREATE_COMMENT: 'CREATE_COMMENT', + CREATE_ACTION: 'CREATE_ACTION', + DELETE_ACTION: 'DELETE_ACTION', + EDIT_NAME: 'EDIT_NAME', + EDIT_COMMENT: 'EDIT_COMMENT', + REJECT_USERNAME: 'REJECT_USERNAME', + SET_USER_STATUS: 'SET_USER_STATUS', + SUSPEND_USER: 'SUSPEND_USER', + SET_COMMENT_STATUS: 'SET_COMMENT_STATUS', + ADD_COMMENT_TAG: 'ADD_COMMENT_TAG', + REMOVE_COMMENT_TAG: 'REMOVE_COMMENT_TAG', + UPDATE_USER_ROLES: 'UPDATE_USER_ROLES', + UPDATE_CONFIG: 'UPDATE_CONFIG', + CREATE_TOKEN: 'CREATE_TOKEN', + REVOKE_TOKEN: 'REVOKE_TOKEN', + UPDATE_ASSET_SETTINGS: 'UPDATE_ASSET_SETTINGS', + UPDATE_ASSET_STATUS: 'UPDATE_ASSET_STATUS', + UPDATE_SETTINGS: 'UPDATE_SETTINGS' +}; diff --git a/perms/constants/query.js b/perms/constants/query.js new file mode 100644 index 000000000..780cc77a0 --- /dev/null +++ b/perms/constants/query.js @@ -0,0 +1,12 @@ +module.exports = { + SEARCH_ASSETS: 'SEARCH_ASSETS', + SEARCH_OTHER_USERS: 'SEARCH_OTHER_USERS', + SEARCH_ACTIONS: 'SEARCH_ACTIONS', + SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS: 'SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS', + SEARCH_OTHERS_COMMENTS: 'SEARCH_OTHERS_COMMENTS', + SEARCH_COMMENT_METRICS: 'SEARCH_COMMENT_METRICS', + LIST_OWN_TOKENS: 'LIST_OWN_TOKENS', + SEARCH_COMMENT_STATUS_HISTORY: 'SEARCH_COMMENT_STATUS_HISTORY', + VIEW_SUSPENSION_INFO: 'VIEW_SUSPENSION_INFO', + VIEW_PROTECTED_SETTINGS: 'VIEW_PROTECTED_SETTINGS', +}; diff --git a/perms/constants/subscription.js b/perms/constants/subscription.js new file mode 100644 index 000000000..4b0e56571 --- /dev/null +++ b/perms/constants/subscription.js @@ -0,0 +1,10 @@ +module.exports = { + SUBSCRIBE_COMMENT_ACCEPTED: 'SUBSCRIBE_COMMENT_ACCEPTED', + SUBSCRIBE_COMMENT_REJECTED: 'SUBSCRIBE_COMMENT_REJECTED', + SUBSCRIBE_COMMENT_FLAGGED: 'SUBSCRIBE_COMMENT_FLAGGED', + SUBSCRIBE_ALL_COMMENT_ADDED: 'SUBSCRIBE_ALL_COMMENT_ADDED', + SUBSCRIBE_ALL_COMMENT_EDITED: 'SUBSCRIBE_ALL_COMMENT_EDITED', + SUBSCRIBE_ALL_USER_SUSPENDED: 'SUBSCRIBE_ALL_USER_SUSPENDED', + SUBSCRIBE_ALL_USER_BANNED: 'SUBSCRIBE_ALL_USER_BANNED', + SUBSCRIBE_ALL_USERNAME_REJECTED: 'SUBSCRIBE_ALL_USERNAME_REJECTED', +}; diff --git a/perms/index.js b/perms/index.js index a6b459efe..2964248ed 100644 --- a/perms/index.js +++ b/perms/index.js @@ -1,35 +1,28 @@ const constants = require('./constants'); -const root = require('./rootReducer'); -const queries = require('./queryReducer'); -const mutations = require('./mutationReducer'); -const subscriptions = require('./subscriptionReducer'); +const reducers = require('./reducers'); +const constantsArray = Object.keys(constants); -const reducers = [ - root, - queries, - mutations, - subscriptions, -]; +/** + * findGrant will try to check all the permissions if the user is allowed to do + * so. + * + * @param {Object} user the user being checked whether they have the required + * permissions + * @param {Array} perms the array of permissions that the user must have + * in order to succeed + */ +const findGrant = (user, perms) => perms.every((perm) => { + for (let key in reducers) { + const reducer = reducers[key]; + const grant = reducer(user, perm); -// this will make 'reducer' a key in this array. hm. -const allPermissions = Object.keys(constants); - -const findGrant = (user, perms) => { - - return perms.every((perm) => { - - for (let key in reducers) { - const reducer = reducers[key]; - const grant = reducer(user, perm); - - if (grant !== null && typeof grant !== 'undefined') { - return grant; - } + if (typeof grant !== 'undefined' && grant !== null) { + return grant; } + } - return false; - }); -}; + return false; +}); /** * returns true, false, or null depending on whether the user has those permissions @@ -40,10 +33,8 @@ const findGrant = (user, perms) => { * @return {Boolean} */ module.exports = (user, ...perms) => { - - // Make sure all the passed permissions are not typos. - const missingPerms = perms.filter((perm) => !allPermissions.includes(perm)); - if (missingPerms.length > 0) { + if (perms.some((perm) => !constantsArray.includes(perm))) { + const missingPerms = perms.filter((perm) => !constantsArray.includes(perm)); throw new Error(`${missingPerms.join(' ')} are not valid permissions.`); } diff --git a/perms/reducers/index.js b/perms/reducers/index.js new file mode 100644 index 000000000..312a79008 --- /dev/null +++ b/perms/reducers/index.js @@ -0,0 +1,20 @@ +const mutation = require('./mutation'); +const query = require('./query'); +const subscription = require('./subscription'); + +module.exports = [ + (user /* , perm*/) => { + + // this runs before everything + if ( + user.status === 'BANNED' || + (user.suspension.until && user.suspension.until > new Date()) + ) { + return false; + } + }, + query, + mutation, + subscription, +] +; \ No newline at end of file diff --git a/perms/mutationReducer.js b/perms/reducers/mutation.js similarity index 91% rename from perms/mutationReducer.js rename to perms/reducers/mutation.js index c44035372..9621ffe00 100644 --- a/perms/mutationReducer.js +++ b/perms/reducers/mutation.js @@ -1,5 +1,5 @@ -const {check} = require('./utils'); -const types = require('./constants'); +const {check} = require('../utils'); +const types = require('../constants'); module.exports = (user, perm) => { switch (perm) { diff --git a/perms/queryReducer.js b/perms/reducers/query.js similarity index 92% rename from perms/queryReducer.js rename to perms/reducers/query.js index b71c3d964..43ae3c05f 100644 --- a/perms/queryReducer.js +++ b/perms/reducers/query.js @@ -1,5 +1,5 @@ -const {check} = require('./utils'); -const types = require('./constants'); +const {check} = require('../utils'); +const types = require('../constants'); module.exports = (user, perm) => { switch (perm) { diff --git a/perms/subscriptionReducer.js b/perms/reducers/subscription.js similarity index 91% rename from perms/subscriptionReducer.js rename to perms/reducers/subscription.js index e0f73526a..9ebde3000 100644 --- a/perms/subscriptionReducer.js +++ b/perms/reducers/subscription.js @@ -1,5 +1,5 @@ -const {check} = require('./utils'); -const types = require('./constants'); +const {check} = require('../utils'); +const types = require('../constants'); module.exports = (user, perm) => { switch (perm) { diff --git a/perms/rootReducer.js b/perms/rootReducer.js deleted file mode 100644 index 7fb665654..000000000 --- a/perms/rootReducer.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = (user /* , perm*/) => { - - // this runs before everything - if ( - user.status === 'BANNED' || - (user.suspension.until && user.suspension.until > new Date()) - ) { - return false; - } -}; diff --git a/perms/utils.js b/perms/utils.js index e72a49c53..aced52a0e 100644 --- a/perms/utils.js +++ b/perms/utils.js @@ -1,4 +1,11 @@ const intersection = require('lodash/intersection'); + +/** + * check will ensure that the user has the desired roles. + * + * @param {Object} user user being checked for roles + * @param {Array} roles roles to check that the user has + */ const check = (user, roles) => { return intersection(roles, user.roles).length > 0; };