Skip to content

Commit

Permalink
on demand permission name exclude method added
Browse files Browse the repository at this point in the history
  • Loading branch information
Nur Alam committed Mar 31, 2024
1 parent 7ac2a8f commit ca3ad65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Permissions

protected string $panel = 'user';

protected array $excludePermissions = [];

public function __construct()
{
$this->splitter = config('permission-generator.route-name-splitter-needle');
Expand All @@ -37,6 +39,13 @@ public function panel(string $panel)
return $this;
}

public function exclude(array $permissions)
{
$this->excludePermissions = $permissions;

return $this;
}

public function fromResources(array $resources)
{
if ($this->hasCachedPermissions()) {
Expand All @@ -59,7 +68,7 @@ public function fromRoutes()
return $this;
}

$routePermissionGenerator = (new RoutePermissionGenerator($this->panel))->generate();
$routePermissionGenerator = (new RoutePermissionGenerator($this->panel))->exclude($this->excludePermissions)->generate();

$this->permissions = $routePermissionGenerator['permissions'];
$this->onlyPermissionsNames = $routePermissionGenerator['only_permission_names'];
Expand Down
19 changes: 18 additions & 1 deletion src/Services/RoutePermissionGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,26 @@ class RoutePermissionGenerator

private string $panel;

protected array $excludePermissions = [];

public function __construct(string $panel = 'user')
{
$this->panel = $panel;
}

/**
* Exclude specific permissions from the permission list.
*
* @param array $permissions exclude permissions on demand.
* @return $this
*/
public function exclude(array $permissions)
{
$this->excludePermissions = $permissions;

return $this;
}

public function generate()
{
$this->permissionGenerateControllers = config(
Expand All @@ -35,6 +50,8 @@ public function generate()

$globalExcludedRoutes = config("permission-generator.exclude-routes.panels.{$this->panel}", []);

$excludedRoutes = array_merge($globalExcludedRoutes, $this->excludePermissions);

$routes = Route::getRoutes();

$onlyPermissionNames = [];
Expand All @@ -45,7 +62,7 @@ public function generate()
$routeName = $route->getName();

// exclude routes which defined in the config
if (in_array($routeName, $globalExcludedRoutes)) {
if (in_array($routeName, $excludedRoutes)) {
continue;
}

Expand Down

0 comments on commit ca3ad65

Please sign in to comment.