From fbc6bb21123d3bb97efe61c19abe7896e3a741a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 18 May 2015 19:06:19 +0200 Subject: [PATCH] More refactoring of user http api, trying to reuse handlers for sign in user and admin operations --- pkg/api/admin_users.go | 46 ------------------- pkg/api/api.go | 7 ++- pkg/api/common.go | 17 +++++-- pkg/api/user.go | 26 +++++++++-- pkg/services/sqlstore/user.go | 9 ++-- .../app/features/admin/adminEditUserCtrl.js | 4 +- 6 files changed, 45 insertions(+), 64 deletions(-) diff --git a/pkg/api/admin_users.go b/pkg/api/admin_users.go index f7e8fca2b5e..23cfc826ed2 100644 --- a/pkg/api/admin_users.go +++ b/pkg/api/admin_users.go @@ -19,26 +19,6 @@ func AdminSearchUsers(c *middleware.Context) { c.JSON(200, query.Result) } -func AdminGetUser(c *middleware.Context) { - userId := c.ParamsInt64(":id") - - query := m.GetUserByIdQuery{Id: userId} - - if err := bus.Dispatch(&query); err != nil { - c.JsonApiErr(500, "Failed to fetch user", err) - return - } - - result := dtos.AdminUserListItem{ - Name: query.Result.Name, - Email: query.Result.Email, - Login: query.Result.Login, - IsGrafanaAdmin: query.Result.IsAdmin, - } - - c.JSON(200, result) -} - func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) { cmd := m.CreateUserCommand{ Login: form.Login, @@ -70,32 +50,6 @@ func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) { c.JsonOK("User created") } -func AdminUpdateUser(c *middleware.Context, form dtos.AdminUpdateUserForm) { - userId := c.ParamsInt64(":id") - - cmd := m.UpdateUserCommand{ - UserId: userId, - Login: form.Login, - Email: form.Email, - Name: form.Name, - } - - if len(cmd.Login) == 0 { - cmd.Login = cmd.Email - if len(cmd.Login) == 0 { - c.JsonApiErr(400, "Validation error, need specify either username or email", nil) - return - } - } - - if err := bus.Dispatch(&cmd); err != nil { - c.JsonApiErr(500, "failed to update user", err) - return - } - - c.JsonOK("User updated") -} - func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) { userId := c.ParamsInt64(":id") diff --git a/pkg/api/api.go b/pkg/api/api.go index cfc1f50b98e..a84c753c7ba 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -56,7 +56,7 @@ func Register(r *macaron.Macaron) { // user r.Group("/user", func() { r.Get("/", wrap(GetSignedInUser)) - r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser) + r.Put("/", bind(m.UpdateUserCommand{}), wrap(UpdateSignedInUser)) r.Post("/using/:id", UserSetUsingOrg) r.Get("/orgs", wrap(GetSignedInUserOrgList)) r.Post("/stars/dashboard/:id", StarDashboard) @@ -66,8 +66,9 @@ func Register(r *macaron.Macaron) { // users r.Group("/users", func() { - r.Get("/:id/", wrap(GetUserById)) + r.Get("/:id", wrap(GetUserById)) r.Get("/:id/org", wrap(GetUserOrgList)) + r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser)) }, reqGrafanaAdmin) // account @@ -122,9 +123,7 @@ func Register(r *macaron.Macaron) { r.Group("/api/admin", func() { r.Get("/settings", AdminGetSettings) r.Get("/users", AdminSearchUsers) - r.Get("/users/:id", AdminGetUser) r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser) - r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser) r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword) r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions) r.Delete("/users/:id", AdminDeleteUser) diff --git a/pkg/api/common.go b/pkg/api/common.go index 8757318159e..4d8b3c28032 100644 --- a/pkg/api/common.go +++ b/pkg/api/common.go @@ -26,12 +26,17 @@ type NormalResponse struct { header http.Header } -func wrap(action func(c *middleware.Context) Response) macaron.Handler { +func wrap(action interface{}) macaron.Handler { + return func(c *middleware.Context) { - res := action(c) - if res == nil { + var res Response + val, err := c.Invoke(action) + if err == nil && val != nil && len(val) > 0 { + res = val[0].Interface().(Response) + } else { res = ServerError } + res.WriteTo(c.Resp) } } @@ -64,6 +69,12 @@ func Json(status int, body interface{}) *NormalResponse { return Respond(status, body).Header("Content-Type", "application/json") } +func ApiSuccess(message string) *NormalResponse { + resp := make(map[string]interface{}) + resp["message"] = message + return Respond(200, resp) +} + func ApiError(status int, message string, err error) *NormalResponse { resp := make(map[string]interface{}) diff --git a/pkg/api/user.go b/pkg/api/user.go index e7cc8ff0366..1b5654ce1f0 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -27,15 +27,31 @@ func getUserUserProfile(userId int64) Response { return Json(200, query.Result) } -func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) { +// POST /api/user +func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { cmd.UserId = c.UserId + return handleUpdateUser(cmd) +} - if err := bus.Dispatch(&cmd); err != nil { - c.JsonApiErr(400, "Failed to update user", err) - return +// POST /api/users/:id +func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { + cmd.UserId = c.ParamsInt64(":id") + return handleUpdateUser(cmd) +} + +func handleUpdateUser(cmd m.UpdateUserCommand) Response { + if len(cmd.Login) == 0 { + cmd.Login = cmd.Email + if len(cmd.Login) == 0 { + return ApiError(400, "Validation error, need specify either username or email", nil) + } } - c.JsonOK("User updated") + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "failed to update user", err) + } + + return ApiSuccess("User updated") } // GET /api/user/orgs diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 663496427ba..b74422b69ca 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -231,10 +231,11 @@ func GetUserProfile(query *m.GetUserProfileQuery) error { } query.Result = m.UserProfileDTO{ - Name: user.Name, - Email: user.Email, - Login: user.Login, - Theme: user.Theme, + Name: user.Name, + Email: user.Email, + Login: user.Login, + Theme: user.Theme, + IsGrafanaAdmin: user.IsAdmin, } return err diff --git a/public/app/features/admin/adminEditUserCtrl.js b/public/app/features/admin/adminEditUserCtrl.js index 19deac532ea..72a7dba0229 100644 --- a/public/app/features/admin/adminEditUserCtrl.js +++ b/public/app/features/admin/adminEditUserCtrl.js @@ -17,7 +17,7 @@ function (angular) { }; $scope.getUser = function(id) { - backendSrv.get('/api/admin/users/' + id).then(function(user) { + backendSrv.get('/api/users/' + id).then(function(user) { $scope.user = user; $scope.user_id = id; $scope.permissions.isGrafanaAdmin = user.isGrafanaAdmin; @@ -52,7 +52,7 @@ function (angular) { $scope.update = function() { if (!$scope.userForm.$valid) { return; } - backendSrv.put('/api/admin/users/' + $scope.user_id + '/details', $scope.user).then(function() { + backendSrv.put('/api/users/' + $scope.user_id, $scope.user).then(function() { $location.path('/admin/users'); }); };