fix mobileapi login
This commit is contained in:
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;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "mobile_device".
|
||||
@@ -17,6 +19,11 @@ use Yii;
|
||||
*/
|
||||
class MobileDevice extends \yii\db\ActiveRecord
|
||||
{
|
||||
|
||||
const STATUS_ACTIVE = 'active';
|
||||
const STATUS_INACTIVE = 'inactive';
|
||||
const STATUS_DELETED = 'deleted';
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
@@ -54,4 +61,15 @@ class MobileDevice extends \yii\db\ActiveRecord
|
||||
'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;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "virtual_key".
|
||||
@@ -44,14 +46,25 @@ class VirtualKey extends \yii\db\ActiveRecord
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => Yii::t('common/mobiledevice', 'ID'),
|
||||
'id_card' => Yii::t('common/mobiledevice', 'Id Card'),
|
||||
'id_key' => Yii::t('common/mobiledevice', 'Id Key'),
|
||||
'valid_until' => Yii::t('common/mobiledevice', 'Valid Until'),
|
||||
'direction_in_at' => Yii::t('common/mobiledevice', 'Direction In At'),
|
||||
'direction_out_at' => Yii::t('common/mobiledevice', 'Direction Out At'),
|
||||
'created_at' => Yii::t('common/mobiledevice', 'Created At'),
|
||||
'updated_at' => Yii::t('common/mobiledevice', 'Updated At'),
|
||||
'id' => Yii::t('common/virtualkey', 'ID'),
|
||||
'id_card' => Yii::t('common/virtualkey', 'Id Card'),
|
||||
'id_key' => Yii::t('common/virtualkey', 'Id Key'),
|
||||
'valid_until' => Yii::t('common/virtualkey', 'Valid Until'),
|
||||
'direction_in_at' => Yii::t('common/virtualkey', 'Direction In At'),
|
||||
'direction_out_at' => Yii::t('common/virtualkey', 'Direction Out At'),
|
||||
'created_at' => Yii::t('common/virtualkey', 'Created 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user