diff --git a/src/Guard.php b/src/Guard.php new file mode 100644 index 0000000..78fb7c6 --- /dev/null +++ b/src/Guard.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 061ecb8..b360710 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'] ?? (new 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'] ?? (new 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 ?? (new 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 ?? (new 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..bcb86ef 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -8,9 +8,11 @@ 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; +use ReflectionException; /** * Class Role @@ -28,10 +30,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'] ?? (new Guard())->getDefaultName(static::class); parent::__construct($attributes); @@ -47,10 +51,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'] ?? (new Guard())->getDefaultName(static::class); $helpers = new Helpers(); if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) { @@ -74,10 +79,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 ?? (new Guard())->getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) @@ -98,10 +104,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 ?? (new Guard())->getDefaultName(static::class); $role = static::where('name', $name) ->where('guard_name', $guardName) @@ -123,6 +130,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac * @return bool * * @throws GuardDoesNotMatch + * @throws ReflectionException */ public function hasPermissionTo($permission): bool { diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 39a500d..502d982 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 (new Guard())->getNames($this); } + /** + * @return string + * @throws \ReflectionException + */ protected function getDefaultGuardName(): string { - $default = config('auth.defaults.guard'); - - return $this->getGuardNames()->first() ?: $default; + return (new Guard())->getDefaultName($this); } /** diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index 1da905a..7443e04 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -9,6 +9,7 @@ use Jenssegers\Mongodb\Relations\BelongsToMany; use Maklad\Permission\Contracts\PermissionInterface as Permission; use Maklad\Permission\Contracts\RoleInterface as Role; +use ReflectionException; /** * Trait HasRoles @@ -159,26 +160,23 @@ public function hasRole($roles): bool if (\is_string($roles) && false !== \strpos($roles, '|')) { $roles = \explode('|', $roles); } - + $hasRole = false; if (\is_string($roles)) { - return $this->roles->contains('name', $roles); - } - - if ($roles instanceof Role) { - return $this->roles->contains('id', $roles->id); - } - - if (\is_array($roles)) { + $hasRole = $this->roles->contains('name', $roles); + } elseif ($roles instanceof Role) { + $hasRole = $this->roles->contains('id', $roles->id); + } elseif (\is_array($roles)) { foreach ($roles as $role) { if ($this->hasRole($role)) { - return true; + $hasRole = true; + break; } } - - return false; + } else { + $hasRole = ! $roles->intersect($this->roles)->isEmpty(); } - return ! $roles->intersect($this->roles)->isEmpty(); + return $hasRole; } /** @@ -207,18 +205,17 @@ public function hasAllRoles($roles): bool } if (\is_string($roles)) { - return $this->roles->contains('name', $roles); - } - - if ($roles instanceof Role) { - return $this->roles->contains('id', $roles->id); + $hasRoles = $this->roles->contains('name', $roles); + } elseif ($roles instanceof Role) { + $hasRoles = $this->roles->contains('id', $roles->id); + } else { + $roles = \collect()->make($roles)->map(function ($role) { + return $role instanceof Role ? $role->name : $role; + }); + $hasRoles = $roles->intersect($this->roles->pluck('name')) == $roles; } - $roles = \collect()->make($roles)->map(function ($role) { - return $role instanceof Role ? $role->name : $role; - }); - - return $roles->intersect($this->roles->pluck('name')) == $roles; + return $hasRoles; } /** @@ -228,6 +225,7 @@ public function hasAllRoles($roles): bool * @param string|null $guardName * * @return bool + * @throws ReflectionException */ public function hasPermissionTo($permission, $guardName = null): bool { @@ -281,6 +279,7 @@ protected function hasPermissionViaRole(Permission $permission): bool * @param string|Permission $permission * * @return bool + * @throws ReflectionException */ public function hasDirectPermission($permission): bool { @@ -327,6 +326,7 @@ public function getAllPermissions(): Collection * @param String|Role $role role name * * @return Role + * @throws ReflectionException */ protected function getStoredRole($role): Role {