0 follower

Final Class Yiisoft\Rbac\Php\AssignmentsStorage

InheritanceYiisoft\Rbac\Php\AssignmentsStorage » Yiisoft\Rbac\SimpleAssignmentsStorage
ImplementsYiisoft\Rbac\Php\FileStorageInterface
Uses TraitsYiisoft\Rbac\Php\FileStorageTrait

Storage stores roles and permissions in PHP file specified in AssignmentsStorage::$assignmentFile.

It is suitable for authorization data that is not too big (for example, the authorization data for a personal blog system).

Method Details

Hide inherited methods

__construct() public method

public __construct( string $filePath, callable|null $getFileUpdatedAt null ): mixed
$filePath string
$getFileUpdatedAt callable|null

                public function __construct(string $filePath, ?callable $getFileUpdatedAt = null)
{
    $this->initFileProperties($filePath, $getFileUpdatedAt);
    $this->load();
}

            
add() public method

public add( \Yiisoft\Rbac\Assignment $assignment ): void
$assignment \Yiisoft\Rbac\Assignment

                public function add(Assignment $assignment): void
{
    parent::add($assignment);
    $this->save();
}

            
clear() public method

public clear( ): void

                public function clear(): void
{
    parent::clear();
    $this->save();
}

            
getFileUpdatedAt() public method
public getFileUpdatedAt( ): integer

                public function getFileUpdatedAt(): int
{
    $getFileUpdatedAt = $this->getFileUpdatedAt;
    $fileUpdatedAt = $getFileUpdatedAt($this->filePath);
    if (!is_int($fileUpdatedAt)) {
        throw new RuntimeException('getFileUpdatedAt callable must return a UNIX timestamp.');
    }
    return $fileUpdatedAt;
}

            
load() public method

public load( ): void

                public function load(): void
{
    parent::clear();
    /** @psalm-var list<RawAssignment> $assignments */
    $assignments = $this->loadFromFile($this->filePath);
    if (empty($assignments)) {
        return;
    }
    $fileUpdatedAt = $this->getFileUpdatedAt();
    foreach ($assignments as $assignment) {
        /** @psalm-suppress InvalidPropertyAssignmentValue */
        $this->assignments[$assignment['user_id']][$assignment['item_name']] = new Assignment(
            userId: $assignment['user_id'],
            itemName: $assignment['item_name'],
            createdAt: $assignment['created_at'] ?? $fileUpdatedAt,
        );
    }
}

            
remove() public method

public remove( string $itemName, string $userId ): void
$itemName string
$userId string

                public function remove(string $itemName, string $userId): void
{
    if (!$this->exists($itemName, $userId)) {
        return;
    }
    parent::remove($itemName, $userId);
    $this->save();
}

            
removeByItemName() public method

public removeByItemName( string $itemName ): void
$itemName string

                public function removeByItemName(string $itemName): void
{
    parent::removeByItemName($itemName);
    $this->save();
}

            
removeByUserId() public method

public removeByUserId( string $userId ): void
$userId string

                public function removeByUserId(string $userId): void
{
    parent::removeByUserId($userId);
    $this->save();
}

            
renameItem() public method

public renameItem( string $oldName, string $newName ): void
$oldName string
$newName string

                public function renameItem(string $oldName, string $newName): void
{
    parent::renameItem($oldName, $newName);
    $this->save();
}