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 {@see 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 mixed __construct ( string $filePath, callable|null $getFileUpdatedAt null )
$filePath string
$getFileUpdatedAt callable|null

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

            
add() public method

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

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

            
clear() public method

public void clear ( )

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

            
getFileUpdatedAt() public method
public integer getFileUpdatedAt ( )

                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 void load ( )

                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 void remove ( string $itemName, string $userId )
$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 void removeByItemName ( string $itemName )
$itemName string

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

            
removeByUserId() public method

public void removeByUserId ( string $userId )
$userId string

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

            
renameItem() public method

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

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