From 1a106e5c3881a5e0553fb88315dc0959f6436bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 23 Feb 2015 11:24:22 +0100 Subject: [PATCH] Added change password ability to admin > edit user view, #1446 --- pkg/api/admin_users.go | 31 +++++++++++++++++ pkg/api/api.go | 3 +- pkg/api/dtos/user.go | 4 +++ src/app/features/admin/adminEditUserCtrl.js | 33 ++++++++++++------- .../features/admin/partials/edit_user.html | 31 ++++++++++------- src/app/features/admin/partials/users.html | 2 +- src/app/routes/backend/all.js | 2 +- 7 files changed, 79 insertions(+), 27 deletions(-) diff --git a/pkg/api/admin_users.go b/pkg/api/admin_users.go index 9402eb41b20..ec83aa47df1 100644 --- a/pkg/api/admin_users.go +++ b/pkg/api/admin_users.go @@ -5,6 +5,7 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/util" ) func AdminSearchUsers(c *middleware.Context) { @@ -91,6 +92,36 @@ func AdminUpdateUser(c *middleware.Context, form dtos.AdminUpdateUserForm) { c.JsonOK("User updated") } +func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPasswordForm) { + userId := c.ParamsInt64(":id") + + if len(form.Password) < 4 { + c.JsonApiErr(400, "New password too short", nil) + return + } + + userQuery := m.GetUserByIdQuery{Id: userId} + + if err := bus.Dispatch(&userQuery); err != nil { + c.JsonApiErr(500, "Could not read user from database", err) + return + } + + passwordHashed := util.EncodePassword(form.Password, userQuery.Result.Salt) + + cmd := m.ChangeUserPasswordCommand{ + UserId: userId, + NewPassword: passwordHashed, + } + + if err := bus.Dispatch(&cmd); err != nil { + c.JsonApiErr(500, "Failed to update user password", err) + return + } + + c.JsonOK("User password updated") +} + func AdminDeleteUser(c *middleware.Context) { userId := c.ParamsInt64(":id") diff --git a/pkg/api/api.go b/pkg/api/api.go index c6d050547ef..84fee61005f 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -102,7 +102,8 @@ func Register(r *macaron.Macaron) { r.Get("/users", AdminSearchUsers) r.Get("/users/:id", AdminGetUser) r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser) - r.Put("/users/:id", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser) + r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser) + r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword) r.Delete("/users/:id", AdminDeleteUser) }, reqGrafanaAdmin) diff --git a/pkg/api/dtos/user.go b/pkg/api/dtos/user.go index 69fc3797029..3d671e64654 100644 --- a/pkg/api/dtos/user.go +++ b/pkg/api/dtos/user.go @@ -12,3 +12,7 @@ type AdminUpdateUserForm struct { Login string `json:"login"` Name string `json:"name"` } + +type AdminUpdateUserPasswordForm struct { + Password string `json:"password" binding:"Required"` +} diff --git a/src/app/features/admin/adminEditUserCtrl.js b/src/app/features/admin/adminEditUserCtrl.js index 86b59c4599b..0d473bd950e 100644 --- a/src/app/features/admin/adminEditUserCtrl.js +++ b/src/app/features/admin/adminEditUserCtrl.js @@ -11,10 +11,7 @@ function (angular) { $scope.init = function() { if ($routeParams.id) { - $scope.createMode = false; $scope.getUser($routeParams.id); - } else { - $scope.createMode = true; } }; @@ -25,17 +22,29 @@ function (angular) { }); }; + $scope.setPassword = function () { + if (!$scope.passwordForm.$valid) { return; } + + var payload = { password: $scope.password }; + backendSrv.put('/api/admin/users/' + $scope.user_id + '/password', payload).then(function() { + $location.path('/admin/users'); + }); + }; + + $scope.create = function() { + if (!$scope.userForm.$valid) { return; } + + backendSrv.post('/api/admin/users', $scope.user).then(function() { + $location.path('/admin/users'); + }); + }; + $scope.update = function() { if (!$scope.userForm.$valid) { return; } - if ($scope.createMode) { - backendSrv.post('/api/admin/users', $scope.user).then(function() { - $location.path('/admin/users'); - }); - } else { - backendSrv.put('/api/admin/users/' + $scope.user_id, $scope.user).then(function() { - $location.path('/admin/users'); - }); - } + + backendSrv.put('/api/admin/users/' + $scope.user_id + '/details', $scope.user).then(function() { + $location.path('/admin/users'); + }); }; $scope.init(); diff --git a/src/app/features/admin/partials/edit_user.html b/src/app/features/admin/partials/edit_user.html index ecc03fd1aad..bc060347b86 100644 --- a/src/app/features/admin/partials/edit_user.html +++ b/src/app/features/admin/partials/edit_user.html @@ -2,18 +2,14 @@
-

- Create a new user -

- -

+

Edit user

@@ -52,14 +48,25 @@
+
-
+
+ + + +

+ Change password +

+ +
+
+
  • - Password + New password
  • - +
@@ -67,8 +74,8 @@

- - +
+
diff --git a/src/app/features/admin/partials/users.html b/src/app/features/admin/partials/users.html index fb86b18eacb..a27d755ba9d 100644 --- a/src/app/features/admin/partials/users.html +++ b/src/app/features/admin/partials/users.html @@ -18,7 +18,7 @@ Name Login Email - Admin + Grafana Admin diff --git a/src/app/routes/backend/all.js b/src/app/routes/backend/all.js index 29e6c3df623..e570603d808 100644 --- a/src/app/routes/backend/all.js +++ b/src/app/routes/backend/all.js @@ -75,7 +75,7 @@ define([ controller : 'AdminUsersCtrl', }) .when('/admin/users/create', { - templateUrl: 'app/features/admin/partials/edit_user.html', + templateUrl: 'app/features/admin/partials/new_user.html', controller : 'AdminEditUserCtrl', }) .when('/admin/users/edit/:id', {