user and role management
This commit is contained in:
parent
ab01fc2f76
commit
6286add31c
5 changed files with 57 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
|||
import { EntityManager } from "typeorm";
|
||||
import { DeleteResult, EntityManager, InsertResult } from "typeorm";
|
||||
import { dataSource } from "../data-source";
|
||||
import { rolePermission } from "../entity/role_permission";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
@ -11,6 +11,7 @@ import {
|
|||
import PermissionHelper from "../helpers/permissionHelper";
|
||||
import RolePermissionService from "../service/rolePermissionService";
|
||||
import { role } from "../entity/role";
|
||||
import { PermissionString } from "../type/permissionTypes";
|
||||
|
||||
export default abstract class RolePermissionCommandHandler {
|
||||
/**
|
||||
|
@ -41,16 +42,34 @@ export default abstract class RolePermissionCommandHandler {
|
|||
});
|
||||
}
|
||||
|
||||
private static async updatePermissionsAdd(manager: EntityManager, userId: number, permission: string): Promise<void> {
|
||||
return await manager.createQueryBuilder().relation(role, "permissions").of(userId).add(permission);
|
||||
private static async updatePermissionsAdd(
|
||||
manager: EntityManager,
|
||||
roleId: number,
|
||||
permission: PermissionString
|
||||
): Promise<InsertResult> {
|
||||
return await manager
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(rolePermission)
|
||||
.values({
|
||||
permission: permission,
|
||||
roleId: roleId,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
private static async updatePermissionsRemove(
|
||||
manager: EntityManager,
|
||||
userId: number,
|
||||
permission: string
|
||||
): Promise<void> {
|
||||
return await manager.createQueryBuilder().relation(role, "permissions").of(userId).remove(permission);
|
||||
roleId: number,
|
||||
permission: PermissionString
|
||||
): Promise<DeleteResult> {
|
||||
return await manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(rolePermission)
|
||||
.where("userId = :id", { id: roleId })
|
||||
.andWhere("permission = :permission", { permission: permission })
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -47,7 +47,7 @@ export default abstract class UserCommandHandler {
|
|||
lastname: updateUser.lastname,
|
||||
username: updateUser.username,
|
||||
})
|
||||
.where("id := id", { id: updateUser.id })
|
||||
.where("id = :id", { id: updateUser.id })
|
||||
.execute()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
|
@ -64,8 +64,8 @@ export default abstract class UserCommandHandler {
|
|||
let currentRoles = (await UserService.getAssignedRolesByUserId(updateUserRoles.id)).map((r) => r.id);
|
||||
return await dataSource.manager
|
||||
.transaction(async (manager) => {
|
||||
let newRoles = currentRoles.filter((r) => !updateUserRoles.roleIds.includes(r));
|
||||
let removeRoles = updateUserRoles.roleIds.filter((r) => !currentRoles.includes(r));
|
||||
let newRoles = updateUserRoles.roleIds.filter((r) => !currentRoles.includes(r));
|
||||
let removeRoles = currentRoles.filter((r) => !updateUserRoles.roleIds.includes(r));
|
||||
|
||||
for (let role of newRoles) {
|
||||
await this.updateRolesAdd(manager, updateUserRoles.id, role);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { EntityManager } from "typeorm";
|
||||
import { DeleteResult, EntityManager, InsertResult } from "typeorm";
|
||||
import { dataSource } from "../data-source";
|
||||
import { user } from "../entity/user";
|
||||
import { userPermission } from "../entity/user_permission";
|
||||
|
@ -10,6 +10,7 @@ import {
|
|||
} from "./userPermissionCommand";
|
||||
import UserPermissionService from "../service/userPermissionService";
|
||||
import PermissionHelper from "../helpers/permissionHelper";
|
||||
import { PermissionString } from "../type/permissionTypes";
|
||||
|
||||
export default abstract class UserPermissionCommandHandler {
|
||||
/**
|
||||
|
@ -36,20 +37,39 @@ export default abstract class UserPermissionCommandHandler {
|
|||
})
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
throw new InternalException("Failed saving user permissions");
|
||||
});
|
||||
}
|
||||
|
||||
private static async updatePermissionsAdd(manager: EntityManager, userId: number, permission: string): Promise<void> {
|
||||
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).add(permission);
|
||||
private static async updatePermissionsAdd(
|
||||
manager: EntityManager,
|
||||
userId: number,
|
||||
permission: PermissionString
|
||||
): Promise<InsertResult> {
|
||||
return await manager
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(userPermission)
|
||||
.values({
|
||||
permission: permission,
|
||||
userId: userId,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
private static async updatePermissionsRemove(
|
||||
manager: EntityManager,
|
||||
userId: number,
|
||||
permission: string
|
||||
): Promise<void> {
|
||||
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).remove(permission);
|
||||
permission: PermissionString
|
||||
): Promise<DeleteResult> {
|
||||
return await manager
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(userPermission)
|
||||
.where("userId = :id", { id: userId })
|
||||
.andWhere("permission = :permission", { permission: permission })
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,7 +78,7 @@ export class Initial1724317398939 implements MigrationInterface {
|
|||
columnNames: ["userId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "user",
|
||||
onDelete: "No Action",
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ export class Permissions1724661484664 implements MigrationInterface {
|
|||
columnNames: ["userId"],
|
||||
referencedColumnNames: ["id"],
|
||||
referencedTableName: "user",
|
||||
onDelete: "No Action",
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue