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
......@@ -29,9 +29,10 @@ func AdminGetUser(c *middleware.Context) {
}
result := m.UserDTO{
Name: query.Result.Name,
Email: query.Result.Email,
Login: query.Result.Login,
Name: query.Result.Name,
Email: query.Result.Email,
Login: query.Result.Login,
IsGrafanaAdmin: query.Result.IsAdmin,
}
c.JSON(200, result)
......@@ -122,6 +123,22 @@ func AdminUpdateUserPassword(c *middleware.Context, form dtos.AdminUpdateUserPas
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) {
userId := c.ParamsInt64(":id")
......
......@@ -104,6 +104,7 @@ func Register(r *macaron.Macaron) {
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)
}, reqGrafanaAdmin)
......
......@@ -16,3 +16,7 @@ type AdminUpdateUserForm struct {
type AdminUpdateUserPasswordForm struct {
Password string `json:"password" binding:"Required"`
}
type AdminUpdateUserPermissionsForm struct {
IsGrafanaAdmin bool `json:"IsGrafanaAdmin" binding:"Required"`
}
......@@ -59,6 +59,11 @@ type ChangeUserPasswordCommand struct {
UserId int64 `json:"-"`
}
type UpdateUserPermissionsCommand struct {
IsGrafanaAdmin bool
UserId int64 `json:"-"`
}
type DeleteUserCommand struct {
UserId int64
}
......@@ -120,9 +125,10 @@ type SignedInUser struct {
}
type UserDTO struct {
Email string `json:"email"`
Name string `json:"name"`
Login string `json:"login"`
Email string `json:"email"`
Name string `json:"name"`
Login string `json:"login"`
IsGrafanaAdmin bool `json:"isGrafanaAdmin"`
}
type UserSearchHitDTO struct {
......
......@@ -27,6 +27,7 @@ func init() {
bus.AddHandler("sql", GetUserOrgList)
bus.AddHandler("sql", DeleteUser)
bus.AddHandler("sql", SetUsingOrg)
bus.AddHandler("sql", UpdateUserPermissions)
}
func getOrgIdForNewUser(userEmail string, sess *session) (int64, error) {
......@@ -284,3 +285,15 @@ func DeleteUser(cmd *m.DeleteUserCommand) error {
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) {
module.controller('AdminEditUserCtrl', function($scope, $routeParams, backendSrv, $location) {
$scope.user = {};
$scope.permissions = {};
$scope.init = function() {
if ($routeParams.id) {
......@@ -19,6 +20,7 @@ function (angular) {
backendSrv.get('/api/admin/users/' + id).then(function(user) {
$scope.user = user;
$scope.user_id = id;
$scope.permissions.isGrafanaAdmin = user.isGrafanaAdmin;
});
};
......@@ -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() {
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">
<li><a href="admin/settings">Settings</a></li>
<li><a href="admin/users">Users</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>
......@@ -10,7 +9,7 @@
<div class="page-container">
<div class="page">
<h2>
Edit user
User details
</h2>
<form name="userForm">
......@@ -74,8 +73,26 @@
</div>
<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>
</div>
<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>
<br>
<button type="submit" class="pull-right btn btn-success" ng-click="updatePermissions()">Update</button>
</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">
<li><a href="admin/settings">Settings</a></li>
<li><a href="admin/users">Users</a></li>
<li class="active"><a href="admin/users/create">Create user</a></li>
</ul>
......
......@@ -17,7 +17,7 @@
<th>Name</th>
<th>Login</th>
<th>Email</th>
<th>Grafana Admin</th>
<th style="white-space: nowrap">Grafana Admin</th>
<th></th>
</tr>
<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