76 lines
1.8 KiB
PHP
76 lines
1.8 KiB
PHP
<?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\NotFoundHttpException;
|
|
|
|
/**
|
|
* Login form
|
|
*
|
|
* @property Customer
|
|
*/
|
|
class LoginForm extends Model
|
|
{
|
|
// cardnumber
|
|
public $cardNumber;
|
|
public $deviceIdentifier;
|
|
public $deviceName;
|
|
|
|
public $mobileDevice;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
// username and password are both required
|
|
[['cardNumber', 'deviceIdentifier', 'deviceName'], 'required'],
|
|
// cardNumber is validated by validatePassword()
|
|
['cardNumber', 'validateCardNumber'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 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 validateCardNumber($attribute, $params)
|
|
{
|
|
if (!$this->hasErrors()) {
|
|
/** @var \common\models\Customer $user */
|
|
$this->getMobileDevice();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Finds user by [[username]]
|
|
*
|
|
* @return Customer|null|ActiveRecord
|
|
*/
|
|
public function getMobileDevice()
|
|
{
|
|
|
|
if ($this->mobileDevice === null) {
|
|
$mobileDeviceManager = new MobileDeviceManager();
|
|
$this->mobileDevice = $mobileDeviceManager->loginOrCreate($this->cardNumber, $this->deviceIdentifier, $this->deviceName);
|
|
if ($this->mobileDevice === null) {
|
|
throw new NotFoundHttpException();
|
|
}
|
|
}
|
|
|
|
return $this->mobileDevice;
|
|
}
|
|
}
|