add customer api
This commit is contained in:
70
customerapi/models/LoginForm.php
Normal file
70
customerapi/models/LoginForm.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace customerapi\models;
|
||||
|
||||
use common\models\Customer;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* Login form
|
||||
*/
|
||||
class LoginForm extends Model
|
||||
{
|
||||
public $username;
|
||||
public $password;
|
||||
|
||||
public $customer;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// username and password are both required
|
||||
[['username', 'password'], 'required'],
|
||||
// password is validated by validatePassword()
|
||||
['password', 'validatePassword'],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributeLabels(){
|
||||
return [
|
||||
'username' =>Yii::t('common/site', 'Username'),
|
||||
'password' =>Yii::t('common/site', 'Password'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws \yii\base\InvalidConfigException
|
||||
*/
|
||||
public function validatePassword($attribute, $params)
|
||||
{
|
||||
if (!$this->hasErrors()) {
|
||||
/** @var \common\models\Customer $user */
|
||||
$customer = $this->getCustomer();
|
||||
if (!$customer || !$customer->validatePassword($this->password)) {
|
||||
$this->addError($attribute, 'Incorrect username or password.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds user by [[username]]
|
||||
*
|
||||
* @return Customer|null
|
||||
*/
|
||||
public function getCustomer()
|
||||
{
|
||||
if ( $this->customer === null ){
|
||||
$this->customer = Customer::findIdentity( $this->username );
|
||||
}
|
||||
return $this->customer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user