From 43ffe826fac81e4e653b0246e6e57be0bf9ae031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 20 Jun 2017 17:18:20 -0400 Subject: [PATCH] dashboard acl work --- pkg/api/dashboard_acl.go | 34 ++++++++- pkg/models/dashboard_acl.go | 11 +-- pkg/services/guardian/guardian.go | 2 +- public/app/core/directives/dash_edit_link.js | 8 ++- public/app/features/dashboard/acl/acl.html | 76 +++++++------------- public/app/features/dashboard/acl/acl.ts | 43 +++++------ 6 files changed, 90 insertions(+), 84 deletions(-) diff --git a/pkg/api/dashboard_acl.go b/pkg/api/dashboard_acl.go index a64360c92f7..577dba5e619 100644 --- a/pkg/api/dashboard_acl.go +++ b/pkg/api/dashboard_acl.go @@ -23,7 +23,39 @@ func GetDashboardAclList(c *middleware.Context) Response { return ApiError(500, "Failed to get Dashboard ACL", err) } - return Json(200, &query.Result) + list := query.Result + hasViewRoleAcl := false + hasEditRoleAcl := false + + for _, item := range list { + if item.Role == m.ROLE_EDITOR { + hasEditRoleAcl = true + } + if item.Role == m.ROLE_VIEWER { + hasViewRoleAcl = true + } + } + + if !hasEditRoleAcl { + tmpList := append([]*m.DashboardAclInfoDTO{}, &m.DashboardAclInfoDTO{ + Id: 0, + Role: m.ROLE_EDITOR, + Permissions: m.PERMISSION_EDIT, + PermissionName: "Edit", + }) + list = append(tmpList, list...) + } + if !hasViewRoleAcl { + tmpList := append([]*m.DashboardAclInfoDTO{}, &m.DashboardAclInfoDTO{ + Id: 0, + Role: m.ROLE_VIEWER, + Permissions: m.PERMISSION_VIEW, + PermissionName: "View", + }) + list = append(tmpList, list...) + } + + return Json(200, list) } func PostDashboardAcl(c *middleware.Context, cmd m.SetDashboardAclCommand) Response { diff --git a/pkg/models/dashboard_acl.go b/pkg/models/dashboard_acl.go index 05a28553d40..11e8f71434c 100644 --- a/pkg/models/dashboard_acl.go +++ b/pkg/models/dashboard_acl.go @@ -8,16 +8,18 @@ import ( type PermissionType int const ( + PERMISSION_NONE = 0 PERMISSION_VIEW PermissionType = 1 << iota - PERMISSION_READ_ONLY_EDIT PERMISSION_EDIT + PERMISSION_ADMIN ) func (p PermissionType) String() string { names := map[int]string{ - int(PERMISSION_VIEW): "View", - int(PERMISSION_READ_ONLY_EDIT): "Read-only Edit", - int(PERMISSION_EDIT): "Edit", + int(PERMISSION_NONE): "None", + int(PERMISSION_VIEW): "View", + int(PERMISSION_EDIT): "Edit", + int(PERMISSION_ADMIN): "Admin", } return names[int(p)] } @@ -55,6 +57,7 @@ type DashboardAclInfoDTO struct { UserEmail string `json:"userEmail"` UserGroupId int64 `json:"userGroupId"` UserGroup string `json:"userGroup"` + Role RoleType `json:"role"` Permissions PermissionType `json:"permissions"` PermissionName string `json:"permissionName"` } diff --git a/pkg/services/guardian/guardian.go b/pkg/services/guardian/guardian.go index 087b22cdd2f..7fb33a8d01e 100644 --- a/pkg/services/guardian/guardian.go +++ b/pkg/services/guardian/guardian.go @@ -29,7 +29,7 @@ func (g *DashboardGuardian) CanSave() (bool, error) { } func (g *DashboardGuardian) CanEdit() (bool, error) { - return g.HasPermission(m.PERMISSION_READ_ONLY_EDIT, m.ROLE_READ_ONLY_EDITOR) + return g.HasPermission(m.PERMISSION_EDIT, m.ROLE_READ_ONLY_EDITOR) } func (g *DashboardGuardian) CanView() (bool, error) { diff --git a/public/app/core/directives/dash_edit_link.js b/public/app/core/directives/dash_edit_link.js index 21e8940200c..d5fd80f9143 100644 --- a/public/app/core/directives/dash_edit_link.js +++ b/public/app/core/directives/dash_edit_link.js @@ -22,6 +22,7 @@ function ($, angular, coreModule, _) { restrict: 'A', link: function(scope, elem) { var editorScope; + var modalScope; var lastEditView; function hideEditorPane(hideToShowOtherView) { @@ -47,6 +48,11 @@ function ($, angular, coreModule, _) { editorScope = options.scope ? options.scope.$new() : scope.$new(); editorScope.dismiss = function(hideToShowOtherView) { + if (modalScope) { + modalScope.dismiss(); + modalScope = null; + } + editorScope.$destroy(); lastEditView = null; editorScope = null; @@ -73,7 +79,7 @@ function ($, angular, coreModule, _) { }; if (options.isModal) { - var modalScope = $rootScope.$new(); + modalScope = $rootScope.$new(); modalScope.$on("$destroy", function() { editorScope.dismiss(); }); diff --git a/public/app/features/dashboard/acl/acl.html b/public/app/features/dashboard/acl/acl.html index b51508693ee..b9f4a11b5c0 100644 --- a/public/app/features/dashboard/acl/acl.html +++ b/public/app/features/dashboard/acl/acl.html @@ -36,53 +36,27 @@ -
-
-
Groups & Users
-
- - - {{acl.userLogin}} - -
- -
- @@ -136,11 +110,11 @@ - - - - - - + + + + + + diff --git a/public/app/features/dashboard/acl/acl.ts b/public/app/features/dashboard/acl/acl.ts index 19ed66a4526..069cbfa4704 100644 --- a/public/app/features/dashboard/acl/acl.ts +++ b/public/app/features/dashboard/acl/acl.ts @@ -6,32 +6,21 @@ import _ from 'lodash'; export class AclCtrl { dashboard: any; - userAcl: DashboardAcl[]; - groupAcl: DashboardAcl[]; - permissionTypeOptions = [ + aclItems: DashboardAcl[]; + permissionOptions = [ {value: 1, text: 'View'}, {value: 2, text: 'Edit'}, {value: 4, text: 'Admin'} ]; - roleOptions = [ - {value: 0, text: 'No Access'}, - {value: 1, text: 'View'}, - {value: 2, text: 'Edit'}, - {value: 4, text: 'Admin'} - ]; - - roles = []; - type = 'User Group'; permission = 1; userId: number; userGroupId: number; /** @ngInject */ - constructor(private backendSrv, private dashboardSrv) { - this.userAcl = []; - this.groupAcl = []; + constructor(private backendSrv, private dashboardSrv, private $sce) { + this.aclItems = []; this.dashboard = dashboardSrv.getCurrent(); this.get(this.dashboard.id); } @@ -39,20 +28,22 @@ export class AclCtrl { get(dashboardId: number) { return this.backendSrv.get(`/api/dashboards/id/${dashboardId}/acl`) .then(result => { - this.userAcl = _.filter(result, p => { return p.userId > 0;}); - this.groupAcl = _.filter(result, p => { return p.userGroupId > 0;}); - this.roles = this.setRoles(result); + this.aclItems = _.map(result, item => { + if (item.userId > 0) { + item.icon = "fa fa-fw fa-user"; + item.nameHtml = this.$sce.trustAsHtml(item.userLogin); + } else if (item.userGroupId > 0) { + item.icon = "fa fa-fw fa-users"; + item.nameHtml = this.$sce.trustAsHtml(item.userGroup); + } else if (item.role) { + item.icon = "fa fa-fw fa-street-view"; + item.nameHtml = this.$sce.trustAsHtml(`Everyone with ${item.role} Role`); + } + return item; + }); }); } - setRoles(result: any) { - return [ - {name: 'Viewer', permissions: 1}, - {name: 'Editor', permissions: 2}, - {name: 'Admin', permissions: 4} - ]; - } - addPermission() { if (this.type === 'User') { if (!this.userId) {