add customer api
This commit is contained in:
18
customerapi/components/JwtValidationData.php
Normal file
18
customerapi/components/JwtValidationData.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace customerapi\components;
|
||||
|
||||
class JwtValidationData extends \sizeg\jwt\JwtValidationData
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->validationData->setIssuer('customerapi');
|
||||
$this->validationData->setAudience('customer');
|
||||
$this->validationData->setId('A989C57D19E2AF756BA9585AC4CFAF7974AE3D2BCA7CCA7307B39AB28CC7C2C8');
|
||||
|
||||
parent::init();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
<?php
|
||||
|
||||
use customerapi\components\JwtValidationData;
|
||||
use sizeg\jwt\Jwt;
|
||||
|
||||
$params = array_merge(
|
||||
require(__DIR__ . '/../../common/config/params.php'),
|
||||
require(__DIR__ . '/../../common/config/params-local.php'),
|
||||
@@ -38,6 +42,12 @@ return [
|
||||
'errorHandler' => [
|
||||
'errorAction' => 'site/error',
|
||||
],
|
||||
'jwt' => [
|
||||
'class' => Jwt::class,
|
||||
'key' => 'secret',
|
||||
// You have to configure ValidationData informing all claims you want to validate the token.
|
||||
'jwtValidationData' => JwtValidationData::class,
|
||||
],
|
||||
],
|
||||
'params' => $params,
|
||||
];
|
||||
|
||||
@@ -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'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
13
customerapi/models/DayToDisplay.php
Normal file
13
customerapi/models/DayToDisplay.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models;
|
||||
|
||||
|
||||
class DayToDisplay
|
||||
{
|
||||
public $date;//: number; //seconds
|
||||
public $active;//: true;
|
||||
public $events;//: Event[];
|
||||
public $comment;
|
||||
}
|
||||
21
customerapi/models/EventView.php
Normal file
21
customerapi/models/EventView.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models;
|
||||
|
||||
|
||||
use yii\base\Component;
|
||||
use yii\base\Model;
|
||||
|
||||
class EventView extends Component
|
||||
{
|
||||
public $id;//: number;
|
||||
public $name;//: string;
|
||||
public $start;//: number;
|
||||
public $end;//: number;
|
||||
public $trainer;//?: Trainer;
|
||||
public $seatCount;//: number;
|
||||
public $reservationCount;//: number;
|
||||
public $eventType;//: EventType;
|
||||
public $reservedAt;//: number;
|
||||
}
|
||||
70
customerapi/models/LoginForm.php
Normal file
70
customerapi/models/LoginForm.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
namespace customerapi\models;
|
||||
|
||||
use common\models\Customer;
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* Login form
|
||||
*/
|
||||
class LoginForm extends Model
|
||||
{
|
||||
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 */
|
||||
$customer = $this->getCustomer();
|
||||
if (!$customer || !$customer->validatePassword($this->password)) {
|
||||
$this->addError($attribute, 'Incorrect username or password.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds user by [[username]]
|
||||
*
|
||||
* @return Customer|null
|
||||
*/
|
||||
public function getCustomer()
|
||||
{
|
||||
if ( $this->customer === null ){
|
||||
$this->customer = Customer::findIdentity( $this->username );
|
||||
}
|
||||
return $this->customer;
|
||||
}
|
||||
}
|
||||
58
customerapi/models/available/EventAvailable.php
Normal file
58
customerapi/models/available/EventAvailable.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\Event;
|
||||
use common\models\EventType;
|
||||
|
||||
class EventAvailable extends Event
|
||||
{
|
||||
|
||||
public $reservationCount;
|
||||
|
||||
protected function getTrainerClass()
|
||||
{
|
||||
// override trainer class to have more control
|
||||
// about json fields
|
||||
return TrainerAvailable::class;
|
||||
}
|
||||
|
||||
protected function getEventTypeClass()
|
||||
{
|
||||
return EventTypeAvailable::class;
|
||||
}
|
||||
|
||||
protected function getRoomClass()
|
||||
{
|
||||
return RoomAvailable::class;
|
||||
}
|
||||
|
||||
|
||||
function fields()
|
||||
{
|
||||
|
||||
$fields = [
|
||||
"id" => "id",
|
||||
"start" => "start",
|
||||
"end" => "end",
|
||||
"seat_count" => "seat_count",
|
||||
"active" => "active",
|
||||
// "reservationCount" => "reservationCount"
|
||||
];
|
||||
$fields['trainer'] = 'trainer';
|
||||
$fields['eventType'] = 'eventType';
|
||||
$fields['room'] = 'room';
|
||||
return $fields;
|
||||
}
|
||||
|
||||
|
||||
function extraFields()
|
||||
{
|
||||
$extra= parent::extraFields();
|
||||
$extra[] = 'trainer';
|
||||
return $extra;
|
||||
}
|
||||
|
||||
}
|
||||
56
customerapi/models/available/EventInterval.php
Normal file
56
customerapi/models/available/EventInterval.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* Class DateIntervalHelper
|
||||
* @package customerapi\models\available
|
||||
* @property \DateTime $firstActiveDate
|
||||
* @property \DateTime $lastActiveDate
|
||||
* @property \DateTime $firstDisplayDate
|
||||
* @property \DateTime $lastDisplayDate
|
||||
*/
|
||||
class EventInterval
|
||||
{
|
||||
|
||||
public $countOfActiveDays = 14;
|
||||
public $daysToDisplay = 21;
|
||||
|
||||
|
||||
public $firstActiveDate;
|
||||
public $lastActiveDate;
|
||||
public $firstDisplayDate;
|
||||
public $lastDisplayDate;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
$firstActiveDay = new DateTime();
|
||||
$firstActiveDay->setTime(0, 0);
|
||||
$this->firstActiveDate = $firstActiveDay;
|
||||
|
||||
$lastActiveDay = new DateTime();
|
||||
$lastActiveDay->setTime(0, 0);
|
||||
$lastActiveDay->modify('+' . $this->countOfActiveDays . ' day');
|
||||
$this->lastActiveDate = $lastActiveDay;
|
||||
|
||||
$firstDisplayDate = new DateTime();
|
||||
$firstDisplayDate->modify('this week');
|
||||
$firstDisplayDate->setTime(0, 0);
|
||||
$this->firstDisplayDate = $firstDisplayDate;
|
||||
|
||||
$lastDisplayDate = clone $firstDisplayDate;
|
||||
$lastDisplayDate->setTime(0, 0);
|
||||
$lastDisplayDate->modify('+' . $this->daysToDisplay . ' day');
|
||||
$this->lastDisplayDate = $lastDisplayDate;
|
||||
|
||||
}
|
||||
|
||||
public static function createInterval(){
|
||||
return new EventInterval();
|
||||
}
|
||||
}
|
||||
13
customerapi/models/available/EventRegistrationAvailable.php
Normal file
13
customerapi/models/available/EventRegistrationAvailable.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\EventRegistration;
|
||||
|
||||
class EventRegistrationAvailable extends EventRegistration
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
20
customerapi/models/available/EventTypeAvailable.php
Normal file
20
customerapi/models/available/EventTypeAvailable.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\EventType;
|
||||
|
||||
class EventTypeAvailable extends EventType
|
||||
{
|
||||
|
||||
function fields()
|
||||
{
|
||||
return [
|
||||
'id' => 'id',
|
||||
'name' => 'name'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
19
customerapi/models/available/RoomAvailable.php
Normal file
19
customerapi/models/available/RoomAvailable.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\Room;
|
||||
|
||||
class RoomAvailable extends Room
|
||||
{
|
||||
function fields()
|
||||
{
|
||||
return [
|
||||
'id' => 'id',
|
||||
'name' => 'name',
|
||||
'seat_count' => 'seat_count'
|
||||
];
|
||||
}
|
||||
}
|
||||
20
customerapi/models/available/TrainerAvailable.php
Normal file
20
customerapi/models/available/TrainerAvailable.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace customerapi\models\available;
|
||||
|
||||
|
||||
use common\models\Trainer;
|
||||
|
||||
class TrainerAvailable extends Trainer
|
||||
{
|
||||
|
||||
public function fields()
|
||||
{
|
||||
return [
|
||||
'id' => 'id',
|
||||
'name' => 'name'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
0
customerapi/web/assets/.gitkeep
Normal file
0
customerapi/web/assets/.gitkeep
Normal file
Reference in New Issue
Block a user