2015-01-19 11:01:04 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-08-18 01:17:35 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-08-12 13:03:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-01-19 11:01:04 -06:00
|
|
|
)
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// POST /api/org/users
|
2019-08-12 13:03:48 -05:00
|
|
|
func AddOrgUserToCurrentOrg(c *models.ReqContext, cmd models.AddOrgUserCommand) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
return addOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/orgs/:orgId/users
|
2019-08-12 13:03:48 -05:00
|
|
|
func AddOrgUser(c *models.ReqContext, cmd models.AddOrgUserCommand) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
|
|
|
return addOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
func addOrgUserHelper(cmd models.AddOrgUserCommand) Response {
|
2015-01-19 11:01:04 -06:00
|
|
|
if !cmd.Role.IsValid() {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Invalid role specified", nil)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: cmd.LoginOrEmail}
|
2015-01-19 11:01:04 -06:00
|
|
|
err := bus.Dispatch(&userQuery)
|
|
|
|
if err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "User not found", nil)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
userToAdd := userQuery.Result
|
|
|
|
|
|
|
|
cmd.UserId = userToAdd.Id
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2019-08-12 13:03:48 -05:00
|
|
|
if err == models.ErrOrgUserAlreadyAdded {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(409, "User is already member of this organization", nil)
|
2016-11-23 07:38:44 -06:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Could not add user to organization", err)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("User added to organization")
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/org/users
|
2019-08-12 13:03:48 -05:00
|
|
|
func GetOrgUsersForCurrentOrg(c *models.ReqContext) Response {
|
|
|
|
result, err := getOrgUsersHelper(c.OrgId, c.Query("query"), c.QueryInt("limit"))
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to get users for current organization", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON(200, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/org/users/lookup
|
|
|
|
func GetOrgUsersForCurrentOrgLookup(c *models.ReqContext) Response {
|
|
|
|
isAdmin, err := isOrgAdminFolderAdminOrTeamAdmin(c)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to get users for current organization", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAdmin {
|
|
|
|
return Error(403, "Permission denied", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
orgUsers, err := getOrgUsersHelper(c.OrgId, c.Query("query"), c.QueryInt("limit"))
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to get users for current organization", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*dtos.UserLookupDTO, 0)
|
|
|
|
|
|
|
|
for _, u := range orgUsers {
|
|
|
|
result = append(result, &dtos.UserLookupDTO{
|
|
|
|
UserID: u.UserId,
|
|
|
|
Login: u.Login,
|
|
|
|
AvatarURL: u.AvatarUrl,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON(200, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOrgAdminFolderAdminOrTeamAdmin(c *models.ReqContext) (bool, error) {
|
|
|
|
if c.OrgRole == models.ROLE_ADMIN {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAdminPermissionInFoldersQuery := models.HasAdminPermissionInFoldersQuery{SignedInUser: c.SignedInUser}
|
|
|
|
if err := bus.Dispatch(&hasAdminPermissionInFoldersQuery); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasAdminPermissionInFoldersQuery.Result {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
isAdminOfTeamsQuery := models.IsAdminOfTeamsQuery{SignedInUser: c.SignedInUser}
|
|
|
|
if err := bus.Dispatch(&isAdminOfTeamsQuery); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return isAdminOfTeamsQuery.Result, nil
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/orgs/:orgId/users
|
2019-08-12 13:03:48 -05:00
|
|
|
func GetOrgUsers(c *models.ReqContext) Response {
|
|
|
|
result, err := getOrgUsersHelper(c.ParamsInt64(":orgId"), "", 0)
|
|
|
|
if err != nil {
|
|
|
|
return Error(500, "Failed to get users for organization", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON(200, result)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
func getOrgUsersHelper(orgID int64, query string, limit int) ([]*models.OrgUserDTO, error) {
|
|
|
|
q := models.GetOrgUsersQuery{
|
2018-03-22 06:37:35 -05:00
|
|
|
OrgId: orgID,
|
2018-02-09 03:42:37 -06:00
|
|
|
Query: query,
|
|
|
|
Limit: limit,
|
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2018-02-09 03:42:37 -06:00
|
|
|
if err := bus.Dispatch(&q); err != nil {
|
2019-08-12 13:03:48 -05:00
|
|
|
return nil, err
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2018-02-09 03:42:37 -06:00
|
|
|
for _, user := range q.Result {
|
2017-08-18 01:17:35 -05:00
|
|
|
user.AvatarUrl = dtos.GetGravatarUrl(user.Email)
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
return q.Result, nil
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// PATCH /api/org/users/:userId
|
2019-08-12 13:03:48 -05:00
|
|
|
func UpdateOrgUserForCurrentOrg(c *models.ReqContext, cmd models.UpdateOrgUserCommand) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
cmd.UserId = c.ParamsInt64(":userId")
|
|
|
|
return updateOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH /api/orgs/:orgId/users/:userId
|
2019-08-12 13:03:48 -05:00
|
|
|
func UpdateOrgUser(c *models.ReqContext, cmd models.UpdateOrgUserCommand) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
|
|
|
cmd.UserId = c.ParamsInt64(":userId")
|
|
|
|
return updateOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
func updateOrgUserHelper(cmd models.UpdateOrgUserCommand) Response {
|
2015-05-01 02:48:07 -05:00
|
|
|
if !cmd.Role.IsValid() {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Invalid role specified", nil)
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2019-08-12 13:03:48 -05:00
|
|
|
if err == models.ErrLastOrgAdmin {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Cannot change role so that there is no organization admin left", nil)
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed update org user", err)
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Organization user updated")
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// DELETE /api/org/users/:userId
|
2019-08-12 13:03:48 -05:00
|
|
|
func RemoveOrgUserForCurrentOrg(c *models.ReqContext) Response {
|
|
|
|
return removeOrgUserHelper(&models.RemoveOrgUserCommand{
|
2018-10-11 14:26:06 -05:00
|
|
|
UserId: c.ParamsInt64(":userId"),
|
|
|
|
OrgId: c.OrgId,
|
2018-10-11 00:48:35 -05:00
|
|
|
ShouldDeleteOrphanedUser: true,
|
|
|
|
})
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/orgs/:orgId/users/:userId
|
2019-08-12 13:03:48 -05:00
|
|
|
func RemoveOrgUser(c *models.ReqContext) Response {
|
|
|
|
return removeOrgUserHelper(&models.RemoveOrgUserCommand{
|
2018-10-11 00:48:35 -05:00
|
|
|
UserId: c.ParamsInt64(":userId"),
|
|
|
|
OrgId: c.ParamsInt64(":orgId"),
|
|
|
|
})
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2019-08-12 13:03:48 -05:00
|
|
|
func removeOrgUserHelper(cmd *models.RemoveOrgUserCommand) Response {
|
2018-10-11 00:48:35 -05:00
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
2019-08-12 13:03:48 -05:00
|
|
|
if err == models.ErrLastOrgAdmin {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Cannot remove last organization admin", nil)
|
2015-01-20 08:48:19 -06:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to remove user from organization", err)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2018-10-11 14:20:53 -05:00
|
|
|
if cmd.UserWasDeleted {
|
2018-10-11 00:58:22 -05:00
|
|
|
return Success("User deleted")
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("User removed from organization")
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|