2017-04-18 08:01:05 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-26 08:48:41 -06:00
|
|
|
"context"
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2022-01-26 08:48:41 -06:00
|
|
|
"fmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
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"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-02-03 09:27:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-08-10 03:21:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
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
|
|
|
)
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /teams/{team_id}/members teams getTeamMembers
|
|
|
|
//
|
|
|
|
// Get Team Members.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getTeamMembersResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetTeamMembers(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
teamId, err := strconv.ParseInt(web.Params(c.Req)[":teamId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "teamId is invalid", err)
|
|
|
|
}
|
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
query := models.GetTeamMembersQuery{OrgId: c.OrgID, TeamId: teamId, SignedInUser: c.SignedInUser}
|
2022-02-09 10:46:37 -06:00
|
|
|
|
|
|
|
// With accesscontrol the permission check has been done at middleware layer
|
|
|
|
// and the membership filtering will be done at DB layer based on user permissions
|
2022-04-25 03:42:09 -05:00
|
|
|
if hs.AccessControl.IsDisabled() {
|
2022-02-09 10:46:37 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), query.OrgId, query.TeamId, c.SignedInUser); err != nil {
|
|
|
|
return response.Error(403, "Not allowed to list team members", err)
|
|
|
|
}
|
2022-02-09 06:44:38 -06:00
|
|
|
}
|
2017-04-18 08:01:05 -05:00
|
|
|
|
2022-09-20 11:58:04 -05:00
|
|
|
if err := hs.teamService.GetTeamMembers(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 {
|
2022-08-10 03:21:33 -05:00
|
|
|
authProvider := login.GetAuthProviderLabel(member.AuthModule)
|
2019-07-03 08:52:10 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, filteredMembers)
|
2017-04-18 08:01:05 -05:00
|
|
|
}
|
2017-04-19 08:35:11 -05:00
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /teams/{team_id}/members teams addTeamMember
|
|
|
|
//
|
|
|
|
// Add Team Member.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AddTeamMember(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.AddTeamMemberCommand{}
|
2022-01-14 10:55:57 -06:00
|
|
|
var err error
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd.OrgId = c.OrgID
|
2022-01-14 10:55:57 -06:00
|
|
|
cmd.TeamId, err = strconv.ParseInt(web.Params(c.Req)[":teamId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "teamId is invalid", err)
|
|
|
|
}
|
2019-03-11 07:14:06 -05:00
|
|
|
|
2022-04-25 03:42:09 -05:00
|
|
|
if hs.AccessControl.IsDisabled() {
|
2022-01-26 08:48:41 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), cmd.OrgId, cmd.TeamId, c.SignedInUser); err != nil {
|
|
|
|
return response.Error(403, "Not allowed to add team member", err)
|
|
|
|
}
|
2019-03-11 07:14:06 -05:00
|
|
|
}
|
|
|
|
|
2022-09-20 11:58:04 -05:00
|
|
|
isTeamMember, err := hs.teamService.IsTeamMember(c.OrgID, cmd.TeamId, cmd.UserId)
|
2021-03-19 03:14:14 -05:00
|
|
|
if err != nil {
|
2022-01-26 08:48:41 -06:00
|
|
|
return response.Error(500, "Failed to add team member.", err)
|
|
|
|
}
|
|
|
|
if isTeamMember {
|
|
|
|
return response.Error(400, "User is already added to this team", nil)
|
|
|
|
}
|
2018-02-16 04:45:53 -06:00
|
|
|
|
2022-02-17 07:03:45 -06:00
|
|
|
err = addOrUpdateTeamMember(c.Req.Context(), hs.teamPermissionsService, cmd.UserId, cmd.OrgId, cmd.TeamId, getPermissionName(cmd.Permission))
|
2022-01-26 08:48:41 -06:00
|
|
|
if err != nil {
|
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
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &util.DynMap{
|
2017-12-08 09:25:45 -06:00
|
|
|
"message": "Member added to Team",
|
2017-04-19 08:35:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route PUT /teams/{team_id}/members/{user_id} teams updateTeamMember
|
|
|
|
//
|
|
|
|
// Update Team Member.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
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)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
teamId, err := strconv.ParseInt(web.Params(c.Req)[":teamId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "teamId is invalid", err)
|
|
|
|
}
|
2022-01-26 08:48:41 -06:00
|
|
|
userId, err := strconv.ParseInt(web.Params(c.Req)[":userId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "userId is invalid", err)
|
2019-03-11 07:14:06 -05:00
|
|
|
}
|
2022-08-11 06:28:55 -05:00
|
|
|
orgId := c.OrgID
|
2019-03-11 07:14:06 -05:00
|
|
|
|
2022-04-25 03:42:09 -05:00
|
|
|
if hs.AccessControl.IsDisabled() {
|
2022-01-26 08:48:41 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), orgId, teamId, c.SignedInUser); err != nil {
|
|
|
|
return response.Error(403, "Not allowed to update team member", err)
|
|
|
|
}
|
2019-03-13 04:11:53 -05:00
|
|
|
}
|
|
|
|
|
2022-09-20 11:58:04 -05:00
|
|
|
isTeamMember, err := hs.teamService.IsTeamMember(orgId, teamId, userId)
|
2022-01-14 10:55:57 -06:00
|
|
|
if err != nil {
|
2022-01-26 08:48:41 -06:00
|
|
|
return response.Error(500, "Failed to update team member.", err)
|
|
|
|
}
|
|
|
|
if !isTeamMember {
|
|
|
|
return response.Error(404, "Team member not found.", nil)
|
2022-01-14 10:55:57 -06:00
|
|
|
}
|
2019-03-06 08:37:37 -06:00
|
|
|
|
2022-02-17 07:03:45 -06:00
|
|
|
err = addOrUpdateTeamMember(c.Req.Context(), hs.teamPermissionsService, userId, orgId, teamId, getPermissionName(cmd.Permission))
|
2022-01-26 08:48:41 -06:00
|
|
|
if err != nil {
|
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
|
|
|
}
|
|
|
|
|
2022-01-26 08:48:41 -06:00
|
|
|
func getPermissionName(permission models.PermissionType) string {
|
|
|
|
permissionName := permission.String()
|
|
|
|
// Team member permission is 0, which maps to an empty string.
|
|
|
|
// However, we want the team permission service to display "Member" for team members. This is a hack to make it work.
|
|
|
|
if permissionName == "" {
|
|
|
|
permissionName = "Member"
|
|
|
|
}
|
|
|
|
return permissionName
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route DELETE /teams/{team_id}/members/{user_id} teams removeTeamMember
|
|
|
|
//
|
|
|
|
// Remove Member From Team.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) RemoveTeamMember(c *models.ReqContext) response.Response {
|
2022-08-11 06:28:55 -05:00
|
|
|
orgId := c.OrgID
|
2022-01-14 10:55:57 -06:00
|
|
|
teamId, err := strconv.ParseInt(web.Params(c.Req)[":teamId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "teamId is invalid", err)
|
|
|
|
}
|
|
|
|
userId, err := strconv.ParseInt(web.Params(c.Req)[":userId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "userId is invalid", err)
|
|
|
|
}
|
2019-03-11 07:14:06 -05:00
|
|
|
|
2022-04-25 03:42:09 -05:00
|
|
|
if hs.AccessControl.IsDisabled() {
|
2022-01-26 08:48:41 -06:00
|
|
|
if err := hs.teamGuardian.CanAdmin(c.Req.Context(), orgId, teamId, c.SignedInUser); err != nil {
|
|
|
|
return response.Error(403, "Not allowed to remove team member", err)
|
|
|
|
}
|
2019-03-12 10:59:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-26 08:48:41 -06:00
|
|
|
teamIDString := strconv.FormatInt(teamId, 10)
|
2022-02-17 07:03:45 -06:00
|
|
|
if _, err := hs.teamPermissionsService.SetUserPermission(c.Req.Context(), orgId, accesscontrol.User{ID: userId}, teamIDString, ""); 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
|
|
|
|
2022-01-26 08:48:41 -06:00
|
|
|
// addOrUpdateTeamMember adds or updates a team member.
|
2021-03-19 03:14:14 -05:00
|
|
|
//
|
|
|
|
// Stubbable by tests.
|
2022-05-10 08:48:47 -05:00
|
|
|
var addOrUpdateTeamMember = func(ctx context.Context, resourcePermissionService accesscontrol.TeamPermissionsService, userID, orgID, teamID int64, permission string) error {
|
2022-01-26 08:48:41 -06:00
|
|
|
teamIDString := strconv.FormatInt(teamID, 10)
|
2022-02-03 09:27:05 -06:00
|
|
|
if _, err := resourcePermissionService.SetUserPermission(ctx, orgID, accesscontrol.User{ID: userID}, teamIDString, permission); err != nil {
|
2022-01-26 08:48:41 -06:00
|
|
|
return fmt.Errorf("failed setting permissions for user %d in team %d: %w", userID, teamID, err)
|
|
|
|
}
|
|
|
|
return nil
|
2021-03-19 03:14:14 -05:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters getTeamMembers
|
|
|
|
type GetTeamMembersParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
TeamID string `json:"team_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters addTeamMember
|
|
|
|
type AddTeamMemberParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body models.AddTeamMemberCommand `json:"body"`
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
TeamID string `json:"team_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters updateTeamMember
|
|
|
|
type UpdateTeamMemberParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
|
|
|
Body models.UpdateTeamMemberCommand `json:"body"`
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
TeamID string `json:"team_id"`
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters removeTeamMember
|
|
|
|
type RemoveTeamMemberParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
TeamID string `json:"team_id"`
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
UserID int64 `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getTeamMembersResponse
|
|
|
|
type GetTeamMembersResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body []*models.TeamMemberDTO `json:"body"`
|
|
|
|
}
|