Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace consolecontrollers;
- use commonmodelsUser;
- use Yii;
- use yiiconsoleController;
- class RbacController extends Controller
- {
- public function actionInit()
- {
- $auth = Yii::$app->authManager;
- //CREATE CONTENT MANAGER ROLE
- $contentManagerRole = Yii::$app->authManager->createRole('content_manager');
- $contentManagerRole->description = 'Content manager';
- //CREATE ADMIN ROLE
- $adminRole = Yii::$app->authManager->createRole('admin');
- $adminRole->description = 'Administrator';
- $auth->add($adminRole);
- //COMPLAIN PERMISSIONS
- $indexComplain = $auth->createPermission('complain-index');
- $viewComplain = $auth->createPermission('complain-view');
- $createComplain = $auth->createPermission('complain-create');
- $updateComplain = $auth->createPermission('complain-update');
- $deleteComplain = $auth->createPermission('complain-delete');
- //ANNOUNCEMENT PERMISSIONS
- $indexAnnouncement = $auth->createPermission('announcement-index');
- $viewAnnouncement = $auth->createPermission('announcement-view');
- $createAnnouncement = $auth->createPermission('announcement-create');
- $updateAnnouncement = $auth->createPermission('announcement-update');
- $deleteAnnouncement = $auth->createPermission('announcement-delete');
- $auth->add($indexComplain);
- $auth->add($viewComplain);
- $auth->add($createComplain);
- $auth->add($updateComplain);
- $auth->add($deleteComplain);
- $auth->add($indexAnnouncement);
- $auth->add($viewAnnouncement);
- $auth->add($createAnnouncement);
- $auth->add($updateAnnouncement);
- $auth->add($deleteAnnouncement);
- $auth->add($contentManagerRole);
- //ADDING PERMISSIONS IN ROLE CONTENT MANAGER
- $auth->addChild($contentManagerRole, $indexComplain);
- $auth->addChild($contentManagerRole, $viewComplain);
- $auth->addChild($contentManagerRole, $createComplain);
- $auth->addChild($contentManagerRole, $updateComplain);
- $auth->addChild($contentManagerRole, $deleteComplain);
- $auth->addChild($contentManagerRole, $indexAnnouncement);
- $auth->addChild($contentManagerRole, $viewAnnouncement);
- $auth->addChild($contentManagerRole, $createAnnouncement);
- $auth->addChild($contentManagerRole, $updateAnnouncement);
- $auth->addChild($contentManagerRole, $deleteAnnouncement);
- $auth->addChild($adminRole, $contentManagerRole);
- $auth->assign($contentManagerRole, User::getContentManagerUser()->id);
- $auth->assign($adminRole, User::getAdminUser()->id);
- }
- }
- 'as beforeRequest' => [
- 'class' => 'yiifiltersAccessControl',
- 'rules' => [
- //COMMON
- [
- 'actions' => ['logout', 'index'],
- 'allow' => true,
- 'roles' => ['admin', 'content_manager'],
- ],
- //ADMIN
- [
- 'allow' => true,
- 'roles' => ['admin'],
- ],
- //CONTENT MANAGER
- [
- 'allow' => true,
- 'roles' => ['content_manager'],
- ],
- [
- 'actions' => ['login'],
- 'allow' => true,
- 'roles' => ['?']
- ],
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'actions' => ['index'],
- 'roles' => ['complain-index'],
- ],
- [
- 'allow' => true,
- 'actions' => ['view'],
- 'roles' => ['complain-view'],
- ],
- [
- 'allow' => true,
- 'actions' => ['create'],
- 'roles' => ['complain-create'],
- ],
- [
- 'allow' => true,
- 'actions' => ['update'],
- 'roles' => ['complain-update'],
- ],
- [
- 'allow' => true,
- 'actions' => ['delete'],
- 'roles' => ['complain-delete'],
- ],
- ],
- ],
Add Comment
Please, Sign In to add comment