Hi,
I am using Yii2 basic. I have implemented RBAC as told in Yii 2 guide. I have also implemented the case where user can log in from different tables. I have admin and employee table. I am not able to create groups. It gives me to error as “You have no access to create groups”. Following is my controller
<?php
namespace app\controllers;
use Yii;
use app\models\Groupdetails;
use app\models\GroupdetailsSearch;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\FieldofficerRule;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
/**
* GroupdetailsController implements the CRUD actions for Groupdetails model.
*/
class GroupdetailsController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Groupdetails models.
* @return mixed
*/
public function actionIndex()
{
if (!Yii::$app->user->can('indexAll')) {
$searchModel = new GroupdetailsSearch();
}
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Groupdetails model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
if(\Yii::$app->user->can('viewGroup', ['model' => $model]))
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
else
{
throw new ForbiddenHttpException("You can't view other groups details");
}
}
/**
* Creates a new Groupdetails model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
if(\Yii::$app->user->can('createGroup'))
{
//$this->layout = 'layoutfile';
$model = new Groupdetails();
$model->scenario = 'create';
if ($model->load(Yii::$app->request->post()) ) {
$identity = Yii::$app->user->identity;
$eid = $identity->id;
$model->id = $eid;
$model->filepath = UploadedFile::getInstance($model, 'filepath');
$model->Photo = Yii::$app->security->generateRandomString().'.'.$model->filepath->extension;
$model->save(false);
$model->filepath->saveAs('uploadedgroupimages/'.$model->Photo);
return $this->redirect(['view', 'id' => $model->GroupId]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
else
{
throw new ForbiddenHttpException("You have no access to create groups");
}
}
/**
* Updates an existing Groupdetails model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if(\Yii::$app->user->can('updateGroup', ['model' => $model]))
{
$oldimage = $model->Photo;
if ($model->load(Yii::$app->request->post())) {
$model->filepath = UploadedFile::getInstance($model, 'filepath');
if($model->filepath == null)
{
$model->save(false);
}
else{
unlink('uploadedgroupimages/'.$oldimage);
$model->Photo = Yii::$app->security->generateRandomString().'.'.$model->filepath->extension;
$model->save(false);
$model->filepath->saveAs('uploadedgroupimages/'.$model->Photo);
return $this->redirect(['view', 'id' => $model->GroupId]);
}
}else {
return $this->render('update', [
'model' => $model,
]);
}
}
else
{
throw new ForbiddenHttpException("You can't update other groups details");
}
}
/**
* Deletes an existing Groupdetails model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if(\Yii::$app->user->can('deleteGroup', ['model' => $model]))
{
$oldimage = $model->Photo;
unlink('uploadedgroupimages/'.$oldimage);
$model->delete();
return $this->redirect(['index']);
}
else
{
throw new ForbiddenHttpException("You can't delete other groups details");
}
}
/**
* Finds the Groupdetails model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Groupdetails the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Groupdetails::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Also my Field Officer rule is as below:
<?php
namespace app\rbac;
use yii\rbac\Rule;
use app\models\Groupdetails;
/**
* Checks if authorID matches user passed via params
*/
class FieldofficerRule extends Rule
{
public $name = 'isfieldofficer';
/**
* @param string|int $user the user ID.
* @param Item $item the role or permission that this rule is associated with
* @param array $params parameters passed to ManagerInterface::checkAccess().
* @return bool a value indicating whether the rule permits the role or permission it is associated with.
*/
public function execute($user, $item, $params)
{
return isset($params['model']) ? $params['model']->id == $user : false;
}
}