Commit 7c241fd6 by Torkel Ödegaard

Added permissions section to admin > edit user view, an admin can now make…

Added permissions section to admin > edit user view, an admin can now make another user admin, Closes #1517
parent c14a90a3
...@@ -32,6 +32,7 @@ func AdminGetUser(c *middleware.Context) { ...@@ -32,6 +32,7 @@ func AdminGetUser(c *middleware.Context) {
Name: query.Result.Name, Name: query.Result.Name,
Email: query.Result.Email, Email: query.Result.Email,
Login: query.Result.Login, Login: query.Result.Login,
IsGrafanaAdmin: query.Result.IsAdmin,
} }
c.JSON(200, result) c.JSON(200, result)
...@@ -122,6 +123,22 @@ func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPas ...@@ -122,6 +123,22 @@ func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPas
c.JsonOK("User password updated") c.JsonOK("User password updated")
} }
func AdminUpdateUserPermissions(c *middleware.Context, form dtos.AdminUpdateUserPermissionsForm) {
userId := c.ParamsInt64(":id")
cmd := m.UpdateUserPermissionsCommand{
UserId: userId,
IsGrafanaAdmin: form.IsGrafanaAdmin,
}
if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to update user permissions", err)
return
}
c.JsonOK("User permissions updated")
}
func AdminDeleteUser(c *middleware.Context) { func AdminDeleteUser(c *middleware.Context) {
userId := c.ParamsInt64(":id") userId := c.ParamsInt64(":id")
......
...@@ -104,6 +104,7 @@ func Register(r *macaron.Macaron) { ...@@ -104,6 +104,7 @@ func Register(r *macaron.Macaron) {
r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser) r.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser)
r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser) r.Put("/users/:id/details", bind(dtos.AdminUpdateUserForm{}), AdminUpdateUser)
r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword) r.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword)
r.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions)
r.Delete("/users/:id", AdminDeleteUser) r.Delete("/users/:id", AdminDeleteUser)
}, reqGrafanaAdmin) }, reqGrafanaAdmin)
......
...@@ -16,3 +16,7 @@ type AdminUpdateUserForm struct { ...@@ -16,3 +16,7 @@ type AdminUpdateUserForm struct {
type AdminUpdateUserPasswordForm struct { type AdminUpdateUserPasswordForm struct {
Password string `json:"password" binding:"Required"` Password string `json:"password" binding:"Required"`
} }
type AdminUpdateUserPermissionsForm struct {
IsGrafanaAdmin bool `json:"IsGrafanaAdmin" binding:"Required"`
}
...@@ -59,6 +59,11 @@ type ChangeUserPasswordCommand struct { ...@@ -59,6 +59,11 @@ type ChangeUserPasswordCommand struct {
UserId int64 `json:"-"` UserId int64 `json:"-"`
} }
type UpdateUserPermissionsCommand struct {
IsGrafanaAdmin bool
UserId int64 `json:"-"`
}
type DeleteUserCommand struct { type DeleteUserCommand struct {
UserId int64 UserId int64
} }
...@@ -123,6 +128,7 @@ type UserDTO struct { ...@@ -123,6 +128,7 @@ type UserDTO struct {
Email string `json:"email"` Email string `json:"email"`
Name string `json:"name"` Name string `json:"name"`
Login string `json:"login"` Login string `json:"login"`
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
} }
type UserSearchHitDTO struct { type UserSearchHitDTO struct {
......
...@@ -27,6 +27,7 @@ func init() { ...@@ -27,6 +27,7 @@ func init() {
bus.AddHandler("sql", GetUserOrgList) bus.AddHandler("sql", GetUserOrgList)
bus.AddHandler("sql", DeleteUser) bus.AddHandler("sql", DeleteUser)
bus.AddHandler("sql", SetUsingOrg) bus.AddHandler("sql", SetUsingOrg)
bus.AddHandler("sql", UpdateUserPermissions)
} }
func getOrgIdForNewUser(userEmail string, sess *session) (int64, error) { func getOrgIdForNewUser(userEmail string, sess *session) (int64, error) {
...@@ -284,3 +285,15 @@ func DeleteUser(cmd *m.DeleteUserCommand) error { ...@@ -284,3 +285,15 @@ func DeleteUser(cmd *m.DeleteUserCommand) error {
return err return err
}) })
} }
func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error {
return inTransaction(func(sess *xorm.Session) error {
user := m.User{}
sess.Id(cmd.UserId).Get(&user)
user.IsAdmin = cmd.IsGrafanaAdmin
sess.UseBool("is_admin")
_, err := sess.Id(user.Id).Update(&user)
return err
})
}
...@@ -8,6 +8,7 @@ function (angular) { ...@@ -8,6 +8,7 @@ function (angular) {
module.controller('AdminEditUserCtrl', function($scope, $routeParams, backendSrv, $location) { module.controller('AdminEditUserCtrl', function($scope, $routeParams, backendSrv, $location) {
$scope.user = {}; $scope.user = {};
$scope.permissions = {};
$scope.init = function() { $scope.init = function() {
if ($routeParams.id) { if ($routeParams.id) {
...@@ -19,6 +20,7 @@ function (angular) { ...@@ -19,6 +20,7 @@ function (angular) {
backendSrv.get('/api/admin/users/' + id).then(function(user) { backendSrv.get('/api/admin/users/' + id).then(function(user) {
$scope.user = user; $scope.user = user;
$scope.user_id = id; $scope.user_id = id;
$scope.permissions.isGrafanaAdmin = user.isGrafanaAdmin;
}); });
}; };
...@@ -31,6 +33,14 @@ function (angular) { ...@@ -31,6 +33,14 @@ function (angular) {
}); });
}; };
$scope.updatePermissions = function() {
var payload = $scope.permissions;
backendSrv.put('/api/admin/users/' + $scope.user_id + '/permissions', payload).then(function() {
$location.path('/admin/users');
});
};
$scope.create = function() { $scope.create = function() {
if (!$scope.userForm.$valid) { return; } if (!$scope.userForm.$valid) { return; }
......
<topnav icon="fa fa-fw fa-cogs" title="System Admin" subnav="true"> <topnav icon="fa fa-fw fa-user" title="Global Users" subnav="true">
<ul class="nav"> <ul class="nav">
<li><a href="admin/settings">Settings</a></li>
<li><a href="admin/users">Users</a></li> <li><a href="admin/users">Users</a></li>
<li><a href="admin/users/create">Create user</a></li> <li><a href="admin/users/create">Create user</a></li>
<li class="active"><a href="admin/users/edit/{{user_id}}">Edit user</a></li> <li class="active"><a href="admin/users/edit/{{user_id}}">Edit user</a></li>
...@@ -10,7 +9,7 @@ ...@@ -10,7 +9,7 @@
<div class="page-container"> <div class="page-container">
<div class="page"> <div class="page">
<h2> <h2>
Edit user User details
</h2> </h2>
<form name="userForm"> <form name="userForm">
...@@ -74,8 +73,26 @@ ...@@ -74,8 +73,26 @@
</div> </div>
<br> <br>
<button type="submit" class="pull-right btn btn-success" ng-click="setPassword()">Change password</button> <button type="submit" class="pull-right btn btn-success" ng-click="setPassword()">Update</button>
</form> </form>
<h2>
Permissions
</h2>
<div class="tight-form last">
<ul class="tight-form-list">
<li class="tight-form-item last">
Grafana Admin&nbsp;
<input class="cr1" id="permissions.isGrafanaAdmin" type="checkbox"
ng-model="permissions.isGrafanaAdmin" ng-checked="permissions.isGrafanaAdmin">
<label for="permissions.isGrafanaAdmin" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div> </div>
<br>
<button type="submit" class="pull-right btn btn-success" ng-click="updatePermissions()">Update</button>
</div>
</div> </div>
<topnav icon="fa fa-fw fa-cogs" title="System Admin" subnav="true"> <topnav icon="fa fa-fw fa-cogs" title="Global Users" subnav="true">
<ul class="nav"> <ul class="nav">
<li><a href="admin/settings">Settings</a></li>
<li><a href="admin/users">Users</a></li> <li><a href="admin/users">Users</a></li>
<li class="active"><a href="admin/users/create">Create user</a></li> <li class="active"><a href="admin/users/create">Create user</a></li>
</ul> </ul>
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<th>Name</th> <th>Name</th>
<th>Login</th> <th>Login</th>
<th>Email</th> <th>Email</th>
<th>Grafana Admin</th> <th style="white-space: nowrap">Grafana Admin</th>
<th></th> <th></th>
</tr> </tr>
<tr ng-repeat="user in users"> <tr ng-repeat="user in users">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment