go: handle demoting admin to regular-user level
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-09-04 20:27:14 +02:00
parent e10fdc5042
commit 07d19e6b77
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI

@ -259,8 +259,9 @@ func UpdateUserByAdmin(ctx context.Context, client *ent.Client, id uuid.UUID, em
var u int var u int
switch {
// ignore updates to password when user finished setting up (if not admin). // ignore updates to password when user finished setting up (if not admin).
if !isAdmin && finishedSetup { case !isAdmin && finishedSetup:
u, err = client.User. u, err = client.User.
Update().Where(user.IDEQ(id)). Update().Where(user.IDEQ(id)).
SetEmail(email). SetEmail(email).
@ -268,23 +269,44 @@ func UpdateUserByAdmin(ctx context.Context, client *ent.Client, id uuid.UUID, em
SetIsAdmin(isAdmin). SetIsAdmin(isAdmin).
SetIsActive(active). SetIsActive(active).
Save(ctx) Save(ctx)
} else {
default:
var digest []byte var digest []byte
digest, err = passwd.GetHash(password) if digest, err = passwd.GetHash(password); err != nil {
if err != nil {
log.Errorf("error hashing password: %s", err) log.Errorf("error hashing password: %s", err)
return errors.New("could not hash password") return errors.New("could not hash password")
} }
u, err = client.User. var origU *ent.User
Update().Where(user.IDEQ(id)).
SetEmail(email). if origU, err = QueryUserByUUID(ctx, client, id); err != nil {
SetUsername(username). return err
SetPassword(digest). }
SetIsAdmin(isAdmin).
SetIsActive(active). // handle a situation when an admin account is demoted to a
Save(ctx) // regular-user level. reset last-login so as to force the user to go
// through the initial password change flow.
if origU.IsAdmin && !isAdmin {
u, err = client.User.
Update().Where(user.IDEQ(id)).
SetEmail(email).
SetUsername(username).
SetPassword(digest).
SetIsAdmin(isAdmin).
SetIsActive(active).
SetLastLogin(time.Unix(0, 0)).
Save(ctx)
} else {
u, err = client.User.
Update().Where(user.IDEQ(id)).
SetEmail(email).
SetUsername(username).
SetPassword(digest).
SetIsAdmin(isAdmin).
SetIsActive(active).
Save(ctx)
}
} }
switch { switch {