-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auth guard with multiple dynamic auth models (#605)
- Loading branch information
Showing
11 changed files
with
229 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\Concerns; | ||
|
||
use Illuminate\Config\Repository as ConfigRepository; | ||
|
||
trait LoadsAuthModel | ||
{ | ||
private function getAuthModel(ConfigRepository $config, ?string $guard = null): ?string | ||
{ | ||
if ( | ||
($guard === null && ! ($guard = $config->get('auth.defaults.guard'))) || | ||
! ($provider = $config->get('auth.guards.'.$guard.'.provider')) || | ||
! ($authModel = $config->get('auth.providers.'.$provider.'.model')) | ||
) { | ||
return null; | ||
} | ||
|
||
return $authModel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace NunoMaduro\Larastan\ReturnTypes; | ||
|
||
use Illuminate\Contracts\Auth\Guard; | ||
use NunoMaduro\Larastan\Concerns; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
final class GuardExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
use Concerns\HasContainer; | ||
use Concerns\LoadsAuthModel; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getClass(): string | ||
{ | ||
return Guard::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'user'; | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
$config = $this->getContainer()->get('config'); | ||
$authModel = null; | ||
|
||
if ($config !== null) { | ||
$guard = $this->getGuardFromMethodCall($scope, $methodCall); | ||
$authModel = $this->getAuthModel($config, $guard); | ||
} | ||
|
||
if ($authModel === null) { | ||
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
return TypeCombinator::addNull(new ObjectType($authModel)); | ||
} | ||
|
||
private function getGuardFromMethodCall(Scope $scope, MethodCall $methodCall): ?string | ||
{ | ||
if ( | ||
! ($methodCall->var instanceof StaticCall) && | ||
! ($methodCall->var instanceof MethodCall) && | ||
! ($methodCall->var instanceof FuncCall) | ||
) { | ||
return null; | ||
} | ||
|
||
if (count($methodCall->var->args) !== 1) { | ||
return null; | ||
} | ||
|
||
$guardType = $scope->getType($methodCall->var->args[0]->value); | ||
|
||
if (! $guardType instanceof ConstantStringType) { | ||
return null; | ||
} | ||
|
||
return $guardType->getValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class Admin | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
'defaults' => [ | ||
'guard' => 'web', | ||
'passwords' => 'users', | ||
], | ||
|
||
'guards' => [ | ||
'web' => [ | ||
'driver' => 'session', | ||
'provider' => 'users', | ||
], | ||
|
||
'admin' => [ | ||
'driver' => 'session', | ||
'provider' => 'admins', | ||
], | ||
|
||
'api' => [ | ||
'driver' => 'token', | ||
'provider' => 'users', | ||
'hash' => false, | ||
], | ||
], | ||
|
||
'providers' => [ | ||
'users' => [ | ||
'driver' => 'eloquent', | ||
'model' => App\User::class, | ||
], | ||
|
||
'admins' => [ | ||
'driver' => 'eloquent', | ||
'model' => App\Admin::class, | ||
], | ||
], | ||
|
||
'passwords' => [ | ||
'users' => [ | ||
'provider' => 'users', | ||
'table' => 'password_resets', | ||
'expire' => 60, | ||
'throttle' => 60, | ||
], | ||
], | ||
|
||
'password_timeout' => 10800, | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.