-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
namespace Maklad\Permission; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* Class Guard | ||
* @package Maklad\Permission | ||
*/ | ||
class Guard | ||
{ | ||
/** | ||
* return collection of (guard_name) property if exist on class or object | ||
* otherwise will return collection of guards names that exists in config/auth.php. | ||
* | ||
* @param $model | ||
* | ||
* @return Collection | ||
* @throws \ReflectionException | ||
*/ | ||
public function getNames($model) : Collection | ||
{ | ||
if (\is_object($model)) { | ||
$guardName = $model->guard_name ?? null; | ||
} | ||
|
||
if (! isset($guardName)) { | ||
$class = \is_object($model) ? \get_class($model) : $model; | ||
$guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null; | ||
} | ||
|
||
if ($guardName) { | ||
return collect($guardName); | ||
} | ||
return collect(config('auth.guards')) | ||
->map(function ($guard) { | ||
return config("auth.providers.{$guard['provider']}.model"); | ||
}) | ||
->filter(function ($model) use ($class) { | ||
return $class === $model; | ||
}) | ||
->keys(); | ||
} | ||
|
||
/** | ||
* Return Default Guard name | ||
* | ||
* @param $class | ||
* | ||
* @return string | ||
* @throws \ReflectionException | ||
*/ | ||
public function getDefaultName($class): string | ||
{ | ||
$default = config('auth.defaults.guard'); | ||
return $this->getNames($class)->first() ?: $default; | ||
} | ||
} |
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