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

Update guards.md && custom-decorators.md #3000

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

qd-xiaowang
Copy link

@qd-xiaowang qd-xiaowang commented Mar 29, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Docs
  • Other... Please describe:

What is the current behavior?

return matchRoles(roles, user.roles);
in guards.md

const user = request.user;
in custom-decorators.md

What is the new behavior?

By making this adjustment, Ability to call the matchRoles function.

change request.user to request.body

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I think it adds to the understanding

Because if you import directly, an error message will be displayed

error TS2304: Cannot find name 'matchRoles'.

When I was testing the decorator, the undefine firstname appeared

@qd-xiaowang qd-xiaowang changed the title add matchRoles in guards.md Add function matchRoles in guards.md Mar 29, 2024
@qd-xiaowang qd-xiaowang changed the title Add function matchRoles in guards.md Add matchRoles function in guards.md Mar 29, 2024
@qd-xiaowang qd-xiaowang changed the title Add matchRoles function in guards.md Update guards.md Mar 29, 2024
@qd-xiaowang qd-xiaowang changed the title Update guards.md Update guards.md && custom-decorators.md Mar 30, 2024
@@ -118,7 +118,7 @@ import { createParamDecorator, ExecutionContext } from '@nestjs/common';
export const User = createParamDecorator(
(data: string, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
const user = request.user;
const user = request.body;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would we change this decorator? This is not a decorator that NestJS comes with out of the box. This is a custom decorator that people can define themselves. You are free to create a different decorator to support your use-case.

private matchRoles(requiredRoles: string[], userRoles: string[]): boolean {
for (const role of requiredRoles) {
if (userRoles.includes(role)) {
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks incorrect. This would return true as soon as first role is found. Doesn't match all roles.

Copy link
Author

Choose a reason for hiding this comment

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

Learn a lot,thanks!

return this.matchRoles(roles, user.roles);
}

private matchRoles(requiredRoles: string[], userRoles: string[]): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look this code is formatted. Kindly fix the formatting and the logic as per my previous message.

@@ -202,8 +202,17 @@ export class RolesGuard implements CanActivate {
}
const request = context.switchToHttp().getRequest();
const user = request.user;
return matchRoles(roles, user.roles);
return this.matchRoles(roles, user.roles);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this. I don't see value in adding a private function here. Developers have there own logic to matchRoles.

I would rather just re-write this to:

return matchRoles(roles, user.roles); // Custom function to match roles

@qd-xiaowang
Copy link
Author

qd-xiaowang commented Apr 19, 2024 via email

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

2 participants