People, inside advanced tempalate you do not use "app" in your namespace, that is for basic template. In advanced it goes like this: for frontend part is starts with "frontend", for example "frontend/models", "frontend/controllers". For backend app it starts with "backend", in common folder it starts with "common", in console it starts with "console", you do not use "app".
Soon I will release improved advanced template with more features, and rbac installed out of box with very simple and nice ways of using it in your code/app. Untill then, please read this guide: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html , and if you do not understand something there, tell me.
In short:
Inside your config file (common/config/main.php) as a part of components array, you put this line of code:
Then you use yii migration that will install rbac tables to your database ( you invoke it from your console ):
yii migrate @yii/rbac/migrations/
After that you init your RbacController that will populate these rbac tables in your database with roles, permissions and all (you create roles and permissions in your RbacController, that is what will be inserted to your rbac tables).
And last step is to use this in your code like :
if (\Yii::$app->user->can('createPost')) {
// create post
}
For example if you put this line of code inside post/create action, only users that have this createPost permission will be able to create posts.
I never used PhpManager, but from what I see you haven’t configured it well. Read the Configuring RBAC Manager section of this guide very carefully and make sure that you have configured everything right. Your RbacController either do not know about PhpManager or where to store authorization data. Read the Tip: part of this guide section very carefully.
Yes I have read it already. I already have configured the authManager in /config/web.php. And also, have already manually created the three files based on the Tips part as well. I created a rbac folder in my app directory, and those three files have write permission to ‘Others’ already.
Tip: By default, yii\rbac\PhpManager stores RBAC data in three files: @app/rbac/items.php, @app/rbac/assignments.php and @app/rbac/rules.php. Make sure these files are writable by the Web server process if the authorization needs to be changed online. Sometimes you will need to create these files manually.
Under my /config/web.php, my ‘authManager’ component is as below:
'authManager' => [
'class' => 'yii\rbac\PhpManager',
'itemFile' => '@app/rbac/items.php', //Default path to items.php | NEW CONFIGURATIONS
'assignmentFile' => '@app/rbac/assignments.php', //Default path to assignments.php | NEW CONFIGURATIONS
'ruleFile' => '@app/rbac/rules.php', //Default path to rules.php | NEW CONFIGURATIONS
],
. I am using the basic template. Do you know the reason? I am using PhpManager
My RbacController is
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);
// add "updatePost" permission
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);
// Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($author, 2);
$auth->assign($admin, 1);
}
}