role.service.mt.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { DataSource } from 'typeorm';
  2. import { RoleMt } from '../entities/role.entity';
  3. import { ConcreteCrudService } from '@d8d/shared-crud';
  4. export class RoleServiceMt extends ConcreteCrudService<RoleMt> {
  5. constructor(dataSource: DataSource) {
  6. super(RoleMt, {
  7. userTracking: {
  8. enabled: false
  9. },
  10. tenantOptions: {
  11. enabled: true,
  12. tenantIdField: 'tenantId',
  13. autoExtractFromContext: true
  14. }
  15. });
  16. }
  17. /**
  18. * 根据角色名获取角色(自动添加租户过滤)
  19. */
  20. async getRoleByName(name: string, tenantId?: number): Promise<RoleMt | null> {
  21. try {
  22. const where: any = { name };
  23. // 如果提供了租户ID,添加租户过滤
  24. if (tenantId !== undefined) {
  25. where.tenantId = tenantId;
  26. }
  27. const roles = await this.repository.find({ where });
  28. return roles.length > 0 ? roles[0] : null;
  29. } catch (error) {
  30. console.error('Error getting role by name:', error);
  31. throw new Error('Failed to get role by name');
  32. }
  33. }
  34. /**
  35. * 检查角色是否拥有指定权限
  36. */
  37. async hasPermission(roleId: number, permission: string, tenantId?: number): Promise<boolean> {
  38. try {
  39. const role = await this.getById(roleId, [], { tenantId });
  40. if (!role) return false;
  41. return role.permissions.includes(permission);
  42. } catch (error) {
  43. console.error('Error checking permission:', error);
  44. throw new Error('Failed to check permission');
  45. }
  46. }
  47. /**
  48. * 获取所有角色(自动添加租户过滤)
  49. */
  50. async getRoles(tenantId?: number): Promise<RoleMt[]> {
  51. try {
  52. const where: any = {};
  53. // 如果提供了租户ID,添加租户过滤
  54. if (tenantId !== undefined) {
  55. where.tenantId = tenantId;
  56. }
  57. return await this.repository.find({ where });
  58. } catch (error) {
  59. console.error('Error getting roles:', error);
  60. throw new Error(`Failed to get roles: ${error instanceof Error ? error.message : String(error)}`);
  61. }
  62. }
  63. /**
  64. * 根据ID获取角色(重写以添加租户过滤)
  65. */
  66. async getById(id: number, relations: string[] = [], options?: { tenantId?: number }): Promise<RoleMt | null> {
  67. try {
  68. const where: any = { id };
  69. // 如果提供了租户ID,添加租户过滤
  70. if (options?.tenantId !== undefined) {
  71. where.tenantId = options.tenantId;
  72. }
  73. return await this.repository.findOne({ where, relations });
  74. } catch (error) {
  75. console.error('Error getting role by id:', error);
  76. throw new Error('Failed to get role by id');
  77. }
  78. }
  79. /**
  80. * 更新角色(重写以添加租户过滤)
  81. */
  82. async update(id: number, data: Partial<RoleMt>, options?: { tenantId?: number }): Promise<RoleMt | null> {
  83. try {
  84. // 首先验证角色是否存在且属于指定租户
  85. const existingRole = await this.getById(id, [], options);
  86. if (!existingRole) return null;
  87. // 更新角色
  88. await this.repository.update(id, data);
  89. // 返回更新后的角色
  90. return await this.getById(id, [], options);
  91. } catch (error) {
  92. console.error('Error updating role:', error);
  93. throw new Error('Failed to update role');
  94. }
  95. }
  96. /**
  97. * 删除角色(重写以添加租户过滤)
  98. */
  99. async delete(id: number, options?: { tenantId?: number }): Promise<boolean> {
  100. try {
  101. // 首先验证角色是否存在且属于指定租户
  102. const existingRole = await this.getById(id, [], options);
  103. if (!existingRole) return false;
  104. // 删除角色
  105. const result = await this.repository.delete(id);
  106. return result.affected === 1;
  107. } catch (error) {
  108. console.error('Error deleting role:', error);
  109. throw new Error('Failed to delete role');
  110. }
  111. }
  112. }