2016-07-25 16:37:14 +05:30
|
|
|
<?php
|
2020-06-12 02:57:02 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
namespace api\validators;
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2016-07-25 16:37:14 +05:30
|
|
|
use common\helpers\Error as E;
|
|
|
|
use common\models\Account;
|
2017-09-30 03:14:05 +05:30
|
|
|
use yii\di\Instance;
|
2016-07-25 16:37:14 +05:30
|
|
|
use yii\validators\Validator;
|
2017-09-30 03:14:05 +05:30
|
|
|
use yii\web\User;
|
2016-07-25 16:37:14 +05:30
|
|
|
|
|
|
|
class PasswordRequiredValidator extends Validator {
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public Account $account;
|
2016-07-25 16:37:14 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
|
2017-09-30 03:14:05 +05:30
|
|
|
/**
|
|
|
|
* @var User|string
|
|
|
|
*/
|
|
|
|
public $user = 'user';
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2017-09-30 03:14:05 +05:30
|
|
|
$this->user = Instance::ensure($this->user, User::class);
|
2016-07-25 16:37:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateValue($value) {
|
2017-09-30 03:14:05 +05:30
|
|
|
if ($this->user->can(P::ESCAPE_IDENTITY_VERIFICATION)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:37:14 +05:30
|
|
|
if (empty($value)) {
|
|
|
|
return [E::PASSWORD_REQUIRED, []];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->account->validatePassword($value) === false) {
|
2016-07-28 10:14:16 +05:30
|
|
|
return [E::PASSWORD_INCORRECT, []];
|
2016-07-25 16:37:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|