Skip to content

Commit

Permalink
Fixed caching of custom actions and loading of role creation and edit…
Browse files Browse the repository at this point in the history
… pages.
  • Loading branch information
mikebronner committed Nov 17, 2022
1 parent bb37898 commit 173aeac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"GeneaLabs",
"Laravel"
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
# Change Log
[Package Checklist](http://phppackagechecklist.com/#1,2,3,4,6,7,8,9,10,11,12,13,14)

## [0.19.8] - 2022-11-17
### Fixed
- caching of custom actions.
- loading of role creation and edit pages.

## [0.19.5] - 2022-07-20
### Removed
- creation of SuperAdmin permission records.
Expand Down
78 changes: 49 additions & 29 deletions src/Http/Controllers/RolesController.php
Expand Up @@ -4,14 +4,16 @@

namespace GeneaLabs\LaravelGovernor\Http\Controllers;

use Illuminate\View\View;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use GeneaLabs\LaravelGovernor\Role;
use GeneaLabs\LaravelGovernor\Action;
use GeneaLabs\LaravelGovernor\Entity;
use Illuminate\Http\RedirectResponse;
use GeneaLabs\LaravelGovernor\Traits\EntityManagement;
use GeneaLabs\LaravelGovernor\Http\Requests\CreateRoleRequest;
use GeneaLabs\LaravelGovernor\Http\Requests\UpdateRoleRequest;
use GeneaLabs\LaravelGovernor\Role;
use GeneaLabs\LaravelGovernor\Traits\EntityManagement;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Illuminate\View\View;

class RolesController extends Controller
{
Expand All @@ -27,7 +29,6 @@ public function index(): View
$roleClass = config("genealabs-laravel-governor.models.role");
$this->authorize('viewAny', $roleClass);
$roles = (new $roleClass)
->with("users")
->orderBy("name")
->get();

Expand All @@ -50,10 +51,17 @@ public function create(): View
$ownerships = collect(["not" => ""])->merge($ownerships);
$role = new $roleClass;
$this->authorize('create', $role);
$permissionMatrix = $this->createPermissionMatrix($role, $entities);
$customActions = (new Action)
->distinct()
->get()
->filter(function (Action $action): bool {
return $action->model_class !== "";
});
$permissionMatrix = $this->createPermissionMatrix($role, $entities, $customActions);

return view('genealabs-laravel-governor::roles.create')
->with([
"customActions" => $customActions,
"entities" => $entities,
"ownerships" => $ownerships,
"permissionMatrix" => $permissionMatrix,
Expand All @@ -71,7 +79,6 @@ public function store(CreateRoleRequest $request): RedirectResponse
public function edit(Role $role): View
{
$this->authorize('update', $role);

$entityClass = config("genealabs-laravel-governor.models.entity");
$ownershipClass = config("genealabs-laravel-governor.models.ownership");
$roleClass = config("genealabs-laravel-governor.models.role");
Expand All @@ -83,7 +90,7 @@ public function edit(Role $role): View
$permissibleClass = request("filter") === "team_id"
? $teamClass
: $roleClass;
$role->load("permissions.action", "permissions.entity", "permissions.ownership");
$role->load("permissions");

if (request("owner") === "yes") {
return $role
Expand All @@ -95,17 +102,26 @@ public function edit(Role $role): View
$this->parsePolicies();

$entities = (new $entityClass)
->whereNotIn("name", ['Permission (Laravel Governor)', 'Entity (Laravel Governor)', "Action (Laravel Governor)", "Ownership (Laravel Governor)", "Team Invitation (Laravel Governor)"])
->whereNotIn(
"name",
[
"Action (Laravel Governor)",
"Entity (Laravel Governor)",
"Ownership (Laravel Governor)",
"Permission (Laravel Governor)",
"Team Invitation (Laravel Governor)",
],
)
->orderBy("group_name")
->orderBy("name")
->get();
$customActions = (new Action)
->distinct()
->get()
->filter(function ($action): bool {
->filter(function (Action $action): bool {
return $action->model_class !== "";
});

$permissionMatrix = $this->createPermissionMatrix($role, $entities);
$permissionMatrix = $this->createPermissionMatrix($role, $entities, $customActions);
$ownerships = collect(["not" => "no"])->merge($ownerships);

return view('genealabs-laravel-governor::roles.edit', compact(
Expand All @@ -132,33 +148,37 @@ public function destroy(Role $role): RedirectResponse
return redirect()->route('genealabs.laravel-governor.roles.index');
}

protected function createPermissionMatrix(Role $role, Collection $entities): array
protected function createPermissionMatrix(Role $role, Collection $entities, Collection $customActions): array
{
$permissionMatrix = [];

$actionClass = app(config('genealabs-laravel-governor.models.action'));
$actions = (new $actionClass)
->orderBy("name")
->get();

foreach ($entities as $entity) {
foreach ($actions as $action) {
$selectedOwnership = 'no';

foreach ($role->permissions as $permissioncheck) {
if (($permissioncheck->entity->name === $entity->name)
&& ($permissioncheck->action->name === $action->name)) {
$selectedOwnership = $permissioncheck->ownership->name;
}
}

$entities->each(function (Entity $entity) use ($actions, $customActions, &$permissionMatrix, $role) {
// dd($customActions->where("entity", $entity)->first());
$customActions = $customActions->where("entity", $entity);
$permissions = $role->permissions
->where("entity_name", $entity->name);
$actions
->filter(function (Action $action) use ($entity): bool {
return Str::contains($action->name, "\\{$entity->name}:")
|| ! Str::contains($action->name, ":");
})
->each(function (Action $action) use ($customActions, $entity, $permissions, &$permissionMatrix) {
$selectedOwnership = $permissions
->where("action_name", $action->name)
->first()
?->ownership_name
?? "no";
$groupName = ucwords(
$entity->group_name
?? "Ungrouped"
);

$permissionMatrix[$groupName][$entity->name][$action->name] = $selectedOwnership;
}
}
});
});

return $permissionMatrix;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Middleware/ParseCustomPolicyActions.php
Expand Up @@ -32,7 +32,8 @@ protected function registerCustomPolicyActions(): void
return collect();
}

return $this->getCustomActionMethods($policyClass)
return $this
->getCustomActionMethods($policyClass)
->map(function (string $method) use ($modelClass): Action {
$action = app("governor-actions")
->where("name", "{$modelClass}:{$method}")
Expand All @@ -55,7 +56,7 @@ protected function registerCustomPolicyActions(): void

protected function getCustomActionMethods(string $policyClass): Collection
{
return cache()->remember("genealabs:laravel-governor:custom-action-methods", 300, function () use ($policyClass): Collection {
return cache()->remember("genealabs:laravel-governor:custom-action-methods:policy-{$policyClass}", 300, function () use ($policyClass): Collection {
$parentClass = new ReflectionClass(get_parent_class($policyClass));
$parentMethods = collect($parentClass->getMethods(ReflectionMethod::IS_PUBLIC))
->pluck("name");
Expand Down

0 comments on commit 173aeac

Please sign in to comment.