user

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLastAdminDeletionForbidden is returned when attempting to delete the last admin
	ErrLastAdminDeletionForbidden = errors.New("cannot delete the last administrator account")
	// ErrInvalidConfirmationString is returned when the confirmation string is invalid
	ErrInvalidConfirmationString = errors.New("invalid confirmation string")
)
View Source
var (
	// ErrUsernameInvalid is returned when username format is invalid
	ErrUsernameInvalid = errors.New("username must be 1-50 characters and contain only letters, numbers, underscores, and hyphens")

	// ErrEmailInvalid is returned when email format is invalid
	ErrEmailInvalid = errors.New("please enter a valid email address")

	// ErrUsernameExists is returned when username already exists
	ErrUsernameExists = errors.New("username already exists")

	// ErrEmailExists is returned when email already exists
	ErrEmailExists = errors.New("email address already registered")

	// ErrEmailBlocked is returned when email is blocked
	ErrEmailBlocked = errors.New("this email address has been blocked")

	// ErrInvalidRole is returned when role is not in the allowed list
	ErrInvalidRole = errors.New("invalid user role")

	// ErrAdminCreateFailed is returned when admin user creation fails
	ErrAdminCreateFailed = errors.New("admin user creation failed")
)
View Source
var (
	ErrInvalidEmail        = errors.New("invalid email address")
	ErrEmailAlreadyInUse   = errors.New("email already in use")
	ErrInvalidPassword     = errors.New("invalid password")
	ErrProfileUserNotFound = errors.New("user not found")
)
View Source
var (
	ErrUserNotFound        = errors.New("user not found")
	ErrEmailAlreadyBlocked = errors.New("email is already blocked")
	ErrInvalidStatus       = errors.New("user is not in the expected status")
)

Functions

This section is empty.

Types

type AccountDeletionService

type AccountDeletionService struct {
	// contains filtered or unexported fields
}

AccountDeletionService handles account deletion operations

func NewAccountDeletionService

func NewAccountDeletionService(
	userRepo repository.UserRepo,
	deletionRepo repository.UserDeletionRepo,
	emailService email.EmailService,
	logger *util.Logger,
) *AccountDeletionService

NewAccountDeletionService creates a new account deletion service

func (*AccountDeletionService) DeleteAccount

func (s *AccountDeletionService) DeleteAccount(ctx context.Context, userID int, username, userEmail string) error

DeleteAccount performs a hard delete of a user account and all associated data

func (*AccountDeletionService) ValidateConfirmationString

func (s *AccountDeletionService) ValidateConfirmationString(confirmation string) error

ValidateConfirmationString validates that the confirmation string is exactly "DELETE"

type AccountDeletionServiceInterface

type AccountDeletionServiceInterface interface {
	DeleteAccount(ctx context.Context, userID int, username, userEmail string) error
	ValidateConfirmationString(confirmation string) error
}

AccountDeletionServiceInterface defines the interface for account deletion operations

type AdminCreateUserRepo

type AdminCreateUserRepo interface {
	CheckUsernameExists(ctx context.Context, username string) (bool, error)
	CheckEmailExists(ctx context.Context, email string) (bool, error)
	CreateUser(ctx context.Context, user *repository.User) error
}

AdminCreateUserRepo defines the repository methods needed for admin user creation

type AdminCreateUserRequest

type AdminCreateUserRequest struct {
	Username     string
	Name         string
	Email        string
	Role         string
	CustomFields map[string]any
}

AdminCreateUserRequest represents the input for admin user creation

type AdminCreateUserResult

type AdminCreateUserResult struct {
	User          *repository.User
	PlainPassword string
}

AdminCreateUserResult contains the result of admin user creation

type AdminCreateUserService

type AdminCreateUserService struct {
	// contains filtered or unexported fields
}

AdminCreateUserService handles admin-initiated user creation

func NewAdminCreateUserService

func NewAdminCreateUserService(userRepo AdminCreateUserRepo, blockedEmailRepo BlockedEmailRepo) *AdminCreateUserService

NewAdminCreateUserService creates a new admin create user service

func (*AdminCreateUserService) CreateUser

CreateUser creates a new user with verified status and an auto-generated password

type BlockedEmailRepo

type BlockedEmailRepo interface {
	BlockEmail(ctx context.Context, email string, reason string) error
	IsEmailBlocked(ctx context.Context, email string) (bool, error)
	UnblockEmail(ctx context.Context, email string) error
}

BlockedEmailRepo defines the interface for blocked email repository operations

type EmailUpdateRequest

type EmailUpdateRequest struct {
	NewEmail        string
	CurrentPassword string
}

EmailUpdateRequest represents a request to update email

type PasswordUpdateRequest

type PasswordUpdateRequest struct {
	CurrentPassword string
	NewPassword     string
}

PasswordUpdateRequest represents a request to update password

type Profile

type Profile struct {
	ID             int
	Username       string
	Name           string
	Email          string
	Role           string
	ProfilePicture string
	CreatedAt      string
	UpdatedAt      string
	CustomFields   map[string]any
}

Profile represents user profile information

type ProfileService

type ProfileService struct {
	// contains filtered or unexported fields
}

ProfileService handles profile management operations

func NewProfileService

func NewProfileService(
	userRepo repository.UserRepo,
	emailUpdateTokenRepo repository.EmailUpdateTokenRepo,
	userDataExportRepo repository.UserDataExportRepo,
	emailService email.EmailService,
	systemFieldProvider systemFieldProvider,
) *ProfileService

NewProfileService creates a new profile service

func (*ProfileService) ChangePassword

func (s *ProfileService) ChangePassword(ctx context.Context, userID int, currentPassword, newPassword string) error

ChangePassword updates a user's password

func (*ProfileService) ExportUserData

func (s *ProfileService) ExportUserData(ctx context.Context, userID int) (*repository.UserDataExport, error)

ExportUserData generates a JSON export of all user data

func (*ProfileService) GetProfile

func (s *ProfileService) GetProfile(ctx context.Context, userID int) (*Profile, error)

GetProfile retrieves user profile information

func (*ProfileService) UpdateCustomFields

func (s *ProfileService) UpdateCustomFields(ctx context.Context, userID int, customFields map[string]any, isAdmin bool) error

UpdateCustomFields updates a user's custom fields. When isAdmin is false, system field slugs are stripped (defense-in-depth).

func (*ProfileService) UpdateEmail

func (s *ProfileService) UpdateEmail(ctx context.Context, userID int, newEmail, currentPasswordHash string) error

UpdateEmail initiates an email update by creating a verification token and sending an email

func (*ProfileService) VerifyEmailUpdate

func (s *ProfileService) VerifyEmailUpdate(ctx context.Context, token string) (int, string, error)

VerifyEmailUpdate verifies an email update token and updates the user's email

type ProfileServiceInterface

type ProfileServiceInterface interface {
	GetProfile(ctx context.Context, userID int) (*Profile, error)
	UpdateEmail(ctx context.Context, userID int, newEmail, currentPasswordHash string) error
	ChangePassword(ctx context.Context, userID int, currentPassword, newPassword string) error
	ExportUserData(ctx context.Context, userID int) (*repository.UserDataExport, error)
	VerifyEmailUpdate(ctx context.Context, token string) (int, string, error)
	UpdateCustomFields(ctx context.Context, userID int, customFields map[string]any, isAdmin bool) error
}

ProfileServiceInterface defines the interface for profile service operations

type UserManagementService

type UserManagementService struct {
	// contains filtered or unexported fields
}

UserManagementService handles user management operations

func NewUserManagementService

func NewUserManagementService(userRepo UserRepo, blockedEmailRepo BlockedEmailRepo) *UserManagementService

NewUserManagementService creates a new user management service

func (*UserManagementService) ApproveUser

func (s *UserManagementService) ApproveUser(ctx context.Context, userID int) error

ApproveUser approves a pending user by updating their status to verified Uses atomic update to prevent TOCTOU race conditions

func (*UserManagementService) GetAllUsers

func (s *UserManagementService) GetAllUsers(ctx context.Context, status string, limit int, offset int) ([]*repository.User, error)

GetAllUsers retrieves all users with optional status filtering and pagination

func (*UserManagementService) GetPendingUsers

func (s *UserManagementService) GetPendingUsers(ctx context.Context, limit int, offset int) ([]*repository.User, error)

GetPendingUsers retrieves users with pending status with pagination Default limit: 100, max limit: 1000

func (*UserManagementService) GetUserStatus

func (s *UserManagementService) GetUserStatus(ctx context.Context, userID int) (string, error)

GetUserStatus retrieves the current status of a user

func (*UserManagementService) IsEmailBlocked

func (s *UserManagementService) IsEmailBlocked(ctx context.Context, email string) (bool, error)

IsEmailBlocked checks if an email address is blocked

func (*UserManagementService) MarkUserAsSpam

func (s *UserManagementService) MarkUserAsSpam(ctx context.Context, userID int) error

MarkUserAsSpam deletes a user and blocks their email address

func (*UserManagementService) RejectUser

func (s *UserManagementService) RejectUser(ctx context.Context, userID int) error

RejectUser deletes a user account

func (*UserManagementService) SoftDeleteUser

func (s *UserManagementService) SoftDeleteUser(ctx context.Context, userID int) error

SoftDeleteUser soft deletes a user account Only users with 'verified' or 'suspended' status can be soft deleted

func (*UserManagementService) SuspendUser

func (s *UserManagementService) SuspendUser(ctx context.Context, userID int) error

SuspendUser suspends a user account Only users with 'verified' status can be suspended

func (*UserManagementService) UnsuspendUser

func (s *UserManagementService) UnsuspendUser(ctx context.Context, userID int) error

UnsuspendUser unsuspends a user account Only users with 'suspended' status can be unsuspended; status is set to 'verified'

func (*UserManagementService) UpdateUserProfile

func (s *UserManagementService) UpdateUserProfile(
	ctx context.Context,
	userID int,
	name string,
	email string,
	role string,
	customFields map[string]any,
) (*repository.User, error)

UpdateUserProfile updates a user's profile fields (name, email, role, custom fields). All fields are optional — only non-empty fields are updated.

type UserRepo

type UserRepo interface {
	GetPendingUsers(ctx context.Context, limit int, offset int) ([]*repository.User, error)
	UpdateUserStatus(ctx context.Context, userID int, status string) error
	UpdateUserStatusIfCurrentStatus(ctx context.Context, userID int, currentStatus string, newStatus string) error
	DeleteUser(ctx context.Context, userID int) error
	GetUserByID(ctx context.Context, userID int) (*repository.User, error)
	SuspendUser(ctx context.Context, userID int) error
	UnsuspendUser(ctx context.Context, userID int) error
	SoftDeleteUser(ctx context.Context, userID int) error
	GetAllUsers(ctx context.Context, status string, limit int, offset int) ([]*repository.User, error)
	GetUserStatus(ctx context.Context, userID int) (string, error)
	UpdateProfile(ctx context.Context, userID int, name string, email string, role string, customFields map[string]any) error
	CheckEmailExistsForOtherUser(ctx context.Context, userID int, email string) (bool, error)
}

UserRepo defines the interface for user repository operations

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL