mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Описана базовая миграция, добавлена модель аккаунта, добавлена модель авторизации, написаны первичные тесты для этой модели, добавлен модуль авторизации, настроен базовый контроллер. Короче много чего сделано
This commit is contained in:
79
api/models/LoginForm.php
Normal file
79
api/models/LoginForm.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\models\Account;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* Login form
|
||||
*/
|
||||
class LoginForm extends Model
|
||||
{
|
||||
public $username;
|
||||
public $password;
|
||||
public $rememberMe = true;
|
||||
|
||||
private $_user;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// username and password are both required
|
||||
[['username', 'password'], 'required'],
|
||||
// rememberMe must be a boolean value
|
||||
['rememberMe', 'boolean'],
|
||||
// password is validated by validatePassword()
|
||||
['password', 'validatePassword'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the password.
|
||||
* This method serves as the inline validation for password.
|
||||
*
|
||||
* @param string $attribute the attribute currently being validated
|
||||
* @param array $params the additional name-value pairs given in the rule
|
||||
*/
|
||||
public function validatePassword($attribute, $params)
|
||||
{
|
||||
if (!$this->hasErrors()) {
|
||||
$user = $this->getUser();
|
||||
if (!$user || !$user->validatePassword($this->password)) {
|
||||
$this->addError($attribute, 'Incorrect username or password.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs in a user using the provided username and password.
|
||||
*
|
||||
* @return boolean whether the user is logged in successfully
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
if ($this->validate()) {
|
||||
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds user by [[username]]
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
protected function getUser()
|
||||
{
|
||||
if ($this->_user === null) {
|
||||
$this->_user = Account::findByEmail($this->username);
|
||||
}
|
||||
|
||||
return $this->_user;
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\models\User;
|
||||
use common\models\Account;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
@@ -22,7 +22,7 @@ class PasswordResetRequestForm extends Model
|
||||
['email', 'email'],
|
||||
['email', 'exist',
|
||||
'targetClass' => '\common\models\User',
|
||||
'filter' => ['status' => User::STATUS_ACTIVE],
|
||||
'filter' => ['status' => Account::STATUS_ACTIVE],
|
||||
'message' => 'There is no user with such email.'
|
||||
],
|
||||
];
|
||||
@@ -35,14 +35,14 @@ class PasswordResetRequestForm extends Model
|
||||
*/
|
||||
public function sendEmail()
|
||||
{
|
||||
/* @var $user User */
|
||||
$user = User::findOne([
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
/* @var $user Account */
|
||||
$user = Account::findOne([
|
||||
'status' => Account::STATUS_ACTIVE,
|
||||
'email' => $this->email,
|
||||
]);
|
||||
|
||||
if ($user) {
|
||||
if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
|
||||
if (!Account::isPasswordResetTokenValid($user->password_reset_token)) {
|
||||
$user->generatePasswordResetToken();
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\models\User;
|
||||
use common\models\Account;
|
||||
use yii\base\InvalidParamException;
|
||||
use yii\base\Model;
|
||||
use Yii;
|
||||
@@ -14,7 +14,7 @@ class ResetPasswordForm extends Model
|
||||
public $password;
|
||||
|
||||
/**
|
||||
* @var \common\models\User
|
||||
* @var \common\models\Account
|
||||
*/
|
||||
private $_user;
|
||||
|
||||
@@ -31,7 +31,7 @@ class ResetPasswordForm extends Model
|
||||
if (empty($token) || !is_string($token)) {
|
||||
throw new InvalidParamException('Password reset token cannot be blank.');
|
||||
}
|
||||
$this->_user = User::findByPasswordResetToken($token);
|
||||
$this->_user = Account::findByPasswordResetToken($token);
|
||||
if (!$this->_user) {
|
||||
throw new InvalidParamException('Wrong password reset token.');
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace api\models;
|
||||
|
||||
use common\models\User;
|
||||
use common\models\Account;
|
||||
use yii\base\Model;
|
||||
use Yii;
|
||||
|
||||
@@ -39,13 +39,12 @@ class SignupForm extends Model
|
||||
/**
|
||||
* Signs user up.
|
||||
*
|
||||
* @return User|null the saved model or null if saving fails
|
||||
* @return Account|null the saved model or null if saving fails
|
||||
*/
|
||||
public function signup()
|
||||
{
|
||||
if ($this->validate()) {
|
||||
$user = new User();
|
||||
$user->username = $this->username;
|
||||
$user = new Account();
|
||||
$user->email = $this->email;
|
||||
$user->setPassword($this->password);
|
||||
$user->generateAuthKey();
|
||||
|
Reference in New Issue
Block a user