Skip to content

Commit

Permalink
some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr-Moik committed Jan 25, 2022
1 parent 7cc13f9 commit 33e4d0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "control access to models using groups and permissions",
"license": "MIT",
"type": "library",
"version": "2.1.1",
"version": "2.1.2",
"authors": [
{
"name": "Oleksandr-Moik",
Expand Down
25 changes: 11 additions & 14 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Collection;

/**
* For User model
Expand All @@ -20,25 +21,24 @@ trait HasRoles
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class)
->with('permissions', function (BelongsToMany $builder){
->with('permissions', function (BelongsToMany $builder) {
return $builder->select('title');
});
}

public function getPermissionsAttribute(): \Illuminate\Support\Collection
public function getPermissionsAttribute(): Collection
{
$permissions = collect();

$this->roles->each(function (Role $role) use (&$permissions) {
$permissions = $permissions->merge($role->permissions->pluck('title'));
});

return $permissions->unique();
return $this->roles
->reduce(
fn(Collection $carry, Role $role) => $carry->merge($role->permissions->pluck('title')),
collect()
)
->unique();
}

public function hasPermissionKey(string $permission, ?string $module = null): bool
{
if(isset($module)){
if (isset($module)) {
$permission = Permission::key($module, $permission);
}

Expand All @@ -48,10 +48,7 @@ public function hasPermissionKey(string $permission, ?string $module = null): bo
public function hasAdminAccess(): bool
{
return $this->roles
->filter(function (Role $role) {
return in_array($role->id, [Role::Admin, Role::SuperAdmin])
|| $role->admin_access;
})
->filter(fn (Role $role) => in_array($role->id, [Role::Admin, Role::SuperAdmin]) || $role->admin_access)
->count() > 0;
}

Expand Down

0 comments on commit 33e4d0f

Please sign in to comment.