From 8ec8e762256300f8d425475314bf8553da9feafc Mon Sep 17 00:00:00 2001 From: t4rest Date: Tue, 11 Jul 2017 00:45:25 +0300 Subject: [PATCH] test --- Procfile | 2 +- backend/config/main.php | 5 +- backend/config/modules.php | 7 + backend/controllers/AuthController.php | 34 +++++ backend/modules/main/Module.php | 15 +++ .../main/controllers/SiteController.php | 12 ++ backend/modules/main/models/ContactForm.php | 60 +++++++++ .../{ => modules/main}/views/site/error.php | 0 .../modules/main}/views/site/index.php | 0 .../controllers/PoolChoiceController.php | 0 .../pool}/controllers/PoolController.php | 0 .../controllers/PoolUserChoiceController.php | 0 .../pool}/controllers/SiteController.php | 0 backend/{ => modules/pool}/models/.gitkeep | 0 .../pool}/models/PoolChoiceSearch.php | 0 .../{ => modules/pool}/models/PoolSearch.php | 0 .../pool}/models/PoolUserChoiceSearch.php | 0 .../{ => modules/pool}/views/pool/_form.php | 0 .../{ => modules/pool}/views/pool/_search.php | 0 .../{ => modules/pool}/views/pool/create.php | 0 .../{ => modules/pool}/views/pool/index.php | 0 .../{ => modules/pool}/views/pool/update.php | 0 .../{ => modules/pool}/views/pool/view.php | 0 .../pool}/views/pool_choice/_form.php | 0 .../pool}/views/pool_choice/_search.php | 0 .../pool}/views/pool_choice/create.php | 0 .../pool}/views/pool_choice/index.php | 0 .../pool}/views/pool_choice/update.php | 0 .../pool}/views/pool_choice/view.php | 0 .../pool}/views/pool_use_choice/_form.php | 0 .../pool}/views/pool_use_choice/_search.php | 0 .../pool}/views/pool_use_choice/create.php | 0 .../pool}/views/pool_use_choice/index.php | 0 .../pool}/views/pool_use_choice/update.php | 0 .../pool}/views/pool_use_choice/view.php | 0 backend/modules/user/Module.php | 16 +++ backend/modules/user/api/User.php | 23 ++++ .../user/controllers/UserController.php | 20 +++ .../modules/user/datatypes/UserStructure.php | 47 +++++++ backend/views/site/index.php | 17 --- common/clients/AuthAction.php | 22 +++ common/clients/StateStorage.php | 1 + common/clients/TokenStorage.php | 1 + common/clients/Twitter.php | 2 +- common/clients/views/redirect.php | 33 +++++ common/components/AuthHandler.php | 13 +- common/datatypes/Structure.php | 7 + common/exceptions/AccessException.php | 17 +++ common/exceptions/ApisException.php | 126 ++++++++++++++++++ common/exceptions/AuthorizationException.php | 17 +++ common/exceptions/DatabaseException.php | 38 ++++++ common/exceptions/FilesException.php | 25 ++++ common/exceptions/RequestException.php | 35 +++++ common/exceptions/codes/Access.php | 67 ++++++++++ common/exceptions/codes/BaseCodes.php | 66 +++++++++ common/exceptions/codes/Database.php | 25 ++++ common/exceptions/codes/Files.php | 46 +++++++ common/exceptions/codes/Request.php | 54 ++++++++ frontend/config/main.php | 16 +-- frontend/config/modules.php | 10 ++ frontend/config/urlManager.php | 34 +++++ frontend/modules/api/Module.php | 15 +++ .../modules/api/components/MainController.php | 11 ++ .../api/controllers/UserController.php | 38 ++++++ frontend/modules/main/Module.php | 15 +++ .../main/components/MainController.php | 14 ++ .../main}/controllers/SiteController.php | 13 +- .../{ => modules/main}/models/ContactForm.php | 0 .../{ => modules/main}/views/site/about.php | 0 .../{ => modules/main}/views/site/connect.php | 0 .../{ => modules/main}/views/site/contact.php | 0 .../{ => modules/main}/views/site/error.php | 0 frontend/modules/main/views/site/index.php | 14 ++ frontend/views/layouts/main.php | 6 +- heroku/nginx.conf | 4 + .../post-install-cmd.sh | 0 nginx.conf | 9 -- 77 files changed, 990 insertions(+), 62 deletions(-) create mode 100755 backend/config/modules.php create mode 100644 backend/controllers/AuthController.php create mode 100644 backend/modules/main/Module.php create mode 100644 backend/modules/main/controllers/SiteController.php create mode 100644 backend/modules/main/models/ContactForm.php rename backend/{ => modules/main}/views/site/error.php (100%) mode change 100755 => 100644 rename {frontend => backend/modules/main}/views/site/index.php (100%) rename backend/{ => modules/pool}/controllers/PoolChoiceController.php (100%) rename backend/{ => modules/pool}/controllers/PoolController.php (100%) rename backend/{ => modules/pool}/controllers/PoolUserChoiceController.php (100%) rename backend/{ => modules/pool}/controllers/SiteController.php (100%) rename backend/{ => modules/pool}/models/.gitkeep (100%) rename backend/{ => modules/pool}/models/PoolChoiceSearch.php (100%) rename backend/{ => modules/pool}/models/PoolSearch.php (100%) rename backend/{ => modules/pool}/models/PoolUserChoiceSearch.php (100%) rename backend/{ => modules/pool}/views/pool/_form.php (100%) rename backend/{ => modules/pool}/views/pool/_search.php (100%) rename backend/{ => modules/pool}/views/pool/create.php (100%) rename backend/{ => modules/pool}/views/pool/index.php (100%) rename backend/{ => modules/pool}/views/pool/update.php (100%) rename backend/{ => modules/pool}/views/pool/view.php (100%) rename backend/{ => modules/pool}/views/pool_choice/_form.php (100%) rename backend/{ => modules/pool}/views/pool_choice/_search.php (100%) rename backend/{ => modules/pool}/views/pool_choice/create.php (100%) rename backend/{ => modules/pool}/views/pool_choice/index.php (100%) rename backend/{ => modules/pool}/views/pool_choice/update.php (100%) rename backend/{ => modules/pool}/views/pool_choice/view.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/_form.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/_search.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/create.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/index.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/update.php (100%) rename backend/{ => modules/pool}/views/pool_use_choice/view.php (100%) create mode 100644 backend/modules/user/Module.php create mode 100644 backend/modules/user/api/User.php create mode 100644 backend/modules/user/controllers/UserController.php create mode 100644 backend/modules/user/datatypes/UserStructure.php delete mode 100755 backend/views/site/index.php create mode 100644 common/clients/AuthAction.php create mode 100644 common/clients/views/redirect.php create mode 100644 common/datatypes/Structure.php create mode 100644 common/exceptions/AccessException.php create mode 100644 common/exceptions/ApisException.php create mode 100644 common/exceptions/AuthorizationException.php create mode 100644 common/exceptions/DatabaseException.php create mode 100644 common/exceptions/FilesException.php create mode 100644 common/exceptions/RequestException.php create mode 100644 common/exceptions/codes/Access.php create mode 100644 common/exceptions/codes/BaseCodes.php create mode 100644 common/exceptions/codes/Database.php create mode 100644 common/exceptions/codes/Files.php create mode 100644 common/exceptions/codes/Request.php create mode 100755 frontend/config/modules.php create mode 100644 frontend/config/urlManager.php create mode 100644 frontend/modules/api/Module.php create mode 100755 frontend/modules/api/components/MainController.php create mode 100644 frontend/modules/api/controllers/UserController.php create mode 100644 frontend/modules/main/Module.php create mode 100755 frontend/modules/main/components/MainController.php rename frontend/{ => modules/main}/controllers/SiteController.php (90%) rename frontend/{ => modules/main}/models/ContactForm.php (100%) rename frontend/{ => modules/main}/views/site/about.php (100%) rename frontend/{ => modules/main}/views/site/connect.php (100%) rename frontend/{ => modules/main}/views/site/contact.php (100%) rename frontend/{ => modules/main}/views/site/error.php (100%) create mode 100644 frontend/modules/main/views/site/index.php create mode 100644 heroku/nginx.conf rename post-install-cmd.sh => heroku/post-install-cmd.sh (100%) delete mode 100644 nginx.conf diff --git a/Procfile b/Procfile index 4869eb6..74bcedb 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: vendor/bin/heroku-php-nginx -C nginx.conf frontend/web +web: vendor/bin/heroku-php-nginx -C heroku/nginx.conf frontend/web diff --git a/backend/config/main.php b/backend/config/main.php index 06b44c7..df94e73 100644 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -9,9 +9,8 @@ return [ 'id' => 'app-backend', 'basePath' => dirname(__DIR__), - 'controllerNamespace' => 'backend\controllers', - 'bootstrap' => ['log'], - 'modules' => [], + 'defaultRoute' => 'main/default/index', + 'modules' => require(__DIR__ . '/modules.php'), 'bootstrap' => ['log'], 'components' => [ 'request' => [ 'csrfParam' => '_csrf-backend', diff --git a/backend/config/modules.php b/backend/config/modules.php new file mode 100755 index 0000000..73a82c0 --- /dev/null +++ b/backend/config/modules.php @@ -0,0 +1,7 @@ + [ + 'class' => 'backend\modules\main\Module', + ], +]; \ No newline at end of file diff --git a/backend/controllers/AuthController.php b/backend/controllers/AuthController.php new file mode 100644 index 0000000..cc0a380 --- /dev/null +++ b/backend/controllers/AuthController.php @@ -0,0 +1,34 @@ +response->format = \yii\web\Response::FORMAT_JSON; + } + + /** + * @param yii\base\Action $action + * @return bool + * @throws exceptions\AuthorizationException + * @throws yii\web\BadRequestHttpException + */ + public function beforeAction($action) + { + $token = Yii::$app->request->getQueryParam('oauth_token'); + + + if (!Yii::$app->user->loginByAccessToken($token)) { + throw exceptions\AuthorizationException::invalidToken(); + } + + return parent::beforeAction($action); + } +} diff --git a/backend/modules/main/Module.php b/backend/modules/main/Module.php new file mode 100644 index 0000000..6983402 --- /dev/null +++ b/backend/modules/main/Module.php @@ -0,0 +1,15 @@ +render('index'); + } +} diff --git a/backend/modules/main/models/ContactForm.php b/backend/modules/main/models/ContactForm.php new file mode 100644 index 0000000..b5f5975 --- /dev/null +++ b/backend/modules/main/models/ContactForm.php @@ -0,0 +1,60 @@ + 'Verification Code', + ]; + } + + /** + * Sends an email to the specified email address using the information collected by this model. + * + * @param string $email the target email address + * @return bool whether the email was sent + */ + public function sendEmail($email) + { + return Yii::$app->mailer->compose() + ->setTo($email) + ->setFrom([$this->email => $this->name]) + ->setSubject($this->subject) + ->setTextBody($this->body) + ->send(); + } +} diff --git a/backend/views/site/error.php b/backend/modules/main/views/site/error.php old mode 100755 new mode 100644 similarity index 100% rename from backend/views/site/error.php rename to backend/modules/main/views/site/error.php diff --git a/frontend/views/site/index.php b/backend/modules/main/views/site/index.php similarity index 100% rename from frontend/views/site/index.php rename to backend/modules/main/views/site/index.php diff --git a/backend/controllers/PoolChoiceController.php b/backend/modules/pool/controllers/PoolChoiceController.php similarity index 100% rename from backend/controllers/PoolChoiceController.php rename to backend/modules/pool/controllers/PoolChoiceController.php diff --git a/backend/controllers/PoolController.php b/backend/modules/pool/controllers/PoolController.php similarity index 100% rename from backend/controllers/PoolController.php rename to backend/modules/pool/controllers/PoolController.php diff --git a/backend/controllers/PoolUserChoiceController.php b/backend/modules/pool/controllers/PoolUserChoiceController.php similarity index 100% rename from backend/controllers/PoolUserChoiceController.php rename to backend/modules/pool/controllers/PoolUserChoiceController.php diff --git a/backend/controllers/SiteController.php b/backend/modules/pool/controllers/SiteController.php similarity index 100% rename from backend/controllers/SiteController.php rename to backend/modules/pool/controllers/SiteController.php diff --git a/backend/models/.gitkeep b/backend/modules/pool/models/.gitkeep similarity index 100% rename from backend/models/.gitkeep rename to backend/modules/pool/models/.gitkeep diff --git a/backend/models/PoolChoiceSearch.php b/backend/modules/pool/models/PoolChoiceSearch.php similarity index 100% rename from backend/models/PoolChoiceSearch.php rename to backend/modules/pool/models/PoolChoiceSearch.php diff --git a/backend/models/PoolSearch.php b/backend/modules/pool/models/PoolSearch.php similarity index 100% rename from backend/models/PoolSearch.php rename to backend/modules/pool/models/PoolSearch.php diff --git a/backend/models/PoolUserChoiceSearch.php b/backend/modules/pool/models/PoolUserChoiceSearch.php similarity index 100% rename from backend/models/PoolUserChoiceSearch.php rename to backend/modules/pool/models/PoolUserChoiceSearch.php diff --git a/backend/views/pool/_form.php b/backend/modules/pool/views/pool/_form.php similarity index 100% rename from backend/views/pool/_form.php rename to backend/modules/pool/views/pool/_form.php diff --git a/backend/views/pool/_search.php b/backend/modules/pool/views/pool/_search.php similarity index 100% rename from backend/views/pool/_search.php rename to backend/modules/pool/views/pool/_search.php diff --git a/backend/views/pool/create.php b/backend/modules/pool/views/pool/create.php similarity index 100% rename from backend/views/pool/create.php rename to backend/modules/pool/views/pool/create.php diff --git a/backend/views/pool/index.php b/backend/modules/pool/views/pool/index.php similarity index 100% rename from backend/views/pool/index.php rename to backend/modules/pool/views/pool/index.php diff --git a/backend/views/pool/update.php b/backend/modules/pool/views/pool/update.php similarity index 100% rename from backend/views/pool/update.php rename to backend/modules/pool/views/pool/update.php diff --git a/backend/views/pool/view.php b/backend/modules/pool/views/pool/view.php similarity index 100% rename from backend/views/pool/view.php rename to backend/modules/pool/views/pool/view.php diff --git a/backend/views/pool_choice/_form.php b/backend/modules/pool/views/pool_choice/_form.php similarity index 100% rename from backend/views/pool_choice/_form.php rename to backend/modules/pool/views/pool_choice/_form.php diff --git a/backend/views/pool_choice/_search.php b/backend/modules/pool/views/pool_choice/_search.php similarity index 100% rename from backend/views/pool_choice/_search.php rename to backend/modules/pool/views/pool_choice/_search.php diff --git a/backend/views/pool_choice/create.php b/backend/modules/pool/views/pool_choice/create.php similarity index 100% rename from backend/views/pool_choice/create.php rename to backend/modules/pool/views/pool_choice/create.php diff --git a/backend/views/pool_choice/index.php b/backend/modules/pool/views/pool_choice/index.php similarity index 100% rename from backend/views/pool_choice/index.php rename to backend/modules/pool/views/pool_choice/index.php diff --git a/backend/views/pool_choice/update.php b/backend/modules/pool/views/pool_choice/update.php similarity index 100% rename from backend/views/pool_choice/update.php rename to backend/modules/pool/views/pool_choice/update.php diff --git a/backend/views/pool_choice/view.php b/backend/modules/pool/views/pool_choice/view.php similarity index 100% rename from backend/views/pool_choice/view.php rename to backend/modules/pool/views/pool_choice/view.php diff --git a/backend/views/pool_use_choice/_form.php b/backend/modules/pool/views/pool_use_choice/_form.php similarity index 100% rename from backend/views/pool_use_choice/_form.php rename to backend/modules/pool/views/pool_use_choice/_form.php diff --git a/backend/views/pool_use_choice/_search.php b/backend/modules/pool/views/pool_use_choice/_search.php similarity index 100% rename from backend/views/pool_use_choice/_search.php rename to backend/modules/pool/views/pool_use_choice/_search.php diff --git a/backend/views/pool_use_choice/create.php b/backend/modules/pool/views/pool_use_choice/create.php similarity index 100% rename from backend/views/pool_use_choice/create.php rename to backend/modules/pool/views/pool_use_choice/create.php diff --git a/backend/views/pool_use_choice/index.php b/backend/modules/pool/views/pool_use_choice/index.php similarity index 100% rename from backend/views/pool_use_choice/index.php rename to backend/modules/pool/views/pool_use_choice/index.php diff --git a/backend/views/pool_use_choice/update.php b/backend/modules/pool/views/pool_use_choice/update.php similarity index 100% rename from backend/views/pool_use_choice/update.php rename to backend/modules/pool/views/pool_use_choice/update.php diff --git a/backend/views/pool_use_choice/view.php b/backend/modules/pool/views/pool_use_choice/view.php similarity index 100% rename from backend/views/pool_use_choice/view.php rename to backend/modules/pool/views/pool_use_choice/view.php diff --git a/backend/modules/user/Module.php b/backend/modules/user/Module.php new file mode 100644 index 0000000..6564c9a --- /dev/null +++ b/backend/modules/user/Module.php @@ -0,0 +1,16 @@ +user->getIdentity() + ); + + return $user->serialize(); + } +} \ No newline at end of file diff --git a/backend/modules/user/controllers/UserController.php b/backend/modules/user/controllers/UserController.php new file mode 100644 index 0000000..30c524f --- /dev/null +++ b/backend/modules/user/controllers/UserController.php @@ -0,0 +1,20 @@ +api = new UserApi(); + } + return true; + } +} \ No newline at end of file diff --git a/backend/modules/user/datatypes/UserStructure.php b/backend/modules/user/datatypes/UserStructure.php new file mode 100644 index 0000000..bf4d7b9 --- /dev/null +++ b/backend/modules/user/datatypes/UserStructure.php @@ -0,0 +1,47 @@ +user = $user; + } + + public function serialize(): array + { + $user = [ + 'id' => $this->user->id, + 'email' => $this->user->email, + 'username' => $this->user->username, + 'first_name' => $this->user->first_name, + 'last_name' => $this->user->last_name, + 'photo_url' => $this->user->photo_url, + 'timezone' => $this->user->timezone, + 'locale' => $this->user->locale, + 'created_at' => $this->user->created_at, + 'updated_at' => $this->user->updated_at, + ]; + +// if($) + + return $user; + } +} + + + + + + + + diff --git a/backend/views/site/index.php b/backend/views/site/index.php deleted file mode 100755 index 5e5cdd8..0000000 --- a/backend/views/site/index.php +++ /dev/null @@ -1,17 +0,0 @@ -title = 'My Yii Application'; -?> -
- -
-

Congratulations!

- -

You have successfully created your Yii-powered application.

- -

Get started with Yii

-
- -
diff --git a/common/clients/AuthAction.php b/common/clients/AuthAction.php new file mode 100644 index 0000000..fed180e --- /dev/null +++ b/common/clients/AuthAction.php @@ -0,0 +1,22 @@ + Yii::$app->session->get('auth_token'), + 'url' => $url, + 'enforceRedirect' => $enforceRedirect, + ]; + $response = Yii::$app->getResponse(); + $response->content = Yii::$app->getView()->renderFile($viewFile, $viewData); + return $response; + } +} diff --git a/common/clients/StateStorage.php b/common/clients/StateStorage.php index 3f6ff48..db405bb 100644 --- a/common/clients/StateStorage.php +++ b/common/clients/StateStorage.php @@ -1,4 +1,5 @@ + + + + + + + +
+ + + + diff --git a/common/components/AuthHandler.php b/common/components/AuthHandler.php index 788e920..ee50dab 100644 --- a/common/components/AuthHandler.php +++ b/common/components/AuthHandler.php @@ -60,17 +60,16 @@ public function handle() Yii::$app->user->login($user); - } else { - - if ($authNetwork && $authNetwork->user_id != Yii::$app->user->id) { - throw new Exception(Yii::t('app', 'Unable to link {client} account. There is another user using it.', ['client' => $this->client->getTitle()])); - } + } else if ($authNetwork && $authNetwork->user_id != Yii::$app->user->id) { + throw new Exception(Yii::t('app', 'Unable to link {client} account. There is another user using it.', ['client' => $this->client->getTitle()])); } $this->saveAuthClient($authNetwork, $attributes); - return 123; -// return Yii::$app->user->getAuthKey(); + Yii::$app->session->set( + 'auth_token', + Yii::$app->user->getIdentity()->getAuthKey() + ); } /** diff --git a/common/datatypes/Structure.php b/common/datatypes/Structure.php new file mode 100644 index 0000000..2f5d3da --- /dev/null +++ b/common/datatypes/Structure.php @@ -0,0 +1,7 @@ +validateCodeGroup($code)) { + throw new Exception('Passed code does not match exception type'); + } + + parent::__construct($status, $message, $code); + } + + /** + * Validates code group. + * Returns false if passed code does not match exception's code group - i.e. + * passed code does not match current exception type. + * + * @param int $code . Code to validate + * @return bool + */ + protected static function validateCodeGroup(int $code): bool + { + $codeGroup = str_split($code, strlen((string)static::CODES_GROUP)); + + return isset($codeGroup[0]) && $codeGroup[0] == static::CODES_GROUP; + } + + /** + * @return object + * @throws Exception + */ + private static function _createCodeBaseInstance(): object + { + $codesBaseClassName = static::CODES_BASE_CLASS; + if (empty($codesBaseClassName)) { + throw new Exception('CODES_BASE_CLASS must be defined'); + } + + $reflection = new \ReflectionClass($codesBaseClassName); + + return $reflection->newInstance(); + } + + /** + * Returns codes base instance + * + * @return BaseCodes|null + */ + private static function _getCodeBaseInstance() + { + if (empty(self::$_CodeBaseInstance)) { + self::$_CodeBaseInstance = self::_createCodeBaseInstance(); + } + + return self::$_CodeBaseInstance; + } + + /** + * @param $code + * @param array $options + * @return ApisException|static + * @throws Exception + */ + public static function create($code, $options = []): ApisException + { + if (!self::validateCodeGroup($code)) { + throw new Exception('Passed code does not match exception type'); + } + + $codeBase = self::_getCodeBaseInstance(); + $params = $codeBase->getInformation($code, $options); + + return new static($params['statusCode'], $params['message'], $code); + } +} \ No newline at end of file diff --git a/common/exceptions/AuthorizationException.php b/common/exceptions/AuthorizationException.php new file mode 100644 index 0000000..979816a --- /dev/null +++ b/common/exceptions/AuthorizationException.php @@ -0,0 +1,17 @@ + $param]); + } + + /** + * @param null $message + * @return RequestException + * @throws \yii\base\Exception + */ + public static function invalidRequest($message = null) : RequestException + { + $params = []; + if (isset($message)) { + $params['message'] = $message; + } + + return self::create(Request::INVALID_REQUEST, $params); + } +} \ No newline at end of file diff --git a/common/exceptions/codes/Access.php b/common/exceptions/codes/Access.php new file mode 100644 index 0000000..e4ed775 --- /dev/null +++ b/common/exceptions/codes/Access.php @@ -0,0 +1,67 @@ + [ + 'statusCode' => 403, + 'message' => 'You do not have permission to access this resource' + ], + self::AUTHORIZATION_ERROR => [ + 'statusCode' => 401, + 'message' => 'Undefined authorization error' + ], + self::WRONG_USERNAME_OR_PASSWORD => [ + 'statusCode' => 401, + 'message' => 'Wrong username or password' + ], + self::NOT_CONFIRMED_EMAIL => [ + 'statusCode' => 401, + 'message' => 'Email is not confirmed' + ], + self::DISABLED_ACCOUNT => [ + 'statusCode' => 401, + 'message' => 'Account is disabled' + ], + self::TOKEN_NOT_FOUND => [ + 'statusCode' => 401, + 'message' => 'TOTP token was not send' + ], + self::INVALID_TOKEN => [ + 'statusCode' => 401, + 'message' => 'Invalid token' + ], + self::EXPIRED_SUAT => [ + 'statusCode' => 401, + 'message' => 'Expired suat' + ], + self::ARGUS_USER_NOT_FOUND => [ + 'statusCode' => 401, + 'message' => 'Can not find argus user identity. Tokens pair suid/suat are invalid' + ], + self::DISABLED_MODULE => [ + 'statusCode' => 403, + 'message' => 'You can not access this endpoint. Module was disabled by user' + ] + ]; + } +} diff --git a/common/exceptions/codes/BaseCodes.php b/common/exceptions/codes/BaseCodes.php new file mode 100644 index 0000000..10bae73 --- /dev/null +++ b/common/exceptions/codes/BaseCodes.php @@ -0,0 +1,66 @@ +library(); + if (!isset($library[$code])) { + return $this->defaultInformation(); + } + + return $this->prepare($library[$code], $options); + } + + /** + * Prepare code information + * @param array $info + * @param array $options + * @return array + */ + protected function prepare(array $info, array $options = array()) : array + { + $statusCode = $info['statusCode']; + if (isset($options['statusCode'])) { + $statusCode = $options['statusCode']; + unset($options['statusCode']); + } + + $message = $info['message']; + if (isset($options['message'])) { + $message = $options['message']; + unset($options['message']); + } + + foreach ($options as $key => $value) { + $message = str_replace('{' . $key . '}', $value, $message); + } + + return [ + 'statusCode' => $statusCode, + 'message' => $message + ]; + } + + /** + * @return array + */ + protected function defaultInformation() : array + { + return [ + 'statusCode' => 500, + 'message' => 'Internal server error' + ]; + } +} \ No newline at end of file diff --git a/common/exceptions/codes/Database.php b/common/exceptions/codes/Database.php new file mode 100644 index 0000000..4f67502 --- /dev/null +++ b/common/exceptions/codes/Database.php @@ -0,0 +1,25 @@ + [ + 'statusCode' => 500, + 'message' => 'Failed to perform operation on record' + ], + self::RECORD_NOT_FOUND => [ + 'statusCode' => 500, + 'message' => 'Record you are trying to access does not exist' + ] + ]; + } +} diff --git a/common/exceptions/codes/Files.php b/common/exceptions/codes/Files.php new file mode 100644 index 0000000..fa4dcfb --- /dev/null +++ b/common/exceptions/codes/Files.php @@ -0,0 +1,46 @@ + [ + 'statusCode' => 500, + 'message' => 'File [{filename}] is not exists' + ], + self::INCORRECT_FILETYPE => [ + 'statusCode' => 500, + 'message' => 'Uploading file must be an image' + ], + self::FILE_REQUIRED => [ + 'statusCode' => 500, + 'message' => 'Uploading file must be set necessarily' + ], + self::IS_NOT_IMAGE => [ + 'statusCode' => 500, + 'message' => 'File is not image' + ], + self::INVALID_FILESYSTEM_RESPONSE => [ + 'statusCode' => 500, + 'message' => 'GridFs driver response is incorrect' + ], + self::UNSUPPORTED_IMAGE_TYPE => [ + 'statusCode' => 500, + 'message' => 'Unsupported image type' + ] + ]; + } +} diff --git a/common/exceptions/codes/Request.php b/common/exceptions/codes/Request.php new file mode 100644 index 0000000..b1ea3b2 --- /dev/null +++ b/common/exceptions/codes/Request.php @@ -0,0 +1,54 @@ + [ + 'statusCode' => 400, + 'message' => 'Invalid request parameters' + ], + self::INVALID_PARAM => [ + 'statusCode' => 400, + 'message' => 'Invalid parameter [{parameter}]' + ], + self::MISSING_PARAM => [ + 'statusCode' => 400, + 'message' => 'Parameter {type}[{parameter}] was not received' + ], + self::INVALID_PARAM_TYPE => [ + 'statusCode' => 400, + 'message' => 'Invalid parameter type' + ], + self::INVALID_PARAM_VALUE => [ + 'statusCode' => 400, + 'message' => 'Invalid parameter value' + ], + self::UNSUPPORTED_REQUEST => [ + 'statusCode' => 404, + 'message' => 'Unsupported {type} request' + ], + self::INVALID_REQUEST => [ + 'statusCode' => 400, + 'message' => 'Invalid request. Request could not be executed' + ] + ]; + } +} diff --git a/frontend/config/main.php b/frontend/config/main.php index 7cdf778..41dc0cd 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -10,8 +10,8 @@ 'id' => 'app-frontend', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], - 'controllerNamespace' => 'frontend\controllers', - 'defaultRoute' => 'site/index', + 'defaultRoute' => 'main/default/index', + 'modules' => require(__DIR__ . '/modules.php'), 'components' => [ 'request' => [ 'csrfParam' => '_csrf-frontend', @@ -37,17 +37,7 @@ 'errorHandler' => [ 'errorAction' => 'site/error', ], - - 'urlManager' => [ - 'enablePrettyUrl' => true, - 'showScriptName' => false, - 'rules' => [ - /** - * base url rules - */ - '<_c:[\w\-]+>/<_a:[\w\-]+>/' => '<_m>/<_c>/<_a>', - ], - ], + 'urlManager' => require(__DIR__ . '/urlManager.php'), ], 'params' => $params, ]; diff --git a/frontend/config/modules.php b/frontend/config/modules.php new file mode 100755 index 0000000..2136db5 --- /dev/null +++ b/frontend/config/modules.php @@ -0,0 +1,10 @@ + [ + 'class' => 'frontend\modules\main\Module', + ], + 'api' => [ + 'class' => 'frontend\modules\api\Module', + ], +]; \ No newline at end of file diff --git a/frontend/config/urlManager.php b/frontend/config/urlManager.php new file mode 100644 index 0000000..97c84e8 --- /dev/null +++ b/frontend/config/urlManager.php @@ -0,0 +1,34 @@ + true, + 'showScriptName' => false, + 'rules' => [ + + + /** + * main page + */ + '' => 'main/site/index', + + /** + * error action rules + */ + 'error' => 'main/site/error', + + + /** + * site controller + */ + 'site/<_a:[\w\-]+>' => 'main/site/<_a>', + + + ['pattern' => 'api/user', 'verb' => 'GET', 'route' => 'api/user/index'], + ['pattern' => 'api/user', 'verb' => 'PUT', 'route' => 'api/user/update'], + + + /** + * base url rules + */ + '<_m:[\w\-]+>/<_c:[\w\-]+>/<_a:[\w\-]+>/' => '<_m>/<_c>/<_a>', + ], +]; \ No newline at end of file diff --git a/frontend/modules/api/Module.php b/frontend/modules/api/Module.php new file mode 100644 index 0000000..e9dc89d --- /dev/null +++ b/frontend/modules/api/Module.php @@ -0,0 +1,15 @@ +api = new User(); + } + return true; + } + + /** + * @return array + */ + public function actionIndex() + { + return $this->api->info(); + } + + public function actionUpdate() + { + return $this->api->info(); + } + + +} diff --git a/frontend/modules/main/Module.php b/frontend/modules/main/Module.php new file mode 100644 index 0000000..0537c29 --- /dev/null +++ b/frontend/modules/main/Module.php @@ -0,0 +1,15 @@ + YII_ENV_TEST ? 'testme' : null, ], 'auth' => [ - 'class' => 'yii\authclient\AuthAction', + 'class' => 'common\clients\AuthAction', 'successCallback' => [$this, 'onAuthSuccess'], ], ]; @@ -67,9 +66,7 @@ public function actions() public function onAuthSuccess($client) { - $token = (new AuthHandler($client))->handle(); - - return $this->render('connect', ['token' => $token]); + (new AuthHandler($client))->handle(); } /** diff --git a/frontend/models/ContactForm.php b/frontend/modules/main/models/ContactForm.php similarity index 100% rename from frontend/models/ContactForm.php rename to frontend/modules/main/models/ContactForm.php diff --git a/frontend/views/site/about.php b/frontend/modules/main/views/site/about.php similarity index 100% rename from frontend/views/site/about.php rename to frontend/modules/main/views/site/about.php diff --git a/frontend/views/site/connect.php b/frontend/modules/main/views/site/connect.php similarity index 100% rename from frontend/views/site/connect.php rename to frontend/modules/main/views/site/connect.php diff --git a/frontend/views/site/contact.php b/frontend/modules/main/views/site/contact.php similarity index 100% rename from frontend/views/site/contact.php rename to frontend/modules/main/views/site/contact.php diff --git a/frontend/views/site/error.php b/frontend/modules/main/views/site/error.php similarity index 100% rename from frontend/views/site/error.php rename to frontend/modules/main/views/site/error.php diff --git a/frontend/modules/main/views/site/index.php b/frontend/modules/main/views/site/index.php new file mode 100644 index 0000000..1a86c82 --- /dev/null +++ b/frontend/modules/main/views/site/index.php @@ -0,0 +1,14 @@ +title = 'My Yii Application'; +?> +
+ + ['site/auth'], + 'popupMode' => true, + ]) ?> + +
diff --git a/frontend/views/layouts/main.php b/frontend/views/layouts/main.php index 01c0635..c7a845b 100644 --- a/frontend/views/layouts/main.php +++ b/frontend/views/layouts/main.php @@ -36,8 +36,8 @@ ], ]); $menuItems = [ - ['label' => 'Home', 'url' => ['/site/index']], - ['label' => 'About', 'url' => ['/site/about']], + ['label' => 'Home', 'url' => ['/main/site/index']], + ['label' => 'About', 'url' => ['/main/site/about']], // ['label' => 'Contact', 'url' => ['/site/contact']], ]; if (Yii::$app->user->isGuest) { @@ -45,7 +45,7 @@ // $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { $menuItems[] = '
  • ' - . Html::beginForm(['/site/logout'], 'post') + . Html::beginForm(['/main/site/logout'], 'post') . Html::submitButton( 'Logout (' . Yii::$app->user->identity->username . ')', ['class' => 'btn btn-link logout'] diff --git a/heroku/nginx.conf b/heroku/nginx.conf new file mode 100644 index 0000000..9f07de3 --- /dev/null +++ b/heroku/nginx.conf @@ -0,0 +1,4 @@ +location / { + index index.php; + try_files $uri $uri/ /index.php?$args; +} \ No newline at end of file diff --git a/post-install-cmd.sh b/heroku/post-install-cmd.sh similarity index 100% rename from post-install-cmd.sh rename to heroku/post-install-cmd.sh diff --git a/nginx.conf b/nginx.conf deleted file mode 100644 index 87b3b11..0000000 --- a/nginx.conf +++ /dev/null @@ -1,9 +0,0 @@ -location /backend { - index index.php; - try_files $uri /backend/web/index.php?$args; -} - -location / { - index index.php; - try_files $uri $uri/ /index.php?$args; -} \ No newline at end of file