|
@@ -0,0 +1,175 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\models\user;
|
|
|
+
|
|
|
+use dektrium\user\Finder;
|
|
|
+use dektrium\user\helpers\Password;
|
|
|
+use dektrium\user\traits\ModuleTrait;
|
|
|
+use yii\helpers\ArrayHelper;
|
|
|
+use yii\helpers\Html;
|
|
|
+use Yii;
|
|
|
+use yii\base\Model;
|
|
|
+
|
|
|
+/**
|
|
|
+ * LoginForm get user's login and password, validates them and logs the user in. If user has been blocked, it adds
|
|
|
+ * an error to login form.
|
|
|
+ *
|
|
|
+ * @author Dmitry Erofeev <dmeroff@gmail.com>
|
|
|
+ */
|
|
|
+class LoginForm extends Model
|
|
|
+{
|
|
|
+ use ModuleTrait;
|
|
|
+
|
|
|
+ /** @var string User's email or username */
|
|
|
+ public $login;
|
|
|
+
|
|
|
+ /** @var string User's uuid */
|
|
|
+ public $uuid;
|
|
|
+
|
|
|
+ /** @var string User's plain password */
|
|
|
+ public $password;
|
|
|
+
|
|
|
+ /** @var string Whether to remember the user */
|
|
|
+ public $rememberMe = false;
|
|
|
+
|
|
|
+ /** @var User */
|
|
|
+ protected $user;
|
|
|
+
|
|
|
+ /** @var Finder */
|
|
|
+ protected $finder;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param Finder $finder
|
|
|
+ * @param array $config
|
|
|
+ */
|
|
|
+ public function __construct(Finder $finder, $config = [])
|
|
|
+ {
|
|
|
+ $this->finder = $finder;
|
|
|
+ parent::__construct($config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Gets all users to generate the dropdown list when in debug mode.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function loginList()
|
|
|
+ {
|
|
|
+ /** @var \dektrium\user\Module $module */
|
|
|
+ $module = \Yii::$app->getModule('user');
|
|
|
+
|
|
|
+ $userModel = $module->modelMap['User'];
|
|
|
+
|
|
|
+ return ArrayHelper::map($userModel::find()->where(['blocked_at' => null])->all(), 'username', function ($user) {
|
|
|
+ return sprintf('%s (%s)', Html::encode($user->username), Html::encode($user->email));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @inheritdoc */
|
|
|
+ public function attributeLabels()
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'login' => Yii::t('user', 'Login'),
|
|
|
+ 'uuid' => Yii::t('user', 'Nomer Dogovora'),
|
|
|
+ 'password' => Yii::t('user', 'Password'),
|
|
|
+ 'rememberMe' => Yii::t('user', 'Remember me next time'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @inheritdoc */
|
|
|
+ public function rules()
|
|
|
+ {
|
|
|
+ $rules = [
|
|
|
+ 'uuidTrim' => ['uuid', 'trim'],
|
|
|
+ 'requiredFields' => [['uuid'], 'required'],
|
|
|
+ 'confirmationValidate' => [
|
|
|
+ 'login',
|
|
|
+ function ($attribute) {
|
|
|
+ if ($this->user !== null) {
|
|
|
+ $confirmationRequired = $this->module->enableConfirmation
|
|
|
+ && !$this->module->enableUnconfirmedLogin;
|
|
|
+ if ($confirmationRequired && !$this->user->getIsConfirmed()) {
|
|
|
+ $this->addError($attribute, Yii::t('user', 'You need to confirm your email address'));
|
|
|
+ }
|
|
|
+ if ($this->user->getIsBlocked()) {
|
|
|
+ $this->addError($attribute, Yii::t('user', 'Your account has been blocked'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ 'rememberMe' => ['rememberMe', 'boolean'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!$this->module->debug) {
|
|
|
+ $rules = array_merge($rules, [
|
|
|
+ 'requiredFields' => [['uuid', 'password'], 'required'],
|
|
|
+ 'passwordValidate' => [
|
|
|
+ 'password',
|
|
|
+ function ($attribute) {
|
|
|
+ if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) {
|
|
|
+ $this->addError($attribute, Yii::t('user', 'Invalid login or password'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $rules;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Validates if the hash of the given password is identical to the saved hash in the database.
|
|
|
+ * It will always succeed if the module is in DEBUG mode.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function validatePassword($attribute, $params)
|
|
|
+ {
|
|
|
+ if ($this->user === null || !Password::validate($this->password, $this->user->password_hash))
|
|
|
+ $this->addError($attribute, Yii::t('user', 'Invalid login or password'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Validates form and logs the user in.
|
|
|
+ *
|
|
|
+ * @return bool whether the user is logged in successfully
|
|
|
+ */
|
|
|
+ public function login()
|
|
|
+ {
|
|
|
+ if ($this->validate() && $this->user) {
|
|
|
+ $isLogged = Yii::$app->getUser()->login($this->user, $this->rememberMe ? $this->module->rememberFor : 0);
|
|
|
+
|
|
|
+ if ($isLogged) {
|
|
|
+ $this->user->updateAttributes(['last_login_at' => time()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $isLogged;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** @inheritdoc */
|
|
|
+ public function formName()
|
|
|
+ {
|
|
|
+ return 'login-form';
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @inheritdoc */
|
|
|
+ public function beforeValidate()
|
|
|
+ {
|
|
|
+ if (parent::beforeValidate()) {
|
|
|
+
|
|
|
+ if (filter_var($this->login, FILTER_VALIDATE_EMAIL)) {
|
|
|
+ return $this->findUserByEmail($this->login);
|
|
|
+ }else{
|
|
|
+ $this->user = $this->finder->findUser(["uuid"=>$this->uuid])->one();
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|