permissions.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {Injectable} from '@angular/core';
  2. import {ICollection, ISchema, Resource, Service} from 'ngx-jsonapi';
  3. import {Base} from 'ngx-jsonapi/services/base';
  4. @Injectable()
  5. export class PermissionsService extends Service<Permission> {
  6. public resource = Permission;
  7. public type = 'permissions';
  8. public schema: ISchema = {
  9. relationships: {
  10. group: {
  11. hasMany: false
  12. }
  13. }
  14. };
  15. }
  16. export class Permission extends Resource {
  17. public attributes: {
  18. name: string;
  19. slug: string;
  20. };
  21. get name():string {
  22. return this.attributes.name;
  23. }
  24. set name(name: string) {
  25. this.attributes.name = name;
  26. }
  27. get slug():string {
  28. return this.attributes.slug;
  29. }
  30. set slug(slug: string) {
  31. this.attributes.slug = slug;
  32. }
  33. // in vase relationship group hast yek be yeke
  34. // ino beporse
  35. // get group(): PermissionGroup {
  36. // return <PermissionGroup>this.relationships.group.data;
  37. // }
  38. get group():Permission {
  39. return <Permission>this.relationships.group.data;
  40. }
  41. set group(group: Permission) {
  42. this.relationships.group.data = group;
  43. }
  44. // set group(group: PermissionGroup) {
  45. // this.relationships.group.data = group;
  46. // }
  47. // // todo ino beporse
  48. // set groupId(id: string) {
  49. // const permissionGroupService = new PermissionGroupsService();
  50. // const group = permissionGroupService.new();
  51. // group.id = id;
  52. // this.group = group
  53. // }
  54. // todo ino beporse
  55. set groupId(id: string) {
  56. const permissionService = new PermissionsService();
  57. const group = permissionService.new();
  58. group.id = id;
  59. this.group = group
  60. }
  61. }
  62. @Injectable()
  63. export class PermissionGroupsService extends Service<PermissionGroup> {
  64. public resource = PermissionGroup;
  65. public type = 'permission-groups';
  66. public schema: ISchema = {
  67. relationships: {
  68. permissions: {
  69. hasMany: true
  70. }
  71. }
  72. };
  73. }
  74. export class PermissionGroup extends Resource {
  75. public attributes: {
  76. name: string;
  77. slug: string;
  78. };
  79. get name():string {
  80. return this.attributes.name;
  81. }
  82. set name(name: string) {
  83. this.attributes.name = name;
  84. }
  85. get slug():string {
  86. return this.attributes.slug;
  87. }
  88. set slug(slug: string) {
  89. this.attributes.slug = slug;
  90. }
  91. // in vase relation pemisions hast ke chand be chand hast
  92. get permissions(): ICollection<Permission> {
  93. return <ICollection<Permission>>this.relationships.permissions.data;
  94. }
  95. set permissions(permissions: ICollection<Permission>) {
  96. this.relationships.permissions.data = permissions;
  97. }
  98. // in permision haro migire foreach mizane ideshon ro bar migardone
  99. get permissionsIds(): string[] {
  100. let ids: string[] = [];
  101. Base.forEach(this.permissions, (Permission: Permission) => {
  102. ids.push(Permission.id);
  103. });
  104. return ids;
  105. }
  106. // in baray new kardan ye permision hast
  107. set PermissionsIds(ids: string[]) {
  108. const selected_permissions: ICollection<Permission> = Base.newCollection<Permission>();
  109. for (const key in ids) {
  110. if (key in ids) {
  111. const permission_id = ids[key];
  112. const permissionsService = new PermissionsService();
  113. const permission = permissionsService.new();
  114. permission.id = permission_id;
  115. selected_permissions[permission_id] = permission;
  116. }
  117. this.permissions = selected_permissions
  118. }
  119. }
  120. }