2016-07-25 16:37:14 +05:30
|
|
|
<?php
|
|
|
|
namespace api\validators;
|
|
|
|
|
|
|
|
use common\helpers\Error as E;
|
|
|
|
use common\models\Account;
|
2017-09-30 03:14:05 +05:30
|
|
|
use common\rbac\Permissions as P;
|
2016-07-25 16:37:14 +05:30
|
|
|
use yii\base\InvalidConfigException;
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
public $account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
|
|
|
if (!$this->account instanceof Account) {
|
|
|
|
throw new InvalidConfigException('account should be instance of ' . Account::class);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|