Skip to content

Commit 0547e63

Browse files
author
olimar
committed
Created Facades for Oauth2 get api methods;
1 parent c33fc32 commit 0547e63

File tree

7 files changed

+164
-9
lines changed

7 files changed

+164
-9
lines changed

Config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
'secret' => env('CLIENT_SECRET'),
2929
],
3030

31+
'api-version' => env('API_VERSION', 'v1'),
3132
];

Facades/Oauth2.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 05/06/2018 - 14:54
6+
*/
7+
8+
namespace Modules\OpenId\Facades;
9+
10+
11+
use Illuminate\Support\Facades\Facade;
12+
13+
class Oauth2 extends Facade
14+
{
15+
public static function getFacadeAccessor()
16+
{
17+
return 'oauth2';
18+
}
19+
}

Http/ViewComposers/UserSystemsComposer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010

1111
use Illuminate\Support\Facades\Auth;
12-
use Illuminate\Support\Facades\Session;
1312
use Illuminate\View\View;
1413

1514
class UserSystemsComposer
@@ -71,15 +70,12 @@ private function greeting(): string
7170
*/
7271
private function systems(): array
7372
{
74-
if (!Session::has('systems') && Auth::check()){
75-
$client = \OpenId::getClient();
76-
77-
$response = $client->get('api/systemsByUser');
78-
79-
$response = json_decode($response->getBody() , true);
80-
Session::put($response);
73+
if (!\Session::has('systems') && Auth::check()) {
74+
\Session::put(\Oauth2::getSystems());
8175
}
8276

8377
return \Session::get('systems');
8478
}
79+
80+
8581
}

Providers/OpenIdServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Blade;
88
use Modules\OpenId\Guards\CustomSessionGuard;
99
use Modules\OpenId\Guards\CustomTokenGuard;
10+
use Modules\OpenId\Services\Api;
1011
use Modules\OpenId\Services\Client;
1112

1213
class OpenIdServiceProvider extends ServiceProvider
@@ -35,6 +36,10 @@ public function boot()
3536
$this->app->singleton('openid', function ($app) {
3637
return new Client();
3738
});
39+
40+
$this->app->singleton('oauth2', function ($app) {
41+
return new Api();
42+
});
3843
}
3944

4045
/**
@@ -101,5 +106,11 @@ protected function registerBladeDirectives()
101106
Blade::directive('openidComponents', function () {
102107
return "<?php echo view('openid::menu')->render(); ?>";
103108
});
109+
110+
Blade::directive('login', function ($route) {
111+
if(\Auth::check()) return route($route);
112+
113+
return "<?php echo config('openid.server') . '/login?' . http_build_query(['continue' => route($route)]); ?>";
114+
});
104115
}
105116
}

Services/Api.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Created by Olimar Ferraz
4+
5+
* Date: 05/06/2018 - 14:17
6+
*/
7+
8+
namespace Modules\OpenId\Services;
9+
10+
class Api
11+
{
12+
/**
13+
* @var \GuzzleHttp\Client
14+
*/
15+
private static $client;
16+
/**
17+
* @var string
18+
*/
19+
private static $version;
20+
/**
21+
* @var bool
22+
*/
23+
private static $initialized = FALSE;
24+
25+
/**
26+
* Class constructor
27+
*/
28+
private static function initialize()
29+
{
30+
if (self::$initialized) return;
31+
32+
self::$client = \OpenId::getClient();
33+
self::$version = config('openid.api-version');
34+
self::$initialized = TRUE;
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public static function getSystems()
41+
{
42+
return self::getResponse('system');
43+
}
44+
45+
/**
46+
* @return array
47+
*/
48+
public static function getSystemRoles()
49+
{
50+
return self::getResponse('system/roles');
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public static function getUser()
57+
{
58+
return self::getResponse('user');
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public static function getUserSystems()
65+
{
66+
return self::getResponse('user/systems');
67+
}
68+
69+
/**
70+
* @return array
71+
*/
72+
public static function getUserPermissions()
73+
{
74+
return self::getResponse('user/permissions');
75+
}
76+
77+
/**
78+
*
79+
*/
80+
public static function createUserPermission()
81+
{
82+
//
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public static function getStates()
89+
{
90+
return self::getResponse('address/states');
91+
}
92+
93+
/**
94+
* @param string $state
95+
*
96+
* @return array
97+
*/
98+
public static function getCities(string $state)
99+
{
100+
return self::getResponse('address/cities/' . $state);
101+
}
102+
103+
/**
104+
* @return array
105+
*/
106+
public static function isAddressFilled()
107+
{
108+
return self::getResponse('address/filled');
109+
}
110+
111+
/**
112+
* @param string $uri
113+
*
114+
* @return array
115+
*/
116+
private static function getResponse(string $uri): array
117+
{
118+
self::initialize();
119+
120+
$response = self::$client->get('api/' . self::$version . '/' . $uri);
121+
122+
return json_decode($response->getBody(), TRUE);
123+
}
124+
}

Services/Client.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static function getClient()
1818
]);
1919
}
2020

21+
/**
22+
* @return \GuzzleHttp\Client
23+
*/
2124
public static function getServerClient()
2225
{
2326
return new \GuzzleHttp\Client([

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"Modules\\OpenId\\Providers\\OpenIdServiceProvider"
3131
],
3232
"aliases": {
33-
"OpenId": "Modules\\OpenId\\Facades\\OpenId"
33+
"OpenId": "Modules\\OpenId\\Facades\\OpenId",
34+
"Oauth2": "Modules\\OpenId\\Facades\\Oauth2"
3435
}
3536
}
3637
}

0 commit comments

Comments
 (0)