Documentation
¶
Overview ¶
Package types defines the domain models and request/response types used by the admin plugin.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BanUserRequest ¶
type BanUserRequest struct {
Reason string `json:"reason"` // Ban reason (required)
ExpiresAt *time.Time `json:"expiresAt,omitempty"` // Ban expiration (nil = permanent)
}
BanUserRequest represents a request to ban a user.
Validation:
- reason: Required, provides context for ban decision
- expiresAt: Optional, nil for permanent ban
Example (temporary ban):
{
"reason": "Spam",
"expiresAt": "2024-12-31T23:59:59Z"
}
Example (permanent ban):
{
"reason": "Terms of service violation",
"expiresAt": null
}
type StatsResponse ¶
type StatsResponse struct {
TotalUsers int `json:"totalUsers"` // Total registered users
}
StatsResponse represents platform statistics.
Example Response:
{
"totalUsers": 1234
}
type Store ¶
type Store interface {
// Create creates a new user with admin fields.
Create(ctx context.Context, user User) (User, error)
// GetByEmail retrieves a user by email address.
GetByEmail(ctx context.Context, email string) (User, error)
// GetByID retrieves a user by ID.
GetByID(ctx context.Context, id string) (User, error)
// Update updates user fields (name, email, disabled, etc.).
// Note: Does not update role - use AssignRole/RemoveRole instead.
Update(ctx context.Context, user User) error
// Delete soft-deletes a user by setting updated_at timestamp.
Delete(ctx context.Context, id string) error
// List retrieves paginated users.
List(ctx context.Context, offset, limit int) ([]User, error)
// ListUsersRaw retrieves paginated users as raw map data.
// This supports flexible admin UIs without schema changes.
ListUsersRaw(ctx context.Context, offset, limit int) ([]map[string]any, error)
// GetUserRaw retrieves a user as raw map data.
GetUserRaw(ctx context.Context, userID string) (map[string]any, error)
// Count returns total user count.
Count(ctx context.Context) (int, error)
// AssignRole assigns a role to a user (e.g., "admin").
AssignRole(ctx context.Context, userID string, role string) error
// RemoveRole removes a user's role.
RemoveRole(ctx context.Context, userID string, role string) error
// GetRole retrieves a user's role (empty string if no role).
GetRole(ctx context.Context, userID string) (string, error)
// BanUser bans a user with a reason and optional expiry date.
// If expiry is nil, the ban is permanent.
// Increments ban_counter for repeat offender tracking.
BanUser(ctx context.Context, userID, reason string, expiry *time.Time) error
// UnbanUser removes the ban from a user.
UnbanUser(ctx context.Context, userID string) error
// GetStats retrieves platform statistics.
GetStats(ctx context.Context) (StatsResponse, error)
}
Store defines the interface for admin user storage operations.
This interface extends the core auth.UserStore with admin-specific functionality:
- Role assignment and retrieval
- User ban management with expiry dates
- Platform statistics
- Raw database access for admin UI (flexible schema)
All methods are context-aware for cancellation and timeout support.
Thread Safety: Implementations must be safe for concurrent use.
type UpdateRoleRequest ¶
type UpdateRoleRequest struct {
Role string `json:"role"` // New role to assign (required)
}
UpdateRoleRequest represents a request to update a user's role.
Validation:
- role: Required, the new role to assign to the user (e.g., "admin", "user")
Example:
{
"role": "admin"
}
type User ¶
type User struct {
auth.User
Role string `json:"role"` // User role for RBAC (e.g., "admin")
// Ban management fields
Banned bool `json:"banned"` // Current ban status
BanReason string `json:"banReason,omitempty"` // Reason for ban
BanExpiry *time.Time `json:"banExpiry,omitempty"` // Ban expiration (nil = permanent)
BanCounter int `json:"banCounter,omitempty"` // Number of bans (repeat offender tracking)
}
User represents a user with admin-specific extensions.
This model extends auth.User with role-based access control and ban management.
Admin Extensions:
- Role: User role (e.g., "admin") for authorization checks
- Banned: Whether user is currently banned
- BanReason: Admin-provided reason for ban
- BanExpiry: Ban expiration date (nil for permanent bans)
- BanCounter: Number of times user has been banned (for repeat offender tracking)
Database Mapping: These fields are stored in additional columns on the 'user' table:
- role (VARCHAR)
- banned (BOOLEAN)
- ban_reason (TEXT)
- ban_expiry (TIMESTAMP, nullable)
- ban_counter (INTEGER, default 0)
Example:
user := types.User{
User: auth.User{ID: "user_123", Email: "admin@example.com"},
Role: "admin",
Banned: false,
}