service

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package service implements SFBAC permission resolution, administration and bootstrap synchronization on top of the store layer.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound   = store.ErrNotFound
	ErrConflict   = store.ErrConflict
	ErrValidation = errors.New("validation error")
)

Sentinel errors mapped to HTTP status codes by the API layer. ErrNotFound and ErrConflict alias the store's sentinels so errors.Is works across layers.

Functions

func AssignRole

func AssignRole(shared store.Shared, tenant store.Tenant, sub, roleName string) error

AssignRole assigns a role to a user (idempotent) and lazily provisions the user in the shared store.

func Authorize

func Authorize(tenant store.Tenant, sub, operation string) (bool, error)

Authorize reports whether the user holds the operation with FULL scope. Sforza's administrative APIs operate on whole collections, so meta permissions are only honored at FULL scope; RESTRICTED and EMPTY deny.

func BootstrapMeta

func BootstrapMeta(stores *store.Stores, adminSub string) error

BootstrapMeta registers Sforza's own meta resources and operations in the shared database, then ensures the administrator role exists in every tenant with FULL scope on every meta operation and assigns it to adminSub.

func CreateOperation

func CreateOperation(shared store.Shared, name string) error

CreateOperation registers an operation named "resource:action", creating the parent resource when missing.

func CreateResource

func CreateResource(shared store.Shared, name string) error

CreateResource registers a resource; creating an existing one is an error.

func CreateRole

func CreateRole(tenant store.Tenant, name string) error

CreateRole creates a role in the tenant.

func DeleteOperation

func DeleteOperation(shared store.Shared, name string) error

DeleteOperation removes an operation.

func DeleteResource

func DeleteResource(shared store.Shared, name string) error

DeleteResource removes a resource together with its operations.

func DeleteRole

func DeleteRole(tenant store.Tenant, name string) error

DeleteRole removes a role with all its permissions, restricted IDs and user assignments.

func EffectiveOperations

func EffectiveOperations(tenant store.Tenant, sub string) ([]model.OperationScope, error)

EffectiveOperations returns every operation available to the user with its effective scope, sorted by operation name. Restricted IDs are never included here.

func EnsureOperation

func EnsureOperation(shared store.Shared, name string) error

EnsureOperation registers an operation if missing (idempotent variant used by bootstrap synchronization).

func EnsureRole

func EnsureRole(tenant store.Tenant, name string) error

EnsureRole creates a role if missing.

func EnsureUser

func EnsureUser(shared store.Shared, sub string) error

EnsureUser lazily provisions a user in the shared store.

func GetRole

func GetRole(tenant store.Tenant, name string) (*store.Role, error)

GetRole returns a role by name.

func ListOperations

func ListOperations(shared store.Shared) ([]store.Operation, error)

ListOperations returns all registered operations.

func ListResources

func ListResources(shared store.Shared) ([]store.Resource, error)

ListResources returns all registered resources.

func ListRoles

func ListRoles(tenant store.Tenant) ([]store.Role, error)

ListRoles returns all roles in the tenant.

func ListUsers

func ListUsers(shared store.Shared) ([]store.User, error)

ListUsers returns all provisioned users.

func MetaOperations

func MetaOperations(tenant store.Tenant, sub string) ([]model.OperationScope, error)

MetaOperations returns the subset of effective operations that belong to Sforza's own meta authorization model.

func RecordIDs

func RecordIDs(tenant store.Tenant, sub string, operations []string) (map[string][]string, error)

RecordIDs returns, for each requested operation whose effective scope is RESTRICTED, the sorted set of accessible record IDs. Operations with FULL, EMPTY or no assignment are omitted from the result.

func RemoveRolePermission

func RemoveRolePermission(tenant store.Tenant, roleName, operation string) error

RemoveRolePermission deletes (operation, scope) from a role together with the operation's restricted IDs.

func RemoveUserPermission

func RemoveUserPermission(tenant store.Tenant, sub, operation string) error

RemoveUserPermission deletes a user-level override together with the operation's restricted IDs.

func RenameRole

func RenameRole(tenant store.Tenant, name, newName string) error

RenameRole changes a role's name.

func RolePermissions

func RolePermissions(tenant store.Tenant, name string) ([]model.OperationScope, error)

RolePermissions returns the (operation, scope) pairs assigned to a role.

func SetRolePermission

func SetRolePermission(shared store.Shared, tenant store.Tenant, roleName, operation string, scope model.Scope) error

SetRolePermission upserts (operation, scope) on a role. The operation must be registered in the shared store.

func SetUserPermission

func SetUserPermission(shared store.Shared, tenant store.Tenant, sub, operation string, scope model.Scope) error

SetUserPermission upserts a user-level (operation, scope) override and lazily provisions the user.

func Sync

func Sync(stores *store.Stores, files []BootstrapFile) error

Sync applies bootstrap files to the databases. It is additive and idempotent: declared entities are created or updated to match, while entities not mentioned are left untouched (other services may own them).

func UnassignRole

func UnassignRole(tenant store.Tenant, sub, roleName string) error

UnassignRole removes a role from a user.

func UpdateRoleRestrictedIDs

func UpdateRoleRestrictedIDs(tenant store.Tenant, roleName, operation string, add, remove []string) error

UpdateRoleRestrictedIDs adds and removes record IDs for a role's permission on an operation. The permission must already be assigned.

func UpdateUserRestrictedIDs

func UpdateUserRestrictedIDs(tenant store.Tenant, sub, operation string, add, remove []string) error

UpdateUserRestrictedIDs adds and removes record IDs for a user's permission on an operation. The permission must already be assigned.

func UserRoles

func UserRoles(tenant store.Tenant, sub string) ([]string, error)

UserRoles returns the names of the roles assigned to a user.

Types

type BootstrapFile

type BootstrapFile struct {
	Resources  []string                   `yaml:"resources"`
	Operations []string                   `yaml:"operations"`
	Tenants    map[string]TenantBootstrap `yaml:"tenants"`
}

BootstrapFile is the YAML document a microservice contributes to register its resources, operations and tenant configuration.

resources:
  - product
operations:
  - product:read
  - product:write
tenants:
  tenant-a:
    roles:
      manager:
        product:read: FULL
        product:write:
          scope: RESTRICTED
          ids: [10, 15]
    users:
      john:
        roles: [manager]
        permissions:
          product:read: FULL

func LoadBootstrapFiles

func LoadBootstrapFiles(patterns []string) ([]BootstrapFile, error)

LoadBootstrapFiles reads every file matched by the given glob patterns and parses it as a BootstrapFile.

type PermissionSpec

type PermissionSpec struct {
	Scope model.Scope
	IDs   []string
}

PermissionSpec is either a bare scope ("FULL") or a mapping with a scope and optional restricted IDs.

func (*PermissionSpec) UnmarshalYAML

func (p *PermissionSpec) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML accepts both the scalar and the mapping form.

type TenantBootstrap

type TenantBootstrap struct {
	Roles map[string]map[string]PermissionSpec `yaml:"roles"`
	Users map[string]UserBootstrap             `yaml:"users"`
}

TenantBootstrap declares roles and users for one tenant.

type UserBootstrap

type UserBootstrap struct {
	Roles       []string                  `yaml:"roles"`
	Permissions map[string]PermissionSpec `yaml:"permissions"`
}

UserBootstrap declares role assignments and permission overrides for a user.

Jump to

Keyboard shortcuts

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