refactor: the whole role and permission flow
This commit is contained in:
@@ -4,10 +4,9 @@ import { DataSource } from "typeorm";
|
||||
import { TokensService } from "./tokens.service";
|
||||
import { AuthMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { NotificationsService } from "../../notifications/providers/notifications.service";
|
||||
import { Role } from "../../users/entities/role.entity";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { RoleEnum } from "../../users/enums/role.enum";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { checkUserRole } from "../../utils/providers/checkRole.utils";
|
||||
import { OTPService } from "../../utils/providers/otp.service";
|
||||
import { PasswordService } from "../../utils/providers/password.service";
|
||||
import { SmsService } from "../../utils/providers/sms.service";
|
||||
@@ -109,8 +108,8 @@ export class AuthService {
|
||||
|
||||
const user = await this.checkUserLoginCredentialWithEmail(email, password);
|
||||
|
||||
const hasAccess = checkUserRole(user.roles, RoleEnum.ADMIN);
|
||||
if (!hasAccess) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
const isAdmin = this.checkUserIsAdmin(user.roles);
|
||||
if (!isAdmin) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
|
||||
const tokens = this.generateAccessAndRefreshToken(user);
|
||||
|
||||
@@ -139,8 +138,9 @@ export class AuthService {
|
||||
|
||||
//check the if the method call is from admin or not
|
||||
|
||||
const hasAccess = checkUserRole(user.roles, RoleEnum.ADMIN);
|
||||
if (isAdmin && !hasAccess) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
const isUserAdmin = this.checkUserIsAdmin(user.roles);
|
||||
|
||||
if (isAdmin && !isUserAdmin) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
|
||||
const existCode = await this.otpService.checkExistOtp(phone, "LOGIN");
|
||||
if (existCode) {
|
||||
@@ -186,9 +186,9 @@ export class AuthService {
|
||||
|
||||
const user = await this.checkUserLoginCredentialWithPhone(phone, code);
|
||||
|
||||
const hasAccess = checkUserRole(user.roles, RoleEnum.ADMIN);
|
||||
const isUserAdmin = this.checkUserIsAdmin(user.roles);
|
||||
|
||||
if (!hasAccess) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
if (!isUserAdmin) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
|
||||
const tokens = this.generateAccessAndRefreshToken(user);
|
||||
|
||||
@@ -245,8 +245,18 @@ export class AuthService {
|
||||
private generateAccessAndRefreshToken(user: User) {
|
||||
return this.tokensService.generateAccessAndRefreshToken({
|
||||
id: user.id,
|
||||
roles: user.roles.map((r) => r.name),
|
||||
isAdmin: user.roles.some((r) => r.isAdmin),
|
||||
permissions: user.roles.flatMap((r) => (r?.permissions?.length ? r.permissions.map((p) => p.name) : [])),
|
||||
});
|
||||
}
|
||||
//****************** */
|
||||
//****************** */
|
||||
// private checkUserPerm(user: User, perm: PermissionEnum) {
|
||||
// return user.roles.some((role) => role?.permissions?.some((p) => p.name === perm));
|
||||
// }
|
||||
//****************** */
|
||||
//****************** */
|
||||
private checkUserIsAdmin(roles: Role[]) {
|
||||
return roles.some((role) => role.isAdmin);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user