-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general): moved permissions middlewares to another module
- Loading branch information
1 parent
1a9354d
commit 813671e
Showing
3 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |