0 follower

Final Class Yiisoft\Rbac\Db\ItemTreeTraversal\MysqlItemTreeTraversal

InheritanceYiisoft\Rbac\Db\ItemTreeTraversal\MysqlItemTreeTraversal
ImplementsYiisoft\Rbac\Db\ItemTreeTraversal\ItemTreeTraversalInterface

An RBAC item tree traversal strategy based on specific functionality for MySQL 5, without support for CTE (Common Table Expressions).

Property Details

Hide inherited properties

$childrenTableName protected property
protected string $childrenTableName null
$database protected property
protected \Yiisoft\Db\Connection\ConnectionInterface $database null
$namesSeparator protected property
protected string $namesSeparator null
$tableName protected property
protected string $tableName null

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()->where(['item.type' => Item::TYPE_PERMISSION]);
    /** @psalm-var RawItem[] */
    return $this->getChildrenRowsCommand($names, baseOuterQuery: $baseOuterQuery)->queryAll();
}

            
getChildRoleRows() public method

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

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

            
getChildrenRows() public method

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

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

            
getHierarchy() public method

public array getHierarchy ( string $name )
$name string

                public function getHierarchy(string $name): array
{
    $sql = "SELECT item.*, hierarchy_base.children FROM (
        SELECT
            child_name,
            MIN(TRIM(BOTH '$this->namesSeparator'
        FROM TRIM(BOTH child_name FROM raw_children))) as children FROM (
            SELECT @r AS child_name, @path := concat(@path, '$this->namesSeparator', @r) as raw_children,
            (SELECT @r := parent FROM $this->childrenTableName WHERE child = child_name LIMIT 1) AS parent
            FROM (SELECT @r := :name, @path := '') val, $this->childrenTableName
        ) raw_hierarchy_base
        GROUP BY child_name
    ) hierarchy_base
    LEFT JOIN $this->tableName AS item ON item.name = hierarchy_base.child_name
    WHERE item.name IS NOT NULL";
    /** @psalm-var Hierarchy */
    return $this
        ->database
        ->createCommand($sql, [':name' => $name])
        ->queryAll();
}

            
getParentRows() public method

public array getParentRows ( string $name )
$name string

                public function getParentRows(string $name): array
{
    $sql = "SELECT DISTINCT item.* FROM (
        SELECT @r AS child_name,
        (SELECT @r := parent FROM $this->childrenTableName WHERE child = child_name LIMIT 1) AS parent
        FROM (SELECT @r := :name) val, $this->childrenTableName
    ) s
    LEFT JOIN $this->tableName AS item ON item.name = s.child_name
    WHERE item.name != :name";
    /** @psalm-var RawItem[] */
    return $this
        ->database
        ->createCommand($sql, [':name' => $name])
        ->queryAll();
}

            
hasChild() public method

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

                public function hasChild(string $parentName, string $childName): bool
{
    $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->getChildrenRowsCommand($parentName, baseOuterQuery: $baseOuterQuery)->queryScalar();
    return $result !== false;
}