<?php
namespace console\controllers;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
    public function actionInit()
    {
        $auth = Yii::$app->authManager;

////////////////////////////////////////////////////////////////////////////

        $org = $auth->createRole('org');
        $auth->add($org);

        $adviser = $auth->createRole('adviser');
        $auth->add($adviser);

        $osa = $auth->createRole('osa');
        $auth->add($osa);
        $auth->addChild($osa, $adviser);
        $auth->addChild($osa, $org);

        $admin = $auth->createRole('admin');
        $auth->add($admin);

///////////////////////////////////////////////////////////////////////////

        $index_organizations = $auth->createPermission('indexOrganizations');
        $index_organizations->description = 'view the list of organizations';
        $auth->add($index_organizations);

        $create_organization = $auth->createPermission('createOrganization');
        $create_organization->description = 'creates an organization';
        $auth->add($create_organization);

        $delete_organization = $auth->createPermission('deleteOrganization');
        $delete_organization->description = 'deletes an organization';
        $auth->add($delete_organization);

        $view_organization = $auth->createPermission('viewOrganization');
        $view_organization->description = 'views the details of an organization';
        $auth->add($view_organization);

        $view_own_organization = $auth->createPermission('viewOwnOrganization');
        $view_own_organization->description = 'views the details of own organization';
        $auth->add($view_own_organization);


        $auth->addChild($org, $view_own_organization);

        $auth->addChild($osa, $index_organizations);
        $auth->addChild($osa, $create_organization);
        $auth->addChild($osa, $delete_organization);
        $auth->addChild($osa, $view_organization);

////////////////////////////////////////////////////////////////////////////

        $auth->assign($org, 4);
        $auth->assign($adviser, 3);
        $auth->assign($osa, 2);
        $auth->assign($admin, 1);
    }
}