registry

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package registry contains interfaces and default implementations for the custom role registry. Default structs compose go-repository-bun repositories but can be replaced by the host application via dependency injection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRoleToDefinition

func CustomRoleToDefinition(record *CustomRole) *types.RoleDefinition

CustomRoleToDefinition exposes the conversion logic for consumers that need to translate Bun models into domain role definitions.

Types

type CustomRole

type CustomRole struct {
	bun.BaseModel `bun:"table:custom_roles"`

	ID          uuid.UUID      `bun:",pk,type:uuid"`
	Name        string         `bun:"name,notnull"`
	Description string         `bun:"description"`
	RoleKey     string         `bun:"role_key"`
	Permissions []string       `bun:"permissions,type:jsonb"`
	Metadata    map[string]any `bun:"metadata,type:jsonb"`
	IsSystem    bool           `bun:"is_system,notnull"`
	TenantID    uuid.UUID      `bun:"tenant_id,type:uuid,notnull,default:'00000000-0000-0000-0000-000000000000'"`
	OrgID       uuid.UUID      `bun:"org_id,type:uuid,notnull,default:'00000000-0000-0000-0000-000000000000'"`
	CreatedAt   time.Time      `bun:"created_at,notnull"`
	UpdatedAt   time.Time      `bun:"updated_at,notnull"`
	CreatedBy   uuid.UUID      `bun:"created_by,type:uuid,notnull"`
	UpdatedBy   uuid.UUID      `bun:"updated_by,type:uuid,notnull"`
}

CustomRole represents the schema stored in custom_roles.

func DefinitionToCustomRole

func DefinitionToCustomRole(definition *types.RoleDefinition) *CustomRole

DefinitionToCustomRole converts a domain role definition into the Bun model.

type RoleAssignment

type RoleAssignment struct {
	bun.BaseModel `bun:"table:user_custom_roles"`

	UserID     uuid.UUID `bun:"user_id,type:uuid,pk"`
	RoleID     uuid.UUID `bun:"role_id,type:uuid,pk"`
	TenantID   uuid.UUID `bun:"tenant_id,type:uuid,pk"`
	OrgID      uuid.UUID `bun:"org_id,type:uuid,pk"`
	AssignedAt time.Time `bun:"assigned_at,notnull"`
	AssignedBy uuid.UUID `bun:"assigned_by,type:uuid,notnull"`
}

RoleAssignment represents rows from user_custom_roles.

type RoleRegistry

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

RoleRegistry persists custom roles and assignments using Bun repositories.

func NewRoleRegistry

func NewRoleRegistry(cfg RoleRegistryConfig) (*RoleRegistry, error)

NewRoleRegistry constructs the default registry. Either DB or both repositories must be provided; when DB is supplied the repositories are created automatically.

func (*RoleRegistry) AssignRole

func (r *RoleRegistry) AssignRole(ctx context.Context, userID, roleID uuid.UUID, scope types.ScopeFilter, actor uuid.UUID) error

AssignRole creates a user->role assignment scoped to tenant/org.

func (*RoleRegistry) CreateRole

func (r *RoleRegistry) CreateRole(ctx context.Context, input types.RoleMutation) (*types.RoleDefinition, error)

CreateRole inserts a custom role scoped to the provided tenant/org.

func (*RoleRegistry) DeleteRole

func (r *RoleRegistry) DeleteRole(ctx context.Context, id uuid.UUID, scope types.ScopeFilter, actor uuid.UUID) error

DeleteRole removes a custom role (unless marked as system).

func (*RoleRegistry) GetRole

GetRole returns a single role matching the scope constraints.

func (*RoleRegistry) ListAssignments

func (r *RoleRegistry) ListAssignments(ctx context.Context, filter types.RoleAssignmentFilter) ([]types.RoleAssignment, error)

ListAssignments returns assignments filtered by scope/user/role.

func (*RoleRegistry) ListRoles

func (r *RoleRegistry) ListRoles(ctx context.Context, filter types.RoleFilter) (types.RolePage, error)

ListRoles returns paginated roles filtered by scope/keyword.

func (*RoleRegistry) UnassignRole

func (r *RoleRegistry) UnassignRole(ctx context.Context, userID, roleID uuid.UUID, scope types.ScopeFilter, actor uuid.UUID) error

UnassignRole removes an existing user->role assignment.

func (*RoleRegistry) UpdateRole

func (r *RoleRegistry) UpdateRole(ctx context.Context, id uuid.UUID, input types.RoleMutation) (*types.RoleDefinition, error)

UpdateRole updates mutable fields on a custom role.

type RoleRegistryConfig

type RoleRegistryConfig struct {
	DB          *bun.DB
	Roles       repository.Repository[*CustomRole]
	Assignments repository.Repository[*RoleAssignment]
	Clock       types.Clock
	Hooks       types.Hooks
	Logger      types.Logger
	IDGenerator types.IDGenerator
}

RoleRegistryConfig configures the Bun-backed role registry.

Jump to

Keyboard shortcuts

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