API Services ModelService - evansims/openfga-php GitHub Wiki
Service implementation for managing OpenFGA authorization models. Provides business-focused operations for working with authorization models, including validation, convenience methods, and enhanced error handling. This service abstracts the underlying repository implementation and adds value through additional functionality.
Table of Contents
OpenFGA\Services
- ModelServiceInterface (interface)
public function cloneModel(string $modelId): OpenFGA\Results\FailureInterface|OpenFGA\Results\SuccessInterface
Clone an authorization model to another store. Copies a model from one store to another, useful for multi-tenant scenarios where you want to replicate a permission structure. The cloned model gets a new ID in the target store.
Name | Type | Description |
---|---|---|
$modelId |
string |
The ID of the model to clone |
FailureInterface
| SuccessInterface
— Success with the cloned model, or Failure with error details
public function createModel(
OpenFGA\Models\Collections\TypeDefinitionsInterface $typeDefinitions,
?OpenFGA\Models\Collections\ConditionsInterface $conditions = NULL,
OpenFGA\Models\Enums\SchemaVersion $schemaVersion = OpenFGA\Models\Enums\SchemaVersion::V1_1,
): OpenFGA\Results\FailureInterface|OpenFGA\Results\SuccessInterface
Create a new authorization model with validation. Creates an immutable authorization model from the provided type definitions and optional conditions. The model is validated before creation to ensure it conforms to OpenFGA's schema requirements.
Name | Type | Description |
---|---|---|
$typeDefinitions |
TypeDefinitionsInterface |
The type definitions for the model |
$conditions |
ConditionsInterface | null
|
Optional conditions for attribute-based access control |
$schemaVersion |
SchemaVersion |
The OpenFGA schema version to use |
FailureInterface
| SuccessInterface
— Success with the created model, or Failure with validation/creation errors
public function findModel(string $modelId): OpenFGA\Results\FailureInterface|OpenFGA\Results\SuccessInterface
Find a specific authorization model by ID. Retrieves a model with enhanced error handling, providing clear messages when models are not found or other errors occur.
Name | Type | Description |
---|---|---|
$modelId |
string |
The unique identifier of the model |
FailureInterface
| SuccessInterface
— Success with the model, or Failure with detailed error information
public function getLatestModel(OpenFGA\Models\StoreInterface|string $store): FailureInterface|Success
Get the most recent authorization model for a store. Retrieves the latest model version, which is typically the active model being used for authorization decisions. This is a convenience method that avoids needing to list all models and manually find the newest one.
Name | Type | Description |
---|---|---|
$store |
StoreInterface | string
|
The store to get the latest model from |
FailureInterface
| Success
— Success with the latest model, or Failure if no models exist
public function listAllModels(
?string $continuationToken = NULL,
?int $pageSize = NULL,
): OpenFGA\Results\FailureInterface|OpenFGA\Results\SuccessInterface
List all authorization models for a store. Retrieves all models with automatic pagination handling. This method aggregates results across multiple pages up to the specified limit.
Name | Type | Description |
---|---|---|
$continuationToken |
string | null
|
Pagination token from a previous response |
$pageSize |
int | null
|
Maximum number of models to retrieve (null for server default) |
FailureInterface
| SuccessInterface
— Success with the models collection, or Failure with error details
public function validateModel(
OpenFGA\Models\Collections\TypeDefinitionsInterface $typeDefinitions,
OpenFGA\Models\Enums\SchemaVersion $schemaVersion = OpenFGA\Models\Enums\SchemaVersion::V1_1,
): OpenFGA\Results\FailureInterface|OpenFGA\Results\SuccessInterface
Validate type definitions before creating a model. Performs validation on type definitions to catch errors before attempting to create a model. This is useful for providing immediate feedback in user interfaces or validation pipelines.
Name | Type | Description |
---|---|---|
$typeDefinitions |
TypeDefinitionsInterface |
The type definitions to validate |
$schemaVersion |
SchemaVersion |
The schema version to validate against |
FailureInterface
| SuccessInterface
— Success if valid, or Failure with validation errors