acl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. }
  42. prepareViewModel(item: DashboardAcl): DashboardAcl {
  43. item.inherited = this.dashboard.id !== item.dashboardId;
  44. item.sortRank = 0;
  45. if (item.userId > 0) {
  46. item.icon = "fa fa-fw fa-user";
  47. item.nameHtml = this.$sce.trustAsHtml(item.userLogin);
  48. item.sortName = item.userLogin;
  49. item.sortRank = 10;
  50. } else if (item.userGroupId > 0) {
  51. item.icon = "fa fa-fw fa-users";
  52. item.nameHtml = this.$sce.trustAsHtml(item.userGroup);
  53. item.sortName = item.userGroup;
  54. item.sortRank = 20;
  55. } else if (item.role) {
  56. item.icon = "fa fa-fw fa-street-view";
  57. item.nameHtml = this.$sce.trustAsHtml(`Everyone with <span class="query-keyword">${item.role}</span> Role`);
  58. item.sortName = item.role;
  59. item.sortRank = 30;
  60. if (item.role === 'Viewer') {
  61. item.sortRank += 1;
  62. }
  63. }
  64. if (item.inherited) {
  65. item.sortRank += 100;
  66. }
  67. return item;
  68. }
  69. update() {
  70. var updated = [];
  71. for (let item of this.items) {
  72. if (item.inherited) {
  73. continue;
  74. }
  75. updated.push({
  76. id: item.id,
  77. userId: item.userId,
  78. userGroupId: item.userGroupId,
  79. role: item.role,
  80. permission: item.permission,
  81. });
  82. }
  83. return this.backendSrv.post(`/api/dashboards/id/${this.dashboard.id}/acl`, { items: updated }).then(() => {
  84. return this.dismiss();
  85. });
  86. }
  87. typeChanged() {
  88. if (this.newType === 'Viewer' || this.newType === 'Editor') {
  89. this.addNewItem({permission: 1, role: this.newType});
  90. this.canUpdate = true;
  91. this.resetNewType();
  92. }
  93. }
  94. permissionChanged() {
  95. this.canUpdate = true;
  96. }
  97. addNewItem(item) {
  98. item.dashboardId = this.dashboard.id;
  99. this.items.push(this.prepareViewModel(item));
  100. this.sortItems();
  101. this.canUpdate = true;
  102. }
  103. userPicked(user) {
  104. this.addNewItem({userId: user.id, userLogin: user.login, permission: 1,});
  105. this.$scope.$broadcast('user-picker-reset');
  106. }
  107. groupPicked(group) {
  108. this.addNewItem({userGroupId: group.id, userGroup: group.name, permission: 1});
  109. this.$scope.$broadcast('user-group-picker-reset');
  110. }
  111. removeItem(index) {
  112. this.items.splice(index, 1);
  113. this.canUpdate = true;
  114. }
  115. }
  116. export function dashAclModal() {
  117. return {
  118. restrict: 'E',
  119. templateUrl: 'public/app/features/dashboard/acl/acl.html',
  120. controller: AclCtrl,
  121. bindToController: true,
  122. controllerAs: 'ctrl',
  123. scope: {
  124. dismiss: "&"
  125. }
  126. };
  127. }
  128. export interface FormModel {
  129. dashboardId: number;
  130. userId?: number;
  131. userGroupId?: number;
  132. PermissionType: number;
  133. }
  134. export interface DashboardAcl {
  135. id?: number;
  136. dashboardId?: number;
  137. userId?: number;
  138. userLogin?: string;
  139. userEmail?: string;
  140. userGroupId?: number;
  141. userGroup?: string;
  142. permission?: number;
  143. permissionName?: string;
  144. role?: string;
  145. icon?: string;
  146. nameHtml?: string;
  147. inherited?: boolean;
  148. sortName?: string;
  149. sortRank?: number;
  150. }
  151. coreModule.directive('dashAclModal', dashAclModal);