yii2 มีระบบจัดการสิทธิต่างๆ เรียกว่า Role Based Access Control (RBAC) โดยสามารถกำหนดสิทธิได้โดยใช้ กลุ่ม (role) และกำหนดสิทธิให้รายคนได้ เพื่อที่จะควบคุมให้แต่ละคนสามารถเข้าไปใช้เมนูต่างๆ ได้ตามความเหมาะสม เราคงไม่ต้องการให้ใครก็ได้ แต่มาเปลี่ยนข้อมูล ตั้งค่าของระบบใหม่ หรือลบงานของคนอื่นออกไปโดยไม่มีเหตุอันควร เริ่มจากการกำหนด role โดย
- เพิ่ม component authManager ในไฟล์ common/config/main.php[code language=”php” title=”main.php”]
‘components’ => [
…
‘authManager’ => [
‘class’ => ‘yii\rbac\DbManager’,
],
…
],
[/code] - สร้างตาราง auth ก่อนโดย command[code language=”text” title=”terminal”]
yii migrate –[email protected]/rbac/migrations/
[/code]
ตอบ yes รอจนเห็น Migrated up successfully. จะเป็นการสร้างตาราง auth_assignment, auth_item, auth_item_child และ auth_rule
ถ้าเห็น Exception ‘yii\base\InvalidConfigException’ with message ‘The configuration for the “user” component must contain a “class” element.’ ให้ไป comment config ทีระบุ user components ออกไปก่อน แลัวลองใหม่ - สร้างไฟล์ใหม่ /backend/controllers/RbacController.php[code language=”php” title=”RbacController.php”]
<?php
namespace backend\controllers;use Yii;
use yii\helpers\Html;
use yii\web\Controller;/**
* Site controller
*/
class RbacController extends Controller
{private $auth = [];
public function init()
{
parent::init();$this->auth = Yii::$app->authManager;
}public function actionIndex()
{
return ‘<h1>RBAC Snippets</h1>’ .
‘<ul>’ .
‘<li>’ . html::a(‘Create Role’, ‘createparentsrole’, [‘target’ => ‘t1’]) . ‘</li>’ .
‘<li>’ . html::a(‘Set Child Role’, ‘childrole’, [‘target’ => ‘t2’]) . ‘</li>’ .
‘<li>’ . html::a(‘Assign To User’, ‘assignrole’, [‘target’ => ‘t3’]) . ‘</li>’ .
‘</ul>’;
}public function actionAssignrole()
{
$administratorsRole = $this->auth->getRole(‘administrators’);
if (is_null($this->auth->getAssignment(‘administrators’, 1))) {
$this->auth->assign($administratorsRole, 1);
}$staffsRole = $this->auth->getRole(‘staffs’);
if (is_null($this->auth->getAssignment(‘staffs’, 1))) {
$this->auth->assign($staffsRole, 1);
}/*
show all roles from table auth_assignment
*/
return ‘<h1>Assignments of user id = 1</h1>’ .
‘<pre>’ . print_r($this->auth->getAssignments(1), true) . ‘</pre>’;
}public function actionChildrole()
{
$administratorsRole = $this->auth->getRole(‘administrators’);
$guestsRole = $this->auth->getRole(‘guests’);if (is_null($this->auth->hasChild($administratorsRole, $guestsRole))) {
$this->auth->addChild($administratorsRole, $guestsRole);
}/*
show all roles from table auth_item_child
*/
return ‘<h1>Children of "administrators" Role</h1>’ .
‘<pre>’ . print_r($this->auth->getChildren(‘administrators’), true) . ‘</pre>’;
}public function actionCreateparentsrole()
{
$roles = [
‘administrators’,
‘guests’,
‘staffs’,
‘users’,
];foreach ($roles as $role) {
if (is_null($this->auth->getRole($role))) {
$newRole = $this->auth->createRole($role);$newRole->description = ‘parent role of ‘ . $role;
$this->auth->add($newRole);
}
}/*
show all roles from table auth_item (type = 1)
*/
return ‘<h1>Lists Of All Roles</h1>’ .
‘<pre>’ . print_r($this->auth->getRoles(), true) . ‘</pre>’;}
}
[/code]ลองเรียก link http://localhost/advanced/backend/web/rbac และดูความเปลี่ยนแปลงตามลำดับ
ดูเพิ่มเติม
About the author