2017-04-18 08:01:05 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2020-11-19 06:34:28 -06:00
|
|
|
|
2017-12-20 14:20:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2017-04-18 08:01:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-19 03:14:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2017-04-19 08:35:11 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2017-04-18 08:01:05 -05:00
|
|
|
)
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// GET /api/teams/:teamId/members
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetTeamMembers(c *models.ReqContext) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetTeamMembersQuery{OrgId: c.OrgId, TeamId: c.ParamsInt64(":teamId")}
|
2017-04-18 08:01:05 -05:00
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get Team Members", err)
|
2017-04-18 08:01:05 -05:00
|
|
|
}
|
|
|
|
|
2020-11-24 05:10:32 -06:00
|
|
|
filteredMembers := make([]*models.TeamMemberDTO, 0, len(query.Result))
|
2017-12-20 14:20:12 -06:00
|
|
|
for _, member := range query.Result {
|
2020-11-24 05:10:32 -06:00
|
|
|
if dtos.IsHiddenUser(member.Login, c.SignedInUser, hs.Cfg) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:20:12 -06:00
|
|
|
member.AvatarUrl = dtos.GetGravatarUrl(member.Email)
|
2018-09-14 10:27:36 -05:00
|
|
|
member.Labels = []string{}
|
|
|
|
|
2022-01-07 14:11:23 -06:00
|
|
|
if hs.License.FeatureEnabled("teamgroupsync") && member.External {
|
2019-07-03 08:52:10 -05:00
|
|
|
authProvider := GetAuthProviderLabel(member.AuthModule)
|
|
|
|
member.Labels = append(member.Labels, authProvider)
|
2018-09-14 10:27:36 -05:00
|
|
|
}
|
2020-11-24 05:10:32 -06:00
|
|
|
|
|
|
|
filteredMembers = append(filteredMembers, member)
|
2017-12-20 14:20:12 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, filteredMembers)
|
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
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AddTeamMember(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.AddTeamMemberCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
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
|
|
|
|
2022-01-07 06:38:00 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), cmd.OrgId, cmd.TeamId, c.SignedInUser); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Not allowed to add team member", err)
|
2019-03-11 07:14:06 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:14:14 -05:00
|
|
|
err := addTeamMember(hs.SQLStore, cmd.UserId, cmd.OrgId, cmd.TeamId, cmd.External, cmd.Permission)
|
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrTeamNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Team not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrTeamMemberAlreadyAdded) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to add Member to Team", err)
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateTeamMember(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateTeamMemberCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2019-03-11 07:14:06 -05:00
|
|
|
teamId := c.ParamsInt64(":teamId")
|
|
|
|
orgId := c.OrgId
|
|
|
|
|
2022-01-07 06:38:00 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), orgId, teamId, c.SignedInUser); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Not allowed to update team member", err)
|
2019-03-11 07:14:06 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if c.OrgRole != models.ROLE_ADMIN {
|
2019-03-13 04:11:53 -05:00
|
|
|
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
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := hs.Bus.Dispatch(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrTeamMemberNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Team member not found.", nil)
|
2019-03-06 08:37:37 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update team member.", err)
|
2019-03-06 08:37:37 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Team member updated")
|
2019-03-06 08:37:37 -06:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:25:45 -06:00
|
|
|
// DELETE /api/teams/:teamId/members/:userId
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) RemoveTeamMember(c *models.ReqContext) response.Response {
|
2019-03-11 07:14:06 -05:00
|
|
|
orgId := c.OrgId
|
|
|
|
teamId := c.ParamsInt64(":teamId")
|
|
|
|
userId := c.ParamsInt64(":userId")
|
|
|
|
|
2022-01-07 06:38:00 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), orgId, teamId, c.SignedInUser); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(403, "Not allowed to remove team member", err)
|
2019-03-11 07:14:06 -05:00
|
|
|
}
|
|
|
|
|
2019-03-12 10:59:39 -05:00
|
|
|
protectLastAdmin := false
|
2020-03-04 05:57:20 -06:00
|
|
|
if c.OrgRole != models.ROLE_ADMIN {
|
2019-03-12 10:59:39 -05:00
|
|
|
protectLastAdmin = true
|
|
|
|
}
|
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := hs.Bus.Dispatch(c.Req.Context(), &models.RemoveTeamMemberCommand{OrgId: orgId, TeamId: teamId, UserId: userId, ProtectLastAdmin: protectLastAdmin}); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrTeamNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Team not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrTeamMemberNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Team member not found", nil)
|
2018-02-16 04:45:53 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to remove Member from Team", err)
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Team Member removed")
|
2017-04-19 08:35:11 -05:00
|
|
|
}
|
2021-03-19 03:14:14 -05:00
|
|
|
|
|
|
|
// addTeamMember adds a team member.
|
|
|
|
//
|
|
|
|
// Stubbable by tests.
|
|
|
|
var addTeamMember = func(sqlStore *sqlstore.SQLStore, userID, orgID, teamID int64, isExternal bool,
|
|
|
|
permission models.PermissionType) error {
|
|
|
|
return sqlStore.AddTeamMember(userID, orgID, teamID, isExternal, permission)
|
|
|
|
}
|