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

View File

@ -259,8 +259,9 @@ func UpdateUserByAdmin(ctx context.Context, client *ent.Client, id uuid.UUID, em
var u int
switch {
// ignore updates to password when user finished setting up (if not admin).
if !isAdmin && finishedSetup {
case !isAdmin && finishedSetup:
u, err = client.User.
Update().Where(user.IDEQ(id)).
SetEmail(email).
@ -268,23 +269,44 @@ func UpdateUserByAdmin(ctx context.Context, client *ent.Client, id uuid.UUID, em
SetIsAdmin(isAdmin).
SetIsActive(active).
Save(ctx)
} else {
default:
var digest []byte
digest, err = passwd.GetHash(password)
if err != nil {
if digest, err = passwd.GetHash(password); err != nil {
log.Errorf("error hashing password: %s", err)
return errors.New("could not hash password")
}
u, err = client.User.
Update().Where(user.IDEQ(id)).
SetEmail(email).
SetUsername(username).
SetPassword(digest).
SetIsAdmin(isAdmin).
SetIsActive(active).
Save(ctx)
var origU *ent.User
if origU, err = QueryUserByUUID(ctx, client, id); err != nil {
return err
}
// handle a situation when an admin account is demoted to a
// 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 {