Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role assign and unassign routes #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error.faileduserquery": "Failed to query users",
"error.missingfields": "Required values are missing from request"
}
64 changes: 64 additions & 0 deletions lib/UsersModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ class UsersModule extends AbstractApiModule {
handlers: { get: this.requestHandler(), put: this.requestHandler(), patch: this.requestHandler() },
permissions: { get: ['read:me'], put: ['write:me'], patch: ['write:me'] },
},
{
route: '/role/unassign',
modifiers: ['post'],
handlers: { post: this.unassignRole.bind(this) },
permissions: { post: ['write:users'] },
},
{
route: '/role/assign',
modifiers: ['post'],
handlers: { post: this.assignRole.bind(this) },
permissions: { post: ['write:users'] },
},
...this.routes.slice(1)
];
}
Expand Down Expand Up @@ -71,6 +83,58 @@ class UsersModule extends AbstractApiModule {
}
}
}

/**
* Handles removing a user role
* @param {ClientRequest} req
* @param {ServerResponse} res
* @param {Function} next
*/
async unassignRole(req, res, next) {
let dropRole = req.body.role;
let userId = req.body._id;
if (!dropRole || !userId) return res.status(500).send(this.app.lang.t('error.missingfields'));
this.find({_id: userId}, {})
.then((results) => {
if (!results) return res.status(500).send(this.app.lang.t('error.faileduserquery'));

let filteredRoles = results[0].roles.map(role => {
if (role.toString() !== dropRole) return role.toString();
}).filter(role => typeof role !== 'undefined');

this.update({ _id: userId }, { roles: filteredRoles }, {})
.then(results => {
return res.status(200).json(results);
});
})
.catch(next);
}

/**
* Handles adding a user role
* @param {ClientRequest} req
* @param {ServerResponse} res
* @param {Function} next
*/
async assignRole(req, res, next) {
let newRole = req.body.role;
let userId = req.body._id;
if (!newRole || !userId) return res.status(500).send(this.app.lang.t('error.missingfields'));
this.find({_id: userId}, {})
.then((results) => {
if (!results) return res.status(500).send(this.app.lang.t('error.faileduserquery'));

let newRoles = results[0].roles.map(role => {
return role.toString();
});
newRoles.push(newRole);
this.update({ _id: userId }, { roles: newRoles }, {})
.then(results => {
return res.status(200).json(results);
});
})
.catch(next);
}
}

export default UsersModule;