acl.ts 4.9 KB

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