Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from justcoded/develop
Browse files Browse the repository at this point in the history
Register form, Password reset feature, Admin user role edit
  • Loading branch information
aprokopenko authored Mar 20, 2018
2 parents 232885a + 19847c3 commit 4ff1b73
Show file tree
Hide file tree
Showing 15 changed files with 570 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ v0.9
---------------------
* Upgrade app folder structure: moved controllers from web to root, moved commands to console,
* Moved migration component/views custom configuration to a separate module.
* Admin panel: Added user "Roles" fields to edit form.
* Issue #13: Registration is absent
* Issue #14: Reset password

v0.8.6
---------------------
Expand Down
90 changes: 88 additions & 2 deletions app/controllers/AuthController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Yii;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\forms\LoginForm;
use app\forms\ContactForm;
use app\forms\PasswordRequestForm;
use app\forms\PasswordUpdateForm;
use app\models\User;

class AuthController extends Controller
{
Expand Down Expand Up @@ -44,7 +47,7 @@ public function behaviors()
*/
public function actionLogin()
{
if ( ! Yii::$app->user->isGuest) {
if (! Yii::$app->user->isGuest) {
return $this->goHome();
}

Expand All @@ -71,4 +74,87 @@ public function actionLogout()
return $this->goHome();
}

/**
* Register action.
*
* @return string|Response
*/
public function actionRegister()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}

$model = new RegisterForm();

if ($model->load(Yii::$app->request->post()) && $model->register()) {
Yii::$app->session->addFlash('success', 'You have been successfully registered');

return $this->goBack();
}

return $this->render('register', [
'model' => $model,
]);
}

/**
* Password request action
*
* @return string|Response
*/
public function actionPasswordRequest()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}

$model = new PasswordRequestForm();

if ($model->load(Yii::$app->request->post()) && $model->request()) {
Yii::$app->session->addFlash(
'success',
'If the email address is registered in the system, we would send a letter there shortly.'
);

return $this->goBack();
}

return $this->render('password-request', [
'model' => $model,
]);
}

/**
* Password update action
*
* @param string $token Password reset token.
*
* @return string|Response
* @throws NotFoundHttpException If token not found in DB.
*/
public function actionPasswordUpdate($token)
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}

if (!User::isPasswordResetTokenValid($token)) {
throw new NotFoundHttpException('Page not found.');
}

$model = new PasswordUpdateForm();
$model->resetToken = $token;

if ($model->load(Yii::$app->request->post()) && $model->update()) {
Yii::$app->session->addFlash('success', 'Your password has been successfully updated!');

return $this->goBack();
}

return $this->render('password-update', [
'model' => $model,
]);
}

}
70 changes: 70 additions & 0 deletions app/forms/PasswordRequestForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace app\forms;

use app\models\User;
use Yii;
use yii\base\Model;
use yii\helpers\Url;

class PasswordRequestForm extends Model
{
public $email;


/**
* @return array the validation rules.
*/
public function rules()
{
return [
['email', 'required'],
['email', 'email'],
];
}

/**
* Sends an email to the user with reset token if the email exists in the DB
*
* @return bool
*/
public function request()
{
if ($this->validate()) {
$user = User::findByUsername($this->email);

if (empty($user)) {
return true; //needed for security reasons
}

$user->generatePasswordResetToken();
$user->save();

Yii::$app->mailer->compose()
->setTo($user->email)
->setFrom(settings()->app->systemFriendlyEmail)
->setSubject('Restore your password on ' . Yii::$app->name)
->setTextBody($this->getMessageBody($user->password_reset_token))
->send();

return true;
}

return false;
}

/**
* Render a message with reset token
*
* @param string $resetToken
*
* @return string
*/
protected function getMessageBody($resetToken)
{
return 'To restore your password, please, follow this link ' . Url::to([
'auth/password-update',
'token' => $resetToken,
], true);
}
}
54 changes: 54 additions & 0 deletions app/forms/PasswordUpdateForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace app\forms;

use app\models\User;
use yii\base\Model;

class PasswordUpdateForm extends Model
{
public $resetToken;
public $newPassword;
public $newPasswordRepeat;


/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['newPassword', 'newPasswordRepeat'], 'required'],
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'],
];
}

/**
* Updates user's password if $resetToken is valid
*
* @return bool
*/
public function update()
{
$user = User::findByPasswordResetToken($this->resetToken);

if (empty($user)) {
return false;
}

if ($this->validate()) {
$user->setPassword($this->newPassword);
$user->removePasswordResetToken();

if (!$user->save()) {
$this->addErrors($user->errors);

return false;
}

return true;
}

return false;
}
}
70 changes: 70 additions & 0 deletions app/forms/RegisterForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace app\forms;

use app\models\User;
use yii\base\Model;

/**
* RegisterForm is the model behind the register form.
*/
class RegisterForm extends Model
{
public $email;
public $password;
public $passwordRepeat;
public $firstName;
public $lastName;


/**
* @return array of the validation rules
*/
public function rules()
{
return [
[['email', 'password', 'passwordRepeat', 'firstName', 'lastName'], 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => User::className()],
['passwordRepeat', 'compare', 'compareAttribute' => 'password'],
];
}

/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'passwordRepeat' => 'Repeat Password',
];
}

/**
* Registers a user
*
* @return bool whether the model passes validation
*/
public function register()
{
if ($this->validate()) {
$user = new User();
$user->email = $user->username = $this->email;
$user->first_name = $this->firstName;
$user->last_name = $this->lastName;

$user->setPassword($this->password);
$user->generateAuthKey();

if (!$user->save()) {
$this->addErrors($user->errors);

return false;
}

return true;
}

return false;
}
}
Loading

0 comments on commit 4ff1b73

Please sign in to comment.