Hi,
As so far, I can log in and log out from different tables.
As discussed so far, I have a MultiUser model in models folder as you told. I have employee table with id, FirstName,LastName, etc. I have admin table with columns id, Name,username,password,etc.
I have a groupdetails table where there is columns such as GroupId, id, GroupName,StageOfGroup,etc where relationship is assigned as One employee has many groupdetails. I have created all the permissions, roles and assigned permissions to admin and field officer role as stated in RBAC in yii2.
I have not created a foreign key reference in employee and auth_assignment table.
Now in auth_assignment table I have following

Now when I log as admin, I can create, view, update and delete the group details.
But when I login as employee whose id is 2 then I can only create groupdetails, but I cannot view, update and delete those groupdetails which I have created.
Following is the groupdetails controller
<?php
namespace app\controllers;
use Yii;
use app\models\Groupdetails;
use app\models\GroupdetailsSearch;
use yii\web\UploadedFile;
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()
{
$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)
{
$model=$this->findModel($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();
if ($model->load(Yii::$app->request->post()) ) {
$model->id=\Yii::$app->user->identity->getOnlyid();
$model->save(false);
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]))
{
if ($model->load(Yii::$app->request->post())) {
$model->id=\Yii::$app->user->identity->getOnlyid();
$model->save();
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]))
{
$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.');
}
}
I tried to change the user_id field in auth_assignment table where Admin role is to id 1 of admin table and Field officer to employee whose id is 2, but still it is the same.
How should I resolve this?