grafana/pkg/api/org_users.go

128 lines
3.3 KiB
Go
Raw Normal View History

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"
m "github.com/grafana/grafana/pkg/models"
)
// POST /api/org/users
2018-03-07 10:54:50 -06:00
func AddOrgUserToCurrentOrg(c *m.ReqContext, cmd m.AddOrgUserCommand) Response {
cmd.OrgId = c.OrgId
return addOrgUserHelper(cmd)
}
// POST /api/orgs/:orgId/users
2018-03-07 10:54:50 -06:00
func AddOrgUser(c *m.ReqContext, cmd m.AddOrgUserCommand) Response {
cmd.OrgId = c.ParamsInt64(":orgId")
return addOrgUserHelper(cmd)
}
func addOrgUserHelper(cmd m.AddOrgUserCommand) Response {
if !cmd.Role.IsValid() {
2018-03-22 16:13:46 -05:00
return Error(400, "Invalid role specified", nil)
}
userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.LoginOrEmail}
err := bus.Dispatch(&userQuery)
if err != nil {
2018-03-22 16:13:46 -05:00
return Error(404, "User not found", nil)
}
userToAdd := userQuery.Result
cmd.UserId = userToAdd.Id
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrOrgUserAlreadyAdded {
2018-03-22 16:13:46 -05:00
return Error(409, "User is already member of this organization", nil)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Could not add user to organization", err)
}
2018-03-22 16:13:46 -05:00
return Success("User added to organization")
}
// GET /api/org/users
2018-03-07 10:54:50 -06:00
func GetOrgUsersForCurrentOrg(c *m.ReqContext) Response {
return getOrgUsersHelper(c.OrgId, c.Params("query"), c.ParamsInt("limit"))
}
// GET /api/orgs/:orgId/users
2018-03-07 10:54:50 -06:00
func GetOrgUsers(c *m.ReqContext) Response {
return getOrgUsersHelper(c.ParamsInt64(":orgId"), "", 0)
}
2018-03-22 06:37:35 -05:00
func getOrgUsersHelper(orgID int64, query string, limit int) Response {
q := m.GetOrgUsersQuery{
2018-03-22 06:37:35 -05:00
OrgId: orgID,
Query: query,
Limit: limit,
}
if err := bus.Dispatch(&q); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get account user", err)
}
for _, user := range q.Result {
2017-08-18 01:17:35 -05:00
user.AvatarUrl = dtos.GetGravatarUrl(user.Email)
}
2018-03-22 16:13:46 -05:00
return JSON(200, q.Result)
}
// PATCH /api/org/users/:userId
2018-03-07 10:54:50 -06:00
func UpdateOrgUserForCurrentOrg(c *m.ReqContext, cmd m.UpdateOrgUserCommand) Response {
cmd.OrgId = c.OrgId
cmd.UserId = c.ParamsInt64(":userId")
return updateOrgUserHelper(cmd)
}
// PATCH /api/orgs/:orgId/users/:userId
2018-03-07 10:54:50 -06:00
func UpdateOrgUser(c *m.ReqContext, cmd m.UpdateOrgUserCommand) Response {
cmd.OrgId = c.ParamsInt64(":orgId")
cmd.UserId = c.ParamsInt64(":userId")
return updateOrgUserHelper(cmd)
}
func updateOrgUserHelper(cmd m.UpdateOrgUserCommand) Response {
if !cmd.Role.IsValid() {
2018-03-22 16:13:46 -05:00
return Error(400, "Invalid role specified", nil)
}
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrLastOrgAdmin {
2018-03-22 16:13:46 -05:00
return Error(400, "Cannot change role so that there is no organization admin left", nil)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed update org user", err)
}
2018-03-22 16:13:46 -05:00
return Success("Organization user updated")
}
// DELETE /api/org/users/:userId
2018-03-07 10:54:50 -06:00
func RemoveOrgUserForCurrentOrg(c *m.ReqContext) Response {
2018-03-22 06:37:35 -05:00
userID := c.ParamsInt64(":userId")
return removeOrgUserHelper(c.OrgId, userID)
}
// DELETE /api/orgs/:orgId/users/:userId
2018-03-07 10:54:50 -06:00
func RemoveOrgUser(c *m.ReqContext) Response {
2018-03-22 06:37:35 -05:00
userID := c.ParamsInt64(":userId")
orgID := c.ParamsInt64(":orgId")
return removeOrgUserHelper(orgID, userID)
}
2018-03-22 06:37:35 -05:00
func removeOrgUserHelper(orgID int64, userID int64) Response {
cmd := m.RemoveOrgUserCommand{OrgId: orgID, UserId: userID}
if err := bus.Dispatch(&cmd); err != nil {
if err == m.ErrLastOrgAdmin {
2018-03-22 16:13:46 -05:00
return Error(400, "Cannot remove last organization admin", nil)
}
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to remove user from organization", err)
}
2018-03-22 16:13:46 -05:00
return Success("User removed from organization")
}