Batch disable users (#17254)

* batch disable users

* batch revoke users tokens

* split batch disable user and revoke token

* fix tests for batch disable users

* Chore: add BatchDisableUsers() to the bus
This commit is contained in:
Alexander Zobnin
2019-05-31 13:22:22 +03:00
committed by GitHub
parent 1497f3d79a
commit 60ddad8fdb
5 changed files with 116 additions and 0 deletions
+26
View File
@@ -28,6 +28,7 @@ func (ss *SqlStore) addUserQueryAndCommandHandlers() {
bus.AddHandler("sql", SearchUsers)
bus.AddHandler("sql", GetUserOrgList)
bus.AddHandler("sql", DisableUser)
bus.AddHandler("sql", BatchDisableUsers)
bus.AddHandler("sql", DeleteUser)
bus.AddHandler("sql", UpdateUserPermissions)
bus.AddHandler("sql", SetUserHelpFlag)
@@ -487,6 +488,31 @@ func DisableUser(cmd *m.DisableUserCommand) error {
return err
}
func BatchDisableUsers(cmd *m.BatchDisableUsersCommand) error {
return inTransaction(func(sess *DBSession) error {
userIds := cmd.UserIds
if len(userIds) == 0 {
return nil
}
user_id_params := strings.Repeat(",?", len(userIds)-1)
disableSQL := "UPDATE " + dialect.Quote("user") + " SET is_disabled=? WHERE Id IN (?" + user_id_params + ")"
disableParams := []interface{}{disableSQL, cmd.IsDisabled}
for _, v := range userIds {
disableParams = append(disableParams, v)
}
_, err := sess.Exec(disableParams...)
if err != nil {
return err
}
return nil
})
}
func DeleteUser(cmd *m.DeleteUserCommand) error {
return inTransaction(func(sess *DBSession) error {
return deleteUserInTransaction(sess, cmd)
+34
View File
@@ -175,6 +175,40 @@ func TestUserDataAccess(t *testing.T) {
So(found, ShouldBeTrue)
})
})
Convey("When batch disabling users", func() {
userIdsToDisable := []int64{}
for i := 0; i < 3; i++ {
userIdsToDisable = append(userIdsToDisable, users[i].Id)
}
disableCmd := m.BatchDisableUsersCommand{UserIds: userIdsToDisable, IsDisabled: true}
err = BatchDisableUsers(&disableCmd)
So(err, ShouldBeNil)
Convey("Should disable all provided users", func() {
query := m.SearchUsersQuery{}
err = SearchUsers(&query)
So(query.Result.TotalCount, ShouldEqual, 5)
for _, user := range query.Result.Users {
shouldBeDisabled := false
// Check if user id is in the userIdsToDisable list
for _, disabledUserId := range userIdsToDisable {
if user.Id == disabledUserId {
So(user.IsDisabled, ShouldBeTrue)
shouldBeDisabled = true
}
}
// Otherwise user shouldn't be disabled
if !shouldBeDisabled {
So(user.IsDisabled, ShouldBeFalse)
}
}
})
})
})
Convey("Given one grafana admin user", func() {