2023-04-13 00:07:08 +02:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-01 22:48:11 +02:00
|
|
|
"errors"
|
2023-04-13 00:07:08 +02:00
|
|
|
"fmt"
|
2023-05-06 00:03:41 +02:00
|
|
|
"time"
|
2023-04-13 00:07:08 +02:00
|
|
|
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent/user"
|
2023-05-03 06:30:12 +02:00
|
|
|
passwd "git.dotya.ml/mirre-mt/pcmt/modules/password"
|
2023-04-19 02:12:27 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
2023-05-06 00:03:41 +02:00
|
|
|
"github.com/google/uuid"
|
2023-05-09 17:35:00 +02:00
|
|
|
"golang.org/x/exp/slog"
|
2023-04-13 00:07:08 +02:00
|
|
|
)
|
|
|
|
|
2023-05-06 00:03:41 +02:00
|
|
|
type User struct {
|
|
|
|
ID uuid.UUID
|
|
|
|
Username string
|
|
|
|
IsActive bool
|
|
|
|
IsAdmin bool
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
IsLoggedIn bool
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// CreateUser adds a user entry to the database.
|
2023-05-01 22:48:11 +02:00
|
|
|
func CreateUser(ctx context.Context, client *ent.Client, email, username, password string) (*ent.User, error) {
|
2023-05-09 17:35:00 +02:00
|
|
|
slogger := ctx.Value(CtxKey{}).(*slogging.Logger)
|
|
|
|
log := *slogger
|
|
|
|
|
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "modules/user")),
|
|
|
|
)
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-05-03 06:30:12 +02:00
|
|
|
digest, err := passwd.GetHash(password)
|
|
|
|
if err != nil {
|
2023-05-03 22:43:51 +02:00
|
|
|
log.Errorf("error hashing password: %s", err)
|
2023-05-03 06:30:12 +02:00
|
|
|
return nil, errors.New("could not hash password")
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
u, err := client.User.
|
|
|
|
Create().
|
2023-05-01 22:48:11 +02:00
|
|
|
SetEmail(email).
|
2023-04-13 00:07:08 +02:00
|
|
|
SetUsername(username).
|
2023-05-03 06:30:12 +02:00
|
|
|
SetPassword(digest).
|
2023-04-13 00:07:08 +02:00
|
|
|
Save(ctx)
|
2023-05-01 22:48:11 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
switch {
|
|
|
|
case ent.IsConstraintError(err):
|
|
|
|
log.Errorf("the username '%s' already exists", username)
|
|
|
|
return nil, errors.New("username is not unique")
|
2023-05-01 22:48:11 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
case err != nil:
|
2023-04-13 00:07:08 +02:00
|
|
|
return nil, fmt.Errorf("failed creating user: %w", err)
|
|
|
|
}
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-05-01 22:48:11 +02:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.User, error) {
|
2023-05-09 17:35:00 +02:00
|
|
|
slogger := ctx.Value(CtxKey{}).(*slogging.Logger)
|
|
|
|
log := *slogger
|
|
|
|
|
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "modules/user")),
|
|
|
|
)
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
u, err := client.User.
|
|
|
|
Query().
|
|
|
|
Where(user.Username(username)).
|
|
|
|
// `Only` fails if no user found,
|
|
|
|
// or more than 1 user returned.
|
|
|
|
Only(ctx)
|
2023-05-03 06:08:28 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
switch {
|
|
|
|
case ent.IsNotFound(err):
|
|
|
|
return nil, fmt.Errorf("user not found: %q", err)
|
2023-05-03 06:08:28 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
case err != nil:
|
|
|
|
log.Warn("error querying user", "error", err, "username requested", username)
|
2023-04-13 00:07:08 +02:00
|
|
|
return nil, fmt.Errorf("failed querying user: %w", err)
|
|
|
|
}
|
2023-04-19 02:12:27 +02:00
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
return u, nil
|
|
|
|
}
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
// 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) {
|
2023-05-09 17:35:00 +02:00
|
|
|
slogger := ctx.Value(CtxKey{}).(*slogging.Logger)
|
|
|
|
log := *slogger
|
|
|
|
|
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "modules/user")),
|
|
|
|
)
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
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}.
|
2023-05-09 17:35:00 +02:00
|
|
|
func UsernameExists(ctx context.Context, client *ent.Client, username string) (bool, error) { //nolint:dupl
|
|
|
|
slogger := ctx.Value(CtxKey{}).(*slogging.Logger)
|
|
|
|
log := *slogger
|
|
|
|
|
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "modules/user")),
|
|
|
|
)
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
usr, err := client.User.
|
|
|
|
Query().
|
|
|
|
Where(user.Username(username)).
|
|
|
|
Only(ctx)
|
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
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
|
2023-05-01 22:48:11 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
case err != nil:
|
|
|
|
log.Warn("failed to check whether user exists", "error", err, "username queried", username)
|
2023-05-01 22:48:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
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}.
|
2023-05-09 17:35:00 +02:00
|
|
|
func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, error) { //nolint:dupl
|
|
|
|
slogger := ctx.Value(CtxKey{}).(*slogging.Logger)
|
|
|
|
log := *slogger
|
|
|
|
|
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "modules/user")),
|
|
|
|
)
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
usr, err := client.User.
|
|
|
|
Query().
|
|
|
|
Where(user.Email(email)).
|
|
|
|
Only(ctx)
|
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
switch {
|
|
|
|
case ent.IsNotFound(err):
|
|
|
|
log.Infof("user email '%s' not found", email)
|
|
|
|
return false, nil
|
2023-05-01 22:48:11 +02:00
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
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)
|
2023-05-01 22:48:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-03 22:42:08 +02:00
|
|
|
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
|
2023-05-01 22:48:11 +02:00
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|