This repository was archived by the owner on Feb 18, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +98
-1
lines changed Expand file tree Collapse file tree 5 files changed +98
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Auth ;
4+
5+ use App \Entities \Users \User ;
6+ use App \Contracts \UserResolverInterface ;
7+
8+ /**
9+ * Class EloquentUserResolver
10+ * @package App\Auth
11+ */
12+ class EloquentUserResolver implements UserResolverInterface
13+ {
14+
15+ /**
16+ * @var User
17+ */
18+ protected $ user ;
19+
20+ /**
21+ * EloquentUserResolver constructor.
22+ * @param User $user
23+ */
24+ public function __construct (User $ user )
25+ {
26+ $ this ->user = $ user ;
27+ }
28+
29+ /**
30+ * Resolve user with eloquent
31+ * @param $id
32+ * @return mixed
33+ */
34+ public function resolveById ($ id )
35+ {
36+ return $ this ->user ->findOrFail ($ id );
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Contracts ;
4+
5+ /**
6+ * Interface UserResolverInterface
7+ * @package App\Auth
8+ */
9+ interface UserResolverInterface
10+ {
11+
12+ /**
13+ * Resolve a user from ID
14+ * @param $id
15+ * @return mixed
16+ */
17+ public function resolveById ($ id );
18+ }
Original file line number Diff line number Diff line change 22
33namespace App \Providers ;
44
5+ use App \Auth \EloquentUserResolver ;
56use Illuminate \Support \ServiceProvider ;
7+ use App \Contracts \UserResolverInterface ;
68use App \Exceptions \OAuthExceptionHandler ;
79use App \Exceptions \UnauthorizedExceptionHandler ;
810use League \OAuth2 \Server \Exception \OAuthException ;
@@ -23,6 +25,7 @@ class AppServiceProvider extends ServiceProvider
2325 public function boot ()
2426 {
2527 $ this ->registerOAuthExceptionHandler ();
28+ $ this ->app ->bind (UserResolverInterface::class, EloquentUserResolver::class);
2629 }
2730
2831 /**
Original file line number Diff line number Diff line change 22
33namespace App \Providers ;
44
5+ use Dingo \Api \Auth \Provider \OAuth2 ;
6+ use App \Contracts \UserResolverInterface ;
57use Illuminate \Contracts \Auth \Access \Gate as GateContract ;
68use Illuminate \Foundation \Support \Providers \AuthServiceProvider as ServiceProvider ;
79
10+ /**
11+ * Class AuthServiceProvider
12+ * @package App\Providers
13+ */
814class AuthServiceProvider extends ServiceProvider
915{
1016 /**
@@ -25,7 +31,27 @@ class AuthServiceProvider extends ServiceProvider
2531 public function boot (GateContract $ gate )
2632 {
2733 parent ::registerPolicies ($ gate );
34+ $ this ->registerOAuthProvider ();
35+ }
36+
37+ /**
38+ * Register the oAuth 2 server provider
39+ */
40+ public function registerOAuthProvider ()
41+ {
42+ app ('Dingo\Api\Auth\Auth ' )->extend ('oauth ' , function ($ app ) {
43+ $ provider = new OAuth2 ($ app ['oauth2-server.authorizer ' ]->getChecker ());
44+
45+ $ provider ->setUserResolver (function ($ id ) {
46+ $ resolver = app (UserResolverInterface::class);
47+ return $ resolver ->resolveById ($ id );
48+ });
49+
50+ $ provider ->setClientResolver (function ($ id ) {
51+
52+ });
2853
29- //
54+ return $ provider ;
55+ });
3056 }
3157}
Original file line number Diff line number Diff line change 66use Carbon \Carbon ;
77use App \Tests \TestCase ;
88use App \Entities \Users \User ;
9+ use App \Contracts \UserResolverInterface ;
910use Illuminate \Foundation \Testing \DatabaseMigrations ;
1011
1112/**
@@ -199,4 +200,15 @@ public function itValidatesClientCredentials()
199200 'client_secret ' => "12345 "
200201 ])->seeStatusCode (200 );
201202 }
203+
204+ /**
205+ * @test
206+ */
207+ public function itResolvesUserFromId ()
208+ {
209+ $ user = factory (User::class)->create ();
210+ $ resolver = app (UserResolverInterface::class);
211+ $ resolved = $ resolver ->resolveById ($ user ->id );
212+ $ this ->assertEquals ($ user ->id , $ resolved ->id );
213+ }
202214}
You can’t perform that action at this time.
0 commit comments