Skip to content

Commit

Permalink
Avoid unused data and hooks when using the default role level
Browse files Browse the repository at this point in the history
  • Loading branch information
joyceverheije committed Oct 27, 2023
1 parent f9a096a commit 5d4060b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Http/Controllers/Admin/RoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ protected function getIndexOption($option, $item = null)
protected function formData($request): array
{
return [
'permission_modules' => Permission::permissionableParentModuleItems(),
'permission_modules' => Permission::permissionableModules()->filter(function ($module) {
return !strpos($module, '.');
}),
];
}

Expand Down
12 changes: 9 additions & 3 deletions src/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Enums\PermissionLevel;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Group;
Expand Down Expand Up @@ -203,15 +204,20 @@ protected function formData($request)
$titleThumbnail = $user->cmsImage($role, $crop, $params);
}

if (TwillPermissions::levelIs(PermissionLevel::LEVEL_ROLE_GROUP_ITEM)) {
$permissionsData = [
'permissionModules' => $this->getPermissionModules(),
];
}

return [
'roleList' => $this->getRoleList(),
'titleThumbnail' => $titleThumbnail ?? null,
'permissionModules' => $this->getPermissionModules(),
'groupPermissionMapping' => $this->getGroupPermissionMapping(),
'with2faSettings' => $with2faSettings,
'qrCode' => $qrCode ?? null,
'groupPermissionMapping' => $this->getGroupPermissionMapping(),
'groupOptions' => $this->getGroups(),
];
] + ($permissionsData ?? []);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Repositories/Behaviors/HandleUserPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace A17\Twill\Repositories\Behaviors;

use A17\Twill\Enums\PermissionLevel;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Model;
use A17\Twill\Models\Permission;
use A17\Twill\Models\User;
Expand All @@ -19,7 +21,8 @@ trait HandleUserPermissions
*/
public function getFormFieldsHandleUserPermissions($object, $fields)
{
if (!config('twill.enabled.permissions-management')) {
if (!config('twill.enabled.permissions-management') ||
!TwillPermissions::levelIs(PermissionLevel::LEVEL_ROLE_GROUP_ITEM)) {
return $fields;
}

Expand Down Expand Up @@ -48,7 +51,9 @@ public function afterSaveHandleUserPermissions($object, $fields)

$this->addOrRemoveUserToEveryoneGroup($object);

$this->updateUserItemPermissions($object, $fields);
if (TwillPermissions::levelIs(PermissionLevel::LEVEL_ROLE_GROUP_ITEM)) {
$this->updateUserItemPermissions($object, $fields);
}
}

private function addOrRemoveUserToEveryoneGroup(User $user)
Expand Down
2 changes: 1 addition & 1 deletion views/roles/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
field-name="manage-modules"
:field-values="false"
>
@foreach($permission_modules as $module_name => $module_items)
@foreach($permission_modules as $module_name)
<x-twill::select
:name="'module_' . $module_name . '_permissions'"
:label="ucfirst($module_name) . ' permissions'"
Expand Down

0 comments on commit 5d4060b

Please sign in to comment.