Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit e07acc3

Browse files
committed
User Resolver
1 parent f946629 commit e07acc3

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

app/Auth/EloquentUserResolver.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Providers;
44

5+
use App\Auth\EloquentUserResolver;
56
use Illuminate\Support\ServiceProvider;
7+
use App\Contracts\UserResolverInterface;
68
use App\Exceptions\OAuthExceptionHandler;
79
use App\Exceptions\UnauthorizedExceptionHandler;
810
use 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
/**

app/Providers/AuthServiceProvider.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace App\Providers;
44

5+
use Dingo\Api\Auth\Provider\OAuth2;
6+
use App\Contracts\UserResolverInterface;
57
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
68
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
79

10+
/**
11+
* Class AuthServiceProvider
12+
* @package App\Providers
13+
*/
814
class 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
}

tests/Auth/OAuthServerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Carbon\Carbon;
77
use App\Tests\TestCase;
88
use App\Entities\Users\User;
9+
use App\Contracts\UserResolverInterface;
910
use 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
}

0 commit comments

Comments
 (0)