Files
grafana/public/app/features/dashboard/acl/acl.ts

205 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-05-22 10:37:53 +02:00
///<reference path="../../../headers/common.d.ts" />
import coreModule from 'app/core/core_module';
import _ from 'lodash';
export class AclCtrl {
dashboard: any;
2017-06-21 19:02:03 -04:00
items: DashboardAcl[];
2017-06-20 17:18:20 -04:00
permissionOptions = [
{value: 1, text: 'View'},
2017-06-20 12:36:07 -04:00
{value: 2, text: 'Edit'},
{value: 4, text: 'Admin'}
];
2017-06-21 17:57:04 -04:00
aclTypes = [
{value: 'Group', text: 'User Group'},
{value: 'User', text: 'User'},
{value: 'Viewer', text: 'Everyone With Viewer Role'},
{value: 'Editor', text: 'Everyone With Editor Role'}
];
2017-06-15 20:21:14 +02:00
2017-06-21 19:02:03 -04:00
dismiss: () => void;
2017-06-21 17:14:06 -04:00
newType: string;
canUpdate: boolean;
error: string;
2017-09-22 08:42:07 +02:00
readonly duplicateError = 'This permission exists already.';
2017-06-15 20:21:14 +02:00
2017-05-22 10:37:53 +02:00
/** @ngInject */
2017-09-22 08:42:07 +02:00
constructor(private backendSrv, dashboardSrv, private $sce, private $scope) {
2017-06-21 19:02:03 -04:00
this.items = [];
2017-06-21 17:57:04 -04:00
this.resetNewType();
2017-06-20 12:36:07 -04:00
this.dashboard = dashboardSrv.getCurrent();
this.get(this.dashboard.id);
2017-05-22 10:37:53 +02:00
}
2017-06-21 17:57:04 -04:00
resetNewType() {
this.newType = 'Group';
}
2017-05-22 10:37:53 +02:00
get(dashboardId: number) {
2017-06-20 01:07:54 +02:00
return this.backendSrv.get(`/api/dashboards/id/${dashboardId}/acl`)
2017-05-22 10:37:53 +02:00
.then(result => {
2017-06-21 19:02:03 -04:00
this.items = _.map(result, this.prepareViewModel.bind(this));
2017-06-22 15:16:41 -04:00
this.sortItems();
2017-05-22 10:37:53 +02:00
});
}
2017-06-22 15:16:41 -04:00
sortItems() {
this.items = _.orderBy(this.items, ['sortRank', 'sortName'], ['desc', 'asc']);
}
2017-06-21 17:14:06 -04:00
prepareViewModel(item: DashboardAcl): DashboardAcl {
item.inherited = !this.dashboard.meta.isFolder && this.dashboard.id !== item.dashboardId;
2017-06-22 15:16:41 -04:00
item.sortRank = 0;
2017-06-21 17:14:06 -04:00
if (item.userId > 0) {
item.icon = "fa fa-fw fa-user";
item.nameHtml = this.$sce.trustAsHtml(item.userLogin);
2017-06-22 15:16:41 -04:00
item.sortName = item.userLogin;
item.sortRank = 10;
2017-12-08 18:25:45 +03:00
} else if (item.teamId > 0) {
2017-06-21 17:14:06 -04:00
item.icon = "fa fa-fw fa-users";
2017-12-08 18:25:45 +03:00
item.nameHtml = this.$sce.trustAsHtml(item.team);
item.sortName = item.team;
2017-06-22 15:16:41 -04:00
item.sortRank = 20;
2017-06-21 17:14:06 -04:00
} else if (item.role) {
item.icon = "fa fa-fw fa-street-view";
item.nameHtml = this.$sce.trustAsHtml(`Everyone with <span class="query-keyword">${item.role}</span> Role`);
2017-06-22 15:16:41 -04:00
item.sortName = item.role;
item.sortRank = 30;
if (item.role === 'Viewer') {
item.sortRank += 1;
}
}
if (item.inherited) {
item.sortRank += 100;
}
2017-06-21 17:14:06 -04:00
return item;
}
2017-06-21 17:14:06 -04:00
update() {
2017-06-22 15:16:41 -04:00
var updated = [];
for (let item of this.items) {
if (item.inherited) {
continue;
}
updated.push({
id: item.id,
userId: item.userId,
2017-12-08 18:25:45 +03:00
teamId: item.teamId,
2017-06-22 15:16:41 -04:00
role: item.role,
permission: item.permission,
});
}
return this.backendSrv.post(`/api/dashboards/id/${this.dashboard.id}/acl`, { items: updated }).then(() => {
return this.dismiss();
2017-06-15 20:21:14 +02:00
});
}
2017-06-21 17:57:04 -04:00
typeChanged() {
if (this.newType === 'Viewer' || this.newType === 'Editor') {
2017-06-22 17:10:43 -04:00
this.addNewItem({permission: 1, role: this.newType});
2017-06-21 17:57:04 -04:00
this.canUpdate = true;
this.resetNewType();
}
}
2017-06-21 17:14:06 -04:00
permissionChanged() {
this.canUpdate = true;
2017-06-15 20:21:14 +02:00
}
2017-06-22 15:16:41 -04:00
addNewItem(item) {
if (!this.isValid(item)) {
return;
}
this.error = '';
2017-06-22 15:16:41 -04:00
item.dashboardId = this.dashboard.id;
this.items.push(this.prepareViewModel(item));
this.sortItems();
2017-06-21 17:57:04 -04:00
this.canUpdate = true;
2017-06-22 15:16:41 -04:00
}
isValid(item) {
const dupe = _.find(this.items, (it) => { return this.isDuplicate(it, item); });
if (dupe) {
this.error = this.duplicateError;
return false;
}
return true;
}
isDuplicate(origItem, newItem) {
if (origItem.inherited) {
return false;
}
return (origItem.role && newItem.role && origItem.role === newItem.role) ||
(origItem.userId && newItem.userId && origItem.userId === newItem.userId) ||
2017-12-08 18:25:45 +03:00
(origItem.teamId && newItem.teamId && origItem.teamId === newItem.teamId);
}
2017-06-22 15:16:41 -04:00
userPicked(user) {
this.addNewItem({userId: user.id, userLogin: user.login, permission: 1,});
2017-06-21 17:57:04 -04:00
this.$scope.$broadcast('user-picker-reset');
}
groupPicked(group) {
2017-12-08 18:25:45 +03:00
this.addNewItem({teamId: group.id, team: group.name, permission: 1});
this.$scope.$broadcast('team-picker-reset');
2017-06-21 17:14:06 -04:00
}
removeItem(index) {
2017-06-21 19:02:03 -04:00
this.items.splice(index, 1);
2017-06-21 17:14:06 -04:00
this.canUpdate = true;
2017-05-22 10:37:53 +02:00
}
}
2017-06-20 12:36:07 -04:00
export function dashAclModal() {
2017-05-22 10:37:53 +02:00
return {
restrict: 'E',
templateUrl: 'public/app/features/dashboard/acl/acl.html',
controller: AclCtrl,
bindToController: true,
controllerAs: 'ctrl',
2017-06-20 12:36:07 -04:00
scope: {
dismiss: "&"
}
2017-05-22 10:37:53 +02:00
};
}
export interface FormModel {
dashboardId: number;
userId?: number;
2017-12-08 18:25:45 +03:00
teamId?: number;
PermissionType: number;
}
2017-06-20 12:36:07 -04:00
export interface DashboardAcl {
2017-06-21 17:14:06 -04:00
id?: number;
dashboardId?: number;
2017-06-21 17:57:04 -04:00
userId?: number;
2017-06-22 15:16:41 -04:00
userLogin?: string;
2017-06-21 17:14:06 -04:00
userEmail?: string;
2017-12-08 18:25:45 +03:00
teamId?: number;
team?: string;
2017-06-21 17:14:06 -04:00
permission?: number;
permissionName?: string;
role?: string;
icon?: string;
nameHtml?: string;
2017-06-22 15:16:41 -04:00
inherited?: boolean;
sortName?: string;
sortRank?: number;
2017-05-22 10:37:53 +02:00
}
2017-06-20 12:36:07 -04:00
coreModule.directive('dashAclModal', dashAclModal);