add customer api
This commit is contained in:
@@ -15,7 +15,7 @@ use common\models\Ticket;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class CustomerController extends CustomerapiController
|
||||
class CustomerApiController extends RestController
|
||||
{
|
||||
|
||||
|
||||
@@ -9,20 +9,84 @@
|
||||
namespace customerapi\controllers;
|
||||
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Ticket;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use common\models\Event;
|
||||
use customerapi\models\available\EventInterval;
|
||||
use customerapi\models\available\EventAvailable;
|
||||
use customerapi\models\DayToDisplay;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
||||
class EventController extends CustomerapiController
|
||||
/** @noinspection PhpUnused */
|
||||
|
||||
class EventController extends CustomerApiController
|
||||
{
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
/**
|
||||
* interface EventsAvailableResponse {
|
||||
* days: DayToDisplay[];
|
||||
* events: Event[];
|
||||
* }
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEvent( )
|
||||
public function actionAvailable()
|
||||
{
|
||||
$interval = EventInterval::createInterval();
|
||||
|
||||
// compose day objects
|
||||
$dates = [];
|
||||
for ($i = 0; $i < $interval->daysToDisplay; $i++) {
|
||||
$day = clone $interval->firstDisplayDate;
|
||||
$day->modify('+' . $i . ' day');
|
||||
$dayToDisplay = new DayToDisplay();
|
||||
$dayToDisplay->date = $day->getTimestamp();
|
||||
$afterFirstActiveDay = $interval->firstActiveDate < $day || $interval->firstActiveDate == $day;
|
||||
$beforeLastActiveDay = $interval->lastActiveDate > $day || $interval->lastActiveDate == $day;
|
||||
|
||||
$dayToDisplay->active = ($afterFirstActiveDay && $beforeLastActiveDay);
|
||||
$dayToDisplay->comment = '#' . idate('W', $day->getTimestamp()) . ' - ' . $day->format('Y-m-d H:i:s');
|
||||
$dayToDisplay->events = [];
|
||||
$dates[] = $dayToDisplay;
|
||||
}
|
||||
|
||||
// get events between active dates
|
||||
$query = EventAvailable::find();
|
||||
$query = $query->select(
|
||||
[
|
||||
'{{event}}.*',
|
||||
// 'COUNT({{event_registration}}.id) AS reservationCount'
|
||||
]);
|
||||
|
||||
$events = $query
|
||||
->innerJoinWith('trainer')
|
||||
->innerJoinWith('eventType')
|
||||
->innerJoinWith('room')
|
||||
->joinWith('activeEventRegistrations')
|
||||
->andWhere(['>=', 'event.start', $interval->firstActiveDate->getTimestamp()])
|
||||
->andWhere(['<', 'event.start', (clone $interval->lastActiveDate)->modify('+1 day')->getTimestamp()])
|
||||
->andWhere(['event.active' => '1'])
|
||||
->all();
|
||||
|
||||
// set events per day
|
||||
/** @var Event $event */
|
||||
foreach ($events as $event) {
|
||||
$eventDay = new DateTime();
|
||||
$eventDay->setTimestamp($event->start);
|
||||
$eventDay->setTime(0, 0);
|
||||
|
||||
/** @var DayToDisplay $date */
|
||||
foreach ($dates as $date) {
|
||||
if ($date->date === $eventDay->getTimestamp()) {
|
||||
$date->events[] = $event;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
$this->asJson([
|
||||
'interval' => $interval,
|
||||
'dates' => $dates
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
67
customerapi/controllers/EventRegistrationController.php
Normal file
67
customerapi/controllers/EventRegistrationController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: rocho
|
||||
* Date: 2018.08.29.
|
||||
* Time: 21:58
|
||||
*/
|
||||
|
||||
namespace customerapi\controllers;
|
||||
|
||||
|
||||
use common\manager\EventRegistrationManager;
|
||||
use common\models\CardEventRegistrationForm;
|
||||
use common\models\Customer;
|
||||
use customerapi\models\available\EventInterval;
|
||||
use customerapi\models\available\EventRegistrationAvailable;
|
||||
use Throwable;
|
||||
use Yii;
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
|
||||
class EventRegistrationController extends CustomerApiController
|
||||
{
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function actionIndex()
|
||||
{
|
||||
$interval = EventInterval::createInterval();
|
||||
|
||||
$registrations = EventRegistrationAvailable::find()
|
||||
->innerJoinWith('event')
|
||||
->andWhere(['and',
|
||||
['>=', 'event.start', $interval->firstActiveDate->getTimestamp()],
|
||||
['<', 'event.start', $interval->lastActiveDate->getTimestamp()],
|
||||
['id_customer' => Yii::$app->user->getId()]
|
||||
])->all();
|
||||
|
||||
return $this->asJson(
|
||||
|
||||
$registrations
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
* @param $idEvent
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function actionRegister($idEvent) {
|
||||
/** @var Customer $customer */
|
||||
$customer = Yii::$app->user->getIdentity();
|
||||
$card =$customer->card;
|
||||
|
||||
$form = new CardEventRegistrationForm();
|
||||
$form->event_id = $idEvent;
|
||||
$form->card_number = $card->number;
|
||||
|
||||
$manager = new EventRegistrationManager();
|
||||
$manager->registerCard($form);
|
||||
|
||||
return $form->registration;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
28
customerapi/controllers/PingController.php
Normal file
28
customerapi/controllers/PingController.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: rocho
|
||||
* Date: 2018.08.29.
|
||||
* Time: 21:58
|
||||
*/
|
||||
|
||||
namespace customerapi\controllers;
|
||||
|
||||
use common\components\HttpStatus;
|
||||
use Yii;
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
|
||||
class PingController extends RestController
|
||||
{
|
||||
|
||||
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function actionPing( )
|
||||
{
|
||||
Yii::$app->response->setStatusCode( HttpStatus::NO_CONTENT );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,44 +3,58 @@
|
||||
namespace customerapi\controllers;
|
||||
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Customer;
|
||||
use yii\filters\auth\HttpBasicAuth;
|
||||
use Exception;
|
||||
use Lcobucci\JWT\Token;
|
||||
use sizeg\jwt\JwtHttpBearerAuth;
|
||||
use Yii;
|
||||
use yii\filters\auth\AuthMethod;
|
||||
use yii\rest\Controller;
|
||||
|
||||
class RestController extends \yii\web\Controller
|
||||
class RestController extends Controller
|
||||
{
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
$behaviors['authenticator'] = [
|
||||
'class' => HttpBasicAuth::className(),
|
||||
'auth' => [$this, 'auth']
|
||||
'class' => JwtHttpBearerAuth::class,
|
||||
'auth' => [$this, 'auth'],
|
||||
'optional' => $this->getOptionalActions(),
|
||||
];
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function auth($username, $password)
|
||||
/**
|
||||
* This method will check the token
|
||||
* @param Token $token
|
||||
* @return Customer|null
|
||||
*/
|
||||
public function auth($token)
|
||||
{
|
||||
if ( !isset($token ) ) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
// $query = Card::find();
|
||||
// Card::addCardNumberCondition($query, Helper::fixAsciiChars($username));
|
||||
// $card = $query->one();
|
||||
|
||||
$customer = Customer::findOne(['email' => $username]);
|
||||
|
||||
|
||||
$uid = (string) $token->getClaim('uid');
|
||||
$customer = Customer::findOne(['id_customer' => $uid]);
|
||||
if (isset($customer)) {
|
||||
if ($customer->validatePassword($password)) {
|
||||
\Yii::$app->user->setIdentity($customer);
|
||||
return $customer;
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\Yii::error("Failed to load user: " . $e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Yii::error('Failed to load customer: ' . $e->getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AuthMethod::$optional
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptionalActions(){
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,21 +8,61 @@
|
||||
|
||||
namespace customerapi\controllers;
|
||||
|
||||
use common\models\Customer;
|
||||
use customerapi\models\LoginForm;
|
||||
use sizeg\jwt\Jwt;
|
||||
use sizeg\jwt\JwtHttpBearerAuth;
|
||||
use Yii;
|
||||
|
||||
use common\components\Helper;
|
||||
use common\models\Card;
|
||||
use common\models\Ticket;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
/** @noinspection PhpUnused */
|
||||
|
||||
class UserController extends RestController
|
||||
{
|
||||
|
||||
/**
|
||||
*/
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
public function actionLogin( )
|
||||
{
|
||||
\Yii::$app->response->setStatusCode(204);
|
||||
|
||||
$form = new LoginForm();
|
||||
|
||||
$form->load(\Yii::$app->request->post( ), '');
|
||||
|
||||
if ( $form->validate() ){
|
||||
|
||||
|
||||
/** @var Jwt $jwt */
|
||||
$jwt = Yii::$app->jwt;
|
||||
$signer = $jwt->getSigner('HS256');
|
||||
$key = $jwt->getKey();
|
||||
$time = time();
|
||||
|
||||
// Adoption for lcobucci/jwt ^4.0 version
|
||||
$token = $jwt->getBuilder()
|
||||
->issuedBy('customerapi')// Configures the issuer (iss claim)
|
||||
->permittedFor('customer')// Configures the audience (aud claim)
|
||||
->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)
|
||||
->expiresAt($time + 3600)// Configures the expiration time of the token (exp claim)
|
||||
->withClaim('uid', $form->getCustomer()->getId())// Configures a new claim, called "uid"
|
||||
->getToken($signer, $key); // Retrieves the generated token
|
||||
|
||||
return $this->asJson([
|
||||
'token' => (string)$token,
|
||||
]);
|
||||
} else {
|
||||
return $this->asJson(
|
||||
[
|
||||
'errors' => $form->getErrors()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function getOptionalActions()
|
||||
{
|
||||
return ['login'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user