permission

package
v0.0.0-...-8acab51 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package permission provides page-level permission management.

Index

Constants

View Source
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

View Source
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

func (h *Handler) GetAvailablePermissions(c echo.Context) error

GetAvailablePermissions returns all available permissions

func (*Handler) GetMyPermissions

func (h *Handler) GetMyPermissions(c echo.Context) error

GetMyPermissions returns the current user's permissions

func (*Handler) GetUserPermissions

func (h *Handler) GetUserPermissions(c echo.Context) error

GetUserPermissions returns a specific user's permissions (admin only)

func (*Handler) RegisterAdminRoutes

func (h *Handler) RegisterAdminRoutes(g *echo.Group)

RegisterAdminRoutes registers admin-focused permission management endpoints.

func (*Handler) RegisterCurrentUserRoutes

func (h *Handler) RegisterCurrentUserRoutes(g *echo.Group)

RegisterCurrentUserRoutes registers endpoints needed during authenticated frontend bootstrap. Keeping these separate lets the server expose them before heavier route registration finishes.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(g *echo.Group)

RegisterRoutes registers the permission routes

func (*Handler) SetUserPermissions

func (h *Handler) SetUserPermissions(c echo.Context) error

SetUserPermissions sets a user's permissions (admin only)

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

func (r *Repository) DeleteUserPermissions(ctx context.Context, userID uuid.UUID) error

DeleteUserPermissions removes all permissions for a user

func (*Repository) GetUserPermissions

func (r *Repository) GetUserPermissions(ctx context.Context, userID uuid.UUID) ([]string, error)

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

func (s *Service) DeleteUserPermissions(ctx context.Context, userID uuid.UUID) error

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

func (s *Service) GetDefaultPermissionsForRole(role string) []string

GetDefaultPermissionsForRole returns the default permissions for a role

func (*Service) GetEffectivePermissions

func (s *Service) GetEffectivePermissions(ctx context.Context, userID uuid.UUID) ([]string, error)

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

func (s *Service) RemovePermission(ctx context.Context, userID uuid.UUID, permission string) error

RemovePermission removes a single permission from a user

func (*Service) SetUserPermissions

func (s *Service) SetUserPermissions(ctx context.Context, userID uuid.UUID, permissions []string, grantedBy *string) error

SetUserPermissions sets custom permissions for 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

Jump to

Keyboard shortcuts

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