Skip to content

Commit

Permalink
Merge 63adf2f into 98e2de3
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Mar 17, 2018
2 parents 98e2de3 + 63adf2f commit fc1ba19
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 56 deletions.
58 changes: 58 additions & 0 deletions src/Guard.php
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;
}
}
14 changes: 10 additions & 4 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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',
Expand All @@ -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)
Expand Down Expand Up @@ -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();

Expand Down
16 changes: 12 additions & 4 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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()) {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -123,6 +130,7 @@ public static function findByName(string $name, $guardName = null): RoleInterfac
* @return bool
*
* @throws GuardDoesNotMatch
* @throws ReflectionException
*/
public function hasPermissionTo($permission): bool
{
Expand Down
28 changes: 13 additions & 15 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -107,6 +108,7 @@ public function revokePermissionTo(...$permissions)
* @param string|Permission $permission
*
* @return Permission
* @throws \ReflectionException
*/
protected function getStoredPermission($permission): Permission
{
Expand All @@ -121,6 +123,7 @@ protected function getStoredPermission($permission): Permission
* @param Model $roleOrPermission
*
* @throws GuardDoesNotMatch
* @throws \ReflectionException
*/
protected function ensureModelSharesGuard(Model $roleOrPermission)
{
Expand All @@ -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);
}

/**
Expand Down
52 changes: 19 additions & 33 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -152,33 +153,31 @@ public function syncRoles(...$roles)
*
* @param string|array|Role|\Illuminate\Support\Collection $roles
*
* @param bool $strict if(true) checks if has All Roles
*
* @return bool
*/
public function hasRole($roles): bool
public function hasRole($roles, bool $strict = false): bool
{
if (\is_string($roles) && false !== \strpos($roles, '|')) {
$roles = \explode('|', $roles);
}

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) || $roles instanceof Collection) {
$roles = \collect()->make($roles)->map(function ($role) {
return $role instanceof Role ? $role->name : $role;
});

if (\is_array($roles)) {
foreach ($roles as $role) {
if ($this->hasRole($role)) {
return true;
}
if ($strict) {
$hasRoles = $roles->intersect($this->roles->pluck('name')) == $roles;
} else {
$hasRoles = ! $roles->intersect($this->roles->pluck('name'))->isEmpty();
}

return false;
} else {
$hasRoles = $this->roles->contains('name', $roles->name?? $roles);
}

return ! $roles->intersect($this->roles)->isEmpty();
return $hasRoles;
}

/**
Expand All @@ -202,23 +201,7 @@ public function hasAnyRole($roles): bool
*/
public function hasAllRoles($roles): bool
{
if (\is_string($roles) && false !== strpos($roles, '|')) {
$roles = \explode('|', $roles);
}

if (\is_string($roles)) {
return $this->roles->contains('name', $roles);
}

if ($roles instanceof Role) {
return $this->roles->contains('id', $roles->id);
}

$roles = \collect()->make($roles)->map(function ($role) {
return $role instanceof Role ? $role->name : $role;
});

return $roles->intersect($this->roles->pluck('name')) == $roles;
return $this->hasRole($roles, true);
}

/**
Expand All @@ -228,6 +211,7 @@ public function hasAllRoles($roles): bool
* @param string|null $guardName
*
* @return bool
* @throws ReflectionException
*/
public function hasPermissionTo($permission, $guardName = null): bool
{
Expand Down Expand Up @@ -281,6 +265,7 @@ protected function hasPermissionViaRole(Permission $permission): bool
* @param string|Permission $permission
*
* @return bool
* @throws ReflectionException
*/
public function hasDirectPermission($permission): bool
{
Expand Down Expand Up @@ -327,6 +312,7 @@ public function getAllPermissions(): Collection
* @param String|Role $role role name
*
* @return Role
* @throws ReflectionException
*/
protected function getStoredRole($role): Role
{
Expand Down

0 comments on commit fc1ba19

Please sign in to comment.