From 4038b4d17e47684c1aaa05ea432c52123f24dcf1 Mon Sep 17 00:00:00 2001 From: Mukesh Date: Thu, 26 May 2016 10:21:23 +0530 Subject: [PATCH] Set active org through admin api (#5179) --- pkg/api/api.go | 1 + pkg/api/user.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/api/api.go b/pkg/api/api.go index f4b096f0b04..070f400aaad 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -115,6 +115,7 @@ func Register(r *macaron.Macaron) { r.Get("/:id", wrap(GetUserById)) r.Get("/:id/orgs", wrap(GetUserOrgList)) r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser)) + r.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg)) }, reqGrafanaAdmin) // org information available to all users. diff --git a/pkg/api/user.go b/pkg/api/user.go index 8f54feaf6a0..f98eec02c40 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -40,6 +40,24 @@ func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { return handleUpdateUser(cmd) } +//POST /api/users/:id/using/:orgId +func UpdateUserActiveOrg(c *middleware.Context) Response { + userId := c.ParamsInt64(":id") + orgId := c.ParamsInt64(":orgId") + + if !validateUsingOrg(userId, orgId) { + return ApiError(401, "Not a valid organization", nil) + } + + cmd := m.SetUsingOrgCommand{UserId: userId, OrgId: orgId} + + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "Failed change active organization", err) + } + + return ApiSuccess("Active organization changed") +} + func handleUpdateUser(cmd m.UpdateUserCommand) Response { if len(cmd.Login) == 0 { cmd.Login = cmd.Email