0 follower

Final Class Yiisoft\Rbac\Db\ItemTreeTraversal\SqliteCteItemTreeTraversal

InheritanceYiisoft\Rbac\Db\ItemTreeTraversal\SqliteCteItemTreeTraversal » Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal
ImplementsYiisoft\Rbac\Db\ItemTreeTraversal\ItemTreeTraversalInterface

An RBAC item tree traversal strategy based on CTE (common table expression) for SQLite. Should be used only with versions >= 3.8.3 (lower versions don't support this functionality).

Method Details

Hide inherited methods

__construct() public method
public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $database, string $tableName, string $childrenTableName, string $namesSeparator )
$database \Yiisoft\Db\Connection\ConnectionInterface

Yii Database connection instance.

$tableName string

A name of the table for storing RBAC items.

$childrenTableName string

A name of the table for storing relations between RBAC items.

$namesSeparator string

Separator used for joining item names.

                public function __construct(
    protected ConnectionInterface $database,
    protected string $tableName,
    protected string $childrenTableName,
    protected string $namesSeparator,
) {
}

            
getChildPermissionRows() public method
public array getChildPermissionRows ( string|array $names )
$names string|array

                public function getChildPermissionRows(string|array $names): array
{
    $baseOuterQuery = $this->getChildrenBaseOuterQuery($names)->andWhere(['item.type' => Item::TYPE_PERMISSION]);
    /** @psalm-var RawItem[] */
    return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

            
getChildRoleRows() public method
public array getChildRoleRows ( string|array $names )
$names string|array

                public function getChildRoleRows(string|array $names): array
{
    $baseOuterQuery = $this->getChildrenBaseOuterQuery($names)->andWhere(['item.type' => Item::TYPE_ROLE]);
    /** @psalm-var RawItem[] */
    return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

            
getChildrenRows() public method
public array getChildrenRows ( string|array $names )
$names string|array

                public function getChildrenRows(string|array $names): array
{
    $baseOuterQuery = $this->getChildrenBaseOuterQuery($names);
    /** @psalm-var RawItem[] */
    return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}

            
getEmptyChildrenExpression() protected method
protected string getEmptyChildrenExpression ( )

                protected function getEmptyChildrenExpression(): string
{
    return "''";
}

            
getHierarchy() public method
public array getHierarchy ( string $name )
$name string

                public function getHierarchy(string $name): array
{
    $baseOuterQuery = (new Query($this->database))->select(['item.*', 'parent_of.children']);
    $cteSelectRelationQuery = (new Query($this->database))
        ->select(['parent', new Expression($this->getTrimConcatChildrenExpression())])
        ->from("$this->childrenTableName AS item_child_recursive")
        ->innerJoin('parent_of', [
            'item_child_recursive.child' => new Expression('{{parent_of}}.[[child_name]]'),
        ]);
    /** @infection-ignore-all FalseValue, union */
    $cteSelectItemQuery = (new Query($this->database))
        ->select(['name', new Expression($this->getEmptyChildrenExpression())])
        ->from($this->tableName)
        ->where(['name' => $name])
        ->union($cteSelectRelationQuery, all: true);
    /** @infection-ignore-all FalseValue, recursive */
    $outerQuery = $baseOuterQuery
        ->withQuery($cteSelectItemQuery, 'parent_of(child_name, children)', recursive: true)
        ->from('parent_of')
        ->leftJoin(
            ['item' => $this->tableName],
            ['item.name' => new Expression('{{parent_of}}.[[child_name]]')],
        );
    /** @psalm-var Hierarchy */
    return $outerQuery->all();
}

            
getParentRows() public method
public array getParentRows ( string $name )
$name string

                public function getParentRows(string $name): array
{
    $baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=', 'item.name', $name]);
    /** @psalm-var RawItem[] */
    return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}

            
getTrimConcatChildrenExpression() protected method

protected string getTrimConcatChildrenExpression ( )

                protected function getTrimConcatChildrenExpression(): string
{
    return "TRIM(children || '$this->namesSeparator' || item_child_recursive.child, '$this->namesSeparator')";
}

            
hasChild() public method
public boolean hasChild ( string $parentName, string $childName )
$parentName string
$childName string

                public function hasChild(string $parentName, string $childName): bool
{
    /**
     * @infection-ignore-all
     * - ArrayItemRemoval, select.
     */
    $baseOuterQuery = (new Query($this->database))
        ->select([new Expression('1 AS item_child_exists')])
        ->andWhere(['item.name' => $childName]);
    /** @psalm-var array<0, 1>|false $result */
    $result = $this
        ->getRowsCommand($parentName, baseOuterQuery: $baseOuterQuery, areParents: false)
        ->queryScalar();
    return $result !== false;
}