From 0d61f895773fd91f338769700ed70f3968fe528c Mon Sep 17 00:00:00 2001 From: Leonard Gram Date: Mon, 11 Mar 2019 11:26:01 +0100 Subject: [PATCH] teams: cleanup. --- pkg/api/team.go | 7 ++++++- pkg/services/teams/team.go | 10 +--------- pkg/services/teams/teams_test.go | 16 +++++++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/api/team.go b/pkg/api/team.go index 3d357fa9763..6e62b186f83 100644 --- a/pkg/api/team.go +++ b/pkg/api/team.go @@ -41,7 +41,12 @@ func (hs *HTTPServer) CreateTeam(c *m.ReqContext, cmd m.CreateTeamCommand) Respo func UpdateTeam(c *m.ReqContext, cmd m.UpdateTeamCommand) Response { cmd.OrgId = c.OrgId cmd.Id = c.ParamsInt64(":teamId") - if err := teams.UpdateTeam(c.SignedInUser, &cmd); err != nil { + + if err := teams.CanUpdateTeam(cmd.OrgId, cmd.Id, c.SignedInUser); err != nil { + return Error(403, "User not allowed to update team", err) + } + + if err := bus.Dispatch(&cmd); err != nil { if err == m.ErrTeamNameTaken { return Error(400, "Team name taken", err) } diff --git a/pkg/services/teams/team.go b/pkg/services/teams/team.go index ae9327699be..9419d649204 100644 --- a/pkg/services/teams/team.go +++ b/pkg/services/teams/team.go @@ -5,7 +5,7 @@ import ( m "github.com/grafana/grafana/pkg/models" ) -func canUpdateTeam(orgId int64, teamId int64, user *m.SignedInUser) error { +func CanUpdateTeam(orgId int64, teamId int64, user *m.SignedInUser) error { if user.OrgRole == m.ROLE_ADMIN { return nil } @@ -34,11 +34,3 @@ func canUpdateTeam(orgId int64, teamId int64, user *m.SignedInUser) error { return m.ErrNotAllowedToUpdateTeam } - -func UpdateTeam(user *m.SignedInUser, cmd *m.UpdateTeamCommand) error { - if err := canUpdateTeam(cmd.OrgId, cmd.Id, user); err != nil { - return err - } - - return bus.Dispatch(cmd) -} diff --git a/pkg/services/teams/teams_test.go b/pkg/services/teams/teams_test.go index 1282eefc611..7fac1be6880 100644 --- a/pkg/services/teams/teams_test.go +++ b/pkg/services/teams/teams_test.go @@ -40,12 +40,12 @@ func TestUpdateTeam(t *testing.T) { return nil }) - err := UpdateTeam(&editor, &updateTeamCmd) + err := CanUpdateTeam(&editor, &updateTeamCmd) So(err, ShouldEqual, m.ErrNotAllowedToUpdateTeam) }) }) - Convey("Given an editor and a team he is a member of", func() { + Convey("Given an editor and a team he is an admin in", func() { Convey("Should be able to update the team", func() { teamUpdatedCallback := updateTeamCalled() @@ -59,7 +59,7 @@ func TestUpdateTeam(t *testing.T) { return nil }) - err := UpdateTeam(&editor, &updateTeamCmd) + err := CanUpdateTeam(&editor, &updateTeamCmd) So(teamUpdatedCallback(), ShouldBeTrue) So(err, ShouldBeNil) }) @@ -88,7 +88,7 @@ func TestUpdateTeam(t *testing.T) { return nil }) - err := UpdateTeam(&editor, &cmd) + err := CanUpdateTeam(&editor, &cmd) So(err, ShouldEqual, m.ErrNotAllowedToUpdateTeamInDifferentOrg) }) }) @@ -96,12 +96,18 @@ func TestUpdateTeam(t *testing.T) { Convey("Given an org admin and a team", func() { Convey("Should be able to update the team", func() { teamUpdatedCallback := updateTeamCalled() - err := UpdateTeam(&admin, &updateTeamCmd) + err := CanUpdateTeam(&admin, &updateTeamCmd) So(teamUpdatedCallback(), ShouldBeTrue) So(err, ShouldBeNil) }) }) + Convey("Given that the editorsCanOwn feature toggle is disabled", func() { + + Convey("Given an editor and a team he is an admin", func() { + + }) + }) }) }