You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.0 KiB

7 years ago
<?php
namespace backend\controllers;
use core\forms\user\ProfileEditForm;
7 years ago
use core\forms\user\UserForm;
use core\services\user\ProfileService;
7 years ago
use core\services\user\UserManageService;
3 years ago
use DomainException;
7 years ago
use Yii;
use core\entities\user\User;
use backend\forms\UserSearch;
3 years ago
use yii\base\Exception;
7 years ago
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
3 years ago
use yii\web\Response;
use yii\web\UploadedFile;
7 years ago
/**
* UserController implements the CRUD actions for User model.
*/
class UserController extends Controller
{
3 years ago
private UserManageService $service;
private ProfileService $profile_service;
7 years ago
public function __construct($id, $module, UserManageService $service, ProfileService $profile_service, $config = [])
7 years ago
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
$this->profile_service = $profile_service;
7 years ago
}
/**
* @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,
7 years ago
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all User models.
3 years ago
* @return string
7 years ago
*/
3 years ago
public function actionIndex(): string
7 years ago
{
$searchModel = new UserSearch();
7 years ago
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
7 years ago
'dataProvider' => $dataProvider,
]);
}
/**
* @param $id
*
* @return string
* @throws NotFoundHttpException
*/
3 years ago
public function actionView($id): string
7 years ago
{
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.
3 years ago
* @return Response|string
* @throws Exception
* @throws \yii\db\Exception
7 years ago
*/
3 years ago
public function actionCreate(): Response|string
7 years ago
{
$form = new UserForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$user = $this->service->create($form);
7 years ago
return $this->redirect(['view', 'id' => $user->id]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('create', [
'model' => $form,
]);
}
/**
* @param $id
3 years ago
* @return Response|string
* @throws Exception
* @throws NotFoundHttpException
3 years ago
* @throws \yii\db\Exception
*/
3 years ago
public function actionUpdate($id): Response|string
7 years ago
{
$user = $this->findModel($id);
$form = new UserForm($user);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->edit($user->id, $form);
7 years ago
return $this->redirect(['view', 'id' => $user->id]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('update', [
'model' => $form,
'user' => $user,
7 years ago
]);
}
/**
* Deletes an existing User model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*
7 years ago
* @param integer $id
*
3 years ago
* @return Response
7 years ago
*/
3 years ago
public function actionDelete(int $id): Response
7 years ago
{
3 years ago
$this->service->remove($id);
7 years ago
return $this->redirect(['index']);
}
3 years ago
/**
* @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');
3 years ago
$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']);
3 years ago
} catch (DomainException $e) {
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('profile', [
'model' => $form,
'user' => $user,
]);
}
7 years ago
/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
7 years ago
* @param integer $id
*
7 years ago
* @return User the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
3 years ago
protected function findModel(int $id): User
7 years ago
{
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}