user and role management

This commit is contained in:
Julian Krauser 2024-09-02 15:57:13 +02:00
parent ab01fc2f76
commit 6286add31c
5 changed files with 57 additions and 18 deletions

View file

@ -1,4 +1,4 @@
import { EntityManager } from "typeorm"; import { DeleteResult, EntityManager, InsertResult } from "typeorm";
import { dataSource } from "../data-source"; import { dataSource } from "../data-source";
import { rolePermission } from "../entity/role_permission"; import { rolePermission } from "../entity/role_permission";
import InternalException from "../exceptions/internalException"; import InternalException from "../exceptions/internalException";
@ -11,6 +11,7 @@ import {
import PermissionHelper from "../helpers/permissionHelper"; import PermissionHelper from "../helpers/permissionHelper";
import RolePermissionService from "../service/rolePermissionService"; import RolePermissionService from "../service/rolePermissionService";
import { role } from "../entity/role"; import { role } from "../entity/role";
import { PermissionString } from "../type/permissionTypes";
export default abstract class RolePermissionCommandHandler { 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> { private static async updatePermissionsAdd(
return await manager.createQueryBuilder().relation(role, "permissions").of(userId).add(permission); 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( private static async updatePermissionsRemove(
manager: EntityManager, manager: EntityManager,
userId: number, roleId: number,
permission: string permission: PermissionString
): Promise<void> { ): Promise<DeleteResult> {
return await manager.createQueryBuilder().relation(role, "permissions").of(userId).remove(permission); return await manager
.createQueryBuilder()
.delete()
.from(rolePermission)
.where("userId = :id", { id: roleId })
.andWhere("permission = :permission", { permission: permission })
.execute();
} }
/** /**

View file

@ -47,7 +47,7 @@ export default abstract class UserCommandHandler {
lastname: updateUser.lastname, lastname: updateUser.lastname,
username: updateUser.username, username: updateUser.username,
}) })
.where("id := id", { id: updateUser.id }) .where("id = :id", { id: updateUser.id })
.execute() .execute()
.then(() => {}) .then(() => {})
.catch((err) => { .catch((err) => {
@ -64,8 +64,8 @@ export default abstract class UserCommandHandler {
let currentRoles = (await UserService.getAssignedRolesByUserId(updateUserRoles.id)).map((r) => r.id); let currentRoles = (await UserService.getAssignedRolesByUserId(updateUserRoles.id)).map((r) => r.id);
return await dataSource.manager return await dataSource.manager
.transaction(async (manager) => { .transaction(async (manager) => {
let newRoles = currentRoles.filter((r) => !updateUserRoles.roleIds.includes(r)); let newRoles = updateUserRoles.roleIds.filter((r) => !currentRoles.includes(r));
let removeRoles = updateUserRoles.roleIds.filter((r) => !currentRoles.includes(r)); let removeRoles = currentRoles.filter((r) => !updateUserRoles.roleIds.includes(r));
for (let role of newRoles) { for (let role of newRoles) {
await this.updateRolesAdd(manager, updateUserRoles.id, role); await this.updateRolesAdd(manager, updateUserRoles.id, role);

View file

@ -1,4 +1,4 @@
import { EntityManager } from "typeorm"; import { DeleteResult, EntityManager, InsertResult } from "typeorm";
import { dataSource } from "../data-source"; import { dataSource } from "../data-source";
import { user } from "../entity/user"; import { user } from "../entity/user";
import { userPermission } from "../entity/user_permission"; import { userPermission } from "../entity/user_permission";
@ -10,6 +10,7 @@ import {
} from "./userPermissionCommand"; } from "./userPermissionCommand";
import UserPermissionService from "../service/userPermissionService"; import UserPermissionService from "../service/userPermissionService";
import PermissionHelper from "../helpers/permissionHelper"; import PermissionHelper from "../helpers/permissionHelper";
import { PermissionString } from "../type/permissionTypes";
export default abstract class UserPermissionCommandHandler { export default abstract class UserPermissionCommandHandler {
/** /**
@ -36,20 +37,39 @@ export default abstract class UserPermissionCommandHandler {
}) })
.then(() => {}) .then(() => {})
.catch((err) => { .catch((err) => {
console.log(err);
throw new InternalException("Failed saving user permissions"); throw new InternalException("Failed saving user permissions");
}); });
} }
private static async updatePermissionsAdd(manager: EntityManager, userId: number, permission: string): Promise<void> { private static async updatePermissionsAdd(
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).add(permission); 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( private static async updatePermissionsRemove(
manager: EntityManager, manager: EntityManager,
userId: number, userId: number,
permission: string permission: PermissionString
): Promise<void> { ): Promise<DeleteResult> {
return await manager.createQueryBuilder().relation(user, "permissions").of(userId).remove(permission); return await manager
.createQueryBuilder()
.delete()
.from(userPermission)
.where("userId = :id", { id: userId })
.andWhere("permission = :permission", { permission: permission })
.execute();
} }
/** /**

View file

@ -78,7 +78,7 @@ export class Initial1724317398939 implements MigrationInterface {
columnNames: ["userId"], columnNames: ["userId"],
referencedColumnNames: ["id"], referencedColumnNames: ["id"],
referencedTableName: "user", referencedTableName: "user",
onDelete: "No Action", onDelete: "CASCADE",
}) })
); );
} }

View file

@ -32,7 +32,7 @@ export class Permissions1724661484664 implements MigrationInterface {
columnNames: ["userId"], columnNames: ["userId"],
referencedColumnNames: ["id"], referencedColumnNames: ["id"],
referencedTableName: "user", referencedTableName: "user",
onDelete: "No Action", onDelete: "CASCADE",
}) })
); );
} }