acl.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. tabIndex: any;
  7. dashboard: any;
  8. userPermissions: Permission[];
  9. userGroupPermissions: Permission[];
  10. permissionTypeOptions = [
  11. {value: 1, text: 'View'},
  12. {value: 2, text: 'Read-only Edit'},
  13. {value: 4, text: 'Edit'}
  14. ];
  15. userId: number;
  16. type = 'User';
  17. userGroupId: number;
  18. permission = 1;
  19. /** @ngInject */
  20. constructor(private backendSrv, private $scope, $sce, private uiSegmentSrv) {
  21. this.tabIndex = 0;
  22. this.userPermissions = [];
  23. this.userGroupPermissions = [];
  24. this.get(this.dashboard.id);
  25. }
  26. get(dashboardId: number) {
  27. return this.backendSrv.get(`/api/dashboards/${dashboardId}/acl`)
  28. .then(result => {
  29. this.userPermissions = _.filter(result, p => { return p.userId > 0;});
  30. this.userGroupPermissions = _.filter(result, p => { return p.userGroupId > 0;});
  31. });
  32. }
  33. addPermission() {
  34. if (this.type === 'User') {
  35. if (!this.userId) {
  36. return;
  37. }
  38. this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, {
  39. userId: this.userId,
  40. permissionType: this.permission
  41. }).then(() => {
  42. this.userId = null;
  43. this.get(this.dashboard.id);
  44. });
  45. } else {
  46. if (!this.userGroupId) {
  47. return;
  48. }
  49. this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, {
  50. userGroupId: this.userGroupId,
  51. permissionType: this.permission
  52. }).then(() => {
  53. this.userGroupId = null;
  54. this.get(this.dashboard.id);
  55. });
  56. }
  57. }
  58. removeUserPermission(permission: Permission) {
  59. this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user/${permission.userId}`).then(() => {
  60. this.get(permission.dashboardId);
  61. });
  62. }
  63. removeUserGroupPermission(permission: Permission) {
  64. this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user-group/${permission.userGroupId}`).then(() => {
  65. this.get(permission.dashboardId);
  66. });
  67. }
  68. }
  69. export function aclSettings() {
  70. return {
  71. restrict: 'E',
  72. templateUrl: 'public/app/features/dashboard/acl/acl.html',
  73. controller: AclCtrl,
  74. bindToController: true,
  75. controllerAs: 'ctrl',
  76. scope: { dashboard: "=" }
  77. };
  78. }
  79. export interface FormModel {
  80. dashboardId: number;
  81. userId?: number;
  82. userGroupId?: number;
  83. PermissionType: number;
  84. }
  85. export interface Permission {
  86. id: number;
  87. orgId: number;
  88. dashboardId: number;
  89. created: Date;
  90. updated: Date;
  91. userId: number;
  92. userLogin: number;
  93. userEmail: string;
  94. userGroupId: number;
  95. userGroup: string;
  96. permissions: string[];
  97. permissionType: number[];
  98. }
  99. coreModule.directive('aclSettings', aclSettings);