Skip to content

Conversation

@encodedBicoding
Copy link
Collaborator

@encodedBicoding encodedBicoding commented Jun 5, 2019

What does this PR do?

Implements role-based functionality.
This creates a new super admin, also creates a route that will enable the super admin assign role to other users.
This also creates a middle-ware which can be added to protect routes from unauthorized users

Description of Task to be completed?

N/A

How should this be manually tested?

N/A

Any background context you want to provide?

The middle-ware used to check if a user is an admin or a regular user before they can access a certain feature has been commented out. Please uncomment it only when you want to use it.

What are the relevant pivotal tracker stories?

#166265850

Screenshots (if appropriate)

Screenshot (60)

Questions:

if(!user) return util.errorStatus(res, 401, 'Not authorized');
if(user['role'] !== 'admin'){
return util.errorStatus(res, 401, 'Not authorized');
} else {

Choose a reason for hiding this comment

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

Unnecessary 'else' after 'return' no-else-return

@coveralls
Copy link

coveralls commented Jun 5, 2019

Coverage Status

Coverage decreased (-0.3%) to 90.669% when pulling 437f769 on feat/166265850/role-based-functionality into 3a7afc0 on develop.

@emp-daisy
Copy link
Owner

@encodedBicoding Fix all your hound issues and coverage

allowNull: true
},
role: {
type: DataTypes.ENUM('admin', 'user'),
Copy link
Owner

@emp-daisy emp-daisy Jun 5, 2019

Choose a reason for hiding this comment

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

I can see you defined two roles while you use three. Where was "super_admin" defined?

profilePic: null
},
{
firstName: process.env.SUPER_ADMIN_FIRSTNAME,
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this coming from the environment variable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To protect data?

Copy link
Owner

Choose a reason for hiding this comment

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

I doubt it matters for seeded users

}
}

// static async checkUserRole(req, res, next) {
Copy link
Owner

Choose a reason for hiding this comment

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

Remove unused comments

Copy link
Owner

@emp-daisy emp-daisy Jun 5, 2019

Choose a reason for hiding this comment

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

Please format your code well and also add a few line spaces to make them readable.

return util.successStatus(res, 200, 'Password reset successfully');
}
static async assignUserRole(req, res) {
const { id } = req.user ? req.user : req.query;
Copy link
Owner

Choose a reason for hiding this comment

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

Where is this coming from?

Copy link
Owner

Choose a reason for hiding this comment

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

User id should never come from a query in this case, the token used for the request is a better option to prevent using other users details to make changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, noted

if( !id ) { return util.errorStatus(res, 401, 'Not Authorized'); }
try {
const superAdmin = await models.Users.findByPk(id);
if(!superAdmin || superAdmin.role !== 'super_admin') { return util.errorStatus(res, 401, 'Not Authorized'); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the Authorization should be done in a separate file, to avoid repetition.

Copy link
Owner

Choose a reason for hiding this comment

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

+1

@encodedBicoding encodedBicoding force-pushed the feat/166265850/role-based-functionality branch from 148006c to 4ac70aa Compare June 6, 2019 18:58
.set('Authorization', 'bearer kjjodndsfj94mkfdsif0dfdsfmosj')
.send({email: 'john.doe@test.com', role: 'admin'})
.end((err, res) => {
console.log(res.body)

Choose a reason for hiding this comment

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

Unexpected console statement no-console

.set('Authorization', `bearer ${superAdminToken}`)
.send({email: 'john.doe@test.com', role: 'admin'})
.end((err, res) => {
console.log(res.body)

Choose a reason for hiding this comment

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

Unexpected console statement no-console

@encodedBicoding encodedBicoding force-pushed the feat/166265850/role-based-functionality branch from 4ac70aa to 9d327da Compare June 7, 2019 06:59
const token = req.headers.authorization.split(' ')[1];
let token = req.headers.authorization;
if(token.startsWith('bearer ')) {
token = req.headers.authorization.split(' ')[1]

Choose a reason for hiding this comment

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

Use array destructuring prefer-destructuring

}

static async assignUserRole(req, res) {
let token = req.headers.authorization ? req.headers.authorization : req.query.token;
Copy link
Owner

Choose a reason for hiding this comment

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

A middleware already exist for this

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1


const token = req.headers.authorization.split(' ')[1];
let token = req.headers.authorization;
if(token.startsWith('bearer ')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@encodedBicoding please what is the reason for this line?

Copy link
Collaborator Author

@encodedBicoding encodedBicoding Jun 7, 2019

Choose a reason for hiding this comment

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

The token is a string that starts with bearer, or doesn't. The way u implemented it before, it just splits the main token, but that line checks first if there is bearer before the main token before splitting

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, I think that the if block is not necessary since the split token still gets verified in the try block that follows.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I understand what @encodedBicoding is trying to do. He's making a provision to just insert the token without appending the word Bearer. I think it's a good idea to take away the stress of always appending that bearer word but I don't know if it's standard. Maybe I'll look it up after demo today.

}

static async assignUserRole(req, res) {
let token = req.headers.authorization ? req.headers.authorization : req.query.token;
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

@encodedBicoding encodedBicoding force-pushed the feat/166265850/role-based-functionality branch from 9d327da to 133b7a3 Compare June 7, 2019 13:11
const { email_confirm_code, email } = user;


if (email_confirm_code === null) return util.errorStatus(res, 403, 'Email already verified');

Choose a reason for hiding this comment

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

Identifier 'email_confirm_code' is not in camel case camelcase

},
role: {
type: DataTypes.STRING,
allowNull: false
Copy link
Owner

Choose a reason for hiding this comment

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

You can also set a default value so it doesn't have to stated everything

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

true, default value should be user. But it is also set during signup. I just thought to leave it like that.

tags:
- Users
summary: Assign role to users
description: Authenticate by user role before they can perform certain actions
Copy link
Owner

Choose a reason for hiding this comment

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

Something doesn't seem right here... Since it is a secured route, I expected to see the required security added

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will sort that after DEMO today.

@encodedBicoding encodedBicoding force-pushed the feat/166265850/role-based-functionality branch from 133b7a3 to f4a0595 Compare June 7, 2019 16:44
emp-daisy
emp-daisy previously approved these changes Jun 10, 2019
@emp-daisy emp-daisy merged commit 055aa37 into develop Jun 10, 2019
@emp-daisy emp-daisy deleted the feat/166265850/role-based-functionality branch June 10, 2019 10:13
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.

8 participants