From 767211d6c70690ef22e41bd73f135c61fd827f2d Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Fri, 31 Aug 2018 07:50:14 +0200 Subject: [PATCH] add rest application and discount-status rest method --- common/config/bootstrap.php | 1 + common/models/Ticket.php | 20 +-- common/models/User.php | 2 + .../m180829_155430_add_rest_user.php | 44 +++++++ docker/start.sh | 1 + environments/dev/rest/config/main-local.php | 21 +++ environments/dev/rest/config/params-local.php | 3 + environments/dev/rest/web/index-test.php | 18 +++ environments/dev/rest/web/index.php | 18 +++ environments/index.php | 9 ++ environments/prod/rest/config/main-local.php | 9 ++ .../prod/rest/config/params-local.php | 3 + environments/prod/rest/web/index.php | 18 +++ frontend/config/main.php | 16 +-- rest/assets/AppAsset.php | 30 +++++ rest/assets/config-assets.php | 37 ++++++ rest/config/.gitignore | 2 + rest/config/bootstrap.php | 1 + rest/config/main.php | 43 ++++++ rest/config/params.php | 4 + rest/controllers/CustomerController.php | 99 ++++++++++++++ rest/controllers/RestController.php | 37 ++++++ rest/controllers/SiteController.php | 124 ++++++++++++++++++ rest/runtime/.gitignore | 2 + rest/views/layouts/main.php | 84 ++++++++++++ rest/views/site/error.php | 27 ++++ rest/views/site/index.php | 14 ++ rest/views/site/login.php | 39 ++++++ rest/web/.gitignore | 2 + rest/web/css/site.css | 91 +++++++++++++ rest/web/favicon.ico | Bin 0 -> 318 bytes rest/web/profile/.gitkeep | 0 rest/web/robots.txt | 2 + 33 files changed, 805 insertions(+), 16 deletions(-) create mode 100644 console/migrations/m180829_155430_add_rest_user.php create mode 100644 environments/dev/rest/config/main-local.php create mode 100644 environments/dev/rest/config/params-local.php create mode 100644 environments/dev/rest/web/index-test.php create mode 100644 environments/dev/rest/web/index.php create mode 100644 environments/prod/rest/config/main-local.php create mode 100644 environments/prod/rest/config/params-local.php create mode 100644 environments/prod/rest/web/index.php create mode 100644 rest/assets/AppAsset.php create mode 100644 rest/assets/config-assets.php create mode 100644 rest/config/.gitignore create mode 100644 rest/config/bootstrap.php create mode 100644 rest/config/main.php create mode 100644 rest/config/params.php create mode 100644 rest/controllers/CustomerController.php create mode 100644 rest/controllers/RestController.php create mode 100644 rest/controllers/SiteController.php create mode 100644 rest/runtime/.gitignore create mode 100644 rest/views/layouts/main.php create mode 100644 rest/views/site/error.php create mode 100644 rest/views/site/index.php create mode 100644 rest/views/site/login.php create mode 100644 rest/web/.gitignore create mode 100644 rest/web/css/site.css create mode 100644 rest/web/favicon.ico create mode 100644 rest/web/profile/.gitkeep create mode 100644 rest/web/robots.txt diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index ecc13e5..94280a8 100644 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -3,3 +3,4 @@ Yii::setAlias('common', dirname(__DIR__)); Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend'); Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend'); Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console'); +Yii::setAlias('rest', dirname(dirname(__DIR__)) . '/rest'); \ No newline at end of file diff --git a/common/models/Ticket.php b/common/models/Ticket.php index 1b742d6..d564a9b 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -3,7 +3,6 @@ namespace common\models; use Yii; -use yii\db\ActiveRecord; use yii\db\Query; use yii\db\Expression; use common\components\Helper; @@ -241,18 +240,23 @@ class Ticket extends \common\models\BaseFitnessActiveRecord } - /** - * @param \common\models\Card $card the card to which we are looking for - * - * @return array|\yii\db\ActiveRecord[] - */ - public static function readActive($card){ + /** + * @param \common\models\Card $card the card to which we are looking for + * + * @param \DateTime $validOnDay on which day must be the ticket valid + * @return array|\yii\db\ActiveRecord[] + */ + public static function readActive($card, $validOnDay = null){ if ( $card == null ) return []; $query = Ticket::find(); - $today = date('Y-m-d'); + if (!isset( $validOnDay ) ){ + $today = date('Y-m-d'); + }else{ + $today = $validOnDay->format('Y-m-d'); + } $query->andWhere(['ticket.id_card' => $card->id_card]); $query->andWhere( 'ticket.start <= :today' ,[ 'today' => $today] ); diff --git a/common/models/User.php b/common/models/User.php index ce1414d..b90bb21 100644 --- a/common/models/User.php +++ b/common/models/User.php @@ -71,6 +71,8 @@ class User extends ActiveRecord implements IdentityInterface */ public static function findIdentityByAccessToken($token, $type = null) { + $authorization = base64_decode($token); + \Yii::info("findIdentityByAccessToken", $authorization); throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } diff --git a/console/migrations/m180829_155430_add_rest_user.php b/console/migrations/m180829_155430_add_rest_user.php new file mode 100644 index 0000000..f1c69f9 --- /dev/null +++ b/console/migrations/m180829_155430_add_rest_user.php @@ -0,0 +1,44 @@ +username = "discount_system"; + $user->email = "discount_system@rocho-net.hu"; + $user->setPassword("NmqFb\ivjtX1=yT*Aw5Y"); + $user->generateAuthKey(); + $user->save(); + + $role = Yii::$app->authManager->createRole('discount_system'); + Yii::$app->authManager->add($role); + Yii::$app->authManager->assign($role, $user->id); + } + + public function down() + { + echo "m180829_155430_add_rest_user cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/docker/start.sh b/docker/start.sh index 1a10592..7240894 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -9,5 +9,6 @@ docker run \ -p 86:80 \ --name fitness-web \ --hostname test.fintess_web.hu \ + --link mariadb1:mariadb1 \ -e XDEBUG_CONFIG="idekey=PHPSTORM" \ docker_fitness-ub-php-7:latest diff --git a/environments/dev/rest/config/main-local.php b/environments/dev/rest/config/main-local.php new file mode 100644 index 0000000..d9e3809 --- /dev/null +++ b/environments/dev/rest/config/main-local.php @@ -0,0 +1,21 @@ + [ + 'request' => [ + // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation + 'cookieValidationKey' => '', + ], + ], +]; + +if (!YII_ENV_TEST) { + // configuration adjustments for 'dev' environment + $config['bootstrap'][] = 'debug'; + $config['modules']['debug'] = 'yii\debug\Module'; + + $config['bootstrap'][] = 'gii'; + $config['modules']['gii'] = 'yii\gii\Module'; +} + +return $config; diff --git a/environments/dev/rest/config/params-local.php b/environments/dev/rest/config/params-local.php new file mode 100644 index 0000000..d0b9c34 --- /dev/null +++ b/environments/dev/rest/config/params-local.php @@ -0,0 +1,3 @@ +run(); diff --git a/environments/dev/rest/web/index.php b/environments/dev/rest/web/index.php new file mode 100644 index 0000000..6038167 --- /dev/null +++ b/environments/dev/rest/web/index.php @@ -0,0 +1,18 @@ +run(); diff --git a/environments/index.php b/environments/index.php index 0b7ad7c..b766657 100644 --- a/environments/index.php +++ b/environments/index.php @@ -36,6 +36,8 @@ return [ 'backend/web/assets', 'frontend/runtime', 'frontend/web/assets', + 'rest/runtime', + 'rest/web/assets', ], 'setExecutable' => [ 'yii', @@ -44,6 +46,7 @@ return [ 'setCookieValidationKey' => [ 'backend/config/main-local.php', 'frontend/config/main-local.php', + 'rest/config/main-local.php', ], ], 'Production' => [ @@ -53,6 +56,8 @@ return [ 'backend/web/assets', 'frontend/runtime', 'frontend/web/assets', + 'rest/runtime', + 'rest/web/assets', ], 'setExecutable' => [ 'yii', @@ -60,6 +65,7 @@ return [ 'setCookieValidationKey' => [ 'backend/config/main-local.php', 'frontend/config/main-local.php', + 'rest/config/main-local.php', ], ], 'rochonet' => [ @@ -69,6 +75,8 @@ return [ 'backend/web/assets', 'frontend/runtime', 'frontend/web/assets', + 'rest/runtime', + 'rest/web/assets', ], 'setExecutable' => [ 'yii', @@ -76,6 +84,7 @@ return [ 'setCookieValidationKey' => [ 'backend/config/main-local.php', 'frontend/config/main-local.php', + 'rest/config/main-local.php', ], ], ]; diff --git a/environments/prod/rest/config/main-local.php b/environments/prod/rest/config/main-local.php new file mode 100644 index 0000000..af46ba3 --- /dev/null +++ b/environments/prod/rest/config/main-local.php @@ -0,0 +1,9 @@ + [ + 'request' => [ + // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation + 'cookieValidationKey' => '', + ], + ], +]; diff --git a/environments/prod/rest/config/params-local.php b/environments/prod/rest/config/params-local.php new file mode 100644 index 0000000..d0b9c34 --- /dev/null +++ b/environments/prod/rest/config/params-local.php @@ -0,0 +1,3 @@ +run(); diff --git a/frontend/config/main.php b/frontend/config/main.php index 8d20607..6781f21 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -13,14 +13,14 @@ return [ 'bootstrap' => ['log'], 'controllerNamespace' => 'frontend\controllers', 'components' => [ - 'assetsAutoCompress' => - [ - 'class' => '\iisns\assets\AssetsCompressComponent', - 'enabled' => true, - 'jsCompress' => true, - 'cssFileCompile' => true, - 'jsFileCompile' => true, - ], +// 'assetsAutoCompress' => +// [ +// 'class' => '\iisns\assets\AssetsCompressComponent', +// 'enabled' => true, +// 'jsCompress' => true, +// 'cssFileCompile' => true, +// 'jsFileCompile' => true, +// ], 'request' => [ 'enableCsrfValidation'=>false, ], diff --git a/rest/assets/AppAsset.php b/rest/assets/AppAsset.php new file mode 100644 index 0000000..0344d6a --- /dev/null +++ b/rest/assets/AppAsset.php @@ -0,0 +1,30 @@ + + * @since 2.0 + */ +class AppAsset extends AssetBundle +{ + public $basePath = '@webroot'; + public $baseUrl = '@web'; + public $css = [ + 'css/site.css', + ]; + public $js = [ + 'js/app.js', + ]; + public $depends = [ + 'yii\web\YiiAsset', + 'yii\bootstrap\BootstrapAsset', + ]; +} diff --git a/rest/assets/config-assets.php b/rest/assets/config-assets.php new file mode 100644 index 0000000..84d80b2 --- /dev/null +++ b/rest/assets/config-assets.php @@ -0,0 +1,37 @@ + 'java -jar compiler.jar --js {from} --js_output_file {to} --warning_level QUIET', + // Adjust command/callback for CSS files compressing: + 'cssCompressor' => 'java -jar yuicompressor.jar --type css {from} -o {to}', + // The list of asset bundles to compress: + 'bundles' => [ + 'rest\assets\AppAsset', + 'yii\web\YiiAsset', + 'yii\web\JqueryAsset', + 'yii\bootstrap\BootstrapAsset', + 'yii\bootstrap\BootstrapPluginAsset' + ], + // Asset bundle for compression output: + 'targets' => [ + 'allRest' => [ + 'class' => 'rest\assets\MyAsset' , + 'basePath' => '@webroot/assets', + 'baseUrl' => '@web/assets', + 'js' => 'js/all-{hash}.js', + 'css' => 'css/all-{hash}.css', + ], + ], + // Asset manager configuration: + 'assetManager' => [ + 'basePath' => '@webroot/assets', + 'baseUrl' => '@web/assets', + ], +]; \ No newline at end of file diff --git a/rest/config/.gitignore b/rest/config/.gitignore new file mode 100644 index 0000000..20da318 --- /dev/null +++ b/rest/config/.gitignore @@ -0,0 +1,2 @@ +main-local.php +params-local.php \ No newline at end of file diff --git a/rest/config/bootstrap.php b/rest/config/bootstrap.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/rest/config/bootstrap.php @@ -0,0 +1 @@ + 'app-rest', + 'name' =>'Fitness recepció', + 'basePath' => dirname(__DIR__), + 'bootstrap' => ['log'], + 'controllerNamespace' => 'rest\controllers', + 'components' => [ + 'request' => [ + 'enableCsrfValidation'=>false, + 'csrfParam' => '_csrf-rest', + 'parsers' => [ + 'application/json' => 'yii\web\JsonParser', + ] + ], + 'user' => [ + 'identityClass' => 'common\models\User', + 'enableSession' => false, + 'enableAutoLogin' => false, + 'loginUrl' => null, + ], + 'log' => [ + 'traceLevel' => YII_DEBUG ? 3 : 0, + 'targets' => [ + [ + 'class' => 'yii\log\FileTarget', + 'levels' => ['error', 'warning'], + ], + ], + ], + 'errorHandler' => [ + 'errorAction' => 'site/error', + ], + ], + 'params' => $params, +]; diff --git a/rest/config/params.php b/rest/config/params.php new file mode 100644 index 0000000..7f754b9 --- /dev/null +++ b/rest/config/params.php @@ -0,0 +1,4 @@ + 'admin@example.com', +]; diff --git a/rest/controllers/CustomerController.php b/rest/controllers/CustomerController.php new file mode 100644 index 0000000..1609195 --- /dev/null +++ b/rest/controllers/CustomerController.php @@ -0,0 +1,99 @@ +andWhere(['or', + ['and',[ 'in','card.number' , [$number]],"trim(coalesce(card.number, '')) <>'' " ], + ['and', ['in','card.rfid_key' ,[ $number] ],"trim(coalesce(card.rfid_key, '')) <>'' "], + + ]); + + $card = $query->one(); + + if ( !isset($card)){ + throw new NotFoundHttpException("Kártya nem található"); + } + + $customer = $card->customer; + + if ( !isset($customer) ){ + throw new NotFoundHttpException("Vendég nem található"); + } + + if ( isset($lastXDays) ){ + if (!is_numeric($lastXDays)){ + throw new BadRequestHttpException("lastXDays paraméter hibás"); + } + if ( $lastXDays > 6 || $lastXDays < 1){ + throw new BadRequestHttpException("lastXDays paraméter érték hibás"); + } + } + + // check if has valid ticket today + /** @var \common\models\Card $card */ + $tickets = Ticket::readActive($card ); + $hasValidTicket = count($tickets) > 0; + + // try to find any valid ticket in the lastXDays + $minusDay = 1; + while ( !$hasValidTicket && $minusDay <= $lastXDays ){ + /** @var integer $minusDay */ + $day = $this->getDateMinusDays($minusDay); + $tickets = Ticket::readActive($card, $day ); + $hasValidTicket = count($tickets) > 0; + $minusDay = $minusDay + 1; + } + + + $result = [ + 'discount' => $hasValidTicket + ]; + + if ( isset($customer) ){ + $result['card_number'] = $card->number; + $result['name'] = $customer->name; + } + + return $result; + } + + /** + * @param $minusDays + * @return \DateTime + * @throws \Exception + */ + private function getDateMinusDays($minusDays){ + $date = new \DateTime('now'); + $date->sub(new \DateInterval('P'.$minusDays.'D')); + $date->setTime(0,0,0); + return $date; + } + +} \ No newline at end of file diff --git a/rest/controllers/RestController.php b/rest/controllers/RestController.php new file mode 100644 index 0000000..b368c2a --- /dev/null +++ b/rest/controllers/RestController.php @@ -0,0 +1,37 @@ + HttpBasicAuth::className(), + 'auth' => [$this, 'auth'] + ]; + return $behaviors; + } + + public function auth($username, $password) + { + try { + $user = User::findOne(['username' => $username]); + if ($user->validatePassword($password)) { + return $user; + } + } catch (\Exception $e) { + \Yii::error("Failed to load user: " . $e->getMessage()); + } + return null; + } + + +} \ No newline at end of file diff --git a/rest/controllers/SiteController.php b/rest/controllers/SiteController.php new file mode 100644 index 0000000..694c213 --- /dev/null +++ b/rest/controllers/SiteController.php @@ -0,0 +1,124 @@ + [ + 'class' => AccessControl::className(), + 'only' => ['logout' ], + 'rules' => [ + + [ + 'actions' => ['logout'], + 'allow' => true, + 'roles' => ['@'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'logout' => ['post'], + ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function actions() + { + return [ + 'error' => [ + 'class' => 'yii\web\ErrorAction', + ], + 'captcha' => [ + 'class' => 'yii\captcha\CaptchaAction', + 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, + ], + ]; + } + + /** + * Displays homepage. + * + * @return mixed + */ + public function actionIndex() + { + return $this->render('index'); + } + + /** + * Logs in a user. + * + * @return mixed + */ + public function actionLogin() + { + if (!\Yii::$app->user->isGuest) { + return $this->goHome(); + } + + $model = new LoginForm(); + if ($model->load(Yii::$app->request->post()) && $model->login()) { + + $geoip = Helper::getGeoIp(); + + $message = ""; + $user = User::findOne(\Yii::$app->user->id); + if ( isset($geoip)){ + $ip = isset( $geoip->ip ) ? $geoip->ip : ""; + $city = isset( $geoip->city ) ? $geoip->city : ""; + $message = "Bejelentkezés: " .$user->username. " Ip cím:". $ip . " Város: " . $city; + } + + Log::log([ + 'type' =>Log::$TYPE_LOGIN, + 'message' => $message + ]); + + return $this->redirect(['account/select']); + } else { + return $this->render('login', ['model' => $model,]); + } + } + + + + /** + * Logs out the current user. + * + * @return mixed + */ + public function actionLogout() + { + Yii::$app->user->logout(); + + return $this->goHome(); + } + + + + + +} diff --git a/rest/runtime/.gitignore b/rest/runtime/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/rest/runtime/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/rest/views/layouts/main.php b/rest/views/layouts/main.php new file mode 100644 index 0000000..4483daf --- /dev/null +++ b/rest/views/layouts/main.php @@ -0,0 +1,84 @@ + +beginPage() ?> + + + + + + + <?= Html::encode($this->title) ?> + + head() ?> + + +beginBody() ?> + +
+ 'Web Recepció', + 'brandUrl' => Yii::$app->homeUrl, + 'options' => [ + 'class' => 'navbar-inverse navbar-fixed-top', + ], + ]); + echo Nav::widget([ + 'options' => ['class' => 'navbar-nav navbar-right'], + 'items' => $items, + ]); + NavBar::end(); + ?> + +
+ isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [], + ]) ?> + + +
+
+ + + +endBody() ?> + + +endPage() ?> diff --git a/rest/views/site/error.php b/rest/views/site/error.php new file mode 100644 index 0000000..0ba2574 --- /dev/null +++ b/rest/views/site/error.php @@ -0,0 +1,27 @@ +title = $name; +?> +
+ +

title) ?>

+ +
+ +
+ +

+ The above error occurred while the Web server was processing your request. +

+

+ Please contact us if you think this is a server error. Thank you. +

+ +
diff --git a/rest/views/site/index.php b/rest/views/site/index.php new file mode 100644 index 0000000..6aa245e --- /dev/null +++ b/rest/views/site/index.php @@ -0,0 +1,14 @@ +title = 'My Yii Application'; +?> +
+ +
+

Web Recepció

+

Üdvözöljük Web Recepció oldalunkon!

+
+ +
diff --git a/rest/views/site/login.php b/rest/views/site/login.php new file mode 100644 index 0000000..9e14e0f --- /dev/null +++ b/rest/views/site/login.php @@ -0,0 +1,39 @@ +title = Yii::t('common/site' ,'Login'); +$this->params['breadcrumbs'][] = $this->title; +?> +
+

title) ?>

+ +

Please fill out the following fields to login:

+ +
+
+ 'login-form']); ?> + + field($model, 'username') ?> + + field($model, 'password')->passwordInput() ?> + + field($model, 'rememberMe')->checkbox() ?> + +
+ . +
+ +
+ 'btn btn-primary', 'name' => 'login-button']) ?> +
+ + +
+
+
diff --git a/rest/web/.gitignore b/rest/web/.gitignore new file mode 100644 index 0000000..25c74e6 --- /dev/null +++ b/rest/web/.gitignore @@ -0,0 +1,2 @@ +/index.php +/index-test.php diff --git a/rest/web/css/site.css b/rest/web/css/site.css new file mode 100644 index 0000000..698be70 --- /dev/null +++ b/rest/web/css/site.css @@ -0,0 +1,91 @@ +html, +body { + height: 100%; +} + +.wrap { + min-height: 100%; + height: auto; + margin: 0 auto -60px; + padding: 0 0 60px; +} + +.wrap > .container { + padding: 70px 15px 20px; +} + +.footer { + height: 60px; + background-color: #f5f5f5; + border-top: 1px solid #ddd; + padding-top: 20px; +} + +.jumbotron { + text-align: center; + background-color: transparent; +} + +.jumbotron .btn { + font-size: 21px; + padding: 14px 24px; +} + +.not-set { + color: #c55; + font-style: italic; +} + +/* add sorting icons to gridview sort links */ +a.asc:after, a.desc:after { + position: relative; + top: 1px; + display: inline-block; + font-family: 'Glyphicons Halflings'; + font-style: normal; + font-weight: normal; + line-height: 1; + padding-left: 5px; +} + +a.asc:after { + content: /*"\e113"*/ "\e151"; +} + +a.desc:after { + content: /*"\e114"*/ "\e152"; +} + +.sort-numerical a.asc:after { + content: "\e153"; +} + +.sort-numerical a.desc:after { + content: "\e154"; +} + +.sort-ordinal a.asc:after { + content: "\e155"; +} + +.sort-ordinal a.desc:after { + content: "\e156"; +} + +.grid-view th { + white-space: nowrap; +} + +.hint-block { + display: block; + margin-top: 5px; + color: #999; +} + +.error-summary { + color: #a94442; + background: #fdf7f7; + border-left: 3px solid #eed3d7; + padding: 10px 20px; + margin: 0 0 15px 0; +} diff --git a/rest/web/favicon.ico b/rest/web/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..580ed732e86556ec57f3f3395a210246d679c076 GIT binary patch literal 318 zcmZQzU<5(|0RbS%!l1#(z#zuJz@P!d0zj+)#2|4HXaJKC0wf0lAEr2iX{M9K3=BR0 y!E90pK{x=K$Oz&POT#sS8N$ZKhC)h8ip0_|-T#43{vnSYgXBQCu@O54$pHYIza?e> literal 0 HcmV?d00001 diff --git a/rest/web/profile/.gitkeep b/rest/web/profile/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/rest/web/robots.txt b/rest/web/robots.txt new file mode 100644 index 0000000..6f27bb6 --- /dev/null +++ b/rest/web/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: \ No newline at end of file