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,15 +269,35 @@ 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")
} }
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. u, err = client.User.
Update().Where(user.IDEQ(id)). Update().Where(user.IDEQ(id)).
SetEmail(email). SetEmail(email).
@ -286,6 +307,7 @@ func UpdateUserByAdmin(ctx context.Context, client *ent.Client, id uuid.UUID, em
SetIsActive(active). SetIsActive(active).
Save(ctx) Save(ctx)
} }
}
switch { switch {
case ent.IsConstraintError(err): case ent.IsConstraintError(err):