Всех приветствую! Помогите пожалуйста с реализацией следующей конструкции: Есть пользователи, у каждого имеются свои записи(посты), которые сохраняются в БД и выводятся в Gridview Yii2. Как сделать так, чтобы авторизованный пользователь имел доступ только к своим записям их просмотру и редактированию? Настроил в проекте RBAC, по данной инструкции: https://www.youtube.com/watch?v=kXhzSN_InV0 В БД имеются 2 таблицы user и requests(посты).
Модель
public static function tableName(): string
{
return 'requests';
}
/**
* {@inheritdoc}
*/
public function rules(): array
{
return [
[['theme', 'email'], 'required'],
[['email'], 'email'],
['theme', 'string', 'min' => 10],
[['text', 'service', 'category', 'priority'], 'safe'],
[['created_at', 'updated_at'], 'safe'],
[['filename'], 'string', 'max' => 100],
[['file'], 'file'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels(): array
{
return [
'theme' => 'Тема обращения',
'email' => 'Е-майл',
'text' => 'Содержание',
'filename' => 'Файл',
'category' => 'Категория',
'service' => 'Сервис',
'priority' => 'Приоритет',
'created_at' => 'Создано',
'updated_at' =>'Изменено'
];
}
public function getAuthor (): ActiveQuery
{
return $this->hasOne(User::class(),['id'=>'user_id']);
}
public function behaviors(): array
{
return [
[
'class' => TimestampBehavior::class,
'attributes' => [
BaseActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
BaseActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
// если вместо метки времени UNIX используется datetime:
'value' => new Expression('NOW()'),
],
];
}
}
Контроллер
class RequestsController extends Controller
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['index'],
'rules' => [
[
'actions' => ['index'],
'allow' => true,
'roles' => ['@'],
],
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['loqout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'logout' => ['post'],
],
],
];
}