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
21d3d274
Commit
21d3d274
authored
Mar 12, 2019
by
Leonard Gram
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
teams: editors can't remove the last admin from a team.
parent
8593668a
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
3 deletions
+62
-3
pkg/api/api.go
+1
-1
pkg/api/team_members.go
+7
-2
pkg/models/team.go
+1
-0
pkg/models/team_member.go
+1
-0
pkg/services/sqlstore/team.go
+35
-0
pkg/services/sqlstore/team_test.go
+17
-0
No files found.
pkg/api/api.go
View file @
21d3d274
...
@@ -160,7 +160,7 @@ func (hs *HTTPServer) registerRoutes() {
...
@@ -160,7 +160,7 @@ func (hs *HTTPServer) registerRoutes() {
teamsRoute
.
Get
(
"/:teamId/members"
,
Wrap
(
GetTeamMembers
))
teamsRoute
.
Get
(
"/:teamId/members"
,
Wrap
(
GetTeamMembers
))
teamsRoute
.
Post
(
"/:teamId/members"
,
bind
(
m
.
AddTeamMemberCommand
{}),
Wrap
(
AddTeamMember
))
teamsRoute
.
Post
(
"/:teamId/members"
,
bind
(
m
.
AddTeamMemberCommand
{}),
Wrap
(
AddTeamMember
))
teamsRoute
.
Put
(
"/:teamId/members/:userId"
,
bind
(
m
.
UpdateTeamMemberCommand
{}),
Wrap
(
UpdateTeamMember
))
teamsRoute
.
Put
(
"/:teamId/members/:userId"
,
bind
(
m
.
UpdateTeamMemberCommand
{}),
Wrap
(
UpdateTeamMember
))
teamsRoute
.
Delete
(
"/:teamId/members/:userId"
,
Wrap
(
RemoveTeamMember
))
teamsRoute
.
Delete
(
"/:teamId/members/:userId"
,
Wrap
(
hs
.
RemoveTeamMember
))
teamsRoute
.
Get
(
"/:teamId/preferences"
,
Wrap
(
GetTeamPreferences
))
teamsRoute
.
Get
(
"/:teamId/preferences"
,
Wrap
(
GetTeamPreferences
))
teamsRoute
.
Put
(
"/:teamId/preferences"
,
bind
(
dtos
.
UpdatePrefsCmd
{}),
Wrap
(
UpdateTeamPreferences
))
teamsRoute
.
Put
(
"/:teamId/preferences"
,
bind
(
dtos
.
UpdatePrefsCmd
{}),
Wrap
(
UpdateTeamPreferences
))
},
reqAdminOrEditorCanAdmin
)
},
reqAdminOrEditorCanAdmin
)
...
...
pkg/api/team_members.go
View file @
21d3d274
...
@@ -81,7 +81,7 @@ func UpdateTeamMember(c *m.ReqContext, cmd m.UpdateTeamMemberCommand) Response {
...
@@ -81,7 +81,7 @@ func UpdateTeamMember(c *m.ReqContext, cmd m.UpdateTeamMemberCommand) Response {
}
}
// DELETE /api/teams/:teamId/members/:userId
// DELETE /api/teams/:teamId/members/:userId
func
RemoveTeamMember
(
c
*
m
.
ReqContext
)
Response
{
func
(
hs
*
HTTPServer
)
RemoveTeamMember
(
c
*
m
.
ReqContext
)
Response
{
orgId
:=
c
.
OrgId
orgId
:=
c
.
OrgId
teamId
:=
c
.
ParamsInt64
(
":teamId"
)
teamId
:=
c
.
ParamsInt64
(
":teamId"
)
userId
:=
c
.
ParamsInt64
(
":userId"
)
userId
:=
c
.
ParamsInt64
(
":userId"
)
...
@@ -90,7 +90,12 @@ func RemoveTeamMember(c *m.ReqContext) Response {
...
@@ -90,7 +90,12 @@ func RemoveTeamMember(c *m.ReqContext) Response {
return
Error
(
403
,
"Not allowed to remove team member"
,
err
)
return
Error
(
403
,
"Not allowed to remove team member"
,
err
)
}
}
if
err
:=
bus
.
Dispatch
(
&
m
.
RemoveTeamMemberCommand
{
OrgId
:
orgId
,
TeamId
:
teamId
,
UserId
:
userId
});
err
!=
nil
{
protectLastAdmin
:=
false
if
c
.
OrgRole
==
m
.
ROLE_EDITOR
{
protectLastAdmin
=
true
}
if
err
:=
bus
.
Dispatch
(
&
m
.
RemoveTeamMemberCommand
{
OrgId
:
orgId
,
TeamId
:
teamId
,
UserId
:
userId
,
ProtectLastAdmin
:
protectLastAdmin
});
err
!=
nil
{
if
err
==
m
.
ErrTeamNotFound
{
if
err
==
m
.
ErrTeamNotFound
{
return
Error
(
404
,
"Team not found"
,
nil
)
return
Error
(
404
,
"Team not found"
,
nil
)
}
}
...
...
pkg/models/team.go
View file @
21d3d274
...
@@ -10,6 +10,7 @@ var (
...
@@ -10,6 +10,7 @@ var (
ErrTeamNotFound
=
errors
.
New
(
"Team not found"
)
ErrTeamNotFound
=
errors
.
New
(
"Team not found"
)
ErrTeamNameTaken
=
errors
.
New
(
"Team name is taken"
)
ErrTeamNameTaken
=
errors
.
New
(
"Team name is taken"
)
ErrTeamMemberNotFound
=
errors
.
New
(
"Team member not found"
)
ErrTeamMemberNotFound
=
errors
.
New
(
"Team member not found"
)
ErrLastTeamAdmin
=
errors
.
New
(
"Not allowed to remove last admin"
)
ErrNotAllowedToUpdateTeam
=
errors
.
New
(
"User not allowed to update team"
)
ErrNotAllowedToUpdateTeam
=
errors
.
New
(
"User not allowed to update team"
)
ErrNotAllowedToUpdateTeamInDifferentOrg
=
errors
.
New
(
"User not allowed to update team in another org"
)
ErrNotAllowedToUpdateTeamInDifferentOrg
=
errors
.
New
(
"User not allowed to update team in another org"
)
)
)
...
...
pkg/models/team_member.go
View file @
21d3d274
...
@@ -45,6 +45,7 @@ type RemoveTeamMemberCommand struct {
...
@@ -45,6 +45,7 @@ type RemoveTeamMemberCommand struct {
OrgId
int64
`json:"-"`
OrgId
int64
`json:"-"`
UserId
int64
UserId
int64
TeamId
int64
TeamId
int64
ProtectLastAdmin
bool
`json:"-"`
}
}
// ----------------------
// ----------------------
...
...
pkg/services/sqlstore/team.go
View file @
21d3d274
...
@@ -285,6 +285,18 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error {
...
@@ -285,6 +285,18 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error {
return
err
return
err
}
}
if
cmd
.
ProtectLastAdmin
{
lastAdmin
,
err
:=
isLastAdmin
(
sess
,
cmd
.
OrgId
,
cmd
.
TeamId
,
cmd
.
UserId
)
if
err
!=
nil
{
return
err
}
if
lastAdmin
{
return
m
.
ErrLastTeamAdmin
}
}
var
rawSql
=
"DELETE FROM team_member WHERE org_id=? and team_id=? and user_id=?"
var
rawSql
=
"DELETE FROM team_member WHERE org_id=? and team_id=? and user_id=?"
res
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
OrgId
,
cmd
.
TeamId
,
cmd
.
UserId
)
res
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
OrgId
,
cmd
.
TeamId
,
cmd
.
UserId
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -299,6 +311,29 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error {
...
@@ -299,6 +311,29 @@ func RemoveTeamMember(cmd *m.RemoveTeamMemberCommand) error {
})
})
}
}
func
isLastAdmin
(
sess
*
DBSession
,
orgId
int64
,
teamId
int64
,
userId
int64
)
(
bool
,
error
)
{
rawSql
:=
"SELECT user_id FROM team_member WHERE org_id=? and team_id=? and permission=?"
userIds
:=
[]
*
int64
{}
err
:=
sess
.
SQL
(
rawSql
,
orgId
,
teamId
,
m
.
PERMISSION_ADMIN
)
.
Find
(
&
userIds
)
if
err
!=
nil
{
return
false
,
err
}
isAdmin
:=
false
for
_
,
adminId
:=
range
userIds
{
if
userId
==
*
adminId
{
isAdmin
=
true
break
}
}
if
isAdmin
&&
len
(
userIds
)
==
1
{
return
true
,
nil
}
return
false
,
err
}
// GetTeamMembers return a list of members for the specified team
// GetTeamMembers return a list of members for the specified team
func
GetTeamMembers
(
query
*
m
.
GetTeamMembersQuery
)
error
{
func
GetTeamMembers
(
query
*
m
.
GetTeamMembersQuery
)
error
{
query
.
Result
=
make
([]
*
m
.
TeamMemberDTO
,
0
)
query
.
Result
=
make
([]
*
m
.
TeamMemberDTO
,
0
)
...
...
pkg/services/sqlstore/team_test.go
View file @
21d3d274
...
@@ -152,6 +152,23 @@ func TestTeamCommandsAndQueries(t *testing.T) {
...
@@ -152,6 +152,23 @@ func TestTeamCommandsAndQueries(t *testing.T) {
So
(
len
(
q2
.
Result
),
ShouldEqual
,
0
)
So
(
len
(
q2
.
Result
),
ShouldEqual
,
0
)
})
})
Convey
(
"When ProtectLastAdmin is set to true"
,
func
()
{
err
=
AddTeamMember
(
&
m
.
AddTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
group1
.
Result
.
Id
,
UserId
:
userIds
[
0
],
Permission
:
int64
(
m
.
PERMISSION_ADMIN
)})
So
(
err
,
ShouldBeNil
)
Convey
(
"A user should not be able to remove the last admin"
,
func
()
{
err
=
RemoveTeamMember
(
&
m
.
RemoveTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
group1
.
Result
.
Id
,
UserId
:
userIds
[
0
],
ProtectLastAdmin
:
true
})
So
(
err
,
ShouldEqual
,
m
.
ErrLastTeamAdmin
)
})
Convey
(
"A user should be able to remove an admin if there are other admins"
,
func
()
{
err
=
AddTeamMember
(
&
m
.
AddTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
group1
.
Result
.
Id
,
UserId
:
userIds
[
1
],
Permission
:
int64
(
m
.
PERMISSION_ADMIN
)})
err
=
RemoveTeamMember
(
&
m
.
RemoveTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
group1
.
Result
.
Id
,
UserId
:
userIds
[
0
],
ProtectLastAdmin
:
true
})
So
(
err
,
ShouldEqual
,
nil
)
})
})
Convey
(
"Should be able to remove a group with users and permissions"
,
func
()
{
Convey
(
"Should be able to remove a group with users and permissions"
,
func
()
{
groupId
:=
group2
.
Result
.
Id
groupId
:=
group2
.
Result
.
Id
err
:=
AddTeamMember
(
&
m
.
AddTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
groupId
,
UserId
:
userIds
[
1
]})
err
:=
AddTeamMember
(
&
m
.
AddTeamMemberCommand
{
OrgId
:
testOrgId
,
TeamId
:
groupId
,
UserId
:
userIds
[
1
]})
...
...
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