diff --git a/src/Guard.php b/src/Guard.php new file mode 100644 index 0000000..c388e72 --- /dev/null +++ b/src/Guard.php @@ -0,0 +1,65 @@ +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 static function getDefaultName($class): string + { + $default = config('auth.defaults.guard'); + return static::getNames($class)->first() ?: $default; + } +} diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 061ecb8..2d1dfa2 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -9,6 +9,7 @@ use Maklad\Permission\Contracts\PermissionInterface; use Maklad\Permission\Exceptions\PermissionAlreadyExists; use Maklad\Permission\Exceptions\PermissionDoesNotExist; +use Maklad\Permission\Guard; use Maklad\Permission\Helpers; use Maklad\Permission\PermissionRegistrar; use Maklad\Permission\Traits\RefreshesPermissionCache; @@ -28,10 +29,12 @@ class Permission extends Model implements PermissionInterface * Permission constructor. * * @param array $attributes + * + * @throws \ReflectionException */ public function __construct(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard'); + $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); parent::__construct($attributes); @@ -47,11 +50,12 @@ public function __construct(array $attributes = []) * * @return $this|\Illuminate\Database\Eloquent\Model * @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists + * @throws \ReflectionException */ public static function create(array $attributes = []) { $helpers = new Helpers(); - $attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard'); + $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); if (static::getPermissions()->where('name', $attributes['name'])->where( 'guard_name', @@ -77,10 +81,11 @@ public static function create(array $attributes = []) * * @return PermissionInterface * @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists + * @throws \ReflectionException */ public static function findOrCreate(string $name, $guardName = null): PermissionInterface { - $guardName = $guardName ?? config('auth.defaults.guard'); + $guardName = $guardName ?? Guard::getDefaultName(static::class); $permission = static::getPermissions() ->where('name', $name) @@ -123,10 +128,11 @@ public function users(): BelongsToMany * * @return PermissionInterface * @throws PermissionDoesNotExist + * @throws \ReflectionException */ public static function findByName(string $name, $guardName = null): PermissionInterface { - $guardName = $guardName ?? \config('auth.defaults.guard'); + $guardName = $guardName ?? Guard::getDefaultName(static::class); $permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first(); diff --git a/src/Models/Role.php b/src/Models/Role.php index d256c24..1724d7a 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -8,6 +8,7 @@ use Maklad\Permission\Exceptions\GuardDoesNotMatch; use Maklad\Permission\Exceptions\RoleAlreadyExists; use Maklad\Permission\Exceptions\RoleDoesNotExist; +use Maklad\Permission\Guard; use Maklad\Permission\Helpers; use Maklad\Permission\Traits\HasPermissions; use Maklad\Permission\Traits\RefreshesPermissionCache; @@ -28,10 +29,12 @@ class Role extends Model implements RoleInterface * Role constructor. * * @param array $attributes + * + * @throws \ReflectionException */ public function __construct(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard'); + $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); parent::__construct($attributes); @@ -47,10 +50,11 @@ public function __construct(array $attributes = []) * @throws RoleAlreadyExists * @internal param array $attributesĀ§ * + * @throws \ReflectionException */ public static function create(array $attributes = []) { - $attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard'); + $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class); $helpers = new Helpers(); if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { @@ -74,10 +78,11 @@ public static function create(array $attributes = []) * * @return RoleInterface * @throws \Maklad\Permission\Exceptions\RoleAlreadyExists + * @throws \ReflectionException */ public static function findOrCreate(string $name, $guardName = null): RoleInterface { - $guardName = $guardName ?? config('auth.defaults.guard'); + $guardName = $guardName ?? Guard::getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) @@ -98,10 +103,11 @@ public static function findOrCreate(string $name, $guardName = null): RoleInterf * * @return RoleInterface * @throws RoleDoesNotExist + * @throws \ReflectionException */ public static function findByName(string $name, $guardName = null): RoleInterface { - $guardName = $guardName ?? config('auth.defaults.guard'); + $guardName = $guardName ?? Guard::getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 39a500d..8895e97 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -8,6 +8,7 @@ use Jenssegers\Mongodb\Relations\BelongsToMany; use Maklad\Permission\Contracts\PermissionInterface as Permission; use Maklad\Permission\Exceptions\GuardDoesNotMatch; +use Maklad\Permission\Guard; use Maklad\Permission\Helpers; use Maklad\Permission\PermissionRegistrar; @@ -107,6 +108,7 @@ public function revokePermissionTo(...$permissions) * @param string|Permission $permission * * @return Permission + * @throws \ReflectionException */ protected function getStoredPermission($permission): Permission { @@ -121,6 +123,7 @@ protected function getStoredPermission($permission): Permission * @param Model $roleOrPermission * * @throws GuardDoesNotMatch + * @throws \ReflectionException */ protected function ensureModelSharesGuard(Model $roleOrPermission) { @@ -133,27 +136,22 @@ protected function ensureModelSharesGuard(Model $roleOrPermission) } } + /** + * @return Collection + * @throws \ReflectionException + */ protected function getGuardNames(): Collection { - if ($this->guard_name) { - return collect($this->guard_name); - } - - return collect(config('auth.guards')) - ->map(function ($guard) { - return config("auth.providers.{$guard['provider']}.model"); - }) - ->filter(function ($model) { - return \get_class($this) === $model; - }) - ->keys(); + return Guard::getNames($this); } + /** + * @return string + * @throws \ReflectionException + */ protected function getDefaultGuardName(): string { - $default = config('auth.defaults.guard'); - - return $this->getGuardNames()->first() ?: $default; + return Guard::getDefaultName($this); } /**