2015-01-19 11:01:04 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "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
|
|
|
|
func AddOrgUserToCurrentOrg(c *middleware.Context, cmd m.AddOrgUserCommand) Response {
|
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
return addOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/orgs/:orgId/users
|
|
|
|
func AddOrgUser(c *middleware.Context, cmd m.AddOrgUserCommand) Response {
|
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
|
|
|
return addOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addOrgUserHelper(cmd m.AddOrgUserCommand) Response {
|
2015-01-19 11:01:04 -06:00
|
|
|
if !cmd.Role.IsValid() {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(400, "Invalid role specified", nil)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.LoginOrEmail}
|
|
|
|
err := bus.Dispatch(&userQuery)
|
|
|
|
if err != nil {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(404, "User not found", nil)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
userToAdd := userQuery.Result
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// if userToAdd.Id == c.UserId {
|
|
|
|
// return ApiError(400, "Cannot add yourself as user", nil)
|
|
|
|
// }
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
cmd.UserId = userToAdd.Id
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Could not add user to organization", err)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiSuccess("User added to organization")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/org/users
|
|
|
|
func GetOrgUsersForCurrentOrg(c *middleware.Context) Response {
|
|
|
|
return getOrgUsersHelper(c.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/orgs/:orgId/users
|
|
|
|
func GetOrgUsers(c *middleware.Context) Response {
|
|
|
|
return getOrgUsersHelper(c.ParamsInt64(":orgId"))
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
func getOrgUsersHelper(orgId int64) Response {
|
|
|
|
query := m.GetOrgUsersQuery{OrgId: orgId}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed to get account user", err)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return Json(200, query.Result)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// PATCH /api/org/users/:userId
|
|
|
|
func UpdateOrgUserForCurrentOrg(c *middleware.Context, cmd m.UpdateOrgUserCommand) Response {
|
|
|
|
cmd.OrgId = c.OrgId
|
|
|
|
cmd.UserId = c.ParamsInt64(":userId")
|
|
|
|
return updateOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH /api/orgs/:orgId/users/:userId
|
|
|
|
func UpdateOrgUser(c *middleware.Context, cmd m.UpdateOrgUserCommand) Response {
|
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
|
|
|
cmd.UserId = c.ParamsInt64(":userId")
|
|
|
|
return updateOrgUserHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateOrgUserHelper(cmd m.UpdateOrgUserCommand) Response {
|
2015-05-01 02:48:07 -05:00
|
|
|
if !cmd.Role.IsValid() {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(400, "Invalid role specified", nil)
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed update org user", err)
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiSuccess("Organization user updated")
|
2015-05-01 02:48:07 -05:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// DELETE /api/org/users/:userId
|
|
|
|
func RemoveOrgUserForCurrentOrg(c *middleware.Context) Response {
|
|
|
|
userId := c.ParamsInt64(":userId")
|
|
|
|
return removeOrgUserHelper(c.OrgId, userId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/orgs/:orgId/users/:userId
|
|
|
|
func RemoveOrgUser(c *middleware.Context) Response {
|
|
|
|
userId := c.ParamsInt64(":userId")
|
|
|
|
orgId := c.ParamsInt64(":orgId")
|
|
|
|
return removeOrgUserHelper(orgId, userId)
|
|
|
|
}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
func removeOrgUserHelper(orgId int64, userId int64) Response {
|
|
|
|
cmd := m.RemoveOrgUserCommand{OrgId: orgId, UserId: userId}
|
2015-01-19 11:01:04 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2015-02-23 13:07:49 -06:00
|
|
|
if err == m.ErrLastOrgAdmin {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(400, "Cannot remove last organization admin", nil)
|
2015-01-20 08:48:19 -06:00
|
|
|
}
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed to remove user from organization", err)
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiSuccess("User removed from organization")
|
2015-01-19 11:01:04 -06:00
|
|
|
}
|