user

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvitationNotFound  = errors.New("invitation not found")
	ErrInvitationInvalid   = errors.New("invitation token is invalid")
	ErrInvitationExpired   = errors.New("invitation has expired")
	ErrInvitationUsed      = errors.New("invitation already used")
	ErrInvitationRevoked   = errors.New("invitation has been revoked")
	ErrInvitationBadStatus = errors.New("invitation status is invalid")
)
View Source
var (
	ErrPasswordResetNotFound  = errors.New("password reset token not found")
	ErrPasswordResetInvalid   = errors.New("password reset token is invalid")
	ErrPasswordResetExpired   = errors.New("password reset token has expired")
	ErrPasswordResetUsed      = errors.New("password reset token already used")
	ErrPasswordResetRevoked   = errors.New("password reset token has been revoked")
	ErrPasswordResetBadStatus = errors.New("password reset token status is invalid")
)
View Source
var (
	ErrUserNotFound = errors.New("user not found")
	// ErrSuperAdminProtected indicates operations are blocked on super admin users.
	ErrSuperAdminProtected = errors.New("super admin user is protected")
)

Sentinel errors

Functions

func NewUserModule

func NewUserModule(logger logging.Logger, deps *core.Dependencies) core.Module

Types

type ActivateInvitationAPIRequest

type ActivateInvitationAPIRequest struct {
	InviteJWT       string `json:"inviteJwt"`
	Nickname        string `json:"nickname"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmPassword"`
}

type ActivateInvitationRequest

type ActivateInvitationRequest struct {
	InviteJWT       string
	Nickname        string
	Password        string
	ConfirmPassword string
}

type ActivateInvitationResponse

type ActivateInvitationResponse struct {
	User UserBasicDTO `json:"user"`
}

type ActivatePasswordResetAPIRequest

type ActivatePasswordResetAPIRequest struct {
	ResetJWT        string `json:"resetJwt"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirmPassword"`
}

type ActivatePasswordResetRequest

type ActivatePasswordResetRequest struct {
	ResetJWT        string
	Password        string
	ConfirmPassword string
}

type ActivatePasswordResetResponse

type ActivatePasswordResetResponse struct {
	User UserBasicDTO `json:"user"`
}

type AssignRolesRequest

type AssignRolesRequest struct {
	RoleIDs []string `json:"roleIds"` // Array of role UUIDs
}

AssignRolesRequest represents the assign roles request

type CreateInvitationAPIRequest

type CreateInvitationAPIRequest struct {
	Username   string   `json:"username"`
	Email      string   `json:"email"`
	DomainType string   `json:"domainType"`
	DomainKey  string   `json:"domainKey"`
	RoleIDs    []string `json:"roleIds"`
}

type CreateInvitationRequest

type CreateInvitationRequest struct {
	Username   string
	Email      string
	DomainType string
	DomainKey  string
	RoleIDs    []string
	CreatedBy  uuid.UUID
}

type CreateInvitationResponse

type CreateInvitationResponse struct {
	ID        uuid.UUID `json:"id"`
	InviteJWT string    `json:"inviteJwt"`
	Username  string    `json:"username"`
	Email     string    `json:"email"`
	ExpiresAt time.Time `json:"expiresAt"`
}

type CreatePasswordResetRequest

type CreatePasswordResetRequest struct {
	UserID    uuid.UUID
	CreatedBy uuid.UUID
}

type CreatePasswordResetResponse

type CreatePasswordResetResponse struct {
	ID        uuid.UUID `json:"id"`
	ResetJWT  string    `json:"resetJwt"`
	Email     string    `json:"email"`
	ExpiresAt time.Time `json:"expiresAt"`
}

type FreezeUserRequest

type FreezeUserRequest struct {
	Status string `json:"status"` // "suspended" or "active"
}

FreezeUserRequest represents the freeze/unfreeze user request

type InvitationItem

type InvitationItem struct {
	ID         uuid.UUID  `json:"id"`
	Username   string     `json:"username,omitempty"`
	Email      string     `json:"email"`
	DomainType string     `json:"domainType"`
	DomainKey  string     `json:"domainKey"`
	RoleIDs    []string   `json:"roleIds"`
	Status     string     `json:"status"`
	ExpiresAt  time.Time  `json:"expiresAt"`
	UsedAt     *time.Time `json:"usedAt,omitempty"`
}

type InvitationService

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

InvitationService handles invite-jwt based user onboarding.

func NewInvitationService

func NewInvitationService(
	client *ent.Client,
	logger logging.Logger,
	jwtService *jwt.JWTService,
	domainResolver core.DomainResolver,
	providers *core.InvitationProviderRegistry,
) *InvitationService

func (*InvitationService) ActivateInvitation

func (*InvitationService) CreateInvitation

func (*InvitationService) GetInvitation

func (s *InvitationService) GetInvitation(ctx context.Context, id uuid.UUID) (*InvitationItem, error)

func (*InvitationService) ListInvitations

func (s *InvitationService) ListInvitations(
	ctx context.Context,
	filters ListInvitationFilters,
) (*ListInvitationResult, error)

func (*InvitationService) RevokeInvitation

func (s *InvitationService) RevokeInvitation(ctx context.Context, id uuid.UUID) error

func (*InvitationService) ValidateInvitation

func (*InvitationService) WithRBACManager

func (s *InvitationService) WithRBACManager(rbacManager *rbac.RBACManager) *InvitationService

type ListInvitationFilters

type ListInvitationFilters struct {
	DomainType string
	DomainKey  string
	Status     string
	Page       int
	PageSize   int
}

type ListInvitationResult

type ListInvitationResult struct {
	Items      []InvitationItem `json:"items"`
	Total      int              `json:"total"`
	Page       int              `json:"page"`
	PageSize   int              `json:"pageSize"`
	TotalPages int              `json:"totalPages"`
}

type PasswordResetService

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

PasswordResetService handles invite-style reset-jwt workflow.

func NewPasswordResetService

func NewPasswordResetService(
	client *ent.Client,
	logger logging.Logger,
	jwtService *jwt.JWTService,
) *PasswordResetService

func (*PasswordResetService) ActivatePasswordReset

func (*PasswordResetService) CreatePasswordReset

func (*PasswordResetService) ValidatePasswordReset

type RestoreUserRequest

type RestoreUserRequest struct {
}

RestoreUserRequest represents the restore user request

type RoleDTO

type RoleDTO struct {
	ID          uuid.UUID `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description,omitempty"`
	Permissions []string  `json:"permissions,omitempty"`
}

RoleDTO represents a role with permissions

type UpdateProfileRequest

type UpdateProfileRequest struct {
	Nickname string `json:"nickname,omitempty"`
	Email    string `json:"email,omitempty"`
	Phone    string `json:"phone,omitempty"`
	Avatar   string `json:"avatar,omitempty"`
}

UpdateProfileRequest represents profile update data

type UpdateSettingsRequest

type UpdateSettingsRequest struct {
	Settings map[string]any `json:"settings"`
}

UpdateSettingsRequest represents the update settings request

type UserBasicDTO

type UserBasicDTO struct {
	ID       uuid.UUID `json:"id"`
	Username string    `json:"username"`
	Email    string    `json:"email"`
	Status   string    `json:"status,omitempty"`
}

UserBasicDTO represents basic user information

type UserHandler

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

func NewUserHandler

func NewUserHandler(
	userService *UserService,
	invitationService *InvitationService,
	passwordResetService *PasswordResetService,
	logger logging.Logger,
) *UserHandler

func (*UserHandler) ActivateInvitation

func (h *UserHandler) ActivateInvitation(w http.ResponseWriter, r *http.Request)

func (*UserHandler) ActivatePasswordReset

func (h *UserHandler) ActivatePasswordReset(w http.ResponseWriter, r *http.Request)

ActivatePasswordReset consumes reset JWT and updates password. @Summary Activate password reset @Tags Users @Accept json @Produce json @Param request body ActivatePasswordResetAPIRequest true "Activation payload" @Success 200 {object} ActivatePasswordResetResponse @Router /users/password-resets/activate [post]

func (*UserHandler) AssignRoles

func (h *UserHandler) AssignRoles(w http.ResponseWriter, r *http.Request)

AssignRoles handles assigning roles to a user @Summary Assign roles to a user @Tags Users @Accept json @Produce json @Param id path string true "User ID" @Param request body AssignRolesRequest true "Role IDs" @Success 200 {object} map[string]string @Router /users/{id}/roles [post]

func (*UserHandler) CreateInvitation

func (h *UserHandler) CreateInvitation(w http.ResponseWriter, r *http.Request)

func (*UserHandler) CreatePasswordReset

func (h *UserHandler) CreatePasswordReset(w http.ResponseWriter, r *http.Request)

CreatePasswordReset handles creating reset token for target user. @Summary Create password reset token @Tags Users @Accept json @Produce json @Param id path string true "User ID" @Success 201 {object} CreatePasswordResetResponse @Router /users/{id}/password-resets [post]

func (*UserHandler) DeleteUser

func (h *UserHandler) DeleteUser(w http.ResponseWriter, r *http.Request)

DeleteUser handles soft-deleting a user. @Summary Delete a user @Tags Users @Accept json @Produce json @Param id path string true "User ID" @Success 200 {object} map[string]string @Router /users/{id} [delete]

func (*UserHandler) FreezeUser

func (h *UserHandler) FreezeUser(w http.ResponseWriter, r *http.Request)

FreezeUser handles freezing/unfreezing a user @Summary Freeze or unfreeze a user @Tags Users @Accept json @Produce json @Param id path string true "User ID" @Param request body FreezeUserRequest true "Freeze request" @Success 200 {object} map[string]string @Router /users/{id}/freeze [post]

func (*UserHandler) GetInvitation

func (h *UserHandler) GetInvitation(w http.ResponseWriter, r *http.Request)

func (*UserHandler) GetProfile

func (h *UserHandler) GetProfile(w http.ResponseWriter, r *http.Request)

GetProfile handles getting current user's profile @Summary Get current user profile @Tags Profile @Accept json @Produce json @Success 200 {object} UserProfileDTO @Router /profile [get]

func (*UserHandler) ListInvitations

func (h *UserHandler) ListInvitations(w http.ResponseWriter, r *http.Request)

func (*UserHandler) ListUsers

func (h *UserHandler) ListUsers(w http.ResponseWriter, r *http.Request)

ListUsers handles listing users with filters @Summary List users with filters @Tags Users @Accept json @Produce json @Param q query string false "Search query (username, email, phone)" @Param status query string false "Status filter (active, suspended, inactive)" @Param role_id query string false "Role ID filter" @Param include_deleted query boolean false "Include soft-deleted users" @Param page query int false "Page number (default:1)" @Param pageSize query int false "Page size (default: 20, max: 100)" @Success 200 {object} UserListResult @Router /users [get]

func (*UserHandler) RestoreUser

func (h *UserHandler) RestoreUser(w http.ResponseWriter, r *http.Request)

RestoreUser handles restoring a soft-deleted user @Summary Restore a soft-deleted user @Tags Users @Accept json @Produce json @Param id path string true "User ID" @Success 200 {object} map[string]string @Router /users/{id}/restore [post]

func (*UserHandler) RevokeInvitation

func (h *UserHandler) RevokeInvitation(w http.ResponseWriter, r *http.Request)

func (*UserHandler) UpdateProfile

func (h *UserHandler) UpdateProfile(w http.ResponseWriter, r *http.Request)

UpdateProfile handles updating current user's profile @Summary Update current user profile @Tags Profile @Accept json @Produce json @Param request body UpdateProfileRequest true "Profile data" @Success 200 {object} UserProfileDTO @Router /profile [put]

func (*UserHandler) UpdateSettings

func (h *UserHandler) UpdateSettings(w http.ResponseWriter, r *http.Request)

UpdateSettings handles updating current user's settings @Summary Update current user settings @Tags Profile @Accept json @Produce json @Param request body UpdateSettingsRequest true "Settings data" @Success 200 {object} map[string]string @Router /profile/settings [put]

func (*UserHandler) ValidateInvitation

func (h *UserHandler) ValidateInvitation(w http.ResponseWriter, r *http.Request)

func (*UserHandler) ValidatePasswordReset

func (h *UserHandler) ValidatePasswordReset(w http.ResponseWriter, r *http.Request)

ValidatePasswordReset validates a reset JWT. @Summary Validate password reset token @Tags Users @Accept json @Produce json @Param resetJwt query string true "Reset JWT" @Success 200 {object} ValidatePasswordResetResponse @Router /users/password-resets/validate [get]

type UserListFilters

type UserListFilters struct {
	Query          string    // Search query (username, email, phone)
	Status         string    // Status filter (active, suspended, inactive)
	RoleID         uuid.UUID // Role ID filter
	IncludeDeleted bool      // Include soft-deleted users
	Page           int       // Page number (1-based)
	PageSize       int       // Items per page
}

UserListFilters represents filters for listing users

type UserListResult

type UserListResult struct {
	Users      []*UserProfileDTO `json:"users"`
	Total      int               `json:"total"`
	Page       int               `json:"page"`
	PageSize   int               `json:"pageSize"`
	TotalPages int               `json:"totalPages"`
}

UserListResult represents paginated user list result

type UserModule

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

func (*UserModule) Name

func (m *UserModule) Name() string

func (*UserModule) RegisterPrivateRoutes

func (m *UserModule) RegisterPrivateRoutes(r chi.Router)

RegisterPrivateRoutes registers protected user endpoints (profile, user management)

func (*UserModule) RegisterPublicRoutes

func (m *UserModule) RegisterPublicRoutes(r chi.Router)

RegisterPublicRoutes registers public user endpoints.

type UserProfileDTO

type UserProfileDTO struct {
	ID           uuid.UUID      `json:"id"`
	Username     string         `json:"username"`
	Email        string         `json:"email"`
	Nickname     string         `json:"nickname"`
	Phone        string         `json:"phone,omitempty"`
	Avatar       string         `json:"avatar,omitempty"`
	Status       string         `json:"status"`
	Settings     map[string]any `json:"settings,omitempty"`
	IsSuperAdmin bool           `json:"isSuperAdmin,omitempty"`
	Roles        []RoleDTO      `json:"roles,omitempty"`
}

UserProfileDTO represents user profile with roles and permissions

type UserService

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

UserService provides user management operations

func NewUserService

func NewUserService(client *ent.Client, logger logging.Logger) *UserService

NewUserService creates a new instance of UserService

func (*UserService) AssignRoles

func (s *UserService) AssignRoles(ctx context.Context, userID uuid.UUID, roleIDs []uuid.UUID) error

AssignRoles assigns multiple roles to a user

func (*UserService) DeleteUser

func (s *UserService) DeleteUser(ctx context.Context, userID uuid.UUID) error

DeleteUser performs soft-delete on user record.

func (*UserService) FreezeUser

func (s *UserService) FreezeUser(ctx context.Context, userID uuid.UUID) error

FreezeUser sets user status to "suspended"

func (*UserService) GetProfile

func (s *UserService) GetProfile(ctx context.Context, userID uuid.UUID) (*UserProfileDTO, error)

GetProfile retrieves user profile with roles and permissions loaded

func (*UserService) GetUserByID

func (s *UserService) GetUserByID(ctx context.Context, userID uuid.UUID) (*ent.User, error)

GetUserByID retrieves a user by ID

func (*UserService) ListUsers

func (s *UserService) ListUsers(ctx context.Context, filters UserListFilters) (*UserListResult, error)

ListUsers retrieves users with enhanced search and filtering

func (*UserService) RestoreUser

func (s *UserService) RestoreUser(ctx context.Context, userID uuid.UUID) error

RestoreUser restores a soft-deleted user

func (*UserService) UnfreezeUser

func (s *UserService) UnfreezeUser(ctx context.Context, userID uuid.UUID) error

UnfreezeUser sets user status to "active"

func (*UserService) UpdateProfile

func (s *UserService) UpdateProfile(ctx context.Context, userID uuid.UUID, req UpdateProfileRequest) (*UserProfileDTO, error)

UpdateProfile updates user profile information

func (*UserService) UpdateSettings

func (s *UserService) UpdateSettings(ctx context.Context, userID uuid.UUID, settings map[string]any) error

UpdateSettings updates user settings JSON

func (*UserService) WithRBACManager

func (s *UserService) WithRBACManager(rbacManager *rbac.RBACManager) *UserService

type ValidateInvitationRequest

type ValidateInvitationRequest struct {
	InviteJWT string
}

type ValidateInvitationResponse

type ValidateInvitationResponse struct {
	Valid      bool      `json:"valid"`
	Username   string    `json:"username"`
	Email      string    `json:"email"`
	DomainType string    `json:"domainType"`
	DomainKey  string    `json:"domainKey"`
	RoleIDs    []string  `json:"roleIds"`
	ExpiresAt  time.Time `json:"expiresAt"`
}

type ValidatePasswordResetRequest

type ValidatePasswordResetRequest struct {
	ResetJWT string
}

type ValidatePasswordResetResponse

type ValidatePasswordResetResponse struct {
	Valid     bool      `json:"valid"`
	UserID    uuid.UUID `json:"userId"`
	Email     string    `json:"email"`
	ExpiresAt time.Time `json:"expiresAt"`
}

Jump to

Keyboard shortcuts

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