Commit 91087dd5 by Edward Simarmata

rbac yang belum selesai

parent 1584aac4
<?php
namespace backend\controllers;
use Yii;
use common\modules\auth\models\AuthItem;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* RbacController implements the CRUD actions for AuthItem model.
*/
class RbacController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
// Create user assignment
public function actionAssigment(){
$auth = Yii::$app->authManager;
$sekpentk = $auth->createRole('sekpenTK');
$sekpensd = $auth->createRole('sekpenSD');
$sekpensmp = $auth->createRole('sekpenSMP');
$auth->assign($sekpentk, 2);
$auth->assign($sekpensd, 3);
$auth->assign($sekpensmp, 4);
}
// Create role
public function actionCreate_role(){
$auth = Yii::$app->authManager;
$index = $auth->createPermission('sekolah/index');
$create = $auth->createPermission('sekolah/create');
$view = $auth->createPermission('sekolah/view');
$update = $auth->createPermission('sekolah/update');
$delete = $auth->createPermission('sekolah/delete');
// add "admin" role give this role the index create view update delete
$sekpentk = $auth->createRole('sekpenTK');
$auth->add($sekpentk);
$auth->addChild($sekpentk, $index);
$auth->addChild($sekpentk, $create);
$auth->addChild($sekpentk, $view);
$auth->addChild($sekpentk, $update);
$auth->addChild($sekpentk, $delete);
$sekpensd = $auth->createRole('sekpenSD');
$auth->add($sekpensd);
$auth->addChild($sekpensd, $index);
$auth->addChild($sekpensd, $create);
$auth->addChild($sekpensd, $view);
$auth->addChild($sekpensd, $update);
$auth->addChild($sekpensd, $delete);
$sekpensmp = $auth->createRole('sekpenSMP');
$auth->add($sekpensmp);
$auth->addChild($sekpensmp, $index);
$auth->addChild($sekpensmp, $create);
$auth->addChild($sekpensmp, $view);
$auth->addChild($sekpensmp, $update);
$auth->addChild($sekpensmp, $delete);
}
public function actionCreate_permission()
{
$auth = Yii::$app->authManager;
$index = $auth->createPermission('sekolah/index');
$index->description = ' Create a sekolah';
$auth->add($index);
$create = $auth->createPermission('sekolah/create');
$create->description = ' create a sekolah';
$auth->add($create);
$view = $auth->createPermission('sekolah/view');
$view->description = ' View a sekolah';
$auth->add($view);
$update = $auth->createPermission('sekolah/update');
$update->description = ' Update a sekolah';
$auth->add($update);
$delete = $auth->createPermission('sekolah/delete');
$delete->description = ' Delete a sekolah';
$auth->add($delete);
}
/**
* Lists all AuthItem models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => AuthItem::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single AuthItem model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new AuthItem model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new AuthItem();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->name]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing AuthItem model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->name]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing AuthItem model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the AuthItem model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return AuthItem the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = AuthItem::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
......@@ -8,6 +8,7 @@ use app\models\SekolahSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
/**
* SekolahController implements the CRUD actions for Sekolah model.
......@@ -19,14 +20,27 @@ class SekolahController extends Controller
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
$behaviors['access'] = [
'class' => AccessControl::className(),
'rules' =>[
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function($rule, $action){
$action = Yii::$app->controller->action->id;
$controller = Yii::$app->controller->id;
$route = "$controller/$action";
$post = Yii::$app->request->post();
if (\Yii::$app->user->can($route)){
return true;
}
}
],
],
];
return $behaviors;
}
/**
......
......@@ -9,7 +9,7 @@ use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use yii\filters\AccessControl;
/**
* UsersController implements the CRUD actions for Users model.
*/
......@@ -20,14 +20,27 @@ class UsersController extends Controller
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
$behaviors['access'] = [
'class' => AccessControl::className(),
'rules' =>[
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function($rule, $action){
$action = Yii::$app->controller->action->id;
$controller = Yii::$app->controller->id;
$route = "$controller/$action";
$post = Yii::$app->request->post();
if (\Yii::$app->user->can($route)){
return true;
}
}
],
],
];
return $behaviors;
}
/**
......@@ -64,7 +77,6 @@ class UsersController extends Controller
*/
public function actionCreate()
{
if(Yii::$app->user->can('sekpenTK')){
$model = new Users();
if ($model->load(Yii::$app->request->post()) && $model->save() ) {
......@@ -77,12 +89,8 @@ class UsersController extends Controller
]);
}
}
else
{
throw new ForbiddenHttpException;
}
}
/**
* Updates an existing Users model.
......
......@@ -37,7 +37,7 @@ class Sekolah extends \yii\db\ActiveRecord
public function rules()
{
return [
[['nama_sekolah', 'alamat_sekolah', 'foto_sekolah', 'visi', 'misi', 'created_at', 'updated_at', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['nama_sekolah', 'alamat_sekolah','jenjang' ,'foto_sekolah', 'visi', 'misi', 'created_at', 'updated_at', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['created_at', 'updated_at', 'deleted_at'], 'safe'],
[['deleted'], 'integer'],
[['nama_sekolah', 'alamat_sekolah', 'foto_sekolah', 'visi', 'misi'], 'string', 'max' => 100],
......@@ -54,6 +54,7 @@ class Sekolah extends \yii\db\ActiveRecord
'id' => 'ID',
'nama_sekolah' => 'Nama Sekolah',
'alamat_sekolah' => 'Alamat Sekolah',
'jenjang' => 'Jenjang',
'foto_sekolah' => 'Foto Sekolah',
'visi' => 'Visi',
'misi' => 'Misi',
......
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\modules\auth\models\AuthItem */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="auth-item-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'type')->textInput() ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'rule_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'data')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'updated_at')->textInput() ?>
<?= $form->field($model, 'deleted_at')->textInput() ?>
<?= $form->field($model, 'deleted')->textInput() ?>
<?= $form->field($model, 'created_by')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'updated_by')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'deleted_by')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\modules\auth\models\AuthItem */
$this->title = 'Create Auth Item';
$this->params['breadcrumbs'][] = ['label' => 'Auth Items', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="auth-item-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Auth Items';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="auth-item-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create Auth Item', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'type',
'description:ntext',
'rule_name',
'data:ntext',
// 'created_at',
// 'updated_at',
// 'deleted_at',
// 'deleted',
// 'created_by',
// 'updated_by',
// 'deleted_by',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\modules\auth\models\AuthItem */
$this->title = 'Update Auth Item: ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Auth Items', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->name]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="auth-item-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\modules\auth\models\AuthItem */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Auth Items', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="auth-item-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->name], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->name], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'name',
'type',
'description:ntext',
'rule_name',
'data:ntext',
'created_at',
'updated_at',
'deleted_at',
'deleted',
'created_by',
'updated_by',
'deleted_by',
],
]) ?>
</div>
<?php
namespace common\modules\auth;
/**
* auth module definition class
*/
class Module extends \yii\base\Module
{
/**
* @inheritdoc
*/
public $controllerNamespace = 'common\modules\auth\controllers';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}
<?php
namespace common\modules\auth\controllers;
use yii\web\Controller;
/**
* Default controller for the `auth` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}
<?php
namespace common\modules\auth\models;
use Yii;
/**
* This is the model class for table "auth_assignment".
*
* @property string $item_name
* @property string $user_id
* @property integer $created_at
* @property string $updated_at
* @property string $deleted_at
* @property integer $deleted
* @property string $created_by
* @property string $updated_by
* @property string $deleted_by
*
* @property AuthItem $itemName
*/
class AuthAssignment extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'auth_assignment';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['item_name', 'user_id', 'updated_at', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['created_at', 'deleted'], 'integer'],
[['updated_at', 'deleted_at'], 'safe'],
[['item_name', 'user_id'], 'string', 'max' => 64],
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 32],
[['item_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['item_name' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'item_name' => 'Item Name',
'user_id' => 'User ID',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'deleted_at' => 'Deleted At',
'deleted' => 'Deleted',
'created_by' => 'Created By',
'updated_by' => 'Updated By',
'deleted_by' => 'Deleted By',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getItemName()
{
return $this->hasOne(AuthItem::className(), ['name' => 'item_name']);
}
}
<?php
namespace common\modules\auth\models;
use Yii;
/**
* This is the model class for table "auth_item".
*
* @property string $name
* @property integer $type
* @property string $description
* @property string $rule_name
* @property string $data
* @property integer $created_at
* @property integer $updated_at
* @property string $deleted_at
* @property integer $deleted
* @property string $created_by
* @property string $updated_by
* @property string $deleted_by
*
* @property AuthAssignment[] $authAssignments
* @property AuthRule $ruleName
* @property AuthItemChild[] $authItemChildren
* @property AuthItemChild[] $authItemChildren0
* @property AuthItem[] $children
* @property AuthItem[] $parents
*/
class AuthItem extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'auth_item';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'type', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['type', 'created_at', 'updated_at', 'deleted'], 'integer'],
[['description', 'data'], 'string'],
[['deleted_at'], 'safe'],
[['name', 'rule_name'], 'string', 'max' => 64],
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 32],
[['rule_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthRule::className(), 'targetAttribute' => ['rule_name' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => 'Name',
'type' => 'Type',
'description' => 'Description',
'rule_name' => 'Rule Name',
'data' => 'Data',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'deleted_at' => 'Deleted At',
'deleted' => 'Deleted',
'created_by' => 'Created By',
'updated_by' => 'Updated By',
'deleted_by' => 'Deleted By',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthAssignments()
{
return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRuleName()
{
return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren()
{
return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItemChildren0()
{
return $this->hasMany(AuthItemChild::className(), ['child' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChildren()
{
return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable('auth_item_child', ['parent' => 'name']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParents()
{
return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']);
}
}
<?php
namespace common\modules\auth\models;
use Yii;
/**
* This is the model class for table "auth_item_child".
*
* @property string $parent
* @property string $child
* @property string $created_at
* @property string $updated_at
* @property string $deleted_at
* @property integer $deleted
* @property string $created_by
* @property string $updated_by
* @property string $deleted_by
*
* @property AuthItem $parent0
* @property AuthItem $child0
*/
class AuthItemChild extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'auth_item_child';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['parent', 'child', 'created_at', 'updated_at', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['created_at', 'updated_at', 'deleted_at'], 'safe'],
[['deleted'], 'integer'],
[['parent', 'child'], 'string', 'max' => 64],
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 32],
[['parent'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['parent' => 'name']],
[['child'], 'exist', 'skipOnError' => true, 'targetClass' => AuthItem::className(), 'targetAttribute' => ['child' => 'name']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'parent' => 'Parent',
'child' => 'Child',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'deleted_at' => 'Deleted At',
'deleted' => 'Deleted',
'created_by' => 'Created By',
'updated_by' => 'Updated By',
'deleted_by' => 'Deleted By',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParent0()
{
return $this->hasOne(AuthItem::className(), ['name' => 'parent']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChild0()
{
return $this->hasOne(AuthItem::className(), ['name' => 'child']);
}
}
<?php
namespace common\modules\auth\models;
use Yii;
/**
* This is the model class for table "auth_rule".
*
* @property string $name
* @property string $data
* @property integer $created_at
* @property integer $updated_at
* @property string $deleted_at
* @property integer $deleted
* @property string $created_by
* @property string $updated_by
* @property string $deleted_by
*
* @property AuthItem[] $authItems
*/
class AuthRule extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'auth_rule';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'deleted_at', 'deleted', 'created_by', 'updated_by', 'deleted_by'], 'required'],
[['data'], 'string'],
[['created_at', 'updated_at', 'deleted'], 'integer'],
[['deleted_at'], 'safe'],
[['name'], 'string', 'max' => 64],
[['created_by', 'updated_by', 'deleted_by'], 'string', 'max' => 32],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'name' => 'Name',
'data' => 'Data',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'deleted_at' => 'Deleted At',
'deleted' => 'Deleted',
'created_by' => 'Created By',
'updated_by' => 'Updated By',
'deleted_by' => 'Deleted By',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAuthItems()
{
return $this->hasMany(AuthItem::className(), ['rule_name' => 'name']);
}
}
<div class="auth-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment