fix mobileapi login

This commit is contained in:
2022-02-15 22:49:47 +01:00
parent d6caffb11c
commit aec6913000
8 changed files with 219 additions and 54 deletions

View File

@@ -1,15 +1,23 @@
<?php
namespace mobileapi\models;
use common\manager\MobileDeviceManager;
use common\models\Customer;
use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
/**
* Login form
*
* @property Customer
*/
class LoginForm extends Model
{
// cardnumber
public $username;
public $password;
@@ -27,12 +35,13 @@ class LoginForm extends Model
['password', 'validatePassword'],
];
}
public function attributeLabels(){
return [
'username' =>Yii::t('common/site', 'Username'),
'password' =>Yii::t('common/site', 'Password'),
];
public function attributeLabels()
{
return [
'username' => Yii::t('common/site', 'Username'),
'password' => Yii::t('common/site', 'Password'),
];
}
/**
@@ -45,26 +54,34 @@ class LoginForm extends Model
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
if ($this->hasErrors()) {
/** @var \common\models\Customer $user */
$customer = $this->getCustomer();
if (!$customer || !$customer->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
throw new BadRequestHttpException();
}
$customer = $this->getCustomer();
}
/**
* Finds user by [[username]]
*
* @return Customer|null
* @return Customer|null|ActiveRecord
*/
public function getCustomer()
{
if ( $this->customer === null ){
$this->customer = Customer::findIdentity( $this->username );
if ($this->customer === null) {
$mobileDeviceManager = new MobileDeviceManager();
$mobileDevice = $mobileDeviceManager->loginOrCreate($this->username, $this->password);
/** @var Customer */
$this->customer = Customer::find()->andWhere([
'id_customer_card' => $mobileDevice->id_card
])->one();
if ($this->customer == null) {
throw new NotFoundHttpException();
}
}
return $this->customer;
return $this->customer;
}
}