2017-04-18 08:01:05 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-12-20 14:20:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2017-04-18 08:01:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2019-03-12 07:59:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/teamguardian"
|
2018-09-14 10:27:36 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2017-04-19 08:35:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2017-04-18 08:01:05 -05:00
|
|
|
)
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// GET /api/teams/:teamId/members
|
2018-03-07 10:54:50 -06:00
|
|
|
func GetTeamMembers(c *m.ReqContext) Response {
|
2018-02-09 10:26:15 -06:00
|
|
|
query := m.GetTeamMembersQuery{OrgId: c.OrgId, TeamId: c.ParamsInt64(":teamId")}
|
2017-04-18 08:01:05 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get Team Members", err)
|
2017-04-18 08:01:05 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 14:20:12 -06:00
|
|
|
for _, member := range query.Result {
|
|
|
|
member.AvatarUrl = dtos.GetGravatarUrl(member.Email)
|
2018-09-14 10:27:36 -05:00
|
|
|
member.Labels = []string{}
|
|
|
|
|
2019-07-03 08:52:10 -05:00
|
|
|
if setting.IsEnterprise && member.External {
|
|
|
|
authProvider := GetAuthProviderLabel(member.AuthModule)
|
|
|
|
member.Labels = append(member.Labels, authProvider)
|
2018-09-14 10:27:36 -05:00
|
|
|
}
|
2017-12-20 14:20:12 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, query.Result)
|
2017-04-18 08:01:05 -05:00
|
|
|
}
|
2017-04-19 08:35:11 -05:00
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// POST /api/teams/:teamId/members
|
2019-03-14 06:10:10 -05:00
|
|
|
func (hs *HTTPServer) AddTeamMember(c *m.ReqContext, cmd m.AddTeamMemberCommand) Response {
|
2019-03-13 10:46:35 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
cmd.TeamId = c.ParamsInt64(":teamId")
|
2019-03-11 07:14:06 -05:00
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := teamguardian.CanAdmin(hs.Bus, cmd.OrgId, cmd.TeamId, c.SignedInUser); err != nil {
|
2019-03-11 07:14:06 -05:00
|
|
|
return Error(403, "Not allowed to add team member", err)
|
|
|
|
}
|
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
2018-02-16 04:45:53 -06:00
|
|
|
if err == m.ErrTeamNotFound {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Team not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
if err == m.ErrTeamMemberAlreadyAdded {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "User is already added to this team", nil)
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
2018-02-16 04:45:53 -06:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to add Member to Team", err)
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &util.DynMap{
|
2017-12-08 09:25:45 -06:00
|
|
|
"message": "Member added to Team",
|
2017-04-19 08:35:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-06 08:37:37 -06:00
|
|
|
// PUT /:teamId/members/:userId
|
2019-03-14 06:10:10 -05:00
|
|
|
func (hs *HTTPServer) UpdateTeamMember(c *m.ReqContext, cmd m.UpdateTeamMemberCommand) Response {
|
2019-03-11 07:14:06 -05:00
|
|
|
teamId := c.ParamsInt64(":teamId")
|
|
|
|
orgId := c.OrgId
|
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := teamguardian.CanAdmin(hs.Bus, orgId, teamId, c.SignedInUser); err != nil {
|
2019-03-11 07:14:06 -05:00
|
|
|
return Error(403, "Not allowed to update team member", err)
|
|
|
|
}
|
|
|
|
|
2019-03-13 04:11:53 -05:00
|
|
|
if c.OrgRole != m.ROLE_ADMIN {
|
|
|
|
cmd.ProtectLastAdmin = true
|
|
|
|
}
|
|
|
|
|
2019-03-11 07:14:06 -05:00
|
|
|
cmd.TeamId = teamId
|
2019-03-06 08:37:37 -06:00
|
|
|
cmd.UserId = c.ParamsInt64(":userId")
|
2019-03-11 07:14:06 -05:00
|
|
|
cmd.OrgId = orgId
|
2019-03-06 08:37:37 -06:00
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
2019-03-06 08:37:37 -06:00
|
|
|
if err == m.ErrTeamMemberNotFound {
|
|
|
|
return Error(404, "Team member not found.", nil)
|
|
|
|
}
|
|
|
|
return Error(500, "Failed to update team member.", err)
|
|
|
|
}
|
|
|
|
return Success("Team member updated")
|
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// DELETE /api/teams/:teamId/members/:userId
|
2019-03-12 10:59:39 -05:00
|
|
|
func (hs *HTTPServer) RemoveTeamMember(c *m.ReqContext) Response {
|
2019-03-11 07:14:06 -05:00
|
|
|
orgId := c.OrgId
|
|
|
|
teamId := c.ParamsInt64(":teamId")
|
|
|
|
userId := c.ParamsInt64(":userId")
|
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := teamguardian.CanAdmin(hs.Bus, orgId, teamId, c.SignedInUser); err != nil {
|
2019-03-11 07:14:06 -05:00
|
|
|
return Error(403, "Not allowed to remove team member", err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 10:59:39 -05:00
|
|
|
protectLastAdmin := false
|
2019-03-13 04:11:53 -05:00
|
|
|
if c.OrgRole != m.ROLE_ADMIN {
|
2019-03-12 10:59:39 -05:00
|
|
|
protectLastAdmin = true
|
|
|
|
}
|
|
|
|
|
2019-03-14 06:10:10 -05:00
|
|
|
if err := hs.Bus.Dispatch(&m.RemoveTeamMemberCommand{OrgId: orgId, TeamId: teamId, UserId: userId, ProtectLastAdmin: protectLastAdmin}); err != nil {
|
2018-02-16 04:45:53 -06:00
|
|
|
if err == m.ErrTeamNotFound {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Team not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == m.ErrTeamMemberNotFound {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Team member not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to remove Member from Team", err)
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Team Member removed")
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|