diff --git a/public/app/features/dashboard/acl/acl.html b/public/app/features/dashboard/acl/acl.html index f779c11a6ed..74e3b33712a 100644 --- a/public/app/features/dashboard/acl/acl.html +++ b/public/app/features/dashboard/acl/acl.html @@ -40,13 +40,8 @@ {{permission.userLogin}} - {{permission.permissions}} - - - - Edit - -    + + @@ -70,13 +65,8 @@ {{permission.userGroup}} - {{permission.permissions}} - - - - Edit - -    + + diff --git a/public/app/features/dashboard/acl/acl.ts b/public/app/features/dashboard/acl/acl.ts index ef2593de402..b1ef4fd270c 100644 --- a/public/app/features/dashboard/acl/acl.ts +++ b/public/app/features/dashboard/acl/acl.ts @@ -14,13 +14,15 @@ export class AclCtrl { {value: 2, text: 'Read-only Edit'}, {value: 4, text: 'Edit'} ]; - userId: number; + type = 'User'; - userGroupId: number; permission = 1; + userId: number; + userGroupId: number; + /** @ngInject */ - constructor(private backendSrv, private $scope, $sce, private uiSegmentSrv) { + constructor(private backendSrv, private $scope) { this.tabIndex = 0; this.userPermissions = []; this.userGroupPermissions = []; @@ -40,38 +42,56 @@ export class AclCtrl { if (!this.userId) { return; } - - this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, { - userId: this.userId, - permissionType: this.permission - }).then(() => { + return this.addOrUpdateUserPermission(this.userId, this.permission).then(() => { this.userId = null; - this.get(this.dashboard.id); + return this.get(this.dashboard.id); }); } else { if (!this.userGroupId) { return; } - this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, { - userGroupId: this.userGroupId, - permissionType: this.permission - }).then(() => { + this.addOrUpdateUserGroupPermission(this.userGroupId, this.permission).then(() => { this.userGroupId = null; - this.get(this.dashboard.id); + return this.get(this.dashboard.id); }); } } + addOrUpdateUserPermission(userId: number, permissionType: number) { + return this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, { + userId: userId, + permissionType: permissionType + }); + } + + addOrUpdateUserGroupPermission(userGroupId: number, permissionType: number) { + return this.backendSrv.post(`/api/dashboards/${this.dashboard.id}/acl`, { + userGroupId: userGroupId, + permissionType: permissionType + }); + } + + updatePermission(permission: any) { + if (permission.userId > 0) { + return this.addOrUpdateUserPermission(permission.userId, permission.permissionType); + } else { + if (!permission.userGroupId) { + return; + } + return this.addOrUpdateUserGroupPermission(permission.userGroupId, permission.permissionType); + } + } + removeUserPermission(permission: Permission) { - this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user/${permission.userId}`).then(() => { - this.get(permission.dashboardId); + return this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user/${permission.userId}`).then(() => { + return this.get(permission.dashboardId); }); } removeUserGroupPermission(permission: Permission) { - this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user-group/${permission.userGroupId}`).then(() => { - this.get(permission.dashboardId); + return this.backendSrv.delete(`/api/dashboards/${permission.dashboardId}/acl/user-group/${permission.userGroupId}`).then(() => { + return this.get(permission.dashboardId); }); } } diff --git a/public/app/features/dashboard/acl/specs/acl_specs.ts b/public/app/features/dashboard/acl/specs/acl_specs.ts new file mode 100644 index 00000000000..e518dc4ea26 --- /dev/null +++ b/public/app/features/dashboard/acl/specs/acl_specs.ts @@ -0,0 +1,51 @@ +import {describe, beforeEach, it, expect, sinon, angularMocks} from 'test/lib/common'; +import {AclCtrl} from '../acl'; + +describe('AclCtrl', () => { + var ctx: any = {}; + var backendSrv = { + get: sinon.stub().returns(Promise.resolve([])), + post: sinon.stub().returns(Promise.resolve([])) + }; + + beforeEach(angularMocks.module('grafana.core')); + beforeEach(angularMocks.module('grafana.controllers')); + + beforeEach(angularMocks.inject(($rootScope, $controller, $q, $compile) => { + ctx.$q = $q; + ctx.scope = $rootScope.$new(); + AclCtrl.prototype.dashboard = {dashboard: {id: 1}}; + ctx.ctrl = $controller(AclCtrl, { + $scope: ctx.scope, + backendSrv: backendSrv, + }, { + dashboard: {id: 1} + }); + })); + + describe('when user permission is to be added', () => { + beforeEach(done => { + ctx.ctrl.type = 'User'; + ctx.ctrl.userId = 2; + ctx.ctrl.permission = 1; + + ctx.ctrl.addPermission().then(() => { + done(); + }); + }); + + it('should parse the result and save to db', () => { + expect(backendSrv.post.getCall(0).args[0]).to.eql('/api/dashboards/1/acl'); + expect(backendSrv.post.getCall(0).args[1].userId).to.eql(2); + expect(backendSrv.post.getCall(0).args[1].permissionType).to.eql(1); + }); + + it('should refresh the list after saving.', () => { + expect(backendSrv.get.getCall(1).args[0]).to.eql('/api/dashboards/1/acl'); + }); + + it('should reset userId', () => { + expect(ctx.ctrl.userId).to.eql(null); + }); + }); +}); diff --git a/public/app/features/dashboard/folder_modal/folder.html b/public/app/features/dashboard/folder_modal/folder.html index 5ca67fab2ef..db62632423f 100644 --- a/public/app/features/dashboard/folder_modal/folder.html +++ b/public/app/features/dashboard/folder_modal/folder.html @@ -9,7 +9,7 @@ - + diff --git a/public/app/features/org/specs/user_group_details_ctrl_specs.ts b/public/app/features/org/specs/user_group_details_ctrl_specs.ts index 2dbc64d4368..99b06c79c25 100644 --- a/public/app/features/org/specs/user_group_details_ctrl_specs.ts +++ b/public/app/features/org/specs/user_group_details_ctrl_specs.ts @@ -21,18 +21,18 @@ var backendSrv = { backendSrv: backendSrv, $routeParams: {id: 1} }); - ctx.ctrl.userName = 'login - user@email.com'; + ctx.ctrl.userId = 1; })); describe('when user is chosen to be added to user group', () => { beforeEach(() => { - ctx.scope.addMemberForm = {$valid: true}; - ctx.ctrl.usersSearchCache = [{id: 1, login: 'login'}, {id: 2, login: 'login2'}]; + ctx.ctrl.addMemberForm = {$valid: true}; ctx.ctrl.addMember(); }); it('should parse the result and save to db', () => { expect(backendSrv.post.getCall(0).args[0]).to.eql('/api/user-groups/1/members'); + expect(backendSrv.post.getCall(0).args[1].userId).to.eql(1); }); it('should refresh the list after saving.', () => {