Skip to content

Commit

Permalink
feat(general): moved permissions middlewares to another module
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 11, 2020
1 parent 1a9354d commit 813671e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const cron = require('./cron');

const middlewares = require('../middlewares/generic');
const fetch = require('../middlewares/fetch');
const myPermissions = require('../middlewares/my-permissions');
const campaigns = require('../middlewares/campaigns');
const register = require('../middlewares/register');
const login = require('../middlewares/login');
Expand Down Expand Up @@ -56,7 +57,7 @@ GeneralRouter.get('/bodies', bodies.listAllBodies);
// Not using this line here:
// GeneralRouter.use(middlewares.ensureAuthorized);
// because it'll also affect the middlewares for 404 and 500 errors.
GeneralRouter.get('/my_permissions', middlewares.ensureAuthorized, middlewares.getMyGlobalPermissions);
GeneralRouter.get('/my_permissions', middlewares.ensureAuthorized, myPermissions.getMyGlobalPermissions);
GeneralRouter.get('/members', middlewares.ensureAuthorized, members.listAllUsers);
GeneralRouter.post('/bodies', middlewares.ensureAuthorized, bodies.createBody);
GeneralRouter.get('/circles', middlewares.ensureAuthorized, circles.listAllCircles);
Expand Down
27 changes: 0 additions & 27 deletions middlewares/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,6 @@ exports.maybeAuthorize = async (req, res, next) => {

req.user = accessToken.user;

// Fetching permissions.
// 1) get the list of the circles user's in.
const directCircleMemberships = await CircleMembership.findAll({
where: { user_id: req.user.id }
});

// 2) get the list of all circles with only id and parent_circle_id
// and converting it to a map to not look over the whole
// array each time.
req.allCircles = await Circle.findAll({ fields: ['id', 'parent_circle_id'] });
req.allCirclesMap = _.keyBy(req.allCircles, 'id');

// 3) fetch all the permissions
const indirectCirclesArray = helpers.traverseIndirectCircles(req.allCirclesMap, directCircleMemberships.map((membership) => membership.circle_id));
req.permissions = await Permission.findAll({
where: {
'$circle_permissions.circle_id$': { [Sequelize.Op.in]: indirectCirclesArray },
scope: 'global'
},
include: [CirclePermission]
});

req.permissionsMap = _(req.permissions)
.map((elt) => [elt, 1])
.unzipWith()
.value();

return next();
};

Expand Down
41 changes: 41 additions & 0 deletions middlewares/my-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const _ = require('lodash');

const helpers = require('../lib/helpers');
const {
Circle,
CircleMembership,
CirclePermission,
Permission,
} = require('../models');
const { Sequelize } = require('../lib/sequelize');

exports.getMyGlobalPermissions = async (req, res, next) => {
// Fetching permissions.
// 1) get the list of the circles user's in.
const directCircleMemberships = await CircleMembership.findAll({
where: { user_id: req.user.id }
});

// 2) get the list of all circles with only id and parent_circle_id
// and converting it to a map to not look over the whole
// array each time.
req.allCircles = await Circle.findAll({ fields: ['id', 'parent_circle_id'] });
req.allCirclesMap = _.keyBy(req.allCircles, 'id');

// 3) fetch all the permissions
const indirectCirclesArray = helpers.traverseIndirectCircles(req.allCirclesMap, directCircleMemberships.map((membership) => membership.circle_id));
req.permissions = await Permission.findAll({
where: {
'$circle_permissions.circle_id$': { [Sequelize.Op.in]: indirectCirclesArray },
scope: 'global'
},
include: [CirclePermission]
});

req.permissionsMap = _(req.permissions)
.map((elt) => [elt, 1])
.unzipWith()
.value();

return next();
};

0 comments on commit 813671e

Please sign in to comment.