A
A
Anton Artyomov2017-03-13 15:04:28
Yii
Anton Artyomov, 2017-03-13 15:04:28

How can the data property of the yii\rbac\Item class be used?

The documentation says that there can be any mixed data attached to the current item.
This is clear. But how can this be applied in practice? I couldn't find any examples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-03-13
@ArtyomovAnton

I did not use this miracle property, but you interested me, got into the kernel and dug up the following:
There is an addItem method in yii\rbac\DbManager

/**
     * @inheritdoc
     */
    protected function addItem($item)
    {
        $time = time();
        if ($item->createdAt === null) {
            $item->createdAt = $time;
        }
        if ($item->updatedAt === null) {
            $item->updatedAt = $time;
        }
        $this->db->createCommand()
            ->insert($this->itemTable, [
                'name' => $item->name,
                'type' => $item->type,
                'description' => $item->description,
                'rule_name' => $item->ruleName,
                'data' => $item->data === null ? null : serialize($item->data),
                'created_at' => $item->createdAt,
                'updated_at' => $item->updatedAt,
            ])->execute();

        $this->invalidateCache();

        return true;
    }

This means that when creating any item, and this can be a role or a rule, you can attach arbitrary data to it. For example:
$role = Yii::$app->authManager->createRole('admin');
$role->description = 'Администратор';
$role->data = ['roleColor'=>'#fff', 'razmer_sisek'=>5];
Yii::$app->authManager->add($role);

What would you use later, for example, to change the background or more useful things. Haven't tried it yet, but should work. Something like this:
<?php
$role = Yii::$app->authManager->get('admin');
$bgcolor = $role->data['roleColor'];
?>
<body style="background-color:<?=$bgcolor?>;">

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question