Documentation
¶
Overview ¶
Package permission provides page-level permission management.
Index ¶
- Constants
- Variables
- func AllPagePermissions() []string
- func DefaultAdminPermissions() []string
- func DefaultGuestPermissions() []string
- func DefaultUserPermissions() []string
- func RequirePagePermission(service *Service, pagePermission string) echo.MiddlewareFunc
- type Handler
- func (h *Handler) GetAvailablePermissions(c echo.Context) error
- func (h *Handler) GetMyPermissions(c echo.Context) error
- func (h *Handler) GetUserPermissions(c echo.Context) error
- func (h *Handler) RegisterAdminRoutes(g *echo.Group)
- func (h *Handler) RegisterCurrentUserRoutes(g *echo.Group)
- func (h *Handler) RegisterRoutes(g *echo.Group)
- func (h *Handler) SetUserPermissions(c echo.Context) error
- type PermissionInfo
- type PermissionsResponse
- type Repository
- func (r *Repository) AddPermission(ctx context.Context, userID uuid.UUID, permission string, grantedBy *string) error
- func (r *Repository) DeleteUserPermissions(ctx context.Context, userID uuid.UUID) error
- func (r *Repository) GetUserPermissions(ctx context.Context, userID uuid.UUID) ([]string, error)
- func (r *Repository) GetUsersWithPermission(ctx context.Context, permission string) ([]uuid.UUID, error)
- func (r *Repository) HasPermission(ctx context.Context, userID uuid.UUID, permission string) (bool, error)
- func (r *Repository) RemovePermission(ctx context.Context, userID uuid.UUID, permission string) error
- func (r *Repository) SetUserPermissions(ctx context.Context, userID uuid.UUID, permissions []string, grantedBy *string) error
- type Service
- func (s *Service) AddPermission(ctx context.Context, userID uuid.UUID, permission string, grantedBy *string) error
- func (s *Service) DeleteUserPermissions(ctx context.Context, userID uuid.UUID) error
- func (s *Service) GetAvailablePermissions() []PermissionInfo
- func (s *Service) GetDefaultPermissionsForRole(role string) []string
- func (s *Service) GetEffectivePermissions(ctx context.Context, userID uuid.UUID) ([]string, error)
- func (s *Service) HasPermission(ctx context.Context, userID uuid.UUID, permission string) (bool, error)
- func (s *Service) InitializeUserPermissions(ctx context.Context, userID uuid.UUID, role string, grantedBy *string) error
- func (s *Service) RemovePermission(ctx context.Context, userID uuid.UUID, permission string) error
- func (s *Service) SetUserPermissions(ctx context.Context, userID uuid.UUID, permissions []string, grantedBy *string) error
- type SetPermissionsRequest
- type UserPermission
Constants ¶
const ( // PageChat allows access to the chat page PageChat = "page.chat" // PageHome allows access to the home/dashboard page PageHome = "page.home" // PageChannels allows access to the channels page PageChannels = "page.channels" // PageSettings allows access to the settings page PageSettings = "page.settings" // PageSecurity allows access to the security page PageSecurity = "page.security" // PageUsers allows access to the user management page PageUsers = "page.users" // PageProfile allows access to the profile page PageProfile = "page.profile" // PageProviders allows access to the auth providers page PageProviders = "page.providers" // PageAutomation allows access to the automation page PageAutomation = "page.automation" // PagePlugins allows access to the plugins page PagePlugins = "page.plugins" // PageTools allows access to the tool store page PageTools = "page.tools" // PageSkills allows access to the skill store page PageSkills = "page.skills" )
Page permission constants
Variables ¶
var ( // ErrPermissionNotFound is returned when a permission is not found ErrPermissionNotFound = errors.New("permission not found") )
Functions ¶
func AllPagePermissions ¶
func AllPagePermissions() []string
AllPagePermissions returns all available page permissions
func DefaultAdminPermissions ¶
func DefaultAdminPermissions() []string
DefaultAdminPermissions returns the default permissions for admin role (all)
func DefaultGuestPermissions ¶
func DefaultGuestPermissions() []string
DefaultGuestPermissions returns the default permissions for guest role
func DefaultUserPermissions ¶
func DefaultUserPermissions() []string
DefaultUserPermissions returns the default permissions for user role
func RequirePagePermission ¶
func RequirePagePermission(service *Service, pagePermission string) echo.MiddlewareFunc
RequirePagePermission ensures the authenticated user has the requested page permission. Preview mode keeps full access so the onboarding/admin flow continues to work.
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles HTTP requests for permission management
func NewHandler ¶
func NewHandler(service *Service, userRepo user.Repository) *Handler
NewHandler creates a new permission handler
func (*Handler) GetAvailablePermissions ¶
GetAvailablePermissions returns all available permissions
func (*Handler) GetMyPermissions ¶
GetMyPermissions returns the current user's permissions
func (*Handler) GetUserPermissions ¶
GetUserPermissions returns a specific user's permissions (admin only)
func (*Handler) RegisterAdminRoutes ¶
RegisterAdminRoutes registers admin-focused permission management endpoints.
func (*Handler) RegisterCurrentUserRoutes ¶
RegisterCurrentUserRoutes registers endpoints needed during authenticated frontend bootstrap. Keeping these separate lets the server expose them before heavier route registration finishes.
func (*Handler) RegisterRoutes ¶
RegisterRoutes registers the permission routes
type PermissionInfo ¶
type PermissionInfo struct {
Key string `json:"key"`
Name string `json:"name"`
Description string `json:"description"`
Category string `json:"category"`
}
PermissionInfo provides information about a permission
func GetPermissionInfo ¶
func GetPermissionInfo() []PermissionInfo
GetPermissionInfo returns information about all available permissions
type PermissionsResponse ¶
type PermissionsResponse struct {
UserID uuid.UUID `json:"user_id"`
Role string `json:"role"`
Permissions []string `json:"permissions"`
}
PermissionsResponse represents a response containing user permissions
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository handles permission persistence
func NewRepository ¶
func NewRepository(db *sql.DB) (*Repository, error)
NewRepository creates a new permission repository
func NewRepositoryWithReadDB ¶
func NewRepositoryWithReadDB(writeDB, readDB *sql.DB) (*Repository, error)
NewRepositoryWithReadDB creates a new permission repository with separate write and read database handles.
func (*Repository) AddPermission ¶
func (r *Repository) AddPermission(ctx context.Context, userID uuid.UUID, permission string, grantedBy *string) error
AddPermission adds a single permission to a user
func (*Repository) DeleteUserPermissions ¶
DeleteUserPermissions removes all permissions for a user
func (*Repository) GetUserPermissions ¶
GetUserPermissions returns all permissions for a user
func (*Repository) GetUsersWithPermission ¶
func (r *Repository) GetUsersWithPermission(ctx context.Context, permission string) ([]uuid.UUID, error)
GetUsersWithPermission returns all user IDs that have a specific permission
func (*Repository) HasPermission ¶
func (r *Repository) HasPermission(ctx context.Context, userID uuid.UUID, permission string) (bool, error)
HasPermission checks if a user has a specific permission
func (*Repository) RemovePermission ¶
func (r *Repository) RemovePermission(ctx context.Context, userID uuid.UUID, permission string) error
RemovePermission removes a single permission from a user
func (*Repository) SetUserPermissions ¶
func (r *Repository) SetUserPermissions(ctx context.Context, userID uuid.UUID, permissions []string, grantedBy *string) error
SetUserPermissions replaces all permissions for a user
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles permission business logic
func NewService ¶
func NewService(repo *Repository, userRepo user.Repository) *Service
NewService creates a new permission service
func (*Service) AddPermission ¶
func (s *Service) AddPermission(ctx context.Context, userID uuid.UUID, permission string, grantedBy *string) error
AddPermission adds a single permission to a user
func (*Service) DeleteUserPermissions ¶
DeleteUserPermissions removes all permissions for a user (used when deleting user)
func (*Service) GetAvailablePermissions ¶
func (s *Service) GetAvailablePermissions() []PermissionInfo
GetAvailablePermissions returns all available permissions with info
func (*Service) GetDefaultPermissionsForRole ¶
GetDefaultPermissionsForRole returns the default permissions for a role
func (*Service) GetEffectivePermissions ¶
GetEffectivePermissions returns all effective permissions for a user This combines role-based defaults with custom permissions
func (*Service) HasPermission ¶
func (s *Service) HasPermission(ctx context.Context, userID uuid.UUID, permission string) (bool, error)
HasPermission checks if a user has a specific permission
func (*Service) InitializeUserPermissions ¶
func (s *Service) InitializeUserPermissions(ctx context.Context, userID uuid.UUID, role string, grantedBy *string) error
InitializeUserPermissions sets default permissions for a new user based on role
func (*Service) RemovePermission ¶
RemovePermission removes a single permission from a user
type SetPermissionsRequest ¶
type SetPermissionsRequest struct {
Permissions []string `json:"permissions" validate:"required"`
}
SetPermissionsRequest represents a request to set user permissions
type UserPermission ¶
type UserPermission struct {
ID uuid.UUID `json:"id" db:"id"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
Permission string `json:"permission" db:"permission"`
GrantedBy *string `json:"granted_by,omitempty" db:"granted_by"`
GrantedAt time.Time `json:"granted_at" db:"granted_at"`
}
UserPermission represents a permission assigned to a user
func NewUserPermission ¶
func NewUserPermission(userID uuid.UUID, permission string, grantedBy *string) *UserPermission
NewUserPermission creates a new user permission