Commit 0e853163 by Daniel Lee

WIP: can edit dashboard permission

parent 9e7aa65f
......@@ -40,13 +40,8 @@
<tbody>
<tr ng-repeat="permission in ctrl.userPermissions" class="permissionlist__item">
<td>{{permission.userLogin}}</td>
<td>{{permission.permissions}}</td>
<td>
<a href="#" class="btn btn-inverse btn-small">
<i class="fa fa-edit"></i>
Edit
</a>
&nbsp;&nbsp;
<td><select class="gf-form-input gf-size-auto" ng-model="permission.permissionType" ng-options="p.value as p.text for p in ctrl.permissionTypeOptions" ng-change="ctrl.updatePermission(permission)"></select></td>
<td class="text-right">
<a ng-click="ctrl.removeUserPermission(permission)" class="btn btn-danger btn-small">
<i class="fa fa-remove"></i>
</a>
......@@ -70,13 +65,8 @@
<tbody>
<tr ng-repeat="permission in ctrl.userGroupPermissions" class="permissionlist__item">
<td>{{permission.userGroup}}</td>
<td>{{permission.permissions}}</td>
<td>
<a href="#" class="btn btn-inverse btn-small">
<i class="fa fa-edit"></i>
Edit
</a>
&nbsp;&nbsp;
<td><select class="gf-form-input gf-size-auto" ng-model="permission.permissionType" ng-options="p.value as p.text for p in ctrl.permissionTypeOptions" ng-change="ctrl.updatePermission(permission)"></select></td>
<td class="text-right">
<a ng-click="ctrl.removeUserGroupPermission(permission)" class="btn btn-danger btn-small">
<i class="fa fa-remove"></i>
</a>
......
......@@ -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);
});
}
}
......
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);
});
});
});
......@@ -9,7 +9,7 @@
</a>
</div>
<div class="modal-content folder-modal">
<form name="ctrl.saveForm" ng-submit="ctrl.create()" class="modal-content folder-modal" novalidate>
<div class="p-t-2">
<div class="gf-form">
<span class="gf-form-label width-10">Folder Name</span>
......@@ -17,8 +17,8 @@
</div>
</div>
<div class="gf-form-button-row text-center">
<a type="submit" class="btn btn-success" ng-click="ctrl.create()">Create</a>
<button type="submit" class="btn btn-success">Create</button>
<a class="btn-text" ng-click="dismiss();">Cancel</a>
</div>
</div>
</form>
</div>
......@@ -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.', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment