From 3f7ddddba3836a0c90df4c8aa9f627218ceae284 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 26 Oct 2017 14:27:46 -0600 Subject: [PATCH 001/362] perms cleanup --- perms/constants.js | 44 --------------- perms/constants/index.js | 11 ++++ perms/constants/mutation.js | 20 +++++++ perms/constants/query.js | 12 +++++ perms/constants/subscription.js | 10 ++++ perms/index.js | 53 ++++++++----------- perms/reducers/index.js | 20 +++++++ .../mutation.js} | 4 +- perms/{queryReducer.js => reducers/query.js} | 4 +- .../subscription.js} | 4 +- perms/rootReducer.js | 10 ---- perms/utils.js | 7 +++ 12 files changed, 108 insertions(+), 91 deletions(-) delete mode 100644 perms/constants.js create mode 100644 perms/constants/index.js create mode 100644 perms/constants/mutation.js create mode 100644 perms/constants/query.js create mode 100644 perms/constants/subscription.js create mode 100644 perms/reducers/index.js rename perms/{mutationReducer.js => reducers/mutation.js} (91%) rename perms/{queryReducer.js => reducers/query.js} (92%) rename perms/{subscriptionReducer.js => reducers/subscription.js} (91%) delete mode 100644 perms/rootReducer.js 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; }; From 76a255fb7bf819ac85d5b5b516da9d66456e4cbf Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 2 Nov 2017 17:16:57 -0600 Subject: [PATCH 002/362] initial pass at status support --- .nodemon.json | 8 +- bin/cli-users | 109 +--- circle.yml | 7 +- client/coral-admin/src/containers/Header.js | 2 +- .../routes/Community/containers/Community.js | 2 +- .../components/CloseCommentsInfo.js | 2 +- client/coral-settings/components/Bio.css | 21 - client/coral-settings/components/Bio.js | 18 - .../coral-settings/containers/BioContainer.js | 45 -- errors.js | 2 +- graph/mutators/action.js | 10 - graph/mutators/user.js | 118 +++- graph/resolvers/root_mutation.js | 15 +- graph/resolvers/user.js | 13 +- graph/setupFunctions.js | 6 +- graph/typeDefs.graphql | 194 ++++-- models/enum/user_status.js | 6 - models/enum/user_status_username.js | 25 + models/user.js | 141 +++- package.json | 61 +- perms/constants/mutation.js | 6 +- perms/reducers/index.js | 11 +- perms/reducers/mutation.js | 12 +- perms/reducers/query.js | 14 +- perms/reducers/subscription.js | 8 +- routes/api/account/index.js | 9 - routes/api/users/index.js | 80 +-- routes/index.js | 75 ++- scripts/e2e-ci.sh | 4 +- serve.js | 4 +- services/assets.js | 4 +- services/{domainlist.js => domain_list.js} | 16 +- services/users.js | 293 ++------- test/helpers/mongoose.js | 3 +- test/helpers/rejection.js | 3 + test/server/graph/mutations/addTag.js | 2 +- test/server/graph/mutations/createComment.js | 116 ++-- test/server/graph/mutations/removeTag.js | 2 +- .../graph/mutations/setUserBanStatus.js | 104 +++ .../mutations/setUserSuspensionStatus.js | 115 ++++ .../graph/mutations/setUserUsernameStatus.js | 87 +++ .../graph/mutations/updateAssetSettings.js | 3 +- test/server/graph/mutations/updateSettings.js | 3 +- test/server/routes/api/account/index.js | 57 -- test/server/routes/api/user/index.js | 49 -- .../{domainlist.js => domain_list.js} | 20 +- test/server/services/scraper.js | 22 - test/server/services/users.js | 169 ++--- yarn.lock | 614 ++++-------------- 49 files changed, 1112 insertions(+), 1598 deletions(-) delete mode 100644 client/coral-settings/components/Bio.css delete mode 100644 client/coral-settings/components/Bio.js delete mode 100644 client/coral-settings/containers/BioContainer.js delete mode 100644 models/enum/user_status.js create mode 100644 models/enum/user_status_username.js rename services/{domainlist.js => domain_list.js} (85%) create mode 100644 test/helpers/rejection.js create mode 100644 test/server/graph/mutations/setUserBanStatus.js create mode 100644 test/server/graph/mutations/setUserSuspensionStatus.js create mode 100644 test/server/graph/mutations/setUserUsernameStatus.js delete mode 100644 test/server/routes/api/account/index.js rename test/server/services/{domainlist.js => domain_list.js} (87%) delete mode 100644 test/server/services/scraper.js diff --git a/.nodemon.json b/.nodemon.json index 7f7fd3d59..36fed8700 100644 --- a/.nodemon.json +++ b/.nodemon.json @@ -1,5 +1,11 @@ { + "exec": "npm-run-all --parallel generate-introspection start:development", "verbose": true, "ignore": ["test/*", "client/*", "dist/*", "plugins/*/client"], - "ext": "js,json,graphql" + "ext": "js,json,graphql", + "watch": [ + ".", + "bin/cli", + "bin/cli-serve" + ] } diff --git a/bin/cli-users b/bin/cli-users index 89f53a67d..b82ee561d 100755 --- a/bin/cli-users +++ b/bin/cli-users @@ -106,20 +106,17 @@ async function createUser(options) { } const user = await UsersService.createLocalUser(answers.email.trim(), answers.password.trim(), answers.username.trim()); - console.log(`Created user ${user.id}.`); if (answers.roles.length > 0) { - return Promise.all(answers.roles.map((role) => { - return UsersService - .addRoleToUser(user.id, role) - .then(() => { - console.log(`Added the role ${role} to User ${user.id}.`); - }); - })); + for (const role of answers.roles) { + await UsersService.addRoleToUser(user.id, role); + } } - util.shutdown(); + await UsersService.sendEmailConfirmation(user, answers.email.trim()); + console.log(`Created User ${user.id}.`); + util.shutdown(); } catch (err) { console.error(err); util.shutdown(); @@ -241,12 +238,12 @@ function listUsers() { }); users.forEach((user) => { - let state = user.disabled ? 'Disabled' : 'Enabled'; const profile = user.profiles.find(({provider}) => provider === 'local'); + let state; if (profile && profile.metadata && profile.metadata.confirmed_at) { - state += ', Verified'; + state = 'Verified'; } else { - state += ', Unverified'; + state = 'Unverified'; } table.push([ @@ -336,74 +333,6 @@ function removeRole(userID, role) { }); } -/** - * Ban a user - * @param {String} userID id of the user to ban - */ -function ban(userID) { - UsersService - .setStatus(userID, 'BANNED') - .then(() => { - console.log(`Banned the User ${userID}.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); -} - -/** - * Unban a user - * @param {String} userUD id of the user to remove the role from - */ -function unban(userID) { - UsersService - .setStatus(userID, 'ACTIVE') - .then(() => { - console.log(`Unban the User ${userID}.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); -} - -/** - * Disable a given user. - * @param {String} userID the ID of a user to disable - */ -function disableUser(userID) { - UsersService - .disableUser(userID) - .then(() => { - console.log(`User ${userID} was disabled.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); -} - -/** - * Enabled a given user. - * @param {String} userID the ID of a user to enable - */ -function enableUser(userID) { - UsersService - .enableUser(userID) - .then(() => { - console.log(`User ${userID} was enabled.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); -} - /** * Verifies an email address for a user. * @@ -472,26 +401,6 @@ program .description('removes a role from a given user') .action(removeRole); -program - .command('ban ') - .description('ban a given user') - .action(ban); - -program - .command('uban ') - .description('unban a given user') - .action(unban); - -program - .command('disable ') - .description('disable a given user from logging in') - .action(disableUser); - -program - .command('enable ') - .description('enable a given user from logging in') - .action(enableUser); - program .command('verify ') .description('verifies the given user\'s email address') diff --git a/circle.yml b/circle.yml index 9f35736c6..ebb746669 100644 --- a/circle.yml +++ b/circle.yml @@ -7,6 +7,8 @@ machine: environment: PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin" NODE_ENV: "test" + MOCHA_FILE: "${CIRCLE_TEST_REPORTS}/junit/test-results.xml" + MOCHA_REPORTER: "mocha-junit-reporter" pre: # TODO: use the following to add in support for MongoDB 3.4. # # Upgrade the database version to 3.4. @@ -47,10 +49,11 @@ database: test: override: # Run the tests using the junit reporter. - - MOCHA_FILE=$CIRCLE_TEST_REPORTS/junit/test-results.xml MOCHA_REPORTER=mocha-junit-reporter yarn test + - yarn test + # Run the end to end tests + - yarn e2e:ci # Check dependancies using nsp. - nsp check - - yarn e2e-ci deployment: release: diff --git a/client/coral-admin/src/containers/Header.js b/client/coral-admin/src/containers/Header.js index a5c1f0afc..e2c3692c4 100644 --- a/client/coral-admin/src/containers/Header.js +++ b/client/coral-admin/src/containers/Header.js @@ -14,7 +14,7 @@ export default withQuery(gql` }) flaggedUsernamesCount: userCount(query: { action_type: FLAG, - statuses: [PENDING] + statuses: [SET, CHANGED] }) } `, { diff --git a/client/coral-admin/src/routes/Community/containers/Community.js b/client/coral-admin/src/routes/Community/containers/Community.js index 4cf9eeb8a..6cb5e700d 100644 --- a/client/coral-admin/src/routes/Community/containers/Community.js +++ b/client/coral-admin/src/routes/Community/containers/Community.js @@ -22,7 +22,7 @@ const withData = withQuery(gql` query TalkAdmin_Community { flaggedUsernamesCount: userCount(query: { action_type: FLAG, - statuses: [PENDING] + statuses: [SET, CHANGED] }) ...${getDefinitionName(FlaggedAccounts.fragments.root)} ...${getDefinitionName(FlaggedUser.fragments.root)} diff --git a/client/coral-configure/components/CloseCommentsInfo.js b/client/coral-configure/components/CloseCommentsInfo.js index d10419f3f..cc0e12a77 100644 --- a/client/coral-configure/components/CloseCommentsInfo.js +++ b/client/coral-configure/components/CloseCommentsInfo.js @@ -26,4 +26,4 @@ CloseCommentsInfo.propTypes = { onClick: PropTypes.func, }; -export default CloseCommentsInfo; \ No newline at end of file +export default CloseCommentsInfo; diff --git a/client/coral-settings/components/Bio.css b/client/coral-settings/components/Bio.css deleted file mode 100644 index 8c36b989d..000000000 --- a/client/coral-settings/components/Bio.css +++ /dev/null @@ -1,21 +0,0 @@ -.bio textarea { - width: 100%; - box-sizing: border-box; - border-radius: 2px; - min-height: 100px; - margin: 10px 0; - border: solid 1px #d8d8d8; -} - -.bio h1 { - font-size: 16px; - margin: 3px 0; -} - -.bio p { - margin: 3px 0; -} - -.actions { - text-align: right; -} diff --git a/client/coral-settings/components/Bio.js b/client/coral-settings/components/Bio.js deleted file mode 100644 index cd1347781..000000000 --- a/client/coral-settings/components/Bio.js +++ /dev/null @@ -1,18 +0,0 @@ -import React from 'react'; -import styles from './Bio.css'; -import {Button} from '../../coral-ui'; - -export default ({bio, handleSave, handleInput, handleCancel}) => ( -
-

Bio

-

Tell the community about yourself

-
-