role

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package role provides domain entities for role-based access control. Roles define what actions users can perform (permissions). Users can have multiple roles, and permissions are the union of all roles.

Index

Constants

This section is empty.

Variables

View Source
var (
	OwnerRoleID  = MustParseID("00000000-0000-0000-0000-000000000001")
	AdminRoleID  = MustParseID("00000000-0000-0000-0000-000000000002")
	MemberRoleID = MustParseID("00000000-0000-0000-0000-000000000003")
	ViewerRoleID = MustParseID("00000000-0000-0000-0000-000000000004")
)

System role IDs (fixed UUIDs for system roles).

View Source
var (
	ErrRoleNotFound           = errors.New("role not found")
	ErrCannotModifySystemRole = errors.New("cannot modify system role")
	ErrCannotDeleteSystemRole = errors.New("cannot delete system role")
	ErrRoleSlugExists         = errors.New("role with this slug already exists")
	ErrRoleInUse              = errors.New("role is assigned to users and cannot be deleted")
	ErrInvalidPermission      = errors.New("invalid permission")
	ErrUserRoleNotFound       = errors.New("user role not found")
	ErrUserRoleExists         = errors.New("user already has this role")
)

Errors

Functions

This section is empty.

Types

type ID

type ID uuid.UUID

ID represents a unique role identifier.

func MustParseID

func MustParseID(s string) ID

MustParseID parses a string to a role ID, panics on error.

func NewID

func NewID() ID

NewID generates a new random role ID.

func ParseID

func ParseID(s string) (ID, error)

ParseID parses a string to a role ID.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero checks if the ID is empty/zero.

func (ID) String

func (id ID) String() string

String returns the string representation of the ID.

type Module

type Module struct {
	ID           string
	Name         string
	Description  string
	Icon         string
	DisplayOrder int
	IsActive     bool
	Permissions  []*Permission
}

Module represents a feature grouping for permissions.

type Permission

type Permission struct {
	ID          string // e.g., "assets:read"
	ModuleID    string
	Name        string
	Description string
	IsActive    bool
}

Permission represents a granular permission.

type PermissionRepository

type PermissionRepository interface {
	// ListModulesWithPermissions returns all modules with their permissions.
	ListModulesWithPermissions(ctx context.Context) ([]*Module, error)

	// ListPermissions returns all permissions.
	ListPermissions(ctx context.Context) ([]*Permission, error)

	// GetByID retrieves a permission by its ID.
	GetByID(ctx context.Context, id string) (*Permission, error)

	// Exists checks if a permission exists.
	Exists(ctx context.Context, id string) (bool, error)

	// ValidatePermissions validates multiple permissions.
	// Returns (valid, invalidIDs, error).
	ValidatePermissions(ctx context.Context, ids []string) (bool, []string, error)
}

PermissionRepository defines the interface for permission persistence operations.

type Repository

type Repository interface {

	// Create creates a new role.
	Create(ctx context.Context, role *Role) error

	// GetByID retrieves a role by its ID.
	GetByID(ctx context.Context, id ID) (*Role, error)

	// GetBySlug retrieves a role by slug within a tenant or system.
	// For system roles, tenantID should be nil.
	GetBySlug(ctx context.Context, tenantID *ID, slug string) (*Role, error)

	// ListForTenant returns all roles available for a tenant.
	// Includes both system roles and tenant's custom roles.
	ListForTenant(ctx context.Context, tenantID ID) ([]*Role, error)

	// ListSystemRoles returns only system roles.
	ListSystemRoles(ctx context.Context) ([]*Role, error)

	// Update updates a role (only custom roles can be updated).
	Update(ctx context.Context, role *Role) error

	// Delete deletes a role (only custom roles can be deleted).
	Delete(ctx context.Context, id ID) error

	// GetUserRoles returns all roles for a user in a tenant.
	GetUserRoles(ctx context.Context, tenantID, userID ID) ([]*Role, error)

	// GetUserPermissions returns all permissions for a user (UNION of all roles).
	GetUserPermissions(ctx context.Context, tenantID, userID ID) ([]string, error)

	// HasFullDataAccess checks if user has full data access (any role with has_full_data_access=true).
	HasFullDataAccess(ctx context.Context, tenantID, userID ID) (bool, error)

	// AssignRole assigns a role to a user (adds to user's roles).
	AssignRole(ctx context.Context, tenantID, userID, roleID ID, assignedBy *ID) error

	// RemoveRole removes a role from a user.
	RemoveRole(ctx context.Context, tenantID, userID, roleID ID) error

	// SetUserRoles replaces all roles for a user.
	SetUserRoles(ctx context.Context, tenantID, userID ID, roleIDs []ID, assignedBy *ID) error

	// BulkAssignRoleToUsers assigns a role to multiple users at once.
	BulkAssignRoleToUsers(ctx context.Context, tenantID, roleID ID, userIDs []ID, assignedBy *ID) error

	// ListRoleMembers returns all users who have a specific role in a tenant.
	ListRoleMembers(ctx context.Context, tenantID, roleID ID) ([]*UserRole, error)

	// CountUsersWithRole returns the count of users with a specific role.
	CountUsersWithRole(ctx context.Context, roleID ID) (int, error)
}

Repository defines the interface for role persistence operations.

type Role

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

Role represents a role entity that defines a set of permissions.

func New

func New(
	tenantID ID,
	slug string,
	name string,
	description string,
	hierarchyLevel int,
	hasFullDataAccess bool,
	permissions []string,
	createdBy ID,
) *Role

New creates a new custom role for a tenant.

func Reconstruct

func Reconstruct(
	id ID,
	tenantID *ID,
	slug string,
	name string,
	description string,
	isSystem bool,
	hierarchyLevel int,
	hasFullDataAccess bool,
	permissions []string,
	createdAt time.Time,
	updatedAt time.Time,
	createdBy *ID,
) *Role

Reconstruct creates a role from persistence data.

func (*Role) AddPermission

func (r *Role) AddPermission(permission string) error

AddPermission adds a permission to the role.

func (*Role) CreatedAt

func (r *Role) CreatedAt() time.Time

CreatedAt returns when the role was created.

func (*Role) CreatedBy

func (r *Role) CreatedBy() *ID

CreatedBy returns who created the role.

func (*Role) Description

func (r *Role) Description() string

Description returns the role description.

func (*Role) HasFullDataAccess

func (r *Role) HasFullDataAccess() bool

HasFullDataAccess returns true if users with this role can see all data.

func (*Role) HasPermission

func (r *Role) HasPermission(permission string) bool

HasPermission checks if the role has a specific permission.

func (*Role) HierarchyLevel

func (r *Role) HierarchyLevel() int

HierarchyLevel returns the hierarchy level.

func (*Role) ID

func (r *Role) ID() ID

ID returns the role ID.

func (*Role) IsCustom

func (r *Role) IsCustom() bool

IsCustom returns true if this is a tenant-created custom role.

func (*Role) IsSystem

func (r *Role) IsSystem() bool

IsSystem returns true if this is a system role (immutable).

func (*Role) Name

func (r *Role) Name() string

Name returns the role name.

func (*Role) PermissionCount

func (r *Role) PermissionCount() int

PermissionCount returns the number of permissions.

func (*Role) Permissions

func (r *Role) Permissions() []string

Permissions returns the list of permission IDs.

func (*Role) RemovePermission

func (r *Role) RemovePermission(permission string) error

RemovePermission removes a permission from the role.

func (*Role) SetPermissions

func (r *Role) SetPermissions(permissions []string) error

SetPermissions replaces the role's permissions.

func (*Role) Slug

func (r *Role) Slug() string

Slug returns the role slug.

func (*Role) TenantID

func (r *Role) TenantID() *ID

TenantID returns the tenant ID (nil for system roles).

func (*Role) Update

func (r *Role) Update(name, description string, hierarchyLevel int, hasFullDataAccess bool) error

Update updates the role's basic info.

func (*Role) UpdatedAt

func (r *Role) UpdatedAt() time.Time

UpdatedAt returns when the role was last updated.

type UserRole

type UserRole struct {
	ID         ID
	UserID     ID
	TenantID   ID
	RoleID     ID
	Role       *Role // Populated when fetching with role details
	AssignedAt time.Time
	AssignedBy *ID

	// User details (populated from JOIN when fetching members)
	UserName      string
	UserEmail     string
	UserAvatarURL string
}

UserRole represents a role assigned to a user.

func NewUserRole

func NewUserRole(userID, tenantID, roleID ID, assignedBy *ID) *UserRole

NewUserRole creates a new user role assignment.

Jump to

Keyboard shortcuts

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