fix mobileapi login
This commit is contained in:
parent
d6caffb11c
commit
aec6913000
114
common/manager/MobileDeviceManager.php
Normal file
114
common/manager/MobileDeviceManager.php
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace common\manager;
|
||||||
|
|
||||||
|
use common\models\Card;
|
||||||
|
use common\models\CardEventRegistrationForm;
|
||||||
|
use common\models\Customer;
|
||||||
|
use common\models\Event;
|
||||||
|
use common\models\EventRegistration;
|
||||||
|
use common\models\MobileDevice;
|
||||||
|
use common\models\Ticket;
|
||||||
|
use customerapi\models\available\EventInterval;
|
||||||
|
use customerapi\models\registrations\EventRegistrationAvailable;
|
||||||
|
use customerapi\models\details\EventRegistrationView;
|
||||||
|
use Exception;
|
||||||
|
use Yii;
|
||||||
|
use yii\base\BaseObject;
|
||||||
|
use yii\db\ActiveRecord;
|
||||||
|
use yii\db\Query;
|
||||||
|
use yii\web\BadRequestHttpException;
|
||||||
|
use yii\web\NotFoundHttpException;
|
||||||
|
use yii\web\ServerErrorHttpException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by IntelliJ IDEA.
|
||||||
|
* User: rocho
|
||||||
|
* Date: 2018.12.17.
|
||||||
|
* Time: 6:12
|
||||||
|
*/
|
||||||
|
class MobileDeviceManager extends BaseObject
|
||||||
|
{
|
||||||
|
|
||||||
|
public function login($cardNumber, $deviceIdentifier)
|
||||||
|
{
|
||||||
|
|
||||||
|
$card = Card::find()->andWhere(
|
||||||
|
['number' => $cardNumber]
|
||||||
|
)->one();
|
||||||
|
|
||||||
|
if ( $card == null ){
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$device = MobileDevice::find()
|
||||||
|
->andWhere(
|
||||||
|
[
|
||||||
|
'id_card' => $card->id_card,
|
||||||
|
'device_identifier' => $deviceIdentifier
|
||||||
|
]
|
||||||
|
)->one();
|
||||||
|
|
||||||
|
if ( $device === null ){
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
in_array($device->status, [MobileDevice::STATUS_ACTIVE, MobileDevice::STATUS_INACTIVE], true) === false ){
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $device;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create($cardNumber, $deviceIdentifier)
|
||||||
|
{
|
||||||
|
$card = Card::find()->andWhere(
|
||||||
|
['number' => $cardNumber]
|
||||||
|
)->one();
|
||||||
|
|
||||||
|
if ( $card == null ){
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$device = MobileDevice::find()
|
||||||
|
->andWhere(
|
||||||
|
[
|
||||||
|
'id_card' => $card->id_card,
|
||||||
|
'device_identifier' => $deviceIdentifier
|
||||||
|
]
|
||||||
|
)->one();
|
||||||
|
|
||||||
|
if ( $device ){
|
||||||
|
throw new BadRequestHttpException("Device already exists, can't create");
|
||||||
|
}
|
||||||
|
|
||||||
|
$device = new MobileDevice();
|
||||||
|
$device->device_identifier = $deviceIdentifier;
|
||||||
|
$device->id_card = $card->id_card;
|
||||||
|
$device->status = MobileDevice::STATUS_INACTIVE;
|
||||||
|
$device->save(false);
|
||||||
|
|
||||||
|
return $device;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $cardNumber
|
||||||
|
* @param $deviceIdentifier
|
||||||
|
* @return array|MobileDevice|ActiveRecord
|
||||||
|
* @throws BadRequestHttpException
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public function loginOrCreate($cardNumber, $deviceIdentifier)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
|
||||||
|
return $this->login($cardNumber, $deviceIdentifier);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->create($cardNumber, $deviceIdentifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -3,6 +3,8 @@
|
|||||||
namespace common\models;
|
namespace common\models;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
|
use yii\behaviors\TimestampBehavior;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the model class for table "mobile_device".
|
* This is the model class for table "mobile_device".
|
||||||
@ -17,6 +19,11 @@ use Yii;
|
|||||||
*/
|
*/
|
||||||
class MobileDevice extends \yii\db\ActiveRecord
|
class MobileDevice extends \yii\db\ActiveRecord
|
||||||
{
|
{
|
||||||
|
|
||||||
|
const STATUS_ACTIVE = 'active';
|
||||||
|
const STATUS_INACTIVE = 'inactive';
|
||||||
|
const STATUS_DELETED = 'deleted';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
@ -54,4 +61,15 @@ class MobileDevice extends \yii\db\ActiveRecord
|
|||||||
'updated_at' => Yii::t('common/mobiledevice', 'Updated At'),
|
'updated_at' => Yii::t('common/mobiledevice', 'Updated At'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function behaviors()
|
||||||
|
{
|
||||||
|
return ArrayHelper::merge( [
|
||||||
|
[
|
||||||
|
'class' => TimestampBehavior::className(),
|
||||||
|
'value' => function(){ return date('Y-m-d H:i:s' ); }
|
||||||
|
]
|
||||||
|
],
|
||||||
|
parent::behaviors());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,8 @@
|
|||||||
namespace common\models;
|
namespace common\models;
|
||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
|
use yii\behaviors\TimestampBehavior;
|
||||||
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the model class for table "virtual_key".
|
* This is the model class for table "virtual_key".
|
||||||
@ -44,14 +46,25 @@ class VirtualKey extends \yii\db\ActiveRecord
|
|||||||
public function attributeLabels()
|
public function attributeLabels()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => Yii::t('common/mobiledevice', 'ID'),
|
'id' => Yii::t('common/virtualkey', 'ID'),
|
||||||
'id_card' => Yii::t('common/mobiledevice', 'Id Card'),
|
'id_card' => Yii::t('common/virtualkey', 'Id Card'),
|
||||||
'id_key' => Yii::t('common/mobiledevice', 'Id Key'),
|
'id_key' => Yii::t('common/virtualkey', 'Id Key'),
|
||||||
'valid_until' => Yii::t('common/mobiledevice', 'Valid Until'),
|
'valid_until' => Yii::t('common/virtualkey', 'Valid Until'),
|
||||||
'direction_in_at' => Yii::t('common/mobiledevice', 'Direction In At'),
|
'direction_in_at' => Yii::t('common/virtualkey', 'Direction In At'),
|
||||||
'direction_out_at' => Yii::t('common/mobiledevice', 'Direction Out At'),
|
'direction_out_at' => Yii::t('common/virtualkey', 'Direction Out At'),
|
||||||
'created_at' => Yii::t('common/mobiledevice', 'Created At'),
|
'created_at' => Yii::t('common/virtualkey', 'Created At'),
|
||||||
'updated_at' => Yii::t('common/mobiledevice', 'Updated At'),
|
'updated_at' => Yii::t('common/virtualkey', 'Updated At'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function behaviors()
|
||||||
|
{
|
||||||
|
return ArrayHelper::merge( [
|
||||||
|
[
|
||||||
|
'class' => TimestampBehavior::className(),
|
||||||
|
'value' => function(){ return date('Y-m-d H:i:s' ); }
|
||||||
|
]
|
||||||
|
],
|
||||||
|
parent::behaviors());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,7 @@ class LoginController extends Controller
|
|||||||
public function actionLogin()
|
public function actionLogin()
|
||||||
{
|
{
|
||||||
$form = new LoginForm();
|
$form = new LoginForm();
|
||||||
|
$form->scenario = "default";
|
||||||
|
|
||||||
$form->load(\Yii::$app->request->post(), '');
|
$form->load(\Yii::$app->request->post(), '');
|
||||||
|
|
||||||
|
|||||||
@ -44,7 +44,7 @@ return [
|
|||||||
],
|
],
|
||||||
'jwt' => [
|
'jwt' => [
|
||||||
'class' => Jwt::class,
|
'class' => Jwt::class,
|
||||||
'key' => 'secret',
|
'key' => 'dianaveronika',
|
||||||
// You have to configure ValidationData informing all claims you want to validate the token.
|
// You have to configure ValidationData informing all claims you want to validate the token.
|
||||||
'jwtValidationData' => JwtValidationData::class,
|
'jwtValidationData' => JwtValidationData::class,
|
||||||
],
|
],
|
||||||
|
|||||||
@ -27,7 +27,9 @@ class LoginController extends RestController
|
|||||||
public function actionLogin()
|
public function actionLogin()
|
||||||
{
|
{
|
||||||
$form = new LoginForm();
|
$form = new LoginForm();
|
||||||
|
$post = \Yii::$app->request->post();
|
||||||
|
|
||||||
|
$post2 = $_POST;
|
||||||
$form->load(\Yii::$app->request->post(), '');
|
$form->load(\Yii::$app->request->post(), '');
|
||||||
|
|
||||||
if ($form->validate()) {
|
if ($form->validate()) {
|
||||||
|
|||||||
@ -17,11 +17,11 @@ class RestController extends Controller
|
|||||||
public function behaviors()
|
public function behaviors()
|
||||||
{
|
{
|
||||||
$behaviors = parent::behaviors();
|
$behaviors = parent::behaviors();
|
||||||
// $behaviors['authenticator'] = [
|
$behaviors['authenticator'] = [
|
||||||
// 'class' => JwtHttpBearerAuth::class,
|
'class' => JwtHttpBearerAuth::class,
|
||||||
// 'auth' => [$this, 'auth'],
|
'auth' => [$this, 'auth'],
|
||||||
// 'optional' => $this->getOptionalActions(),
|
'optional' => $this->getOptionalActions(),
|
||||||
// ];
|
];
|
||||||
return $behaviors;
|
return $behaviors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,31 +30,31 @@ class RestController extends Controller
|
|||||||
* @param Token $token
|
* @param Token $token
|
||||||
* @return Customer|null
|
* @return Customer|null
|
||||||
*/
|
*/
|
||||||
// public function auth($token)
|
public function auth($token)
|
||||||
// {
|
{
|
||||||
// if ( !isset($token ) ) {
|
if ( !isset($token ) ) {
|
||||||
// return null;
|
return null;
|
||||||
// }
|
}
|
||||||
// try {
|
try {
|
||||||
// $uid = (string) $token->getClaim('uid');
|
$uid = (string) $token->getClaim('uid');
|
||||||
// $customer = Customer::findOne(['id_customer' => $uid]);
|
$customer = Customer::findOne(['id_customer' => $uid]);
|
||||||
// if (isset($customer)) {
|
if (isset($customer)) {
|
||||||
// \Yii::$app->user->setIdentity($customer);
|
\Yii::$app->user->setIdentity($customer);
|
||||||
// return $customer;
|
return $customer;
|
||||||
// }
|
}
|
||||||
// } catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// Yii::error('Failed to load customer: ' . $e->getMessage());
|
Yii::error('Failed to load customer: ' . $e->getMessage());
|
||||||
// }
|
}
|
||||||
// return null;
|
return null;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// /**
|
/**
|
||||||
// * @see AuthMethod::$optional
|
* @see AuthMethod::$optional
|
||||||
// * @return array
|
* @return array
|
||||||
// */
|
*/
|
||||||
// protected function getOptionalActions(){
|
protected function getOptionalActions(){
|
||||||
// return [];
|
return [];
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace mobileapi\models;
|
namespace mobileapi\models;
|
||||||
|
|
||||||
|
use common\manager\MobileDeviceManager;
|
||||||
use common\models\Customer;
|
use common\models\Customer;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\base\Model;
|
use yii\base\Model;
|
||||||
|
use yii\db\ActiveRecord;
|
||||||
|
use yii\web\BadRequestHttpException;
|
||||||
|
use yii\web\NotFoundHttpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Login form
|
* Login form
|
||||||
|
*
|
||||||
|
* @property Customer
|
||||||
*/
|
*/
|
||||||
class LoginForm extends Model
|
class LoginForm extends Model
|
||||||
{
|
{
|
||||||
|
// cardnumber
|
||||||
public $username;
|
public $username;
|
||||||
public $password;
|
public $password;
|
||||||
|
|
||||||
@ -27,12 +35,13 @@ class LoginForm extends Model
|
|||||||
['password', 'validatePassword'],
|
['password', 'validatePassword'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function attributeLabels(){
|
public function attributeLabels()
|
||||||
return [
|
{
|
||||||
'username' =>Yii::t('common/site', 'Username'),
|
return [
|
||||||
'password' =>Yii::t('common/site', 'Password'),
|
'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)
|
public function validatePassword($attribute, $params)
|
||||||
{
|
{
|
||||||
if (!$this->hasErrors()) {
|
if ($this->hasErrors()) {
|
||||||
/** @var \common\models\Customer $user */
|
/** @var \common\models\Customer $user */
|
||||||
$customer = $this->getCustomer();
|
throw new BadRequestHttpException();
|
||||||
if (!$customer || !$customer->validatePassword($this->password)) {
|
|
||||||
$this->addError($attribute, 'Incorrect username or password.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$customer = $this->getCustomer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds user by [[username]]
|
* Finds user by [[username]]
|
||||||
*
|
*
|
||||||
* @return Customer|null
|
* @return Customer|null|ActiveRecord
|
||||||
*/
|
*/
|
||||||
public function getCustomer()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user