0 follower

Final Class Yiisoft\Rbac\Db\AssignmentsStorage

InheritanceYiisoft\Rbac\Db\AssignmentsStorage
ImplementsYiisoft\Rbac\AssignmentsStorageInterface

Warning: Do not use directly! Use with Manager from {@link https://github.com/yiisoft/rbac} package.

Storage for RBAC assignments in the form of database table. Operations are performed using Yii Database.

Psalm Types

Name Value
RawAssignment array{item_name: string, user_id: string, created_at: integer|string}

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $database, string $tableName 'yii_rbac_assignment' )
$database \Yiisoft\Db\Connection\ConnectionInterface

Yii Database connection instance.

$tableName string

A name of the table for storing RBAC assignments.

                public function __construct(
    private readonly ConnectionInterface $database,
    private readonly string $tableName = 'yii_rbac_assignment',
) {
}

            
add() public method

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

                public function add(Assignment $assignment): void
{
    $this
        ->database
        ->createCommand()
        ->insert(
            $this->tableName,
            [
                'item_name' => $assignment->getItemName(),
                'user_id' => $assignment->getUserId(),
                'created_at' => $assignment->getCreatedAt(),
            ],
        )
        ->execute();
}

            
clear() public method

public void clear ( )

                public function clear(): void
{
    $this
        ->database
        ->createCommand()
        ->delete($this->tableName)
        ->execute();
}

            
exists() public method

public boolean exists ( string $itemName, string $userId )
$itemName string
$userId string

                public function exists(string $itemName, string $userId): bool
{
    return (new Query($this->database))
        ->from($this->tableName)
        ->where(['item_name' => $itemName, 'user_id' => $userId])
        ->exists();
}

            
filterUserItemNames() public method

public array filterUserItemNames ( string $userId, array $itemNames )
$userId string
$itemNames array

                public function filterUserItemNames(string $userId, array $itemNames): array
{
    /** @var string[] */
    return (new Query($this->database))
        ->select('item_name')
        ->from($this->tableName)
        ->where(['user_id' => $userId, 'item_name' => $itemNames])
        ->column();
}

            
get() public method

public \Yiisoft\Rbac\Assignment|null get ( string $itemName, string $userId )
$itemName string
$userId string

                public function get(string $itemName, string $userId): ?Assignment
{
    /**
     * @psalm-var RawAssignment|null $row
     * @infection-ignore-all
     * - ArrayItemRemoval, select.
     */
    $row = (new Query($this->database))
        ->select(['created_at'])
        ->from($this->tableName)
        ->where(['item_name' => $itemName, 'user_id' => $userId])
        ->one();
    return $row === null ? null : new Assignment($userId, $itemName, (int) $row['created_at']);
}

            
getAll() public method

public array getAll ( )

                public function getAll(): array
{
    /** @psalm-var RawAssignment[] $rows */
    $rows = (new Query($this->database))
        ->from($this->tableName)
        ->all();
    $assignments = [];
    foreach ($rows as $row) {
        $assignments[$row['user_id']][$row['item_name']] = new Assignment(
            $row['user_id'],
            $row['item_name'],
            (int) $row['created_at'],
        );
    }
    return $assignments;
}

            
getByItemNames() public method

public array getByItemNames ( array $itemNames )
$itemNames array

                public function getByItemNames(array $itemNames): array
{
    if (empty($itemNames)) {
        /** @infection-ignore-all  */
        return [];
    }
    /** @psalm-var RawAssignment[] $rawAssignments */
    $rawAssignments = (new Query($this->database))
        ->from($this->tableName)
        ->where(['item_name' => $itemNames])
        ->all();
    $assignments = [];
    foreach ($rawAssignments as $rawAssignment) {
        $assignments[] = new Assignment(
            $rawAssignment['user_id'],
            $rawAssignment['item_name'],
            (int) $rawAssignment['created_at'],
        );
    }
    return $assignments;
}

            
getByUserId() public method

public array getByUserId ( string $userId )
$userId string

                public function getByUserId(string $userId): array
{
    /** @psalm-var list<array{item_name: string, created_at: int|string}> $rawAssignments */
    $rawAssignments = (new Query($this->database))
        ->select(['item_name', 'created_at'])
        ->from($this->tableName)
        ->where(['user_id' => $userId])
        ->all();
    $assignments = [];
    foreach ($rawAssignments as $rawAssignment) {
        $assignments[$rawAssignment['item_name']] = new Assignment(
            $userId,
            $rawAssignment['item_name'],
            (int) $rawAssignment['created_at'],
        );
    }
    return $assignments;
}

            
hasItem() public method

public boolean hasItem ( string $name )
$name string

                public function hasItem(string $name): bool
{
    return (new Query($this->database))
        ->from($this->tableName)
        ->where(['item_name' => $name])
        ->exists();
}

            
remove() public method

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

                public function remove(string $itemName, string $userId): void
{
    $this
        ->database
        ->createCommand()
        ->delete($this->tableName, ['item_name' => $itemName, 'user_id' => $userId])
        ->execute();
}

            
removeByItemName() public method

public void removeByItemName ( string $itemName )
$itemName string

                public function removeByItemName(string $itemName): void
{
    $this
        ->database
        ->createCommand()
        ->delete($this->tableName, ['item_name' => $itemName])
        ->execute();
}

            
removeByUserId() public method

public void removeByUserId ( string $userId )
$userId string

                public function removeByUserId(string $userId): void
{
    $this
        ->database
        ->createCommand()
        ->delete($this->tableName, ['user_id' => $userId])
        ->execute();
}

            
renameItem() public method

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

                public function renameItem(string $oldName, string $newName): void
{
    $this
        ->database
        ->createCommand()
        ->update($this->tableName, columns: ['item_name' => $newName], condition: ['item_name' => $oldName])
        ->execute();
}

            
userHasItem() public method

public boolean userHasItem ( string $userId, array $itemNames )
$userId string
$itemNames array

                public function userHasItem(string $userId, array $itemNames): bool
{
    if (empty($itemNames)) {
        /** @infection-ignore-all  */
        return false;
    }
    return (new Query($this->database))
        ->from($this->tableName)
        ->where(['user_id' => $userId, 'item_name' => $itemNames])
        ->exists();
}