Skip to content

Commit

Permalink
feat(hooks): Better hook triggers informations
Browse files Browse the repository at this point in the history
  • Loading branch information
D34THWINGS committed Feb 15, 2019
1 parent c06305d commit a120374
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/apps/routes/deleteApp.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
method: 'DELETE',
path: '/apps/{app}',
config: {
pre: [hasScopeInList('apps:delete'), hasRoleInList(permissions.deleteApp)],
pre: [hasScopeInList(['apps:delete']), hasRoleInList(permissions.deleteApp)],
validate: {
params,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/apps/routes/getApp.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
method: 'GET',
path: '/apps/{app}',
config: {
pre: [hasScopeInList('apps:get')],
pre: [hasScopeInList(['apps:get'])],
validate: {
params,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/apps/routes/getApps.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
method: 'GET',
path: '/apps',
config: {
pre: [hasScopeInList('apps:get')],
pre: [hasScopeInList(['apps:get'])],
validate: {
query: Joi.object().keys({
name: Joi.string().max(50),
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/apps/routes/postApp.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
method: 'POST',
path: '/apps',
config: {
pre: [hasScopeInList('apps:create'), hasRoleInList(permissions.addApp)],
pre: [hasScopeInList(['apps:create']), hasRoleInList(permissions.addApp)],
validate: {
payload,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/apps/routes/putApp.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
path: '/apps/{app}',
config: {
pre: [
hasScopeInList('apps:modify'),
hasScopeInList(['apps:modify']),
hasRoleInList(permissions.editApp),
],
validate: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/hooks/routes/deleteHook.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
method: 'DELETE',
path: '/hooks/{hook}',
config: {
pre: [hasScopeInList('hooks:delete'), hasRoleInList(permissions.deleteHook)],
pre: [hasScopeInList(['hooks:delete']), hasRoleInList(permissions.deleteHook)],
validate: {
params,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/hooks/routes/getHook.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
method: 'GET',
path: '/hooks/{hook}',
config: {
pre: [hasScopeInList('hooks:get')],
pre: [hasScopeInList(['hooks:get'])],
validate: {
params,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/hooks/routes/getHooks.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
method: 'GET',
path: '/hooks',
config: {
pre: [hasScopeInList('hooks:get')],
pre: [hasScopeInList(['hooks:get'])],
validate: {
query: Joi.object().keys({
appId: Joi.string().required(),
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/hooks/routes/postHook.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
path: '/hooks',
method: 'POST',
config: {
pre: [hasScopeInList('hooks:create'), hasRoleInList(permissions.addHook)],
pre: [hasScopeInList(['hooks:create']), hasRoleInList(permissions.addHook)],
validate: {
payload: { ...payload, appId: Joi.string().required() },
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/hooks/routes/putHook.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
path: '/hooks/{hook}',
config: {
pre: [
hasScopeInList('hooks:modify'),
hasScopeInList(['hooks:modify']),
hasRoleInList(permissions.editHook),
],
validate: {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const isConnectedUser = {
failAction: 'ignore',
};

function hasScopeInList(...wantedScopes) {
function hasScopeInList(wantedScopes) {
return {
method(request) {
const { scopes } = request.auth.credentials;
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/users/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const userSchema = new mongoose.Schema({
tags: [String],
phone: { type: String, set: formatPhoneNumber },
job: String,
password: String,
password: { type: String, select: false },
roles: [String],
githubHandle: String,
trelloHandle: String,
Expand Down Expand Up @@ -57,7 +57,7 @@ userSchema.methods.comparePassword = function comparePassword(password) {
};

userSchema.statics.findOneByEmailAndPassword = function findOneByEmailAndPassword(email, password) {
return this.findOne({ email })
return this.findOne({ email }).select('+password')
.then((user) => {
if (user === null) throw Error('user_not_found');
return user.comparePassword(password)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/users/routes/cget-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
method: 'GET',
path: '/users',
config: {
pre: [hasScopeInList('users:get')],
pre: [hasScopeInList(['users:get'])],
validate: {
query: Joi.object().keys({
email: Joi.string().max(50),
Expand Down
11 changes: 6 additions & 5 deletions packages/api/src/users/routes/delete-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@ module.exports = {
method: 'DELETE',
path: '/users/{user}',
config: {
pre: [hasScopeInList('users:delete'), hasRoleInList(permissions.deleteUser)],
pre: [hasScopeInList(['users:delete']), hasRoleInList(permissions.deleteUser)],
validate: {
params,
},
},
async handler(req) {
const { User } = req.server.plugins.users.models;
const { n: deleted } = await User.remove({ _id: req.params.user });

if (!deleted) {
const user = await User.findById(req.params.user);
if (!user) {
throw Boom.notFound();
}

await User.remove({ _id: req.params.user });
await req.server.methods.cleanupUserAuth(req.params.user);

req.server.plugins.hooks.trigger(hooks.events.userDeleted, {
userId: req.params.user,
sender: _.omit(req.auth.credentials.user.toJSON(), ['password', 'thirdParty', 'needPasswordChange']),
user: _.omit(user.toJSON(), ['thirdParty', 'needPasswordChange']),
sender: _.omit(req.auth.credentials.user.toJSON(), ['thirdParty', 'needPasswordChange']),
});

return { deleted: true };
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/users/routes/get-me.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
method: 'GET',
path: '/users/me',
config: {
pre: [hasScopeInList('profile:get')],
pre: [hasScopeInList(['profile:get'])],
},
handler(req, res) {
return res.mongodb(req.auth.credentials.user, ['password', 'thirdParty', 'needPasswordChange']);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/users/routes/get-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
method: 'GET',
path: '/users/{user}',
config: {
pre: [hasScopeInList('users:get', 'profile:get')],
pre: [hasScopeInList(['users:get', 'profile:get'])],
validate: {
params,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/users/routes/post-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
method: 'POST',
path: '/users',
config: {
pre: [hasScopeInList('users:create'), hasRoleInList(permissions.addUser)],
pre: [hasScopeInList(['users:create']), hasRoleInList(permissions.addUser)],
validate: {
payload: payload.post,
},
Expand Down
42 changes: 23 additions & 19 deletions packages/api/src/users/routes/put-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const { permissions, hooks } = require('@lvconnect/config/server');

const { hasRoleInList, isConnectedUser, hasScopeInList } = require('../../middlewares');
const { payload, params } = require('./user-validation');
const filter = require('lodash/pickBy');

module.exports = {
method: 'PUT',
path: '/users/{user}',
config: {
pre: [
hasScopeInList('users:modify', 'profile:modify'),
hasScopeInList(['users:modify', 'profile:modify']),
isConnectedUser,
hasRoleInList(permissions.editUser, true),
],
Expand All @@ -23,8 +22,8 @@ module.exports = {
async handler(req, res) {
const { User } = req.server.plugins.users.models;
const { isConnectedUser: isSelf, hasRights, scopes } = req.pre;
const hasEditAnyUserScope = scopes.indexOf('users:modify') !== -1;
const hasEditSelfScope = scopes.indexOf('profile:modify') !== -1;
const hasEditAnyUserScope = scopes.indexOf('users:modify') >= 0;
const hasEditSelfScope = scopes.indexOf('profile:modify') >= 0;
const { githubOrgUserLink, trelloOrgUserLink } = req.server.plugins.tasks;

// Check can edit other users
Expand All @@ -42,30 +41,35 @@ module.exports = {
throw Boom.forbidden('insufficient_rights');
}

const updates = filter({
$set: filter(req.payload, value => value !== null),
$unset: filter(req.payload, value => value === null),
}, update => Object.keys(update).length);
const user = await User.findById(req.params.user);

const savedUser = await User
.findOneAndUpdate({ _id: req.params.user }, updates, { new: true, runSettersOnQuery: true });

if (!savedUser) {
if (!user) {
throw Boom.notFound('User Not Found');
}

if (savedUser.githubHandle && savedUser.thirdParty.github !== 'success') {
githubOrgUserLink({ user: savedUser });
const oldUser = user.toJSON();
Object.assign(user, req.payload);
Object.entries(req.payload).forEach(([key, value]) => {
if (value === null) {
user[key] = undefined;
}
});

await user.save();

if (user.githubHandle && user.thirdParty.github !== 'success') {
githubOrgUserLink({ user });
}
if (savedUser.trelloHandle && savedUser.thirdParty.trello !== 'success') {
trelloOrgUserLink({ user: savedUser });
if (user.trelloHandle && user.thirdParty.trello !== 'success') {
trelloOrgUserLink({ user });
}

req.server.plugins.hooks.trigger(hooks.events.userModified, {
user: _.omit(savedUser.toJSON(), ['password', 'thirdParty', 'needPasswordChange']),
sender: _.omit(req.auth.credentials.user.toJSON(), ['password', 'thirdParty', 'needPasswordChange']),
oldUser: _.omit(oldUser, ['thirdParty', 'needPasswordChange']),
user: _.omit(user.toJSON(), ['thirdParty', 'needPasswordChange']),
sender: _.omit(req.auth.credentials.user.toJSON(), ['thirdParty', 'needPasswordChange']),
});

return res.mongodb(savedUser, ['password', 'thirdParty', 'needPasswordChange']);
return res.mongodb(user, ['thirdParty', 'needPasswordChange']);
},
};
16 changes: 8 additions & 8 deletions packages/api/src/users/routes/user-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ const commonFields = {
lastName: Joi.string().min(2).required(),
roles: Joi.array().items(Joi.string().valid(validRoles)).min(1).required(),
city: Joi.string().valid(cities).required(),
description: Joi.string().max(255).empty(''),
phone: customJoi.string().phone(),
job: Joi.string(),
description: Joi.string().max(255).empty('').allow(null),
phone: customJoi.string().phone().allow(null),
job: Joi.string().allow(null),
tags: Joi.array().items(Joi.string()),
hiredAt: Joi.date().iso(),
leftAt: Joi.date().iso(),
birthDate: Joi.date().iso(),
registrationNumber: Joi.string(),
hiredAt: Joi.date().iso().allow(null),
leftAt: Joi.date().iso().allow(null),
birthDate: Joi.date().iso().allow(null),
registrationNumber: Joi.string().allow(null),
address: Joi.object().keys({
street: Joi.string(),
zipCode: Joi.string(),
city: Joi.string(),
}),
}).allow(null),
};

exports.payload = {
Expand Down
3 changes: 2 additions & 1 deletion packages/app/modules/auth/pages/login.page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const styles = theme => ({
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
height: `calc(100% + ${theme.spacing.unit * 6}px)`,
marginBottom: -theme.spacing.unit * 5,
height: `calc(100vh - ${theme.spacing.unit * 8}px)`,
boxSizing: 'border-box',
background: `url(${bgUrl}) no-repeat`,
backgroundSize: 'cover',
Expand Down

0 comments on commit a120374

Please sign in to comment.