pcmt/modules/user/user.go
leo 0ac668d001
All checks were successful
continuous-integration/drone/push Build is passing
mod/usr: fix typo [skip ci]
2023-05-03 22:43:51 +02:00

177 lines
4.9 KiB
Go

package user
import (
"context"
"errors"
"fmt"
"git.dotya.ml/mirre-mt/pcmt/ent"
"git.dotya.ml/mirre-mt/pcmt/ent/user"
passwd "git.dotya.ml/mirre-mt/pcmt/modules/password"
"git.dotya.ml/mirre-mt/pcmt/slogging"
)
// CreateUser adds a user entry to the database.
func CreateUser(ctx context.Context, client *ent.Client, email, username, password string) (*ent.User, error) {
log := ctx.Value(CtxKey{}).(*slogging.Logger)
digest, err := passwd.GetHash(password)
if err != nil {
log.Errorf("error hashing password: %s", err)
return nil, errors.New("could not hash password")
}
u, err := client.User.
Create().
SetEmail(email).
SetUsername(username).
SetPassword(digest).
Save(ctx)
switch {
case ent.IsConstraintError(err):
log.Errorf("the username '%s' already exists", username)
return nil, errors.New("username is not unique")
case err != nil:
return nil, fmt.Errorf("failed creating user: %w", err)
}
log.Debug(
fmt.Sprintf(
"user successfully created - id: %s, name: %s, isActive: %t, isAdmin: %t, createdAt: %s, updatedAt: %s",
u.ID, u.Username, u.IsActive, u.IsAdmin, u.CreatedAt, u.UpdatedAt,
),
)
return u, nil
}
func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.User, error) {
log := ctx.Value(CtxKey{}).(*slogging.Logger)
u, err := client.User.
Query().
Where(user.Username(username)).
// `Only` fails if no user found,
// or more than 1 user returned.
Only(ctx)
switch {
case ent.IsNotFound(err):
return nil, fmt.Errorf("user not found: %q", err)
case err != nil:
log.Warn("error querying user", "error", err, "username requested", username)
return nil, fmt.Errorf("failed querying user: %w", err)
}
return u, nil
}
// Exists determines whether the username OR email in question was previously
// used to register a user.
func Exists(ctx context.Context, client *ent.Client, username, email string) (bool, error) {
log := ctx.Value(CtxKey{}).(*slogging.Logger)
usernameExists, err := UsernameExists(ctx, client, username)
if err != nil {
log.Warn("failed to check whether username is taken", "error", err, "username requested", username)
return false, fmt.Errorf("failed querying user: %w", err)
}
emailExists, err := EmailExists(ctx, client, email)
if err != nil {
log.Warn("failed to check whether user email exists", "error", err, "user email requested", email)
return false, fmt.Errorf("failed querying user: %w", err)
}
switch {
case usernameExists && emailExists:
log.Infof("user exists: both username '%s' and email: '%s' matched", username, email)
return true, nil
case usernameExists:
log.Infof("username '%s' is already taken", username)
return true, nil
case emailExists:
log.Infof("email '%s' is already registered", email)
return true, nil
}
return false, nil
}
// UsernameExists queries the database to check whether the username is
// available or taken, returns a bool and an error, which will be nil unless
// the error is one of IsNot{Found,Singular}.
func UsernameExists(ctx context.Context, client *ent.Client, username string) (bool, error) {
log := ctx.Value(CtxKey{}).(*slogging.Logger)
usr, err := client.User.
Query().
Where(user.Username(username)).
Only(ctx)
switch {
case ent.IsNotFound(err):
log.Infof("username '%s' not found", username)
return false, nil
case ent.IsNotSingular(err):
log.Errorf("apparently more than one user managed to acquire the username '%s', bailing", username)
return true, err
case err != nil:
log.Warn("failed to check whether user exists", "error", err, "username queried", username)
return false, fmt.Errorf("failed querying username: %w", err)
}
if usr != nil {
log.Infof("username '%s' found, user id: %s", username, usr.ID)
return true, nil
}
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
return false, nil
}
// EmailExists queries the database to check whether the email was already
// used; returns a bool and an error, which will be nil unless the error is not
// one of IsNot{Found,Singular}.
func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, error) {
log := ctx.Value(CtxKey{}).(*slogging.Logger)
usr, err := client.User.
Query().
Where(user.Email(email)).
Only(ctx)
switch {
case ent.IsNotFound(err):
log.Infof("user email '%s' not found", email)
return false, nil
case ent.IsNotSingular(err):
log.Errorf("apparently more than one user managed to register using the email '%s', bailing", email)
return true, err
case err != nil:
log.Warn("failed to check whether email exists", "error", err, "email queried", email)
return false, fmt.Errorf("failed querying user email: %w", err)
}
if usr != nil {
log.Infof("user email '%s' found, user id: %s", email, usr.ID)
return true, nil
}
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
return false, nil
}