This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from justcoded/develop
Register form, Password reset feature, Admin user role edit
- Loading branch information
Showing
15 changed files
with
570 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.