use mobiledevice as login object for mobile api

This commit is contained in:
Roland Schneider 2022-02-22 14:41:16 +01:00
parent 356fc1f0fd
commit 530accd8bf
8 changed files with 90 additions and 71 deletions

View File

@ -119,15 +119,4 @@ class MobileDeviceManager extends BaseObject
} }
public function isMobileDeviceActivatedByIdCard($idCard){
if ( !isset($idCard)){
return false;
}
$device = MobileDevice::find()->andWhere(['id_card' => $idCard])->one();
if ( !isset($device)){
return false;
}
return isset($device->activated_at);
}
} }

View File

@ -3,8 +3,10 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\IdentityInterface;
/** /**
* This is the model class for table "mobile_device". * This is the model class for table "mobile_device".
@ -18,7 +20,7 @@ use yii\helpers\ArrayHelper;
* @property string $created_at * @property string $created_at
* @property string $updated_at * @property string $updated_at
*/ */
class MobileDevice extends \yii\db\ActiveRecord class MobileDevice extends \yii\db\ActiveRecord implements IdentityInterface
{ {
const STATUS_ACTIVE = 'active'; const STATUS_ACTIVE = 'active';
@ -95,4 +97,31 @@ class MobileDevice extends \yii\db\ActiveRecord
} }
return $result; return $result;
} }
public static function findIdentity($id)
{
self::findOne(['id' => $id]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
public function validateAuthKey($authKey)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
} }

View File

@ -3,7 +3,7 @@
namespace mobileapi\components; namespace mobileapi\components;
use common\components\HttpStatus; use common\components\HttpStatus;
use common\manager\MobileDeviceManager; use common\models\MobileDevice;
use Yii; use Yii;
use yii\base\Action; use yii\base\Action;
@ -28,18 +28,18 @@ class ActivatedFilter extends ActionFilter
try { try {
$activated = false; $activated = false;
// get the customer // get the device
$customer = \Yii::$app->user->getIdentity(); /** @var MobileDevice $mobileDevice */
if (isset($customer)) { $mobileDevice = \Yii::$app->user->getIdentity();
$idCard = $customer->id_customer_card; if (isset($mobileDevice)) {
$idCard = $mobileDevice->id_card;
// find out if the device is activated // find out if the device is activated
$mobileDeviceManager = new MobileDeviceManager(); $activated = $mobileDevice->status === MobileDevice::STATUS_ACTIVE;
$activated = $mobileDeviceManager->isMobileDeviceActivatedByIdCard($idCard);
} }
// if device is not activated, throw exception with http status 412 // if device is not activated, throw exception with http status 412
if ($activated === false) { if ($activated === false) {
throw new HttpException( HttpStatus::PRECONDITION_FAILED,"Card not activated"); throw new HttpException( HttpStatus::PRECONDITION_FAILED,"Device is not activated: " . $mobileDevice->id);
} }
} catch (HttpException $e) { } catch (HttpException $e) {
if ($e->statusCode === HttpStatus::PRECONDITION_FAILED && $this->isOptional($action)) { if ($e->statusCode === HttpStatus::PRECONDITION_FAILED && $this->isOptional($action)) {

View File

@ -25,7 +25,7 @@ return [
] ]
], ],
'user' => [ 'user' => [
'identityClass' => 'common\models\Customer', 'identityClass' => 'common\models\MobileDevice',
'enableSession' => false, 'enableSession' => false,
'enableAutoLogin' => false, 'enableAutoLogin' => false,
'loginUrl' => null, 'loginUrl' => null,

View File

@ -30,7 +30,7 @@ class LoginController extends RestController
$form->load(\Yii::$app->request->post(), ''); $form->load(\Yii::$app->request->post(), '');
if (!$form->validate()) { if (!$form->validate()) {
throw new BadRequestHttpException("Hibás e-mail cím vagy jelszó!"); throw new BadRequestHttpException("Hibás bejelentkezés " . print_r($form->getErrors( ),true));
} }
/** @var Jwt $jwt */ /** @var Jwt $jwt */
@ -47,7 +47,7 @@ class LoginController extends RestController
->identifiedBy('A989C57D19E2AF756BA9585AC4CFAF7974AE3D2BCA7CCA7307B39AB28CC7C2C8', true)// Configures the id (jti claim), replicating as a header item ->identifiedBy('A989C57D19E2AF756BA9585AC4CFAF7974AE3D2BCA7CCA7307B39AB28CC7C2C8', true)// Configures the id (jti claim), replicating as a header item
->issuedAt($time)// Configures the time that the token was issue (iat claim) ->issuedAt($time)// Configures the time that the token was issue (iat claim)
->expiresAt($time + $validFor)// Configures the expiration time of the token (exp claim) ->expiresAt($time + $validFor)// Configures the expiration time of the token (exp claim)
->withClaim('uid', $form->getCustomer()->getId())// Configures a new claim, called "uid" ->withClaim('uid', $form->getMobileDevice()->getId())// Configures a new claim, called "uid"
->getToken($signer, $key); // Retrieves the generated token ->getToken($signer, $key); // Retrieves the generated token
return $this->asJson([ return $this->asJson([

View File

@ -4,6 +4,7 @@ namespace mobileapi\controllers;
use common\models\Customer; use common\models\Customer;
use common\models\MobileDevice;
use Exception; use Exception;
use Lcobucci\JWT\Token; use Lcobucci\JWT\Token;
use mobileapi\components\ActivatedFilter; use mobileapi\components\ActivatedFilter;
@ -33,7 +34,7 @@ class RestController extends Controller
/** /**
* This method will check the token * This method will check the token
* @param Token $token * @param Token $token
* @return Customer|null * @return MobileDevice|null
*/ */
public function auth($token) public function auth($token)
{ {
@ -42,13 +43,13 @@ class RestController extends Controller
} }
try { try {
$uid = (string) $token->getClaim('uid'); $uid = (string) $token->getClaim('uid');
$customer = Customer::findOne(['id_customer' => $uid]); $mobileDevice = MobileDevice::findOne(['id' => $uid]);
if (isset($customer)) { if (isset($mobileDevice)) {
\Yii::$app->user->setIdentity($customer); \Yii::$app->user->setIdentity($mobileDevice);
return $customer; return $mobileDevice;
} }
} catch (Exception $e) { } catch (Exception $e) {
Yii::error('Failed to load customer: ' . $e->getMessage()); Yii::error('Failed to load mobile device: ' . $e->getMessage());
} }
return null; return null;
} }

View File

@ -2,8 +2,11 @@
namespace mobileapi\manager; namespace mobileapi\manager;
use Exception;
use Yii;
use common\models\Card; use common\models\Card;
use common\models\CardKeyAssignment; use common\models\CardKeyAssignment;
use common\models\Customer;
use common\models\Key; use common\models\Key;
use common\models\Ticket; use common\models\Ticket;
use Endroid\QrCode\QrCode; use Endroid\QrCode\QrCode;
@ -11,17 +14,27 @@ use yii\web\NotFoundHttpException;
class ApiManager class ApiManager
{ {
/**
* @throws Exception
* @throws NotFoundHttpException
*/
public function getCardPage() public function getCardPage()
{ {
$customer = \Yii::$app->user->getIdentity(); $device = Yii::$app->user->getIdentity();
if (!isset($customer)) { if (!isset($device)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
} }
$card = Card::findOne($customer->id_customer_card); $card = Card::findOne($device->id_card);
if (!isset($card)) { if (!isset($card)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
}
$customer = Customer::findOne(['id_customer_card' => $device->id_card]);
if (!isset($customer)) {
throw new NotFoundHttpException();
} }
$qrCode = new QrCode($card->number); $qrCode = new QrCode($card->number);
@ -34,17 +47,17 @@ class ApiManager
} }
function getTicketPage() public function getTicketPage()
{ {
$customer = \Yii::$app->user->getIdentity(); $device = Yii::$app->user->getIdentity();
if (!isset($customer)) { if (!isset($device)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
} }
$card = Card::findOne($customer->id_customer_card); $card = Card::findOne($device->id_card);
if (!isset($card)) { if (!isset($card)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
} }
$tickets = Ticket::readActive($card); $tickets = Ticket::readActive($card);
@ -68,15 +81,15 @@ class ApiManager
public function getVirtualKeyPage() public function getVirtualKeyPage()
{ {
$customer = \Yii::$app->user->getIdentity(); $device = Yii::$app->user->getIdentity();
if (!isset($customer)) { if (!isset($device)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
} }
$card = Card::findOne($customer->id_customer_card); $card = Card::findOne($device->id_card);
if (!isset($card)) { if (!isset($card)) {
throw new \yii\web\NotFoundHttpException(); throw new NotFoundHttpException();
} }
$keyObject = null; $keyObject = null;
@ -101,9 +114,9 @@ class ApiManager
'key' => $key->number, 'key' => $key->number,
'idKey' => $key->id_key 'idKey' => $key->id_key
]; ];
} catch (\Exception $e) { } catch (Exception $e) {
// failed to get key // failed to get key
\Yii::error('Failed to get virtual key: ' . $e->getMessage()); Yii::error('Failed to get virtual key: ' . $e->getMessage());
} }
return [ return [

View File

@ -7,7 +7,6 @@ use common\models\Customer;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
/** /**
@ -19,10 +18,10 @@ class LoginForm extends Model
{ {
// cardnumber // cardnumber
public $cardNumber; public $cardNumber;
public $deviceId; public $deviceIdentifier;
public $deviceName; public $deviceName;
public $customer; public $mobileDevice;
/** /**
* @inheritdoc * @inheritdoc
@ -31,20 +30,12 @@ class LoginForm extends Model
{ {
return [ return [
// username and password are both required // username and password are both required
[['cardNumber', 'deviceId','deviceName'], 'required'], [['cardNumber', 'deviceIdentifier', 'deviceName'], 'required'],
// cardNumber is validated by validatePassword() // cardNumber is validated by validatePassword()
['cardNumber', 'validateCardNumber'], ['cardNumber', 'validateCardNumber'],
]; ];
} }
public function attributeLabels()
{
return [
'username' => Yii::t('common/site', 'Username'),
'password' => Yii::t('common/site', 'Password'),
];
}
/** /**
* Validates the password. * Validates the password.
* This method serves as the inline validation for password. * This method serves as the inline validation for password.
@ -55,11 +46,11 @@ class LoginForm extends Model
*/ */
public function validateCardNumber($attribute, $params) public function validateCardNumber($attribute, $params)
{ {
if ($this->hasErrors()) { if (!$this->hasErrors()) {
/** @var \common\models\Customer $user */ /** @var \common\models\Customer $user */
throw new BadRequestHttpException(); $this->getMobileDevice();
} }
$customer = $this->getCustomer();
} }
@ -68,21 +59,17 @@ class LoginForm extends Model
* *
* @return Customer|null|ActiveRecord * @return Customer|null|ActiveRecord
*/ */
public function getCustomer() public function getMobileDevice()
{ {
if ($this->customer === null) { if ($this->mobileDevice === null) {
$mobileDeviceManager = new MobileDeviceManager(); $mobileDeviceManager = new MobileDeviceManager();
$mobileDevice = $mobileDeviceManager->loginOrCreate($this->cardNumber, $this->deviceId, $this->deviceName); $this->mobileDevice = $mobileDeviceManager->loginOrCreate($this->cardNumber, $this->deviceIdentifier, $this->deviceName);
/** @var Customer */ if ($this->mobileDevice === null) {
$this->customer = Customer::find()->andWhere([
'id_customer_card' => $mobileDevice->id_card
])->one();
if ($this->customer == null) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
} }
} }
return $this->customer; return $this->mobileDevice;
} }
} }