Validation check for not removing the last account admin

This commit is contained in:
Torkel Ödegaard
2015-01-20 15:48:19 +01:00
parent 4ea5d80099
commit eec178458b
5 changed files with 28 additions and 3 deletions
+6
View File
@@ -103,6 +103,12 @@ func TestAccountDataAccess(t *testing.T) {
So(query.Result.AccountRole, ShouldEqual, "Viewer")
})
})
Convey("Cannot delete last admin account user", func() {
cmd := m.RemoveAccountUserCommand{AccountId: ac1.AccountId, UserId: ac1.Id}
err := RemoveAccountUser(&cmd)
So(err, ShouldEqual, m.ErrLastAccountAdmin)
})
})
})
})
+14
View File
@@ -47,6 +47,20 @@ func RemoveAccountUser(cmd *m.RemoveAccountUserCommand) error {
return inTransaction(func(sess *xorm.Session) error {
var rawSql = "DELETE FROM account_user WHERE account_id=? and user_id=?"
_, err := sess.Exec(rawSql, cmd.AccountId, cmd.UserId)
if err != nil {
return err
}
// validate that there is an admin user left
res, err := sess.Query("SELECT 1 from account_user WHERE account_id=? and role='Admin'", cmd.AccountId)
if err != nil {
return err
}
if len(res) == 0 {
return m.ErrLastAccountAdmin
}
return err
})
}
+2 -2
View File
@@ -20,8 +20,8 @@ func TestMigrations(t *testing.T) {
testDBs := []sqlutil.TestDB{
sqlutil.TestDB_Sqlite3,
sqlutil.TestDB_Mysql,
sqlutil.TestDB_Postgres,
// sqlutil.TestDB_Mysql,
// sqlutil.TestDB_Postgres,
}
for _, testDB := range testDBs {