fitness-web/mobileapi/models/LoginForm.php

88 lines
2.1 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\BadRequestHttpException;
use yii\web\NotFoundHttpException;
/**
* Login form
*
* @property Customer
*/
class LoginForm extends Model
{
// cardnumber
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 */
throw new BadRequestHttpException();
}
$customer = $this->getCustomer();
}
/**
* Finds user by [[username]]
*
* @return Customer|null|ActiveRecord
*/
public function getCustomer()
{
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;
}
}