mirror of https://github.com/grafana/grafana
commit
d6341162cb
@ -1,42 +0,0 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
) |
||||
|
||||
func init() { |
||||
bus.AddHandler("sql", GetAllowedDashboards) |
||||
} |
||||
|
||||
func GetAllowedDashboards(query *m.GetAllowedDashboardsQuery) error { |
||||
dashboardIds := arrayToString(query.DashList, ",") |
||||
|
||||
rawSQL := `select distinct d.id as DashboardId |
||||
from dashboard as d |
||||
left join dashboard as df on d.parent_id = df.id |
||||
left join dashboard_acl as dfa on d.parent_id = dfa.dashboard_id or d.id = dfa.dashboard_id |
||||
left join user_group_member as ugm on ugm.user_group_id = dfa.user_group_id |
||||
where ( |
||||
(d.has_acl = 1 and (dfa.user_id = ? or ugm.user_id = ? or df.created_by = ? or (d.is_folder = 1 and d.created_by = ?))) |
||||
or d.has_acl = 0) |
||||
and d.org_id = ?` |
||||
|
||||
rawSQL = fmt.Sprintf("%v and d.id in(%v)", rawSQL, dashboardIds) |
||||
|
||||
query.Result = make([]int64, 0) |
||||
err := x.SQL(rawSQL, query.UserId, query.UserId, query.UserId, query.UserId, query.OrgId).Find(&query.Result) |
||||
|
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func arrayToString(a []int64, delim string) string { |
||||
return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]") |
||||
} |
||||
@ -1,87 +0,0 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
. "github.com/smartystreets/goconvey/convey" |
||||
) |
||||
|
||||
func TestGuardianDataAccess(t *testing.T) { |
||||
|
||||
Convey("Testing DB", t, func() { |
||||
InitTestDB(t) |
||||
|
||||
Convey("Given one dashboard folder with two dashboard and one dashboard in the root folder", func() { |
||||
folder := insertTestDashboard("1 test dash folder", 1, 0, true, "prod", "webapp") |
||||
dashInRoot := insertTestDashboard("test dash 67", 1, 0, false, "prod", "webapp") |
||||
insertTestDashboard("test dash 23", 1, folder.Id, false, "prod", "webapp") |
||||
insertTestDashboard("test dash 45", 1, folder.Id, false, "prod") |
||||
|
||||
currentUser := createUser("viewer", "Viewer", false) |
||||
|
||||
Convey("and no acls are set", func() { |
||||
Convey("should return all dashboards", func() { |
||||
query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}} |
||||
err := GetAllowedDashboards(query) |
||||
So(err, ShouldBeNil) |
||||
So(len(query.Result), ShouldEqual, 2) |
||||
So(query.Result[0], ShouldEqual, folder.Id) |
||||
So(query.Result[1], ShouldEqual, dashInRoot.Id) |
||||
}) |
||||
}) |
||||
|
||||
Convey("and acl is set for dashboard folder", func() { |
||||
var otherUser int64 = 999 |
||||
updateTestDashboardWithAcl(folder.Id, otherUser, m.PERMISSION_EDIT) |
||||
|
||||
Convey("should not return folder", func() { |
||||
query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}} |
||||
err := GetAllowedDashboards(query) |
||||
So(err, ShouldBeNil) |
||||
So(len(query.Result), ShouldEqual, 1) |
||||
So(query.Result[0], ShouldEqual, dashInRoot.Id) |
||||
}) |
||||
|
||||
Convey("when the user is given permission", func() { |
||||
updateTestDashboardWithAcl(folder.Id, currentUser.Id, m.PERMISSION_EDIT) |
||||
|
||||
Convey("should folder", func() { |
||||
query := &m.GetAllowedDashboardsQuery{UserId: currentUser.Id, OrgId: 1, DashList: []int64{folder.Id, dashInRoot.Id}} |
||||
err := GetAllowedDashboards(query) |
||||
So(err, ShouldBeNil) |
||||
So(len(query.Result), ShouldEqual, 2) |
||||
So(query.Result[0], ShouldEqual, folder.Id) |
||||
So(query.Result[1], ShouldEqual, dashInRoot.Id) |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
||||
|
||||
func createUser(name string, role string, isAdmin bool) m.User { |
||||
setting.AutoAssignOrg = true |
||||
setting.AutoAssignOrgRole = role |
||||
|
||||
currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin} |
||||
err := CreateUser(¤tUserCmd) |
||||
So(err, ShouldBeNil) |
||||
|
||||
q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id} |
||||
GetUserOrgList(&q1) |
||||
So(q1.Result[0].Role, ShouldEqual, role) |
||||
|
||||
return currentUserCmd.Result |
||||
} |
||||
|
||||
func updateTestDashboardWithAcl(dashId int64, userId int64, permission m.PermissionType) { |
||||
err := AddOrUpdateDashboardPermission(&m.AddOrUpdateDashboardPermissionCommand{ |
||||
OrgId: 1, |
||||
UserId: userId, |
||||
DashboardId: dashId, |
||||
Permissions: permission, |
||||
}) |
||||
So(err, ShouldBeNil) |
||||
} |
||||
@ -0,0 +1,38 @@ |
||||
///<reference path="../../headers/common.d.ts" />
|
||||
|
||||
import coreModule from 'app/core/core_module'; |
||||
import appEvents from 'app/core/app_events'; |
||||
import _ from 'lodash'; |
||||
|
||||
export class CreateUserGroupCtrl { |
||||
userGroupName = ''; |
||||
|
||||
/** @ngInject */ |
||||
constructor(private backendSrv, private $scope, $sce, private $location) { |
||||
} |
||||
|
||||
createUserGroup() { |
||||
this.backendSrv.post('/api/user-groups', {name: this.userGroupName}).then((result) => { |
||||
if (result.userGroupId) { |
||||
this.$location.path('/org/user-groups/edit/' + result.userGroupId); |
||||
} |
||||
this.dismiss(); |
||||
}); |
||||
} |
||||
|
||||
dismiss() { |
||||
appEvents.emit('hide-modal'); |
||||
} |
||||
} |
||||
|
||||
export function createUserGroupModal() { |
||||
return { |
||||
restrict: 'E', |
||||
templateUrl: 'public/app/features/org/partials/create_user_group.html', |
||||
controller: CreateUserGroupCtrl, |
||||
bindToController: true, |
||||
controllerAs: 'ctrl', |
||||
}; |
||||
} |
||||
|
||||
coreModule.directive('createUserGroupModal', createUserGroupModal); |
||||
@ -1,23 +1,26 @@ |
||||
<div class="modal-body" ng-controller="UserGroupsCtrl"> |
||||
<div class="modal-body"> |
||||
<div class="modal-header"> |
||||
<h2 class="modal-header-title"> |
||||
Create User Group |
||||
<span class="p-l-1">Create User Group</span> |
||||
</h2> |
||||
<a class="modal-header-close" ng-click="dismiss();"> |
||||
|
||||
<a class="modal-header-close" ng-click="ctrl.dismiss();"> |
||||
<i class="fa fa-remove"></i> |
||||
</a> |
||||
</div> |
||||
|
||||
<div class="modal-content"> |
||||
<form name="createUserGroupForm" class="gf-form-group"> |
||||
<div class="gf-form-inline"> |
||||
<div class="gf-form max-width-21"> |
||||
<input type="text" class="gf-form-input" ng-model='ctrl.userGroupName' placeholder="Name"></input> |
||||
</div> |
||||
<div class="gf-form"> |
||||
<button class="btn gf-form-btn btn-success" ng-click="ctrl.createUserGroup();dismiss();">Create</button> |
||||
</div> |
||||
</div> |
||||
<form name="ctrl.createUserGroupForm" class="gf-form-group" novalidate> |
||||
<div class="p-t-2"> |
||||
<div class="gf-form-inline"> |
||||
<div class="gf-form max-width-21"> |
||||
<input type="text" class="gf-form-input" ng-model='ctrl.userGroupName' required give-focus="true" placeholder="Enter User Group Name"></input> |
||||
</div> |
||||
<div class="gf-form"> |
||||
<button class="btn gf-form-btn btn-success" ng-click="ctrl.createUserGroup();ctrl.dismiss();">Create</button> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
|
||||
Loading…
Reference in new issue