roles and permissions
This commit is contained in:
parent
d77c3ca1a5
commit
9808100d81
21 changed files with 389 additions and 59 deletions
44
src/service/rolePermissionService.ts
Normal file
44
src/service/rolePermissionService.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { rolePermission } from "../entity/role_permission";
|
||||
import { userPermission } from "../entity/user_permission";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
||||
export default abstract class RolePermissionService {
|
||||
/**
|
||||
* @description get permission by role
|
||||
* @param roleId number
|
||||
* @returns {Promise<Array<rolePermission>>}
|
||||
*/
|
||||
static async getByRole(roleId: number): Promise<Array<rolePermission>> {
|
||||
return await dataSource
|
||||
.getRepository(rolePermission)
|
||||
.createQueryBuilder("permission")
|
||||
.where("permission.roleId = :roleId", { roleId: roleId })
|
||||
.getMany()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("permissions not found by role");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get permission by roles
|
||||
* @param roleIds Array<number>
|
||||
* @returns {Promise<Array<rolePermission>>}
|
||||
*/
|
||||
static async getByRoles(roleIds: Array<number>): Promise<Array<rolePermission>> {
|
||||
return await dataSource
|
||||
.getRepository(rolePermission)
|
||||
.createQueryBuilder("permission")
|
||||
.where("permission.roleId IN (:...roleIds)", { roleIds: roleIds })
|
||||
.getMany()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("permissions not found by roles");
|
||||
});
|
||||
}
|
||||
}
|
24
src/service/roleService.ts
Normal file
24
src/service/roleService.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { role } from "../entity/role";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
||||
export default abstract class RoleService {
|
||||
/**
|
||||
* @description get role by id
|
||||
* @param id number
|
||||
* @returns {Promise<role>}
|
||||
*/
|
||||
static async getById(id: number): Promise<role> {
|
||||
return await dataSource
|
||||
.getRepository(role)
|
||||
.createQueryBuilder("role")
|
||||
.where("role.id = :id", { id: id })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("role not found by id");
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { permission } from "../entity/permission";
|
||||
import { userPermission } from "../entity/user_permission";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
||||
export default abstract class PermissionService {
|
||||
export default abstract class UserPermissionService {
|
||||
/**
|
||||
* @description get permission by user
|
||||
* @param user number
|
||||
* @returns {Promise<Array<permission>>}
|
||||
* @param userId number
|
||||
* @returns {Promise<Array<userPermission>>}
|
||||
*/
|
||||
static async getByUser(userId: number): Promise<Array<permission>> {
|
||||
static async getByUser(userId: number): Promise<Array<userPermission>> {
|
||||
return await dataSource
|
||||
.getRepository(permission)
|
||||
.getRepository(userPermission)
|
||||
.createQueryBuilder("permission")
|
||||
.where("permission.userId = :userId", { userId: userId })
|
||||
.getMany()
|
|
@ -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");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue