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

#160555688 Super admin can update user roles #54

Merged
merged 3 commits into from
Sep 20, 2018

Conversation

CEOehis
Copy link
Contributor

@CEOehis CEOehis commented Sep 18, 2018

What does this PR do?

implement the ability for a super admin to assign and revoke admin roles

Description of Task to be completed?

Only a super admin has access to this endpoint in the application. The super admin can make users into "admins" and can revoke such access.

  • include relevant tests
  • add methods to handle user role update
  • update swagger doc

How should this be manually tested?

copy the contents of .env.sample to .env and provide the required values.
Run npm run start:dev
log in with a super-admin account
send a POST request to /api/admin/:userId/roles using the super-admin token in the Authorization header to make a user an admin.
send a DELETE request to /api/admin/:userId/roles using the super-admin token in the Authorization header to revoke the admin access

What are the relevant pivotal tracker stories?

#160555688

DROP TYPE "enum_Users_role";
ALTER TYPE "enum_Users_role_new" RENAME TO "enum_Users_role";
`
),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline before ')' function-paren-newline

),

down: queryInterface => queryInterface.sequelize
.query(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline after '(' function-paren-newline

`
ALTER TYPE "enum_Users_role" ADD VALUE IF NOT EXISTS 'superAdmin';
`
),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline before ')' function-paren-newline

*/
module.exports = {
up: queryInterface => queryInterface.sequelize
.query(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected newline after '(' function-paren-newline

@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch from 35ba878 to 8c30823 Compare September 18, 2018 09:18
@coveralls
Copy link

coveralls commented Sep 18, 2018

Pull Request Test Coverage Report for Build 495

  • 64 of 64 (100.0%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 93.412%

Totals Coverage Status
Change from base Build 487: 0.3%
Covered Lines: 2284
Relevant Lines: 2404

💛 - Coveralls

@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch from 8c30823 to 7ca39cf Compare September 18, 2018 09:26
@CEOehis CEOehis requested review from iamuchejude, emmaadesile, lauragift21 and aknwosu and removed request for lauragift21 September 18, 2018 09:35
@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch from 7ca39cf to a8d587e Compare September 18, 2018 11:26

// get authenticateUser method
const { authenticateUser, authorizeAdmin } = auth;
const { authenticateUser, authorizeAdmin, authorizeSuperAdmin } = auth;
const adminRoutes = require('express').Router();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work declaring the adminRoutes on one line but you can make it more es6.

import { Router } from 'express'
const adminRoutes = Router();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll fix that ASAP.

Copy link
Contributor

@iamuchejude iamuchejude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement fix on line 46 of adminHandleRoles.js in controller folder.
I also think some of your comments are not really necessary like you have on line 2 of server/migrations/20180916093338-user-add-super-admin-role.js.
Here is an article on the best practices for commenting your code to guide you: https://improvingsoftware.com/2011/06/27/5-best-practices-for-commenting-your-code/

}

/**
* assign a user an admin role
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misleading comment.
From what I see, the method is revoking admin role, not assigning admin role.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the resource. That was an oversight. I'll fix that ASAP.

@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch 2 times, most recently from e78d7f4 to 2659dea Compare September 18, 2018 17:15
Copy link
Contributor

@iamuchejude iamuchejude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's advised you describe you add a description of a function parameter in jsdoc for better readability of your code.
Example is:

@params {obj} res response object

Copy link
Contributor

@iamuchejude iamuchejude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's advised you describe you add a description of a function parameter in jsdoc for better readability of your code. You did not do that in lines 99-101 in middleware/auth.js
An example is:

@params {obj} res response object

const { userRole } = req;

if (!isSuperAdmin(userRole)) {
const error = new Error('you are not a Super Admin');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only a Super Admin can take this action

A super admin will be able to update user roles on the platform.
Thus a new role and authorization is necessary.
- add new role to users model
- add method to authorize super admin
- include relevant tests
Only a super admin has access to this endpoint in the application. The super
admin can make users into "admins" and can revoke such access.
- include relevant tests
- add methods to handle user role update
- update swagger doc

[delivers 160555688]
@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch from 2659dea to 70d275e Compare September 20, 2018 10:28
Enable admins to update users' profile as well as assign roles from within
the same method. Also added checks to limit assigning admin role to just
the super admin.
Refactored failing tests: used bulkcreate to create mock users.

- update relevant tests
@CEOehis CEOehis force-pushed the ft-super-admin-role-160555688 branch from 70d275e to e09e004 Compare September 20, 2018 10:56
@aknwosu aknwosu merged commit 675007f into develop Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants