have function params in the correct order

This commit is contained in:
riley
2017-05-16 15:41:27 -06:00
parent c7e9fe3f5f
commit 31647fb2f0
7 changed files with 32 additions and 31 deletions
@@ -6,7 +6,7 @@ import {toggleModal as toggleShortcutModal} from '../actions/moderation';
import {fetchConfig} from '../actions/config';
import {FullLoading} from '../components/FullLoading';
import AdminLogin from '../components/AdminLogin';
import roleUtils from 'coral-framework/utils/roles';
import {can} from 'coral-framework/utils/roles';
class LayoutContainer extends Component {
componentWillMount () {
@@ -36,7 +36,7 @@ class LayoutContainer extends Component {
recaptchaPublic={TALK_RECAPTCHA_PUBLIC}
errorMessage={loginError} />;
}
if (roleUtils.canAccessAdmin(user) && loggedIn) {
if (can(user, 'ACCESS_ADMIN') && loggedIn) {
return <Layout handleLogout={handleLogout} toggleShortcutModal={toggleShortcutModal} {...this.props} />;
} else if (loggedIn) {
return <p>you do not have permission to see this page.</p>;
+12 -13
View File
@@ -1,30 +1,29 @@
import intersection from 'lodash/intersection';
const basicRoles = {
hasStaffTag: ['ADMIN', 'MODERATOR', 'STAFF']
HAS_STAFF_TAG: ['ADMIN', 'MODERATOR', 'STAFF']
};
const queryRoles = {
canAccessConfig: ['ADMIN', 'MODERATOR'],
canAccessAdmin: ['ADMIN', 'MODERATOR'],
canViewUserEmails: ['ADMIN']
UPDATE_CONFIG: ['ADMIN', 'MODERATOR'],
ACCESS_ADMIN: ['ADMIN', 'MODERATOR'],
VIEW_USER_EMAILS: ['ADMIN']
};
const mutationRoles = {
canChangeRoles: ['ADMIN'],
canModerateComments: ['ADMIN', 'MODERATOR']
CHANGE_ROLES: ['ADMIN'],
MODERATE_COMMENTS: ['ADMIN', 'MODERATOR']
};
const roles = {...basicRoles, ...queryRoles, ...mutationRoles};
export const can = (user, perms) => {
for (let perm in perms) {
export const can = (user, ...perms) => {
return perms.every(perm => {
const role = roles[perm];
if (typeof role === 'undefined') {
continue;
throw new Error(`${perm} is not a valid role`);
}
let grant = intersection(role, user.roles).length > 0;
return grant;
}
return false;
return intersection(role, user.roles).length > 0;
});
};
+1 -1
View File
@@ -190,7 +190,7 @@ UserSchema.method('verifyPassword', function(password) {
* operation.
*/
UserSchema.method('can', function(...actions) {
return can(this, null, actions);
return can(this, null, ...actions);
});
// Create the User model.
+12 -8
View File
@@ -3,19 +3,21 @@ const queries = require('./queryReducer');
const mutations = require('./mutationReducer');
const reducers = [
root.reducer,
queries.reducer,
mutations.reducer
root,
queries,
mutations
];
// this will make 'reducer' a key in this array. hm.
const allPermissions = [...Object.keys(root), ...Object.keys(queries), ...Object.keys(mutations)];
const findGrant = (user, perms, context, initialState) => {
const findGrant = (user, perms, context) => {
return perms.every(perm => {
for (let reducer in reducers) {
const grant = reducer(user, perm, context, initialState);
for (let key in reducers) {
const reducer = reducers[key];
const grant = reducer.checkRoles(user, perm, context);
if (grant !== null && typeof grant !== 'undefined') {
return grant;
@@ -38,12 +40,14 @@ module.exports = (user, context, ...perms) => {
// make sure all the passed permissions are not typos
const missingPerms = perms.filter(perm => {
return typeof allPermissions[perm] === 'undefined';
return allPermissions.indexOf(perm) === -1;
});
if (missingPerms.length) {
// not sure if this is working.
throw new Error(`${missingPerms.join(' ')} are not valid permissions.`);
}
return findGrant(user, perms, context, null);
return findGrant(user, perms, context);
};
+2 -2
View File
@@ -15,7 +15,7 @@ module.exports = {
REMOVE_COMMENT_TAG: 'REMOVE_COMMENT_TAG',
UPDATE_USER_ROLES: 'UPDATE_USER_ROLES',
UPDATE_CONFIG: 'UPDATE_CONFIG',
reducer: function (user, perm, context, initialState) {
checkRoles: function (user, perm, context) {
switch (perm) {
case this.CREATE_COMMENT:
return true;
@@ -40,7 +40,7 @@ module.exports = {
case this.UPDATE_CONFIG:
return check(user, ['ADMIN', 'MODERATOR']);
default:
return initialState;
break;
}
}
};
+2 -2
View File
@@ -10,7 +10,7 @@ module.exports = {
SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS: 'SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS',
SEARCH_OTHERS_COMMENTS: 'SEARCH_OTHERS_COMMENTS',
SEARCH_COMMENT_METRICS: 'SEARCH_COMMENT_METRICS',
reducer: function (perm, user, context, initialState) {
checkRoles: function (user, perm, context) {
switch (perm) {
case this.SEARCH_ASSETS:
return check(user, ['ADMIN', 'MODERATOR']);
@@ -25,7 +25,7 @@ module.exports = {
case this.SEARCH_COMMENT_METRICS:
return check(user, ['ADMIN', 'MODERATOR']);
default:
return initialState;
break;
}
}
};
+1 -3
View File
@@ -1,11 +1,9 @@
module.exports = {
reducer: function (perm, user, context, initialState) {
checkRoles: function (user, perm, context) {
// this runs before everything
if (user.status === 'BANNED') {
return false;
}
return initialState;
}
};