You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							137 lines
						
					
					
						
							3.9 KiB
						
					
					
				
			
		
		
	
	
							137 lines
						
					
					
						
							3.9 KiB
						
					
					
				<?php | 
						|
/** | 
						|
 * Created by Error202 | 
						|
 * Date: 15.08.2017 | 
						|
 */ | 
						|
 | 
						|
namespace backend\controllers; | 
						|
 | 
						|
use backend\forms\rbac\RbacCreatePermissionForm; | 
						|
use backend\forms\rbac\RbacEditPermissionForm; | 
						|
use core\services\PermissionManager; | 
						|
use yii\behaviors\TimestampBehavior; | 
						|
use yii\data\ArrayDataProvider; | 
						|
use yii\rbac\Permission; | 
						|
use yii\web\Controller; | 
						|
use yii\filters\VerbFilter; | 
						|
use Yii; | 
						|
use yii\filters\AccessControl; | 
						|
 | 
						|
class PermissionController extends Controller | 
						|
{ | 
						|
    private $permission; | 
						|
 | 
						|
    public function __construct($id, $module, PermissionManager $permission, $config = []) | 
						|
    { | 
						|
        parent::__construct($id, $module, $config); | 
						|
        $this->permission = $permission; | 
						|
    } | 
						|
 | 
						|
    /** | 
						|
     * @inheritdoc | 
						|
     */ | 
						|
    public function behaviors() | 
						|
    { | 
						|
        return [ | 
						|
            [ | 
						|
                'class' => TimestampBehavior::className(), | 
						|
            ], | 
						|
            'access' => [ | 
						|
                'class' => AccessControl::className(), | 
						|
                'rules' => [ | 
						|
                    [ | 
						|
                        'actions' => ['create','view','index', 'update', 'delete'], | 
						|
                        'allow' => true, | 
						|
                        'roles' => ['UserManagement'], | 
						|
                    ], | 
						|
                    [    // all the action are accessible to admin | 
						|
                        'allow' => true, | 
						|
                        'roles' => ['admin'], | 
						|
                    ], | 
						|
                ], | 
						|
            ], | 
						|
            'verbs' => [ | 
						|
                'class' => VerbFilter::className(), | 
						|
                'actions' => [ | 
						|
                    'delete' => ['POST'], | 
						|
                ], | 
						|
            ], | 
						|
        ]; | 
						|
    } | 
						|
 | 
						|
    public function actionIndex() | 
						|
    { | 
						|
        $data = array_map(function (Permission $permission){ | 
						|
            return [ | 
						|
                'name' => $permission->name, | 
						|
                'description' => $permission->description, | 
						|
            ]; | 
						|
        }, $this->permission->getPermissions()); | 
						|
 | 
						|
        $dataProvider = new ArrayDataProvider([ | 
						|
            'allModels' => $data, | 
						|
            'pagination' => [ | 
						|
                'pageSize' => 20, | 
						|
            ], | 
						|
            'sort' => [ | 
						|
                'attributes' => ['name', 'description'], | 
						|
            ], | 
						|
        ]); | 
						|
 | 
						|
        return $this->render('index', ['dataProvider' => $dataProvider]); | 
						|
    } | 
						|
 | 
						|
    public function actionCreate() | 
						|
    { | 
						|
        $form = new RbacCreatePermissionForm(); | 
						|
        if ($form->load(Yii::$app->request->post()) && $form->validate()) { | 
						|
            try { | 
						|
                $this->permission->create($form->name, $form->description, $form->rule_name, $form->data); | 
						|
                return $this->redirect(['view', 'id' => $form->name]); | 
						|
            } catch (\DomainException $e) { | 
						|
                Yii::$app->errorHandler->logException($e); | 
						|
                Yii::$app->session->setFlash('error', $e->getMessage()); | 
						|
            } | 
						|
        } | 
						|
        return $this->render('create', [ | 
						|
            'model' => $form, | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
    public function actionUpdate($id) | 
						|
    { | 
						|
        $permission = $this->findModel($id); | 
						|
 | 
						|
        $form = new RbacEditPermissionForm($permission); | 
						|
        if ($form->load(Yii::$app->request->post()) && $form->validate()) { | 
						|
            try { | 
						|
                $this->permission->update($permission->name, $form->name, $form->description, $form->rule_name, $form->data); | 
						|
                return $this->redirect(['view', 'id' => $form->name]); | 
						|
            } catch (\DomainException $e) { | 
						|
                Yii::$app->errorHandler->logException($e); | 
						|
                Yii::$app->session->setFlash('error', $e->getMessage()); | 
						|
            } | 
						|
        } | 
						|
        return $this->render('update', [ | 
						|
            'model' => $form, | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
    public function actionDelete($id) | 
						|
    { | 
						|
        $this->permission->delete($id); | 
						|
        return $this->redirect(['index']); | 
						|
    } | 
						|
 | 
						|
    public function actionView($id) | 
						|
    { | 
						|
        return $this->render('view', [ | 
						|
            'model' => $this->findModel($id), | 
						|
        ]); | 
						|
    } | 
						|
 | 
						|
    protected function findModel($id) | 
						|
    { | 
						|
        return $this->permission->getPermission($id); | 
						|
    } | 
						|
} |