Commit 33813a627226a9a43b4e81121ea90de0405d481a
1 parent
86777356
функция проверки доступа к проэкту /ga/check
Showing
4 changed files
with
71 additions
and
11 deletions
Show diff stats
app/library/App/Controllers/GaController.php
| ... | ... | @@ -9,8 +9,8 @@ |
| 9 | 9 | namespace App\Controllers; |
| 10 | 10 | |
| 11 | 11 | |
| 12 | +use PhalconRest\Mvc\Controllers\CrudResourceController; | |
| 12 | 13 | use App\Model\Project; |
| 13 | -use Codeception\Exception\ContentNotFound; | |
| 14 | 14 | use DateTime; |
| 15 | 15 | use Google_Client; |
| 16 | 16 | use Google_Service_AnalyticsReporting; |
| ... | ... | @@ -20,7 +20,9 @@ use Google_Service_AnalyticsReporting_GetReportsRequest; |
| 20 | 20 | use Google_Service_AnalyticsReporting_Metric; |
| 21 | 21 | use Google_Service_AnalyticsReporting_OrderBy; |
| 22 | 22 | use Google_Service_AnalyticsReporting_ReportRequest; |
| 23 | -use PhalconRest\Mvc\Controllers\CrudResourceController; | |
| 23 | +use Codeception\Exception\ContentNotFound; | |
| 24 | +use PhalconApi\Exception; | |
| 25 | +use PhalconApi\Constants\ErrorCodes; | |
| 24 | 26 | |
| 25 | 27 | class GaController extends CrudResourceController { |
| 26 | 28 | |
| ... | ... | @@ -28,6 +30,51 @@ class GaController extends CrudResourceController { |
| 28 | 30 | const VIEW_ID = '119240817'; |
| 29 | 31 | const SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'; |
| 30 | 32 | |
| 33 | + public function checkAction() { | |
| 34 | + | |
| 35 | + $data = $this->getPostedData(); | |
| 36 | + | |
| 37 | + /** user params **/ | |
| 38 | + $view_id = $data['view_id']; | |
| 39 | + | |
| 40 | + if (empty($view_id)) { | |
| 41 | + $msg = 'Post-data is invalid, empty `view_id` value'; | |
| 42 | + throw new Exception(ErrorCodes::DATA_NOT_FOUND, $msg, ['view_id' => $view_id]); | |
| 43 | + } | |
| 44 | + | |
| 45 | + $result['view_id'] = $view_id; | |
| 46 | + | |
| 47 | + try { | |
| 48 | + putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); | |
| 49 | + $client = new Google_Client(); | |
| 50 | + $client->useApplicationDefaultCredentials(); | |
| 51 | + $client->setScopes([self::SCOPE]); | |
| 52 | + $analytics = new Google_Service_AnalyticsReporting($client); | |
| 53 | + | |
| 54 | + $request = new Google_Service_AnalyticsReporting_ReportRequest(); | |
| 55 | + $request->setViewId($view_id); | |
| 56 | + | |
| 57 | + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); | |
| 58 | + $body->setReportRequests(array($request)); | |
| 59 | + | |
| 60 | + $analytics->reports->batchGet($body); | |
| 61 | + } | |
| 62 | + catch (\Exception $e) { | |
| 63 | + if ($e->getCode() == 403) { | |
| 64 | + $result['status'] = 'error'; | |
| 65 | + return $result; | |
| 66 | + } | |
| 67 | + else { | |
| 68 | + return $e->getMessage(); | |
| 69 | + } | |
| 70 | + } | |
| 71 | + | |
| 72 | + $result['status'] = 'success'; | |
| 73 | + | |
| 74 | + return $result; | |
| 75 | + | |
| 76 | + } | |
| 77 | + | |
| 31 | 78 | /** |
| 32 | 79 | * Main action for /ga request. Send it google report api. |
| 33 | 80 | * |
| ... | ... | @@ -450,8 +497,8 @@ class GaController extends CrudResourceController { |
| 450 | 497 | */ |
| 451 | 498 | public static function countIterations($request_dim, $request_days) { |
| 452 | 499 | |
| 453 | - if (empty($request_dim)) throw new ContentNotFound('PHP: request_dim not found'); | |
| 454 | - if (empty($request_days)) throw new ContentNotFound('PHP: request_days not found'); | |
| 500 | + if (empty($request_dim)) throw new ContentNotFound('PHP: request_dim not found', ErrorCodes::DATA_NOT_FOUND); | |
| 501 | + if (empty($request_days)) throw new ContentNotFound('PHP: request_days not found', ErrorCodes::DATA_NOT_FOUND); | |
| 455 | 502 | switch ($request_dim) { |
| 456 | 503 | case 'ga:nthDay': |
| 457 | 504 | $iterations = $request_days*1; | ... | ... |
app/library/App/Controllers/UserController.php
app/library/App/Model/User.php
| ... | ... | @@ -56,7 +56,7 @@ class User extends DateTrackingModel |
| 56 | 56 | elseif (empty($username)) |
| 57 | 57 | { |
| 58 | 58 | $msg = 'Post-data is invalid, trying to use empty value of `username`'; |
| 59 | - throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]); | |
| 59 | + throw new Exception(ErrorCodes::DATA_NOT_FOUND, $msg, ['username' => $username]); | |
| 60 | 60 | } |
| 61 | 61 | elseif (strlen($username) < 4) |
| 62 | 62 | { | ... | ... |
app/library/App/Resources/GaResource.php
| ... | ... | @@ -11,6 +11,7 @@ namespace App\Resources; |
| 11 | 11 | |
| 12 | 12 | use App\Constants\AclRoles; |
| 13 | 13 | use App\Controllers\GaController; |
| 14 | +use PhalconApi\Constants\HttpMethods; | |
| 14 | 15 | use PhalconRest\Api\ApiEndpoint; |
| 15 | 16 | use PhalconRest\Api\ApiResource; |
| 16 | 17 | |
| ... | ... | @@ -26,9 +27,9 @@ class GaResource extends ApiResource { |
| 26 | 27 | ->deny(AclRoles::UNAUTHORIZED) |
| 27 | 28 | ->handler(GaController::class) |
| 28 | 29 | |
| 29 | - ->endpoint(ApiEndpoint::factory('', 'GET', 'getAction') | |
| 30 | - ->allow(AclRoles::USER) | |
| 31 | - ->description('Returns data from Google Analytics Api. https://developers.google.com/analytics/devguides/reporting/core/dimsmets') | |
| 30 | + ->endpoint(ApiEndpoint::factory('', HttpMethods::GET, 'getAction') | |
| 31 | + ->allow(AclRoles::AUTHORIZED) | |
| 32 | + ->description('Возвращает данные с Google Core Reporting Api. https://developers.google.com/analytics/devguides/reporting/core/dimsmets') | |
| 32 | 33 | ->exampleResponse([ |
| 33 | 34 | "name" => "rukzachok.com.ua", |
| 34 | 35 | "(Other)" => "646", |
| ... | ... | @@ -59,6 +60,21 @@ class GaResource extends ApiResource { |
| 59 | 60 | ] |
| 60 | 61 | ]) |
| 61 | 62 | ) |
| 63 | + | |
| 64 | + ->endpoint(ApiEndpoint::factory('/check', HttpMethods::POST, 'checkAction') | |
| 65 | + ->allow(AclRoles::AUTHORIZED) | |
| 66 | + ->expectsJsonData() | |
| 67 | + ->description('Проверяет наличие доступа к проэкту') | |
| 68 | + ->exampleResponse([ | |
| 69 | + 'view_id' => 'integer(id представления проэкта с гугл аналитики)', | |
| 70 | + 'status' => 'enum(success|error)' | |
| 71 | + ]) | |
| 72 | + ->paramsDescription([ | |
| 73 | + 'required params' => [ | |
| 74 | + 'view_id' => 'integer(id представления проэкта с гугл аналитики)' | |
| 75 | + ] | |
| 76 | + ]) | |
| 77 | + ) | |
| 62 | 78 | ; |
| 63 | 79 | |
| 64 | 80 | } | ... | ... |