Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
CRAP | |
33.87% |
21 / 62 |
| UserController | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
272.20 | |
33.87% |
21 / 62 |
| behaviors | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| actions | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| actionLogin | |
0.00% |
0 / 1 |
4.05 | |
85.71% |
6 / 7 |
|||
| actionLogout | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| actionSignupAdmin | |
0.00% |
0 / 1 |
5.01 | |
92.31% |
12 / 13 |
|||
| actionConfirmEmail | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 11 |
|||
| actionUpdate | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 10 |
|||
| actionRequestPasswordReset | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 8 |
|||
| actionResetPassword | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 8 |
|||
| <?php | |
| namespace frontend\controllers; | |
| use Yii; | |
| use common\models\User; | |
| use common\models\LoginForm; | |
| use frontend\models\PasswordResetRequestForm; | |
| use frontend\models\ResetPasswordForm; | |
| use frontend\models\SignupForm; | |
| use frontend\models\UserForm; | |
| use yii\base\InvalidParamException; | |
| use yii\web\BadRequestHttpException; | |
| use yii\web\ForbiddenHttpException; | |
| use yii\web\Controller; | |
| use yii\filters\VerbFilter; | |
| use yii\filters\AccessControl; | |
| /** | |
| * User controller | |
| */ | |
| class UserController extends Controller | |
| { | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function behaviors() | |
| { | |
| return [ | |
| 'access' => [ | |
| 'class' => AccessControl::className(), | |
| 'only' => ['signup-admin', 'signup', 'login', 'logout', 'update'], | |
| 'rules' => [ | |
| [ | |
| 'actions' => ['signup-admin', 'signup', 'login'], | |
| 'allow' => true, | |
| 'roles' => ['?'], | |
| ], | |
| [ | |
| 'actions' => ['logout', 'update'], | |
| 'allow' => true, | |
| 'roles' => ['@'], | |
| ], | |
| ], | |
| ], | |
| 'verbs' => [ | |
| 'class' => VerbFilter::className(), | |
| 'actions' => [ | |
| 'logout' => ['post'], | |
| ], | |
| ], | |
| ]; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function actions() | |
| { | |
| return [ | |
| 'signup' => [ | |
| 'class' => 'frontend\controllers\user\SignupAction', | |
| ], | |
| ]; | |
| } | |
| /** | |
| * Logs in a user. | |
| * | |
| * @return mixed | |
| */ | |
| public function actionLogin() | |
| { | |
| if (!Yii::$app->user->isGuest) { | |
| return $this->goHome(); | |
| } | |
| $model = new LoginForm(); | |
| if ($model->load(Yii::$app->request->post()) && $model->login()) { | |
| return $this->goBack(); | |
| } else { | |
| return $this->render('login', [ | |
| 'model' => $model, | |
| ]); | |
| } | |
| } | |
| /** | |
| * Logs out the current user. | |
| * | |
| * @return mixed | |
| */ | |
| public function actionLogout() | |
| { | |
| Yii::$app->user->logout(); | |
| return $this->goHome(); | |
| } | |
| /** | |
| * Signup the admin user. | |
| * @return mixed | |
| */ | |
| public function actionSignupAdmin() | |
| { | |
| $usersCount = User::find()->count(); | |
| if($usersCount > 0) | |
| throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.')); | |
| $model = new SignupForm(); | |
| if ($model->load(Yii::$app->request->post())) { | |
| if ($user = $model->signup()) { | |
| $auth = Yii::$app->authManager; | |
| $adminRole = $auth->getRole('admin'); | |
| $auth->assign($adminRole, $user->getId()); | |
| if (Yii::$app->getUser()->login($user)) { | |
| return $this->goHome(); | |
| } | |
| } | |
| } | |
| return $this->render('signupAdmin', [ | |
| 'model' => $model, | |
| ]); | |
| } | |
| /** | |
| * Confirm a user email with token via email and activate an account. | |
| * @return mixed | |
| */ | |
| public function actionConfirmEmail($token) | |
| { | |
| try { | |
| $user = User::findByEmailConfirmToken($token); | |
| if($user){ | |
| $user->status = User::STATUS_ACTIVE; | |
| $user->removeEmailConfirmToken(); | |
| if($user->save()) { | |
| Yii::$app->session->setFlash('success', Yii::t('app', 'Account has been activated.')); | |
| }else{ | |
| Yii::$app->session->setFlash('error', Yii::t('app', 'Account has not been activated.')); | |
| } | |
| }else{ | |
| Yii::$app->session->setFlash('info', Yii::t('app', 'Account was already activated.')); | |
| } | |
| } catch (InvalidParamException $e) { | |
| throw new BadRequestHttpException($e->getMessage()); | |
| } | |
| return $this->goHome(); | |
| } | |
| /** | |
| * Update the user. | |
| * @return mixed | |
| */ | |
| public function actionUpdate() | |
| { | |
| $user = User::findOne(Yii::$app->user->id); | |
| if(!Yii::$app->user->can('updateUser', ['user' => $user])) | |
| throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.')); | |
| $model = new UserForm(); | |
| $model->load($user->getAttributes(), ''); | |
| if ($model->load(Yii::$app->request->post())) { | |
| if ($user = $model->save()) { | |
| Yii::$app->session->setFlash('success', Yii::t('app', 'Profile was changed')); | |
| } | |
| } | |
| return $this->render('update', [ | |
| 'model' => $model, | |
| ]); | |
| } | |
| /** | |
| * Requests password reset. | |
| * | |
| * @return mixed | |
| */ | |
| public function actionRequestPasswordReset() | |
| { | |
| $model = new PasswordResetRequestForm(); | |
| if ($model->load(Yii::$app->request->post()) && $model->validate()) { | |
| if ($model->sendEmail()) { | |
| Yii::$app->session->setFlash('success', Yii::t('app', 'Check your email for further instructions.')); | |
| return $this->goHome(); | |
| } else { | |
| Yii::$app->session->setFlash('error', Yii::t('app', 'Sorry, we are unable to reset password for email provided.')); | |
| } | |
| } | |
| return $this->render('requestPasswordResetToken', [ | |
| 'model' => $model, | |
| ]); | |
| } | |
| /** | |
| * Resets password. | |
| * | |
| * @param string $token | |
| * @return mixed | |
| * @throws BadRequestHttpException | |
| */ | |
| public function actionResetPassword($token) | |
| { | |
| try { | |
| $model = new ResetPasswordForm($token); | |
| } catch (InvalidParamException $e) { | |
| throw new BadRequestHttpException($e->getMessage()); | |
| } | |
| if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { | |
| Yii::$app->session->setFlash('success', Yii::t('app', 'New password was saved.')); | |
| return $this->goHome(); | |
| } | |
| return $this->render('resetPassword', [ | |
| 'model' => $model, | |
| ]); | |
| } | |
| } |