Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve permissions inheritance based on roles issue #169

Open
wants to merge 3 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions src/Kodeine/Acl/Traits/HasPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,41 @@ public function permissions()
*/
public function getPermissions()
{
// user permissions overridden from role.

$permissions = \Cache::remember(
'acl.getPermissionsById_'.$this->id,
config('acl.cacheMinutes'),
function () {
return $this->getPermissionsInherited();
}
);
$permissions = [];
// permissions based on role.
// more permissive permission wins
// if user has multiple roles we keep
// true values.
foreach ($this->roles as $role) {
foreach ($role->getPermissions() as $slug => $array) {
if ( array_key_exists($slug, $permissions) ) {
foreach ($array as $clearance => $value) {
! $value ?: $permissions[$slug][$clearance] = true;
}
} else {
$permissions = array_merge($permissions, [$slug => $array]);
}
}
}

// permissions based on role.
// more permissive permission wins
// if user has multiple roles we keep
// true values.
foreach ($this->roles as $role) {
foreach ($role->getPermissions() as $slug => $array) {
if ( array_key_exists($slug, $permissions) ) {
foreach ($array as $clearance => $value) {
if( !array_key_exists( $clearance, $permissions[$slug] ) ) {
! $value ?: $permissions[$slug][$clearance] = true;
// user permissions that override roles ones.
foreach ($this->getPermissionsInherited() as $slug => $array) {
if ( array_key_exists($slug, $permissions) ) {
foreach ($array as $clearance => $value) {
$permissions[$slug][$clearance] = $value;
}
} else {
$permissions = array_merge($permissions, [$slug => $array]);
}
} else {
$permissions = array_merge($permissions, [$slug => $array]);
}
return $permissions;
}
}
);

return $permissions;
}
Expand Down Expand Up @@ -98,6 +107,8 @@ function () {
*/
public function assignPermission($permission)
{
$this->deletePermissionCache();

return $this->mapArray($permission, function ($permission) {

$permissionId = $this->parsePermissionId($permission);
Expand All @@ -120,6 +131,8 @@ public function assignPermission($permission)
*/
public function revokePermission($permission)
{
$this->deletePermissionCache();

return $this->mapArray($permission, function ($permission) {

$permissionId = $this->parsePermissionId($permission);
Expand All @@ -143,7 +156,7 @@ public function syncPermissions($permissions)

return $sync;
});

$this->deletePermissionCache();
return $this->permissions()->sync($sync);
}

Expand All @@ -154,6 +167,7 @@ public function syncPermissions($permissions)
*/
public function revokeAllPermissions()
{
$this->deletePermissionCache();
return $this->permissions()->detach();
}

Expand All @@ -165,6 +179,17 @@ public function revokeAllPermissions()
*/


/**
* Delete cache for this traits.
*
* @return null
*/
protected function deletePermissionCache()
{
\Cache::forget('acl.getPermissionsById_'.$this->id);
\Cache::forget('acl.getMergeById_'.$this->id);
}

/**
* Parses permission id from object or array.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Kodeine/Acl/Traits/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ public function hasRole($slug, $operator = null)
*/
public function assignRole($role)
{
$this->deleteRoleCache();

return $this->mapArray($role, function ($role) {

$roleId = $this->parseRoleId($role);

if ( ! $this->roles->keyBy('id')->has($roleId) ) {

$this->roles()->attach($roleId);

return $role;
Expand All @@ -139,6 +142,8 @@ public function assignRole($role)
*/
public function revokeRole($role)
{
$this->deleteRoleCache();

return $this->mapArray($role, function ($role) {

$roleId = $this->parseRoleId($role);
Expand All @@ -163,6 +168,8 @@ public function syncRoles($roles)
return $sync;
});

$this->deleteRoleCache();

return $this->roles()->sync($sync);
}

Expand All @@ -173,6 +180,8 @@ public function syncRoles($roles)
*/
public function revokeAllRoles()
{
$this->deleteRoleCache();

return $this->roles()->detach();
}

Expand All @@ -183,6 +192,16 @@ public function revokeAllRoles()
|
*/

/**
* Delete cache for this traits.
*
* @return null
*/
protected function deleteRoleCache()
{
\Cache::forget('acl.getRolesById_'.$this->id);
}

/**
* @param $slug
* @param $roles
Expand Down