2016-07-25 16:37:14 +05:30
|
|
|
<?php
|
|
|
|
namespace api\validators;
|
|
|
|
|
|
|
|
use common\helpers\Error as E;
|
|
|
|
use common\models\Account;
|
|
|
|
use Yii;
|
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
use yii\validators\Validator;
|
|
|
|
|
|
|
|
class PasswordRequiredValidator extends Validator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Account
|
|
|
|
*/
|
|
|
|
public $account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public $skipOnEmpty = false;
|
|
|
|
|
|
|
|
public function init() {
|
|
|
|
parent::init();
|
|
|
|
if ($this->account === null) {
|
|
|
|
$this->account = Yii::$app->user->identity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->account instanceof Account) {
|
|
|
|
throw new InvalidConfigException('account should be instance of ' . Account::class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateValue($value) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|