2023-08-04 13:34:18 +02:00
|
|
|
package users
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-02-28 08:29:40 +01:00
|
|
|
"github.com/google/uuid"
|
2023-08-04 13:34:18 +02:00
|
|
|
|
2025-02-28 08:29:40 +01:00
|
|
|
"github.com/h44z/wg-portal/internal/app"
|
2023-08-04 13:34:18 +02:00
|
|
|
"github.com/h44z/wg-portal/internal/config"
|
|
|
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// region dependencies
|
|
|
|
|
|
|
|
|
|
type UserDatabaseRepo interface {
|
|
|
|
|
// GetUser returns the user with the given identifier.
|
|
|
|
|
GetUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error)
|
|
|
|
|
// GetUserByEmail returns the user with the given email address.
|
|
|
|
|
GetUserByEmail(ctx context.Context, email string) (*domain.User, error)
|
2025-05-12 22:53:43 +02:00
|
|
|
// GetUserByWebAuthnCredential returns the user for the given WebAuthn credential ID.
|
|
|
|
|
GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error)
|
2025-03-23 23:09:47 +01:00
|
|
|
// GetAllUsers returns all users.
|
|
|
|
|
GetAllUsers(ctx context.Context) ([]domain.User, error)
|
|
|
|
|
// FindUsers returns all users matching the search string.
|
|
|
|
|
FindUsers(ctx context.Context, search string) ([]domain.User, error)
|
|
|
|
|
// SaveUser saves the user with the given identifier.
|
|
|
|
|
SaveUser(ctx context.Context, id domain.UserIdentifier, updateFunc func(u *domain.User) (*domain.User, error)) error
|
|
|
|
|
// DeleteUser deletes the user with the given identifier.
|
|
|
|
|
DeleteUser(ctx context.Context, id domain.UserIdentifier) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PeerDatabaseRepo interface {
|
|
|
|
|
// GetUserPeers returns all peers linked to the given user.
|
|
|
|
|
GetUserPeers(ctx context.Context, id domain.UserIdentifier) ([]domain.Peer, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EventBus interface {
|
|
|
|
|
// Publish sends a message to the message bus.
|
|
|
|
|
Publish(topic string, args ...any)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// endregion dependencies
|
|
|
|
|
|
|
|
|
|
// Manager is the user manager.
|
2023-08-04 13:34:18 +02:00
|
|
|
type Manager struct {
|
|
|
|
|
cfg *config.Config
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
bus EventBus
|
2024-09-22 11:49:23 +02:00
|
|
|
users UserDatabaseRepo
|
|
|
|
|
peers PeerDatabaseRepo
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// NewUserManager creates a new user manager instance.
|
|
|
|
|
func NewUserManager(cfg *config.Config, bus EventBus, users UserDatabaseRepo, peers PeerDatabaseRepo) (
|
2025-01-05 10:06:34 +01:00
|
|
|
*Manager,
|
|
|
|
|
error,
|
|
|
|
|
) {
|
2023-08-04 13:34:18 +02:00
|
|
|
m := &Manager{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
bus: bus,
|
|
|
|
|
|
2024-09-22 11:49:23 +02:00
|
|
|
users: users,
|
|
|
|
|
peers: peers,
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// RegisterUser registers a new user.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) RegisterUser(ctx context.Context, user *domain.User) error {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
createdUser, err := m.create(ctx, user)
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-21 16:42:35 +02:00
|
|
|
m.bus.Publish(app.TopicUserRegistered, *createdUser)
|
2023-08-04 13:34:18 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// StartBackgroundJobs starts the background jobs.
|
|
|
|
|
// This method is non-blocking and returns immediately.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) StartBackgroundJobs(ctx context.Context) {
|
|
|
|
|
go m.runLdapSynchronizationService(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// GetUser returns the user with the given identifier.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) GetUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateUserAccessRights(ctx, id); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.getUser(ctx, id)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// GetUserByEmail returns the user with the given email address.
|
2025-01-11 18:44:55 +01:00
|
|
|
func (m Manager) GetUserByEmail(ctx context.Context, email string) (*domain.User, error) {
|
|
|
|
|
user, err := m.users.GetUserByEmail(ctx, email)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unable to load user for email %s: %w", email, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := domain.ValidateUserAccessRights(ctx, user.Identifier); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.enrichUser(ctx, user), nil
|
2025-01-11 18:44:55 +01:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 22:53:43 +02:00
|
|
|
// GetUserByWebAuthnCredential returns the user for the given WebAuthn credential.
|
|
|
|
|
func (m Manager) GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error) {
|
|
|
|
|
user, err := m.users.GetUserByWebAuthnCredential(ctx, credentialIdBase64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unable to load user for webauthn credential %s: %w", credentialIdBase64, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := domain.ValidateUserAccessRights(ctx, user.Identifier); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.enrichUser(ctx, user), nil
|
2025-05-12 22:53:43 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// GetAllUsers returns all users.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) GetAllUsers(ctx context.Context) ([]domain.User, error) {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
users, err := m.users.GetAllUsers(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unable to load users: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ch := make(chan *domain.User)
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
workers := int(math.Min(float64(len(users)), 10))
|
|
|
|
|
wg.Add(workers)
|
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
for user := range ch {
|
2026-01-21 22:22:22 +01:00
|
|
|
m.enrichUser(ctx, user)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
for i := range users {
|
|
|
|
|
ch <- &users[i]
|
|
|
|
|
}
|
|
|
|
|
close(ch)
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// UpdateUser updates the user with the given identifier.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) UpdateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateUserAccessRights(ctx, user.Identifier); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
user.CopyCalculatedAttributes(existingUser, true) // ensure that crucial attributes stay the same
|
2023-08-04 13:34:18 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.update(ctx, existingUser, user, true)
|
|
|
|
|
}
|
2023-08-04 13:34:18 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
// UpdateUserInternal updates the user with the given identifier. This function must never be called from external.
|
|
|
|
|
// This function allows to override authentications and webauthn credentials.
|
|
|
|
|
func (m Manager) UpdateUserInternal(ctx context.Context, user *domain.User) (*domain.User, error) {
|
|
|
|
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
2025-01-05 10:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.update(ctx, existingUser, user, false)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// CreateUser creates a new user.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) CreateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.create(ctx, user)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// DeleteUser deletes the user with the given identifier.
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) DeleteUser(ctx context.Context, id domain.UserIdentifier) error {
|
2024-01-31 21:14:36 +01:00
|
|
|
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
existingUser, err := m.users.GetUser(ctx, id)
|
|
|
|
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
|
|
|
return fmt.Errorf("unable to find user %s: %w", id, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := m.validateDeletion(ctx, existingUser); err != nil {
|
|
|
|
|
return fmt.Errorf("deletion not allowed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = m.users.DeleteUser(ctx, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("deletion failure: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-05 10:06:34 +01:00
|
|
|
m.bus.Publish(app.TopicUserDeleted, *existingUser)
|
2023-08-04 13:34:18 +02:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// ActivateApi activates the API access for the user with the given identifier.
|
2025-01-11 18:44:55 +01:00
|
|
|
func (m Manager) ActivateApi(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
|
|
|
|
user, err := m.users.GetUser(ctx, id)
|
|
|
|
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
|
|
|
return nil, fmt.Errorf("unable to find user %s: %w", id, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := m.validateApiChange(ctx, user); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
user.ApiToken = uuid.New().String()
|
|
|
|
|
user.ApiTokenCreated = &now
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
user, err = m.update(ctx, user, user, true) // self-update
|
2025-01-11 18:44:55 +01:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, err
|
2025-01-11 18:44:55 +01:00
|
|
|
}
|
2025-04-21 16:42:35 +02:00
|
|
|
m.bus.Publish(app.TopicUserApiEnabled, *user)
|
2025-01-11 18:44:55 +01:00
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-23 23:09:47 +01:00
|
|
|
// DeactivateApi deactivates the API access for the user with the given identifier.
|
2025-01-11 18:44:55 +01:00
|
|
|
func (m Manager) DeactivateApi(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
|
|
|
|
user, err := m.users.GetUser(ctx, id)
|
|
|
|
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
|
|
|
return nil, fmt.Errorf("unable to find user %s: %w", id, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := m.validateApiChange(ctx, user); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.ApiToken = ""
|
|
|
|
|
user.ApiTokenCreated = nil
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
user, err = m.update(ctx, user, user, true) // self-update
|
2025-01-11 18:44:55 +01:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, err
|
2025-01-11 18:44:55 +01:00
|
|
|
}
|
2025-04-21 16:42:35 +02:00
|
|
|
m.bus.Publish(app.TopicUserApiDisabled, *user)
|
2025-01-11 18:44:55 +01:00
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
func (m Manager) validateModifications(ctx context.Context, old, new *domain.User) error {
|
|
|
|
|
currentUser := domain.GetUserInfo(ctx)
|
|
|
|
|
|
|
|
|
|
if currentUser.Id != new.Identifier && !currentUser.IsAdmin {
|
|
|
|
|
return fmt.Errorf("insufficient permissions")
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 22:14:00 +01:00
|
|
|
if err := old.EditAllowed(new); err != nil && currentUser.Id != domain.SystemAdminContextUserInfo().Id {
|
2025-01-11 18:44:55 +01:00
|
|
|
return errors.Join(fmt.Errorf("no access: %w", err), domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := old.CanChangePassword(); err != nil && string(new.Password) != "" {
|
2025-01-11 18:44:55 +01:00
|
|
|
return errors.Join(fmt.Errorf("no access: %w", err), domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-16 09:55:35 +02:00
|
|
|
if err := new.HasWeakPassword(m.cfg.Auth.MinPasswordLength); err != nil {
|
|
|
|
|
return errors.Join(fmt.Errorf("password too weak: %w", err), domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
if currentUser.Id == old.Identifier && old.IsAdmin && !new.IsAdmin {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("cannot remove own admin rights: %w", domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentUser.Id == old.Identifier && new.IsDisabled() {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("cannot disable own user: %w", domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentUser.Id == old.Identifier && new.IsLocked() {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("cannot lock own user: %w", domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m Manager) validateCreation(ctx context.Context, new *domain.User) error {
|
|
|
|
|
currentUser := domain.GetUserInfo(ctx)
|
|
|
|
|
|
|
|
|
|
if !currentUser.IsAdmin {
|
|
|
|
|
return fmt.Errorf("insufficient permissions")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if new.Identifier == "" {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("invalid user identifier: %w", domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if new.Identifier == "all" { // the 'all' user identifier collides with the rest api routes
|
|
|
|
|
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if new.Identifier == "new" { // the 'new' user identifier collides with the rest api routes
|
|
|
|
|
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if new.Identifier == "id" { // the 'id' user identifier collides with the rest api routes
|
|
|
|
|
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 18:44:55 +01:00
|
|
|
if new.Identifier == domain.CtxSystemAdminId || new.Identifier == domain.CtxUnknownUserId {
|
|
|
|
|
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
if len(new.Authentications) != 1 {
|
|
|
|
|
return fmt.Errorf("invalid number of authentications: %d, expected 1: %w",
|
|
|
|
|
len(new.Authentications), domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-21 15:29:53 +02:00
|
|
|
// Admins are allowed to create users for arbitrary sources.
|
2026-01-21 22:22:22 +01:00
|
|
|
if new.Authentications[0].Source != domain.UserSourceDatabase && !currentUser.IsAdmin {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("invalid user source: %s, only %s is allowed: %w",
|
2026-01-21 22:22:22 +01:00
|
|
|
new.Authentications[0].Source, domain.UserSourceDatabase, domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-21 15:29:53 +02:00
|
|
|
// database users must have a password
|
2026-01-21 22:22:22 +01:00
|
|
|
if new.Authentications[0].Source == domain.UserSourceDatabase && string(new.Password) == "" {
|
2025-05-16 09:55:35 +02:00
|
|
|
return fmt.Errorf("missing password: %w", domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := new.HasWeakPassword(m.cfg.Auth.MinPasswordLength); err != nil {
|
|
|
|
|
return errors.Join(fmt.Errorf("password too weak: %w", err), domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m Manager) validateDeletion(ctx context.Context, del *domain.User) error {
|
|
|
|
|
currentUser := domain.GetUserInfo(ctx)
|
|
|
|
|
|
|
|
|
|
if !currentUser.IsAdmin {
|
2025-01-11 18:44:55 +01:00
|
|
|
return domain.ErrNoPermission
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := del.DeleteAllowed(); err != nil {
|
2025-01-11 18:44:55 +01:00
|
|
|
return errors.Join(fmt.Errorf("no access: %w", err), domain.ErrInvalidData)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if currentUser.Id == del.Identifier {
|
2025-01-11 18:44:55 +01:00
|
|
|
return fmt.Errorf("cannot delete own user: %w", domain.ErrInvalidData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m Manager) validateApiChange(ctx context.Context, user *domain.User) error {
|
|
|
|
|
currentUser := domain.GetUserInfo(ctx)
|
|
|
|
|
|
|
|
|
|
if currentUser.Id != user.Identifier {
|
|
|
|
|
return fmt.Errorf("cannot change API access of user: %w", domain.ErrNoPermission)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
// region internal-modifiers
|
2025-04-19 12:12:45 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
func (m Manager) enrichUser(ctx context.Context, user *domain.User) *domain.User {
|
|
|
|
|
if user == nil {
|
|
|
|
|
return nil
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
2026-01-21 22:22:22 +01:00
|
|
|
peers, _ := m.peers.GetUserPeers(ctx, user.Identifier) // ignore error, list will be empty in error case
|
|
|
|
|
user.LinkedPeerCount = len(peers)
|
|
|
|
|
return user
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
func (m Manager) getUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
|
|
|
|
user, err := m.users.GetUser(ctx, id)
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, fmt.Errorf("unable to load user %s: %w", id, err)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
2026-01-21 22:22:22 +01:00
|
|
|
return m.enrichUser(ctx, user), nil
|
|
|
|
|
}
|
2023-08-04 13:34:18 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
func (m Manager) update(ctx context.Context, existingUser, user *domain.User, keepAuthentications bool) (
|
|
|
|
|
*domain.User,
|
|
|
|
|
error,
|
|
|
|
|
) {
|
|
|
|
|
if err := m.validateModifications(ctx, existingUser, user); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("update not allowed: %w", err)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
err := user.HashPassword()
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if user.Password == "" { // keep old password
|
|
|
|
|
user.Password = existingUser.Password
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
err = m.users.SaveUser(ctx, existingUser.Identifier, func(u *domain.User) (*domain.User, error) {
|
|
|
|
|
user.CopyCalculatedAttributes(u, keepAuthentications)
|
|
|
|
|
return user, nil
|
|
|
|
|
})
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, fmt.Errorf("update failure: %w", err)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
m.bus.Publish(app.TopicUserUpdated, *user)
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case !existingUser.IsDisabled() && user.IsDisabled():
|
|
|
|
|
m.bus.Publish(app.TopicUserDisabled, *user)
|
|
|
|
|
case existingUser.IsDisabled() && !user.IsDisabled():
|
|
|
|
|
m.bus.Publish(app.TopicUserEnabled, *user)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
return user, nil
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
func (m Manager) create(ctx context.Context, user *domain.User) (*domain.User, error) {
|
|
|
|
|
if user.Identifier == "" {
|
|
|
|
|
return nil, errors.New("missing user identifier")
|
|
|
|
|
}
|
2024-09-22 11:49:23 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
|
|
|
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
|
|
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
|
|
|
|
}
|
|
|
|
|
if existingUser != nil {
|
|
|
|
|
return nil, errors.Join(fmt.Errorf("user %s already exists", user.Identifier), domain.ErrDuplicateEntry)
|
|
|
|
|
}
|
2025-02-28 08:29:40 +01:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
// Add default authentication if missing
|
|
|
|
|
if len(user.Authentications) == 0 {
|
|
|
|
|
ctxUserInfo := domain.GetUserInfo(ctx)
|
|
|
|
|
now := time.Now()
|
|
|
|
|
user.Authentications = []domain.UserAuthentication{
|
|
|
|
|
{
|
|
|
|
|
BaseModel: domain.BaseModel{
|
|
|
|
|
CreatedBy: ctxUserInfo.UserId(),
|
|
|
|
|
UpdatedBy: ctxUserInfo.UserId(),
|
|
|
|
|
CreatedAt: now,
|
|
|
|
|
UpdatedAt: now,
|
|
|
|
|
},
|
|
|
|
|
UserIdentifier: user.Identifier,
|
|
|
|
|
Source: domain.UserSourceDatabase,
|
|
|
|
|
ProviderName: "",
|
|
|
|
|
},
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
if err := m.validateCreation(ctx, user); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("creation not allowed: %w", err)
|
|
|
|
|
}
|
2023-08-04 13:34:18 +02:00
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
err = user.HashPassword()
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2026-01-21 22:22:22 +01:00
|
|
|
return nil, err
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
err = m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
|
|
|
|
return user, nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("creation failure: %w", err)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:22:22 +01:00
|
|
|
m.bus.Publish(app.TopicUserCreated, *user)
|
|
|
|
|
|
|
|
|
|
return user, nil
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
2026-01-21 22:22:22 +01:00
|
|
|
|
|
|
|
|
// endregion internal-modifiers
|