service = $service; $this->profile_service = $profile_service; } /** * @inheritdoc */ public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'actions' => ['create', 'view', 'index', 'update', 'delete'], 'allow' => true, 'roles' => ['UserManagement'], ], [ 'actions' => ['profile'], 'allow' => true, 'roles' => ['@'], ], [ // all the action are accessible to admin 'allow' => true, 'roles' => ['admin'], ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], ], ]; } /** * Lists all User models. * @return string */ public function actionIndex(): string { $searchModel = new UserSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * @param $id * * @return string * @throws NotFoundHttpException */ public function actionView($id): string { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return Response|string * @throws Exception * @throws \yii\db\Exception */ public function actionCreate(): Response|string { $form = new UserForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $user = $this->service->create($form); return $this->redirect(['view', 'id' => $user->id]); } catch (DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('create', [ 'model' => $form, ]); } /** * @param $id * @return Response|string * @throws Exception * @throws NotFoundHttpException * @throws \yii\db\Exception */ public function actionUpdate($id): Response|string { $user = $this->findModel($id); $form = new UserForm($user); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $this->service->edit($user->id, $form); return $this->redirect(['view', 'id' => $user->id]); } catch (DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('update', [ 'model' => $form, 'user' => $user, ]); } /** * Deletes an existing User model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id * * @return Response */ public function actionDelete(int $id): Response { $this->service->remove($id); return $this->redirect(['index']); } /** * @return Response|string * @throws NotFoundHttpException|Exception */ public function actionProfile(): Response|string { $user = $this->findModel(Yii::$app->user->id); $form = new ProfileEditForm($user); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { $form->user_pic = UploadedFile::getInstance($form, 'user_pic'); $this->profile_service->edit(Yii::$app->user->id, $form); Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.')); return $this->redirect(['/user/profile']); } catch (DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } return $this->render('profile', [ 'model' => $form, 'user' => $user, ]); } /** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel(int $id): User { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }