acl.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import coreModule from 'app/core/core_module';
  3. import appEvents from 'app/core/app_events';
  4. import _ from 'lodash';
  5. export class AclCtrl {
  6. dashboard: any;
  7. items: DashboardAcl[];
  8. permissionOptions = [
  9. {value: 1, text: 'View'},
  10. {value: 2, text: 'Edit'},
  11. {value: 4, text: 'Admin'}
  12. ];
  13. aclTypes = [
  14. {value: 'Group', text: 'User Group'},
  15. {value: 'User', text: 'User'},
  16. {value: 'Viewer', text: 'Everyone With Viewer Role'},
  17. {value: 'Editor', text: 'Everyone With Editor Role'}
  18. ];
  19. dismiss: () => void;
  20. newType: string;
  21. canUpdate: boolean;
  22. /** @ngInject */
  23. constructor(private backendSrv, private dashboardSrv, private $sce, private $scope) {
  24. this.items = [];
  25. this.resetNewType();
  26. this.dashboard = dashboardSrv.getCurrent();
  27. this.get(this.dashboard.id);
  28. }
  29. resetNewType() {
  30. this.newType = 'Group';
  31. }
  32. get(dashboardId: number) {
  33. return this.backendSrv.get(`/api/dashboards/id/${dashboardId}/acl`)
  34. .then(result => {
  35. this.items = _.map(result, this.prepareViewModel.bind(this));
  36. this.sortItems();
  37. });
  38. }
  39. sortItems() {
  40. this.items = _.orderBy(this.items, ['sortRank', 'sortName'], ['desc', 'asc']);
  41. for (let i of this.items) {
  42. console.log(i.sortRank);
  43. }
  44. }
  45. prepareViewModel(item: DashboardAcl): DashboardAcl {
  46. item.inherited = this.dashboard.id !== item.dashboardId;
  47. item.sortRank = 0;
  48. if (item.userId > 0) {
  49. item.icon = "fa fa-fw fa-user";
  50. item.nameHtml = this.$sce.trustAsHtml(item.userLogin);
  51. item.sortName = item.userLogin;
  52. item.sortRank = 10;
  53. } else if (item.userGroupId > 0) {
  54. item.icon = "fa fa-fw fa-users";
  55. item.nameHtml = this.$sce.trustAsHtml(item.userGroup);
  56. item.sortName = item.userGroup;
  57. item.sortRank = 20;
  58. } else if (item.role) {
  59. item.icon = "fa fa-fw fa-street-view";
  60. item.nameHtml = this.$sce.trustAsHtml(`Everyone with <span class="query-keyword">${item.role}</span> Role`);
  61. item.sortName = item.role;
  62. item.sortRank = 30;
  63. if (item.role === 'Viewer') {
  64. item.sortRank += 2;
  65. }
  66. if (item.role === 'Viewer') {
  67. item.sortRank += 1;
  68. }
  69. }
  70. if (item.inherited) {
  71. item.sortRank += 100;
  72. }
  73. return item;
  74. }
  75. update() {
  76. var updated = [];
  77. for (let item of this.items) {
  78. if (item.inherited) {
  79. continue;
  80. }
  81. updated.push({
  82. id: item.id,
  83. userId: item.userId,
  84. userGroupId: item.userGroupId,
  85. role: item.role,
  86. permission: item.permission,
  87. });
  88. }
  89. return this.backendSrv.post(`/api/dashboards/id/${this.dashboard.id}/acl`, { items: updated }).then(() => {
  90. this.dismiss();
  91. });
  92. }
  93. typeChanged() {
  94. if (this.newType === 'Viewer' || this.newType === 'Editor') {
  95. this.items.push(this.prepareViewModel({
  96. permission: 1,
  97. role: this.newType
  98. }));
  99. this.canUpdate = true;
  100. this.resetNewType();
  101. }
  102. }
  103. permissionChanged() {
  104. this.canUpdate = true;
  105. }
  106. addNewItem(item) {
  107. item.dashboardId = this.dashboard.id;
  108. this.items.push(this.prepareViewModel(item));
  109. this.sortItems();
  110. this.canUpdate = true;
  111. }
  112. userPicked(user) {
  113. this.addNewItem({userId: user.id, userLogin: user.login, permission: 1,});
  114. this.$scope.$broadcast('user-picker-reset');
  115. }
  116. groupPicked(group) {
  117. this.addNewItem({userGroupId: group.id, userGroup: group.name, permission: 1});
  118. this.$scope.$broadcast('user-group-picker-reset');
  119. }
  120. removeItem(index) {
  121. this.items.splice(index, 1);
  122. this.canUpdate = true;
  123. }
  124. }
  125. export function dashAclModal() {
  126. return {
  127. restrict: 'E',
  128. templateUrl: 'public/app/features/dashboard/acl/acl.html',
  129. controller: AclCtrl,
  130. bindToController: true,
  131. controllerAs: 'ctrl',
  132. scope: {
  133. dismiss: "&"
  134. }
  135. };
  136. }
  137. export interface FormModel {
  138. dashboardId: number;
  139. userId?: number;
  140. userGroupId?: number;
  141. PermissionType: number;
  142. }
  143. export interface DashboardAcl {
  144. id?: number;
  145. dashboardId?: number;
  146. userId?: number;
  147. userLogin?: string;
  148. userEmail?: string;
  149. userGroupId?: number;
  150. userGroup?: string;
  151. permission?: number;
  152. permissionName?: string;
  153. role?: string;
  154. icon?: string;
  155. nameHtml?: string;
  156. inherited?: boolean;
  157. sortName?: string;
  158. sortRank?: number;
  159. }
  160. coreModule.directive('dashAclModal', dashAclModal);