user

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound           = errors.New("user not found")
	ErrRoleNotFound           = errors.New("role not found")
	ErrInvalidInput           = errors.New("invalid input")
	ErrAlreadyExists          = errors.New("user already exists")
	ErrReservedUsername       = errors.New("reserved username")
	ErrInvalidPassword        = errors.New("invalid password")
	ErrUnauthorized           = errors.New("unauthorized")
	ErrInvalidAPIKey          = errors.New("invalid API key")
	ErrPasswordRequired       = errors.New("password is required for non-OAuth users")
	ErrCannotDeleteSelf       = errors.New("user can't delete self")
	ErrCannotDeleteAdmin      = errors.New("can't delete admin user")
	ErrPasswordChangeRequired = errors.New("password change required")
)

Functions

This section is empty.

Types

type APIKey

type APIKey struct {
	ID         string     `json:"id"`
	UserID     string     `json:"user_id"`
	Name       string     `json:"name"`
	Key        string     `json:"key,omitempty"`
	ExpiresAt  *time.Time `json:"expires_at,omitempty"`
	LastUsedAt *time.Time `json:"last_used_at,omitempty"`
	CreatedAt  time.Time  `json:"created_at"`
}

type CreateUserInput

type CreateUserInput struct {
	Username       string   `json:"username" validate:"required,min=3,max=255"`
	Name           string   `json:"name" validate:"required"`
	Password       string   `json:"password" validate:"required_without=OAuthProvider,min=8"`
	ProfilePicture string   `json:"profile_picture,omitempty"`
	RoleNames      []string `json:"role_names" validate:"required,min=1"`

	OAuthProvider     string                 `json:"oauth_provider,omitempty"`
	OAuthProviderID   string                 `json:"oauth_provider_id,omitempty"`
	OAuthProviderData map[string]interface{} `json:"oauth_provider_data,omitempty"`
}

type Filter

type Filter struct {
	Query   string
	RoleIDs []string
	Active  *bool
	Limit   int
	Offset  int
}

type Permission

type Permission struct {
	ID           string `json:"id"`
	Name         string `json:"name"`
	Description  string `json:"description"`
	ResourceType string `json:"resource_type"`
	Action       string `json:"action"`
}

type PostgresRepository

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

func (*PostgresRepository) AssignRoles

func (r *PostgresRepository) AssignRoles(ctx context.Context, userID string, roleNames []string) error

func (*PostgresRepository) CreateAPIKey

func (r *PostgresRepository) CreateAPIKey(ctx context.Context, apiKey *APIKey, keyHash string) error

func (*PostgresRepository) CreateUser

func (r *PostgresRepository) CreateUser(ctx context.Context, user *User, passwordHash string) error

func (*PostgresRepository) CreateUserIdentity

func (r *PostgresRepository) CreateUserIdentity(ctx context.Context, identity *UserIdentity) error

func (*PostgresRepository) DeleteAPIKey

func (r *PostgresRepository) DeleteAPIKey(ctx context.Context, id string) error

func (*PostgresRepository) DeleteUser

func (r *PostgresRepository) DeleteUser(ctx context.Context, id string) error

func (*PostgresRepository) DeleteUserIdentity

func (r *PostgresRepository) DeleteUserIdentity(ctx context.Context, userID string, provider string) error

func (*PostgresRepository) FindSimilarUsernames added in v0.5.0

func (r *PostgresRepository) FindSimilarUsernames(ctx context.Context, searchTerm string, limit int) ([]string, error)

FindSimilarUsernames finds usernames similar to the given search term

func (*PostgresRepository) GetAPIKey

func (r *PostgresRepository) GetAPIKey(ctx context.Context, id string) (*APIKey, error)

func (*PostgresRepository) GetAPIKeyByHash

func (r *PostgresRepository) GetAPIKeyByHash(ctx context.Context, keyToValidate string) (*APIKey, error)

func (*PostgresRepository) GetPermissionsByRoleName added in v0.2.0

func (r *PostgresRepository) GetPermissionsByRoleName(ctx context.Context, roleName string) ([]Permission, error)

func (*PostgresRepository) GetUser

func (r *PostgresRepository) GetUser(ctx context.Context, id string) (*User, error)

func (*PostgresRepository) GetUserByProviderID

func (r *PostgresRepository) GetUserByProviderID(ctx context.Context, provider string, providerUserID string) (*User, error)

func (*PostgresRepository) GetUserByUsername

func (r *PostgresRepository) GetUserByUsername(ctx context.Context, username string) (*User, error)

func (*PostgresRepository) GetUserIdentities

func (r *PostgresRepository) GetUserIdentities(ctx context.Context, userID string) ([]*UserIdentity, error)

func (*PostgresRepository) HasPermission

func (r *PostgresRepository) HasPermission(ctx context.Context, userID string, resourceType string, action string) (bool, error)

func (*PostgresRepository) ListAPIKeys

func (r *PostgresRepository) ListAPIKeys(ctx context.Context, userID string) ([]*APIKey, error)

func (*PostgresRepository) ListUsers

func (r *PostgresRepository) ListUsers(ctx context.Context, filter Filter) ([]*User, int, error)

func (*PostgresRepository) UpdateAPIKeyLastUsed

func (r *PostgresRepository) UpdateAPIKeyLastUsed(ctx context.Context, id string) error

func (*PostgresRepository) UpdatePreferences

func (r *PostgresRepository) UpdatePreferences(ctx context.Context, userID string, preferences map[string]interface{}) error

func (*PostgresRepository) UpdateRoles

func (r *PostgresRepository) UpdateRoles(ctx context.Context, userID string, roleNames []string) error

func (*PostgresRepository) UpdateUser

func (r *PostgresRepository) UpdateUser(ctx context.Context, id string, updates map[string]interface{}) error

func (*PostgresRepository) UsernameExists

func (r *PostgresRepository) UsernameExists(ctx context.Context, username string) (bool, error)

func (*PostgresRepository) ValidatePassword

func (r *PostgresRepository) ValidatePassword(ctx context.Context, userID string, password string) error

type Repository

type Repository interface {
	CreateUser(ctx context.Context, user *User, password string) error
	GetUser(ctx context.Context, id string) (*User, error)
	GetUserByUsername(ctx context.Context, email string) (*User, error)
	FindSimilarUsernames(ctx context.Context, searchTerm string, limit int) ([]string, error)
	GetUserByProviderID(ctx context.Context, provider string, providerUserID string) (*User, error)
	UpdateUser(ctx context.Context, id string, updates map[string]interface{}) error
	UpdatePreferences(ctx context.Context, userID string, preferences map[string]interface{}) error
	DeleteUser(ctx context.Context, id string) error
	ListUsers(ctx context.Context, filter Filter) ([]*User, int, error)

	CreateUserIdentity(ctx context.Context, identity *UserIdentity) error
	GetUserIdentities(ctx context.Context, userID string) ([]*UserIdentity, error)
	DeleteUserIdentity(ctx context.Context, userID string, provider string) error

	CreateAPIKey(ctx context.Context, apiKey *APIKey, keyHash string) error
	GetAPIKey(ctx context.Context, id string) (*APIKey, error)
	GetAPIKeyByHash(ctx context.Context, keyHash string) (*APIKey, error)
	UpdateAPIKeyLastUsed(ctx context.Context, id string) error
	DeleteAPIKey(ctx context.Context, id string) error
	ListAPIKeys(ctx context.Context, userID string) ([]*APIKey, error)

	AssignRoles(ctx context.Context, userID string, roleNames []string) error
	UpdateRoles(ctx context.Context, userID string, roleNames []string) error
	HasPermission(ctx context.Context, userID string, resourceType string, action string) (bool, error)
	GetPermissionsByRoleName(ctx context.Context, roleName string) ([]Permission, error)
	ValidatePassword(ctx context.Context, userID string, password string) error

	UsernameExists(ctx context.Context, username string) (bool, error)
}

func NewPostgresRepository

func NewPostgresRepository(db *pgxpool.Pool) Repository

type Role

type Role struct {
	ID          string       `json:"id"`
	Name        string       `json:"name"`
	Description string       `json:"description"`
	Permissions []Permission `json:"permissions,omitempty"`
}

type Service

type Service interface {
	Create(ctx context.Context, input CreateUserInput) (*User, error)
	Update(ctx context.Context, id string, input UpdateUserInput) (*User, error)
	Delete(ctx context.Context, currentUserId string, id string) error
	Get(ctx context.Context, id string) (*User, error)
	GetUserByUsername(ctx context.Context, username string) (*User, error)
	FindSimilarUsernames(ctx context.Context, searchTerm string, limit int) ([]string, error)
	List(ctx context.Context, filter Filter) ([]*User, int, error)

	// Authentication
	Authenticate(ctx context.Context, username, password string) (*User, error)
	ValidateAPIKey(ctx context.Context, apiKey string) (*User, error)
	HasPermission(ctx context.Context, userID string, resourceType string, action string) (bool, error)
	GetPermissionsByRoleName(ctx context.Context, roleName string) ([]Permission, error)

	// OAuth
	GetUserByProviderID(ctx context.Context, provider string, providerUserID string) (*User, error)
	AuthenticateOAuth(ctx context.Context, provider string, providerUserID string, userInfo map[string]interface{}) (*User, error)
	LinkOAuthAccount(ctx context.Context, userID string, provider string, providerUserID string, userInfo map[string]interface{}) error
	UnlinkOAuthAccount(ctx context.Context, userID string, provider string) error

	// API Keys
	CreateAPIKey(ctx context.Context, userID string, name string, expiresIn *time.Duration) (*APIKey, error)
	DeleteAPIKey(ctx context.Context, userID string, keyID string) error
	ListAPIKeys(ctx context.Context, userID string) ([]*APIKey, error)

	UpdatePreferences(ctx context.Context, userID string, preferences map[string]interface{}) error
	UpdatePassword(ctx context.Context, userID string, newPassword string) (*User, error)
}

func NewService

func NewService(repo Repository, opts ...ServiceOption) Service

type ServiceOption

type ServiceOption func(*service)

type UpdateUserInput

type UpdateUserInput struct {
	Email          *string                `json:"email,omitempty" validate:"omitempty,email"`
	Name           *string                `json:"name,omitempty"`
	ProfilePicture *string                `json:"profile_picture,omitempty"`
	Password       *string                `json:"password,omitempty" validate:"omitempty,min=8"`
	Active         *bool                  `json:"active,omitempty"`
	Preferences    map[string]interface{} `json:"preferences,omitempty"`
	RoleNames      []string               `json:"role_names,omitempty" validate:"omitempty,min=1"`
}

type User

type User struct {
	ID                 string                 `json:"id"`
	Username           string                 `json:"username"`
	Name               string                 `json:"name"`
	ProfilePicture     string                 `json:"profile_picture,omitempty"`
	Active             bool                   `json:"active"`
	MustChangePassword bool                   `json:"must_change_password"`
	Preferences        map[string]interface{} `json:"preferences"`
	Roles              []Role                 `json:"roles"`
	Identities         []UserIdentity         `json:"identities,omitempty"`
	CreatedAt          time.Time              `json:"created_at"`
	UpdatedAt          time.Time              `json:"updated_at"`
}

type UserIdentity

type UserIdentity struct {
	ID             string                 `json:"id"`
	UserID         string                 `json:"user_id"`
	Provider       string                 `json:"provider"`
	ProviderUserID string                 `json:"provider_user_id"`
	ProviderEmail  string                 `json:"provider_email"`
	ProviderData   map[string]interface{} `json:"provider_data"`
	CreatedAt      time.Time              `json:"created_at"`
	UpdatedAt      time.Time              `json:"updated_at"`
}

Jump to

Keyboard shortcuts

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