Skip to content

Commit

Permalink
added abilities in users
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehedi Hasan Nahid committed Dec 20, 2018
1 parent bf07c9d commit a76f9b7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
20 changes: 20 additions & 0 deletions helpers/permit.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,24 @@ function json_to_array($json)

return $json_out;
}
}

if (!function_exists('array_merge_nested')) {
function array_merge_nested(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_nested($merged[$key], $value);
} else {
if (is_string($key)) {
$merged[$key] = $value;
} else {
$merged[] = $value;
}
}
}

return $merged;
}
}
26 changes: 2 additions & 24 deletions src/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,9 @@ public function roleCan($user, $permission, $params = [])
public function allows($user, $permission, $params = [])
{
if ($user instanceof $this->userModelNamespace) {
$user_permissions = json_to_array($user->permissions);
$role_permissions = json_to_array($user->permission->permission);
$abilities = $this->arrayMergeNested($role_permissions, $user_permissions);
$this->abilities = $user->abilities;

$this->abilities = $abilities;

if (count($abilities) > 0) {
if (count($this->abilities) > 0) {
if (is_array($permission)) {
if ($this->hasOnePermission($permission, $user)) {
return true;
Expand Down Expand Up @@ -476,22 +472,4 @@ public function role($role)
{
return $this->permission->getRole($role);
}

protected function arrayMergeNested(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMergeNested($merged[$key], $value);
} else {
if (is_string($key)) {
$merged[$key] = $value;
} else {
$merged[] = $value;
}
}
}

return $merged;
}
}
16 changes: 12 additions & 4 deletions src/Users/Permitable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
trait Permitable
{
/**
* boot model
* boot trait
*/
protected static function boot()
protected static function bootPermitable()
{
parent::boot();

static::addGlobalScope(new PermissionScope());
}

Expand All @@ -35,6 +33,16 @@ public function getPermissionArrayAttribute()
return json_to_array($this->permissions);
}

public function getAbilitiesAttribute()
{
if ($this->relationLoaded('permission')) {
$role_permissions = json_to_array($this->permission->permission);
}
$user_permissions = json_to_array($this->attributes['permissions']);

return array_merge_nested($role_permissions, $user_permissions);
}

/**
* relationship for permission
*
Expand Down

0 comments on commit a76f9b7

Please sign in to comment.