This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(userRoles): User Roles Settings (#24)
add roles field in the user database added internationalized messages seeded a default admin add the update roles controller and routes add the feature's tests and documentation [Finishes #170947557]
- Loading branch information
1 parent
b50ff29
commit 37278ec
Showing
21 changed files
with
337 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,43 @@ | ||
import db from '../models'; | ||
import Response from '../utils/ResponseHandler'; | ||
|
||
/** | ||
* @description User Controller | ||
* @class UserController | ||
*/ | ||
export default class UserController { | ||
/** | ||
* @description login method | ||
* @static | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} User | ||
* @memberof authController | ||
*/ | ||
static async setRoles(req, res) { | ||
try { | ||
const { email, role } = req.body; | ||
const admin = req.payload; | ||
const avaiableAdmins = await db.User.findAll({ | ||
where: { role: 'super administrator' } | ||
}); | ||
if (admin.role !== 'super administrator' || (avaiableAdmins.length === 2 && role === 'super administrator')) { | ||
return Response.errorResponse(res, 401, res.__('you are not authorised for this operation')); | ||
} | ||
const existingUser = await db.User.findOne({ | ||
where: { email } | ||
}); | ||
if (!existingUser) { | ||
return Response.errorResponse(res, 404, res.__('The user doesn\'t exist')); | ||
} | ||
if (existingUser.role === role) { | ||
return Response.errorResponse(res, 409, res.__('The user is already a %s', role)); | ||
} | ||
|
||
await db.User.update({ role }, { where: { email }, attributes: ['email', 'role'] }); | ||
return Response.success(res, 200, res.__('User roles updated successfully')); | ||
} catch (error) { | ||
return Response.errorResponse(res, 500, res.__(error.message)); | ||
} | ||
} | ||
} |
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
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,11 @@ | ||
import express from 'express'; | ||
import userController from '../controllers/userController'; | ||
import validationResult from '../validation/validationResult'; | ||
import { changeRoles } from '../validation/validationRules'; | ||
import { decode } from '../utils/tokenHandler'; | ||
|
||
const userRouter = express.Router(); | ||
|
||
userRouter.patch('/setRoles', decode, changeRoles, validationResult, userController.setRoles); | ||
|
||
export default userRouter; |
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
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,50 @@ | ||
/** | ||
* @swagger | ||
* definitions: | ||
* setRoles: | ||
* type: object | ||
* properties: | ||
* email: | ||
* type: string | ||
* format: email | ||
* role: | ||
* type: string | ||
* required: | ||
* - role | ||
*/ | ||
|
||
/** | ||
* @swagger | ||
* /api/v1/user/setRoles: | ||
* patch: | ||
* tags: | ||
* - User Roles | ||
* name: updateRoles | ||
* summary: updates the roles of a user | ||
* produces: | ||
* - application/json | ||
* consumes: | ||
* - application/json | ||
* parameters: | ||
* - name: body | ||
* in: body | ||
* schema: | ||
* $ref: '#/definitions/setRoles' | ||
* type: object | ||
* properties: | ||
* email: | ||
* type: string | ||
* role: | ||
* type: string | ||
* required: | ||
* - role | ||
* responses: | ||
* '200': | ||
* description: User role successfully updated. | ||
* '401': | ||
* description: Unauthorized. | ||
* '409': | ||
* description: User roles already set. | ||
* */ |
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
Oops, something went wrong.