diff --git a/app/library/App/Controllers/GaController.php b/app/library/App/Controllers/GaController.php index 4e73a6b..18bb929 100644 --- a/app/library/App/Controllers/GaController.php +++ b/app/library/App/Controllers/GaController.php @@ -9,8 +9,8 @@ namespace App\Controllers; +use PhalconRest\Mvc\Controllers\CrudResourceController; use App\Model\Project; -use Codeception\Exception\ContentNotFound; use DateTime; use Google_Client; use Google_Service_AnalyticsReporting; @@ -20,7 +20,9 @@ use Google_Service_AnalyticsReporting_GetReportsRequest; use Google_Service_AnalyticsReporting_Metric; use Google_Service_AnalyticsReporting_OrderBy; use Google_Service_AnalyticsReporting_ReportRequest; -use PhalconRest\Mvc\Controllers\CrudResourceController; +use Codeception\Exception\ContentNotFound; +use PhalconApi\Exception; +use PhalconApi\Constants\ErrorCodes; class GaController extends CrudResourceController { @@ -28,6 +30,51 @@ class GaController extends CrudResourceController { const VIEW_ID = '119240817'; const SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'; + public function checkAction() { + + $data = $this->getPostedData(); + + /** user params **/ + $view_id = $data['view_id']; + + if (empty($view_id)) { + $msg = 'Post-data is invalid, empty `view_id` value'; + throw new Exception(ErrorCodes::DATA_NOT_FOUND, $msg, ['view_id' => $view_id]); + } + + $result['view_id'] = $view_id; + + try { + putenv('GOOGLE_APPLICATION_CREDENTIALS=/var/www/phalcon/'.self::SECRET_JSON); + $client = new Google_Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes([self::SCOPE]); + $analytics = new Google_Service_AnalyticsReporting($client); + + $request = new Google_Service_AnalyticsReporting_ReportRequest(); + $request->setViewId($view_id); + + $body = new Google_Service_AnalyticsReporting_GetReportsRequest(); + $body->setReportRequests(array($request)); + + $analytics->reports->batchGet($body); + } + catch (\Exception $e) { + if ($e->getCode() == 403) { + $result['status'] = 'error'; + return $result; + } + else { + return $e->getMessage(); + } + } + + $result['status'] = 'success'; + + return $result; + + } + /** * Main action for /ga request. Send it google report api. * @@ -450,8 +497,8 @@ class GaController extends CrudResourceController { */ public static function countIterations($request_dim, $request_days) { - if (empty($request_dim)) throw new ContentNotFound('PHP: request_dim not found'); - if (empty($request_days)) throw new ContentNotFound('PHP: request_days not found'); + if (empty($request_dim)) throw new ContentNotFound('PHP: request_dim not found', ErrorCodes::DATA_NOT_FOUND); + if (empty($request_days)) throw new ContentNotFound('PHP: request_days not found', ErrorCodes::DATA_NOT_FOUND); switch ($request_dim) { case 'ga:nthDay': $iterations = $request_days*1; diff --git a/app/library/App/Controllers/UserController.php b/app/library/App/Controllers/UserController.php index 94febf9..ee1017d 100755 --- a/app/library/App/Controllers/UserController.php +++ b/app/library/App/Controllers/UserController.php @@ -2,9 +2,6 @@ namespace App\Controllers; -use App\Model\User; -use PhalconApi\Constants\ErrorCodes; -use PhalconApi\Exception; use PhalconRest\Mvc\Controllers\CrudResourceController; class UserController extends CrudResourceController diff --git a/app/library/App/Model/User.php b/app/library/App/Model/User.php index 8be8fb5..dd5b024 100755 --- a/app/library/App/Model/User.php +++ b/app/library/App/Model/User.php @@ -56,7 +56,7 @@ class User extends DateTrackingModel elseif (empty($username)) { $msg = 'Post-data is invalid, trying to use empty value of `username`'; - throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]); + throw new Exception(ErrorCodes::DATA_NOT_FOUND, $msg, ['username' => $username]); } elseif (strlen($username) < 4) { diff --git a/app/library/App/Resources/GaResource.php b/app/library/App/Resources/GaResource.php index 92976ab..474c250 100644 --- a/app/library/App/Resources/GaResource.php +++ b/app/library/App/Resources/GaResource.php @@ -11,6 +11,7 @@ namespace App\Resources; use App\Constants\AclRoles; use App\Controllers\GaController; +use PhalconApi\Constants\HttpMethods; use PhalconRest\Api\ApiEndpoint; use PhalconRest\Api\ApiResource; @@ -26,9 +27,9 @@ class GaResource extends ApiResource { ->deny(AclRoles::UNAUTHORIZED) ->handler(GaController::class) - ->endpoint(ApiEndpoint::factory('', 'GET', 'getAction') - ->allow(AclRoles::USER) - ->description('Returns data from Google Analytics Api. https://developers.google.com/analytics/devguides/reporting/core/dimsmets') + ->endpoint(ApiEndpoint::factory('', HttpMethods::GET, 'getAction') + ->allow(AclRoles::AUTHORIZED) + ->description('Возвращает данные с Google Core Reporting Api. https://developers.google.com/analytics/devguides/reporting/core/dimsmets') ->exampleResponse([ "name" => "rukzachok.com.ua", "(Other)" => "646", @@ -59,6 +60,21 @@ class GaResource extends ApiResource { ] ]) ) + + ->endpoint(ApiEndpoint::factory('/check', HttpMethods::POST, 'checkAction') + ->allow(AclRoles::AUTHORIZED) + ->expectsJsonData() + ->description('Проверяет наличие доступа к проэкту') + ->exampleResponse([ + 'view_id' => 'integer(id представления проэкта с гугл аналитики)', + 'status' => 'enum(success|error)' + ]) + ->paramsDescription([ + 'required params' => [ + 'view_id' => 'integer(id представления проэкта с гугл аналитики)' + ] + ]) + ) ; } -- libgit2 0.21.4