models

package
v0.0.0-...-e8003ba Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RoleHierarchy = map[Role]int{
	RoleGuest:       0,
	RoleReadOnly:    10,
	RoleUser:        20,
	RoleAuditor:     30,
	RoleEditor:      40,
	RoleModerator:   50,
	RoleSupport:     60,
	RoleAdmin:       70,
	RoleOwner:       80,
	RoleSystemAdmin: 90,
}

RoleHierarchy defines the privilege level of each role. Higher numbers represent higher privileges. If you define additional roles, place them in here.

Functions

func ListRoles

func ListRoles() []string

ListRoles returns a slice of all existing roles from the RoleHierarchy with the lowest permission role first and the highest last.

func NewDatabaseError

func NewDatabaseError(err error) error

NewDatabaseError creates a new DatabaseError.

func NewTransformationError

func NewTransformationError(msg string) error

NewTransformationError creates a new TransformationError.

func NewValidationError

func NewValidationError(msg string) error

NewValidationError creates a new ValidationError with the given message.

Types

type Claims

type Claims map[string]Role

Claims maps resource paths to roles, representing access permissions.

func (Claims) AddRole

func (c Claims) AddRole(resource string, role Role)

AddRole assigns a Role to the specified resource path in the Claims map.

func (Claims) AsSlice

func (c Claims) AsSlice() []string

AsSlice returns the claims as a colon delimited slice of roles eg. "/": "admin", "/admin": "user" -> ["/:admin", "/admin:user"]

func (Claims) GetEffectiveRole

func (c Claims) GetEffectiveRole(resource string) (Role, bool)

GetEffectiveRole returns the most specific role matching the given resource path. It finds the longest prefix in the Claims map that matches the resource, applying path boundary checks to avoid partial matches (e.g., "/api/" does not match "/api2/"). Returns the matched Role and true if found, or an empty Role and false if no match exists.

func (Claims) HasAtLeast

func (c Claims) HasAtLeast(resource string, required Role) bool

HasAtLeast reports whether the role for the given resource path meets or exceeds the required Role level. Returns false if no role is found.

func (Claims) MarshalJSON

func (c Claims) MarshalJSON() ([]byte, error)

func (*Claims) UnmarshalJSON

func (c *Claims) UnmarshalJSON(data []byte) error

type CreateUserParams

type CreateUserParams struct {
	Email         string  `json:"email"`
	Password      *string `json:"password"`
	Role          Role    `json:"role"`
	Claims        Claims  `json:"claims"`
	OauthProvider *string `json:"oauthProvider"`
	OauthID       *string `json:"oauthId"`
}

func (*CreateUserParams) Validate

func (c *CreateUserParams) Validate() error

type DatabaseError

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

DatabaseError – for failures interacting with the persistence layer. Supports errors.As and errors.Unwrap.

DatabaseError wraps errors related to database or SQL interactions. Will only be provided as a response from internal stores.

func (*DatabaseError) Error

func (e *DatabaseError) Error() string

Error implements the error interface.

func (*DatabaseError) Unwrap

func (e *DatabaseError) Unwrap() error

type GetPaginatedUsersParams

type GetPaginatedUsersParams struct {
	Page  int   `json:"page"`
	Limit int   `json:"limit"`
	Role  *Role `json:"role"`
}

func (*GetPaginatedUsersParams) Validate

func (p *GetPaginatedUsersParams) Validate() error

type PaginationMeta

type PaginationMeta struct {
	Limit      int `json:"limit"`
	Page       int `json:"page"`
	Total      int `json:"total"`
	TotalPages int `json:"totalPages"`
}

type Role

type Role string

Role represents a user role in the system

const (
	RoleGuest       Role = "guest"     // very limited access, items you'd only want publically available
	RoleReadOnly    Role = "readonly"  // Can view, but perhaps not interact or create
	RoleUser        Role = "user"      // standard authenticated user, can usually create and owns their own data
	RoleAuditor     Role = "auditor"   // more access than user but usually not allowed to edit
	RoleEditor      Role = "editor"    // can edit and modify content but perhaps not users
	RoleModerator   Role = "moderator" // Can moderate content, user content etc
	RoleSupport     Role = "support"   //  can perform support actions, perhaps can view sensitive data
	RoleAdmin       Role = "admin"     // Full administrative control within a scope
	RoleOwner       Role = "owner"     // Owner of specific entity, can delegate admins
	RoleSystemAdmin Role = "sysadmin"  // system wide administrator, highest privilege
)

These are the standard roles available by default

func (Role) AtLeast

func (r Role) AtLeast(min Role) bool

func (Role) IsValid

func (r Role) IsValid() bool

IsValid checks if the Role is one of the predefined valid roles.

func (Role) MarshalText

func (r Role) MarshalText() ([]byte, error)

func (Role) String

func (r Role) String() string

String implements the fmt.Stringer interface, providing a string representation of the Role.

func (*Role) UnmarshalText

func (r *Role) UnmarshalText(text []byte) error

UnmarshalText and MarshalText methods

type Session

type Session struct {
	ID        string    // Unique identifier for the session (e.g., UUID)
	UserID    uuid.UUID // ID of the user associated with this session
	ExpiresAt time.Time // When the session becomes invalid
	CreatedAt time.Time // When the session was created
	IpAddress *string   // Optional ip address
	UserAgent *string   // Optional UserAgent
}

Session represents the data stored for a single user session.

type TransformationError

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

TransformationError – for issues converting generic input into backend-specific formats. Supports errors.As.

TransformationError wraps errors that occur during the transformation of inputs.

func (*TransformationError) Error

func (e *TransformationError) Error() string

Error implements the error interface.

type UpdateUserByIDParams

type UpdateUserByIDParams struct {
	ID            uuid.UUID `json:"id"`
	Email         *string   `json:"email"`
	Password      *string   `json:"-"`
	Role          *Role     `json:"role"`
	Claims        *Claims   `json:"claims"`
	OauthProvider *string   `json:"oauthProvider"`
	OauthID       *string   `json:"oauthId"`
	IsActive      *bool     `json:"isActive"`
}

UpdateUserByIDParams is a struct for updating a user Everything is intentionally a pointer to allow for Coalescing at the db level

func (*UpdateUserByIDParams) Verify

func (u *UpdateUserByIDParams) Verify() error

Verify performs validation and transformation on the UpdateUserByIDParams struct. It ensures required fields are present, validates input formats, and hashes the password if provided.

type User

type User struct {
	ID            uuid.UUID `json:"id"`
	Email         string    `json:"email"`
	PasswordHash  *string   `json:"-"`
	Role          Role      `json:"role"`
	Claims        Claims    `json:"claims"`
	OauthProvider *string   `json:"oauthProvider"`
	OauthID       *string   `json:"oauthId"`
	CreatedAt     time.Time `json:"createdAt"`
	UpdatedAt     time.Time `json:"updatedAt"`
	IsActive      bool      `json:"isActive"`
}

func (*User) EnsureRootClaim

func (u *User) EnsureRootClaim()

type UserOAuthParams

type UserOAuthParams struct {
	OauthProvider string `json:"oauthProvider"`
	OauthID       string `json:"oauthId"`
}

type ValidationError

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

ValidationError – for invalid parameters or business rule violations. Supports errors.As.

ValidationError represents an error due to invalid or malformed input.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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