roles and permissions
This commit is contained in:
parent
d77c3ca1a5
commit
9808100d81
21 changed files with 389 additions and 59 deletions
10
src/command/rolePermissionCommand.ts
Normal file
10
src/command/rolePermissionCommand.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { PermissionString } from "../type/permissionTypes";
|
||||
|
||||
export interface CreateRolePermissionCommand {
|
||||
permission: PermissionString;
|
||||
roleId: number;
|
||||
}
|
||||
|
||||
export interface DeleteRolePermissionCommand {
|
||||
id: number;
|
||||
}
|
48
src/command/rolePermissionCommandHandler.ts
Normal file
48
src/command/rolePermissionCommandHandler.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { rolePermission } from "../entity/role_permission";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import RoleService from "../service/roleService";
|
||||
import { CreateRolePermissionCommand, DeleteRolePermissionCommand } from "./rolePermissionCommand";
|
||||
|
||||
export default abstract class UserPermissionCommandHandler {
|
||||
/**
|
||||
* @description grant permission to user
|
||||
* @param CreateRolePermissionCommand
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async create(createPermission: CreateRolePermissionCommand): Promise<number> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(rolePermission)
|
||||
.values({
|
||||
permission: createPermission.permission,
|
||||
role: await RoleService.getById(createPermission.roleId),
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return result.identifiers[0].id;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed saving role permission");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description remove permission from role
|
||||
* @param DeleteRolePermissionCommand
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
static async deleteByToken(deletePermission: DeleteRolePermissionCommand): Promise<any> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(rolePermission)
|
||||
.where("permission.id = :id", { id: deletePermission.id })
|
||||
.execute()
|
||||
.then((res) => {})
|
||||
.catch((err) => {
|
||||
throw new InternalException("failed role permission removal");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
import { PermissionString } from "../type/permissionTypes";
|
||||
|
||||
export interface CreatePermissionCommand {
|
||||
export interface CreateUserPermissionCommand {
|
||||
permission: PermissionString;
|
||||
userId: number;
|
||||
}
|
||||
|
||||
export interface DeletePermissionCommand {
|
||||
export interface DeleteUserPermissionCommand {
|
||||
id: number;
|
||||
}
|
|
@ -1,20 +1,20 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { permission } from "../entity/permission";
|
||||
import { userPermission } from "../entity/user_permission";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import UserService from "../service/userService";
|
||||
import { CreatePermissionCommand, DeletePermissionCommand } from "./permissionCommand";
|
||||
import { CreateUserPermissionCommand, DeleteUserPermissionCommand } from "./userPermissionCommand";
|
||||
|
||||
export default abstract class PermissionCommandHandler {
|
||||
export default abstract class UserPermissionCommandHandler {
|
||||
/**
|
||||
* @description grant permission to user
|
||||
* @param CreatePermissionCommand
|
||||
* @param CreateUserPermissionCommand
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async create(createPermission: CreatePermissionCommand): Promise<number> {
|
||||
static async create(createPermission: CreateUserPermissionCommand): Promise<number> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(permission)
|
||||
.into(userPermission)
|
||||
.values({
|
||||
permission: createPermission.permission,
|
||||
user: await UserService.getById(createPermission.userId),
|
||||
|
@ -24,25 +24,25 @@ export default abstract class PermissionCommandHandler {
|
|||
return result.identifiers[0].id;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed saving permission");
|
||||
throw new InternalException("Failed saving user permission");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description remove permission to user
|
||||
* @param DeletePermissionCommand
|
||||
* @param DeleteUserPermissionCommand
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
static async deleteByToken(deletePermission: DeletePermissionCommand): Promise<any> {
|
||||
static async deleteByToken(deletePermission: DeleteUserPermissionCommand): Promise<any> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(permission)
|
||||
.from(userPermission)
|
||||
.where("permission.id = :id", { id: deletePermission.id })
|
||||
.execute()
|
||||
.then((res) => {})
|
||||
.catch((err) => {
|
||||
throw new InternalException("failed permission removal");
|
||||
throw new InternalException("failed user permission removal");
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue