diff --git a/src/command/rolePermissionCommandHandler.ts b/src/command/rolePermissionCommandHandler.ts index 63c803a..c047a83 100644 --- a/src/command/rolePermissionCommandHandler.ts +++ b/src/command/rolePermissionCommandHandler.ts @@ -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 { - return await manager.createQueryBuilder().relation(role, "permissions").of(userId).add(permission); + private static async updatePermissionsAdd( + manager: EntityManager, + roleId: number, + permission: PermissionString + ): Promise { + return await manager + .createQueryBuilder() + .insert() + .into(rolePermission) + .values({ + permission: permission, + roleId: roleId, + }) + .execute(); } private static async updatePermissionsRemove( manager: EntityManager, - userId: number, - permission: string - ): Promise { - return await manager.createQueryBuilder().relation(role, "permissions").of(userId).remove(permission); + roleId: number, + permission: PermissionString + ): Promise { + return await manager + .createQueryBuilder() + .delete() + .from(rolePermission) + .where("userId = :id", { id: roleId }) + .andWhere("permission = :permission", { permission: permission }) + .execute(); } /** diff --git a/src/command/userCommandHandler.ts b/src/command/userCommandHandler.ts index 4eda082..58f51d2 100644 --- a/src/command/userCommandHandler.ts +++ b/src/command/userCommandHandler.ts @@ -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); diff --git a/src/command/userPermissionCommandHandler.ts b/src/command/userPermissionCommandHandler.ts index e2379e2..ca676e1 100644 --- a/src/command/userPermissionCommandHandler.ts +++ b/src/command/userPermissionCommandHandler.ts @@ -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 { - return await manager.createQueryBuilder().relation(user, "permissions").of(userId).add(permission); + private static async updatePermissionsAdd( + manager: EntityManager, + userId: number, + permission: PermissionString + ): Promise { + return await manager + .createQueryBuilder() + .insert() + .into(userPermission) + .values({ + permission: permission, + userId: userId, + }) + .execute(); } private static async updatePermissionsRemove( manager: EntityManager, userId: number, - permission: string - ): Promise { - return await manager.createQueryBuilder().relation(user, "permissions").of(userId).remove(permission); + permission: PermissionString + ): Promise { + return await manager + .createQueryBuilder() + .delete() + .from(userPermission) + .where("userId = :id", { id: userId }) + .andWhere("permission = :permission", { permission: permission }) + .execute(); } /** diff --git a/src/migrations/1724317398939-initial.ts b/src/migrations/1724317398939-initial.ts index 695cd7a..d26bf43 100644 --- a/src/migrations/1724317398939-initial.ts +++ b/src/migrations/1724317398939-initial.ts @@ -78,7 +78,7 @@ export class Initial1724317398939 implements MigrationInterface { columnNames: ["userId"], referencedColumnNames: ["id"], referencedTableName: "user", - onDelete: "No Action", + onDelete: "CASCADE", }) ); } diff --git a/src/migrations/1724661484664-permissions.ts b/src/migrations/1724661484664-permissions.ts index a54686f..fe90b9f 100644 --- a/src/migrations/1724661484664-permissions.ts +++ b/src/migrations/1724661484664-permissions.ts @@ -32,7 +32,7 @@ export class Permissions1724661484664 implements MigrationInterface { columnNames: ["userId"], referencedColumnNames: ["id"], referencedTableName: "user", - onDelete: "No Action", + onDelete: "CASCADE", }) ); }