Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
004142a1
Unverified
Commit
004142a1
authored
Jun 18, 2018
by
Marcus Efraimsson
Committed by
GitHub
Jun 18, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #12263 from grafana/11076_current_org
Set current org when adding/removing user to org
parents
d6f4313c
6d48d0a8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
96 additions
and
18 deletions
+96
-18
pkg/services/sqlstore/org_test.go
+14
-2
pkg/services/sqlstore/org_users.go
+62
-2
pkg/services/sqlstore/user.go
+11
-7
pkg/services/sqlstore/user_test.go
+7
-7
public/app/features/admin/admin_edit_user_ctrl.ts
+2
-0
No files found.
pkg/services/sqlstore/org_test.go
View file @
004142a1
...
...
@@ -151,7 +151,7 @@ func TestAccountDataAccess(t *testing.T) {
})
Convey
(
"Can set using org"
,
func
()
{
cmd
:=
m
.
SetUsingOrgCommand
{
UserId
:
ac2
.
Id
,
OrgId
:
ac1
.
Id
}
cmd
:=
m
.
SetUsingOrgCommand
{
UserId
:
ac2
.
Id
,
OrgId
:
ac1
.
Org
Id
}
err
:=
SetUsingOrg
(
&
cmd
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -160,13 +160,25 @@ func TestAccountDataAccess(t *testing.T) {
err
:=
GetSignedInUser
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
OrgId
,
ShouldEqual
,
ac1
.
Id
)
So
(
query
.
Result
.
OrgId
,
ShouldEqual
,
ac1
.
Org
Id
)
So
(
query
.
Result
.
Email
,
ShouldEqual
,
"ac2@test.com"
)
So
(
query
.
Result
.
Name
,
ShouldEqual
,
"ac2 name"
)
So
(
query
.
Result
.
Login
,
ShouldEqual
,
"ac2"
)
So
(
query
.
Result
.
OrgName
,
ShouldEqual
,
"ac1@test.com"
)
So
(
query
.
Result
.
OrgRole
,
ShouldEqual
,
"Viewer"
)
})
Convey
(
"Should set last org as current when removing user from current"
,
func
()
{
remCmd
:=
m
.
RemoveOrgUserCommand
{
OrgId
:
ac1
.
OrgId
,
UserId
:
ac2
.
Id
}
err
:=
RemoveOrgUser
(
&
remCmd
)
So
(
err
,
ShouldBeNil
)
query
:=
m
.
GetSignedInUserQuery
{
UserId
:
ac2
.
Id
}
err
=
GetSignedInUser
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
OrgId
,
ShouldEqual
,
ac2
.
OrgId
)
})
})
Convey
(
"Cannot delete last admin org user"
,
func
()
{
...
...
pkg/services/sqlstore/org_users.go
View file @
004142a1
...
...
@@ -20,7 +20,14 @@ func init() {
func
AddOrgUser
(
cmd
*
m
.
AddOrgUserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
// check if user exists
if
res
,
err
:=
sess
.
Query
(
"SELECT 1 from org_user WHERE org_id=? and user_id=?"
,
cmd
.
OrgId
,
cmd
.
UserId
);
err
!=
nil
{
var
user
m
.
User
if
exists
,
err
:=
sess
.
Id
(
cmd
.
UserId
)
.
Get
(
&
user
);
err
!=
nil
{
return
err
}
else
if
!
exists
{
return
m
.
ErrUserNotFound
}
if
res
,
err
:=
sess
.
Query
(
"SELECT 1 from org_user WHERE org_id=? and user_id=?"
,
cmd
.
OrgId
,
user
.
Id
);
err
!=
nil
{
return
err
}
else
if
len
(
res
)
==
1
{
return
m
.
ErrOrgUserAlreadyAdded
...
...
@@ -41,7 +48,26 @@ func AddOrgUser(cmd *m.AddOrgUserCommand) error {
}
_
,
err
:=
sess
.
Insert
(
&
entity
)
return
err
if
err
!=
nil
{
return
err
}
var
userOrgs
[]
*
m
.
UserOrgDTO
sess
.
Table
(
"org_user"
)
sess
.
Join
(
"INNER"
,
"org"
,
"org_user.org_id=org.id"
)
sess
.
Where
(
"org_user.user_id=? AND org_user.org_id=?"
,
user
.
Id
,
user
.
OrgId
)
sess
.
Cols
(
"org.name"
,
"org_user.role"
,
"org_user.org_id"
)
err
=
sess
.
Find
(
&
userOrgs
)
if
err
!=
nil
{
return
err
}
if
len
(
userOrgs
)
==
0
{
return
setUsingOrgInTransaction
(
sess
,
user
.
Id
,
cmd
.
OrgId
)
}
return
nil
})
}
...
...
@@ -110,6 +136,14 @@ func GetOrgUsers(query *m.GetOrgUsersQuery) error {
func
RemoveOrgUser
(
cmd
*
m
.
RemoveOrgUserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
// check if user exists
var
user
m
.
User
if
exists
,
err
:=
sess
.
Id
(
cmd
.
UserId
)
.
Get
(
&
user
);
err
!=
nil
{
return
err
}
else
if
!
exists
{
return
m
.
ErrUserNotFound
}
deletes
:=
[]
string
{
"DELETE FROM org_user WHERE org_id=? and user_id=?"
,
"DELETE FROM dashboard_acl WHERE org_id=? and user_id = ?"
,
...
...
@@ -123,6 +157,32 @@ func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error {
}
}
var
userOrgs
[]
*
m
.
UserOrgDTO
sess
.
Table
(
"org_user"
)
sess
.
Join
(
"INNER"
,
"org"
,
"org_user.org_id=org.id"
)
sess
.
Where
(
"org_user.user_id=?"
,
user
.
Id
)
sess
.
Cols
(
"org.name"
,
"org_user.role"
,
"org_user.org_id"
)
err
:=
sess
.
Find
(
&
userOrgs
)
if
err
!=
nil
{
return
err
}
hasCurrentOrgSet
:=
false
for
_
,
userOrg
:=
range
userOrgs
{
if
user
.
OrgId
==
userOrg
.
OrgId
{
hasCurrentOrgSet
=
true
break
}
}
if
!
hasCurrentOrgSet
&&
len
(
userOrgs
)
>
0
{
err
=
setUsingOrgInTransaction
(
sess
,
user
.
Id
,
userOrgs
[
0
]
.
OrgId
)
if
err
!=
nil
{
return
err
}
}
return
validateOneAdminLeftInOrg
(
cmd
.
OrgId
,
sess
)
})
}
...
...
pkg/services/sqlstore/user.go
View file @
004142a1
...
...
@@ -292,16 +292,20 @@ func SetUsingOrg(cmd *m.SetUsingOrgCommand) error {
}
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
user
:=
m
.
User
{
Id
:
cmd
.
UserId
,
OrgId
:
cmd
.
OrgId
,
}
_
,
err
:=
sess
.
Id
(
cmd
.
UserId
)
.
Update
(
&
user
)
return
err
return
setUsingOrgInTransaction
(
sess
,
cmd
.
UserId
,
cmd
.
OrgId
)
})
}
func
setUsingOrgInTransaction
(
sess
*
DBSession
,
userID
int64
,
orgID
int64
)
error
{
user
:=
m
.
User
{
Id
:
userID
,
OrgId
:
orgID
,
}
_
,
err
:=
sess
.
Id
(
userID
)
.
Update
(
&
user
)
return
err
}
func
GetUserProfile
(
query
*
m
.
GetUserProfileQuery
)
error
{
var
user
m
.
User
has
,
err
:=
x
.
Id
(
query
.
UserId
)
.
Get
(
&
user
)
...
...
pkg/services/sqlstore/user_test.go
View file @
004142a1
...
...
@@ -97,33 +97,33 @@ func TestUserDataAccess(t *testing.T) {
})
Convey
(
"when a user is an org member and has been assigned permissions"
,
func
()
{
err
=
AddOrgUser
(
&
m
.
AddOrgUserCommand
{
LoginOrEmail
:
users
[
0
]
.
Login
,
Role
:
m
.
ROLE_VIEWER
,
OrgId
:
users
[
0
]
.
Org
Id
})
err
=
AddOrgUser
(
&
m
.
AddOrgUserCommand
{
LoginOrEmail
:
users
[
1
]
.
Login
,
Role
:
m
.
ROLE_VIEWER
,
OrgId
:
users
[
0
]
.
OrgId
,
UserId
:
users
[
1
]
.
Id
})
So
(
err
,
ShouldBeNil
)
testHelperUpdateDashboardAcl
(
1
,
m
.
DashboardAcl
{
DashboardId
:
1
,
OrgId
:
users
[
0
]
.
OrgId
,
UserId
:
users
[
0
]
.
Id
,
Permission
:
m
.
PERMISSION_EDIT
})
testHelperUpdateDashboardAcl
(
1
,
m
.
DashboardAcl
{
DashboardId
:
1
,
OrgId
:
users
[
0
]
.
OrgId
,
UserId
:
users
[
1
]
.
Id
,
Permission
:
m
.
PERMISSION_EDIT
})
So
(
err
,
ShouldBeNil
)
err
=
SavePreferences
(
&
m
.
SavePreferencesCommand
{
UserId
:
users
[
0
]
.
Id
,
OrgId
:
users
[
0
]
.
OrgId
,
HomeDashboardId
:
1
,
Theme
:
"dark"
})
err
=
SavePreferences
(
&
m
.
SavePreferencesCommand
{
UserId
:
users
[
1
]
.
Id
,
OrgId
:
users
[
0
]
.
OrgId
,
HomeDashboardId
:
1
,
Theme
:
"dark"
})
So
(
err
,
ShouldBeNil
)
Convey
(
"when the user is deleted"
,
func
()
{
err
=
DeleteUser
(
&
m
.
DeleteUserCommand
{
UserId
:
users
[
0
]
.
Id
})
err
=
DeleteUser
(
&
m
.
DeleteUserCommand
{
UserId
:
users
[
1
]
.
Id
})
So
(
err
,
ShouldBeNil
)
Convey
(
"Should delete connected org users and permissions"
,
func
()
{
query
:=
&
m
.
GetOrgUsersQuery
{
OrgId
:
1
}
query
:=
&
m
.
GetOrgUsersQuery
{
OrgId
:
users
[
0
]
.
OrgId
}
err
=
GetOrgUsersForTest
(
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
1
)
permQuery
:=
&
m
.
GetDashboardAclInfoListQuery
{
DashboardId
:
1
,
OrgId
:
1
}
permQuery
:=
&
m
.
GetDashboardAclInfoListQuery
{
DashboardId
:
1
,
OrgId
:
users
[
0
]
.
OrgId
}
err
=
GetDashboardAclInfoList
(
permQuery
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
permQuery
.
Result
),
ShouldEqual
,
0
)
prefsQuery
:=
&
m
.
GetPreferencesQuery
{
OrgId
:
users
[
0
]
.
OrgId
,
UserId
:
users
[
0
]
.
Id
}
prefsQuery
:=
&
m
.
GetPreferencesQuery
{
OrgId
:
users
[
0
]
.
OrgId
,
UserId
:
users
[
1
]
.
Id
}
err
=
GetPreferences
(
prefsQuery
)
So
(
err
,
ShouldBeNil
)
...
...
public/app/features/admin/admin_edit_user_ctrl.ts
View file @
004142a1
...
...
@@ -75,6 +75,7 @@ export class AdminEditUserCtrl {
$scope
.
removeOrgUser
=
function
(
orgUser
)
{
backendSrv
.
delete
(
'/api/orgs/'
+
orgUser
.
orgId
+
'/users/'
+
$scope
.
user_id
).
then
(
function
()
{
$scope
.
getUser
(
$scope
.
user_id
);
$scope
.
getUserOrgs
(
$scope
.
user_id
);
});
};
...
...
@@ -108,6 +109,7 @@ export class AdminEditUserCtrl {
$scope
.
newOrg
.
loginOrEmail
=
$scope
.
user
.
login
;
backendSrv
.
post
(
'/api/orgs/'
+
orgInfo
.
id
+
'/users/'
,
$scope
.
newOrg
).
then
(
function
()
{
$scope
.
getUser
(
$scope
.
user_id
);
$scope
.
getUserOrgs
(
$scope
.
user_id
);
});
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment