From 14b973776218a50f1386e5e7ca0b779b307f9a7a Mon Sep 17 00:00:00 2001 From: hudecsamuel Date: Wed, 15 Jun 2016 14:26:58 +0200 Subject: [PATCH] Added mergado hooks route to routes.php Changed oauthController changed getrosourceOwnerId to getUserId on Accesstoken Signed-off-by: hudecsamuel --- app/Http/Controllers/HookController.php | 34 +++++++++++++++++++ app/Http/Controllers/OAuthController.php | 22 ++++-------- .../Controllers/ProjectLogsController.php | 2 +- app/Http/Controllers/UserLogsController.php | 2 +- app/Http/Middleware/OAuth2Session.php | 12 +++---- app/Http/routes.php | 5 +-- 6 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 app/Http/Controllers/HookController.php diff --git a/app/Http/Controllers/HookController.php b/app/Http/Controllers/HookController.php new file mode 100644 index 0000000..76ac7c9 --- /dev/null +++ b/app/Http/Controllers/HookController.php @@ -0,0 +1,34 @@ +all(); + + if(!isset($data["action"])) { + return response()->json(["message" => "Action undefined!"], 400); + } + + switch ($data["action"]) { + case "app.enable": + $hook = new CreateHook($data["entity_id"]); + break; + case "app.disable": + $hook = new DeleteHook($data["entity_id"]); + break; + default: + return response()->json(["message" => "Unsupported action!"], 400); + } + + return $hook->run(); + } +} diff --git a/app/Http/Controllers/OAuthController.php b/app/Http/Controllers/OAuthController.php index 400d043..040b3bc 100755 --- a/app/Http/Controllers/OAuthController.php +++ b/app/Http/Controllers/OAuthController.php @@ -3,10 +3,11 @@ namespace App\Http\Controllers; use App\Auth; -use App\FailLog; +use App\Exceptions\AuthorizationException; use App\Http\Requests; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; class OAuthController extends Controller @@ -28,7 +29,6 @@ public function auth($eshopId) public function token(Request $request) { - if ($request->has('code')) { //request token return $this->oauth->getToken($request); @@ -41,26 +41,16 @@ public function token(Request $request) switch ($error) { case 'invalid_entity': - return redirect()->route('error', ['message' => trans('error.invalid-entity')]); - break; + throw new AuthorizationException(trans('error.invalid-entity')); case 'user_permission_error': - return redirect()->route('error', ['message' => trans('error.user_permission_error')]); - break; + throw new AuthorizationException(trans('error.user_permission_error')); case (preg_match('/invalid_request.*/', $error) ? true : false) : - return redirect()->route('error', ['message' => trans('error.oauth_invalid_request')]); - break; + throw new AuthorizationException(trans('error.oauth_invalid_request')); } - FailLog::create([ - "message" => $error . " occured when trying to authorize" - ]); - return redirect()->route('error'); } else { - - FailLog::create([ - "message" => $error . " occured and application will try to authorize again." - ]); + Log::notice($error . " occured and application is going to try to authorize again."); Session::put('oauthError', true); return $this->oauth->getAuthCode(Session::get('entity_id')); diff --git a/app/Http/Controllers/ProjectLogsController.php b/app/Http/Controllers/ProjectLogsController.php index aba1e8e..925b449 100755 --- a/app/Http/Controllers/ProjectLogsController.php +++ b/app/Http/Controllers/ProjectLogsController.php @@ -98,7 +98,7 @@ public function store($eshopId, $projectId, Request $request) $date = date_create_from_format($dateFormat, $request->input('date')); - $user = User::find(Session::get('oauth')->getResourceOwnerId()); + $user = User::find(Session::get('oauth')->getUserId()); $log = Log::create([ 'date' => $date->format('Y-m-d H:i:s'), diff --git a/app/Http/Controllers/UserLogsController.php b/app/Http/Controllers/UserLogsController.php index ce645e0..202db54 100755 --- a/app/Http/Controllers/UserLogsController.php +++ b/app/Http/Controllers/UserLogsController.php @@ -16,7 +16,7 @@ class UserLogsController extends Controller public function __construct() { - $this->user = User::find(Session::get('oauth')->getResourceOwnerId()); + $this->user = User::find(Session::get('oauth')->getUserId()); } /** diff --git a/app/Http/Middleware/OAuth2Session.php b/app/Http/Middleware/OAuth2Session.php index 587ec21..b46858d 100755 --- a/app/Http/Middleware/OAuth2Session.php +++ b/app/Http/Middleware/OAuth2Session.php @@ -24,22 +24,20 @@ class OAuth2Session /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { - $eshopId = $request->route()->parameter('eshop_id'); - if(!(Session::has('oauth')) || Session::get('oauth')->hasExpired()) { + if (!(Session::has('oauth')) || Session::get('oauth')->hasExpired()) { Session::put('next', $request->path()); return redirect()->route('auth', $eshopId); } - if(!Session::has('locale')) - { - Session::put('locale', User::find(Session::get('oauth')->getResourceOwnerId())->locale); + if (!Session::has('locale')) { + Session::put('locale', User::find(Session::get('oauth')->getUserId())->locale); } App::setLocale(Session::get('locale')); diff --git a/app/Http/routes.php b/app/Http/routes.php index e2bec9a..0e31b00 100755 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -19,6 +19,8 @@ Route::get('eshop/{eshop_id}/auth', ['uses' => 'OAuthController@auth', 'as' => 'auth']); }); +Route::post("/_mergado/hook/", "HookController@index"); + /* |-------------------------------------------------------------------------- | Application Routes @@ -29,6 +31,7 @@ | kernel and includes session state, CSRF protection, and more. | */ + Route::group(['middleware' => ['web', 'oauth']], function () { @@ -48,8 +51,6 @@ Route::get('widget/eshop/{eshop_id}/project/{project_id}', 'WidgetController@projectWidget'); Route::get('widget/eshop/{eshop_id}', 'WidgetController@eshopWidget'); - - });