roles and permissions

This commit is contained in:
Julian Krauser 2024-08-27 17:54:59 +02:00
parent d77c3ca1a5
commit 9808100d81
21 changed files with 389 additions and 59 deletions

View file

@ -1,4 +1,5 @@
import { dataSource } from "../data-source";
import { role } from "../entity/role";
import { user } from "../entity/user";
import InternalException from "../exceptions/internalException";
@ -81,4 +82,24 @@ export default abstract class UserService {
throw new InternalException("could not count users");
});
}
/**
* @description get roles assigned to user
* @param userId number
* @returns {Promise<Array<role>>}
*/
static async getAssignedRolesByUserId(userId: number): Promise<Array<role>> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.leftJoinAndSelect("user.roles", "roles")
.where("user.id = :id", { id: userId })
.getOneOrFail()
.then((res) => {
return res.roles;
})
.catch((err) => {
throw new InternalException("could not get roles for user");
});
}
}