Skip to content

Commit

Permalink
Merge pull request #5 from mr-luke/feature/scope-restricted
Browse files Browse the repository at this point in the history
Scope Restrictions
  • Loading branch information
hsmusz committed Nov 22, 2019
2 parents bfc4a1b + c105b74 commit 0193076
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,18 @@ public function getAuthorizableMigration(): array
*/
public function getPermission(Permitable $subject, string $scope, bool $checkScope = true)
{
if($checkScope) {
if ($checkScope) {
$this->checkScope($scope);
}

if (':*' === substr($scope, -2)) {
$scope = substr($scope, 0, -2);

return $subject->permissions->sortbyDesc('level')->first(function ($perm) use ($scope) {
return preg_match("/^(${scope}$|${scope}:)/", $perm['scope']);
});
}

return $subject->permissions->where('scope', $scope)->first();
}

Expand Down
38 changes: 38 additions & 0 deletions src/PermissionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mrluke\Privileges;

/**
* Class PermissionResolver
* @package Mrluke\Privileges
*
* @author Hubert Smusz <hubert.smusz@movecloser.pl>
* @version 1.0.0
*/
class PermissionResolver
{

/**
* @param $subject
* @param string $scope
* @param int|null $level
*
* @return array
*/
public static function getScopeRestrictions($subject, string $scope, int $level = null): array
{
$scopes = $subject->roles->flatMap(function ($role) use ($scope, $level) {
if ($level) {
$permissions = $role->permissions->where('level', $level);
} else {
$permissions = $role->permissions;
}

return preg_grep("/^${scope}:/", array_column($permissions->toArray(), 'scope'));
})->toArray();

return array_map(function ($scope) {
return explode(':', $scope)[1];
}, $scopes);
}
}

0 comments on commit 0193076

Please sign in to comment.