Skip to content

Commit

Permalink
improved permission attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Apr 3, 2020
1 parent babffc0 commit 04f2872
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
45 changes: 35 additions & 10 deletions app/Traits/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public function attachPermissionsToPortalRoles($permissions)

public function attachPermissionsToAllRoles($permissions, $require = 'read-admin-panel')
{
$roles = Role::all()->filter(function ($r) use ($require) {
return $require ? $r->hasPermission($require) : true;
});
$this->getRoles($require)->each(function ($role) use ($permissions) {
foreach ($permissions as $id => $permission) {
if ($this->isActionList($permission)) {
$this->attachPermissionsByAction($role, $id, $permission);

continue;
}

foreach ($roles as $role) {
foreach ($permissions as $permission) {
$this->attachPermission($role, $permission);
}
}
});
}

public function detachPermissionsByRoleNames($roles)
Expand Down Expand Up @@ -200,13 +202,18 @@ public function createModuleWidgetPermission($module, $class)
}

public function createModuleSettingPermission($module, $action)
{
return $this->createModuleControllerPermission($module, $action, 'settings');
}

public function createModuleControllerPermission($module, $action, $controller)
{
if (is_string($module)) {
$module = module($module);
}

$name = $action . '-' . $module->getAlias() . '-settings';
$display_name = Str::title($action) . ' ' . $module->getName() . ' Settings';
$name = $action . '-' . $module->getAlias() . '-' . $controller;
$display_name = Str::title($action) . ' ' . $module->getName() . ' ' . Str::title($controller);

return $this->createPermission($name, $display_name);
}
Expand Down Expand Up @@ -276,7 +283,8 @@ public function isActionList($permission)
return false;
}

return (strlen($permission) == '1') || Str::contains($permission, ',');
// c || c,r,u,d
return (Str::length($permission) == 1) || Str::contains($permission, ',');
}

public function attachPermissionsByAction($role, $page, $action_list)
Expand All @@ -296,6 +304,23 @@ public function attachPermissionsByAction($role, $page, $action_list)

public function getPermissionDisplayName($name)
{
return Str::title(str_replace('-', ' ', $name));
if (!empty($this->alias)) {
$name = str_replace($this->alias, '{Module Placeholder}', $name);
}

$name = Str::title(str_replace('-', ' ', $name));

if (!empty($this->alias)) {
$name = str_replace('{Module Placeholder}', module($this->alias)->getName(), $name);
}

return $name;
}

public function getRoles($require = 'read-admin-panel')
{
return Role::all()->filter(function ($role) use ($require) {
return $require ? $role->hasPermission($require) : true;
});
}
}
3 changes: 1 addition & 2 deletions modules/OfflinePayments/Http/Controllers/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

class Settings extends Controller
{

/**
* Show the form for editing the specified resource.
*
Expand Down Expand Up @@ -157,7 +156,7 @@ public function destroy(DRequest $request)

$message = trans('messages.success.deleted', ['type' => $remove['name']]);

// because it show nofitication.
// because it show notification.
//flash($message)->success();

return response()->json([
Expand Down
9 changes: 5 additions & 4 deletions modules/OfflinePayments/Listeners/InstallModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class InstallModule
{
use Permissions;

public $alias = 'offline-payments';

/**
* Handle the event.
*
Expand All @@ -17,7 +19,7 @@ class InstallModule
*/
public function handle(Event $event)
{
if ($event->alias != 'offline-payments') {
if ($event->alias != $this->alias) {
return;
}

Expand All @@ -26,10 +28,9 @@ public function handle(Event $event)

protected function updatePermissions()
{
// c=create, r=read, u=update, d=delete
$this->attachPermissionsToAdminRoles([
$this->createModuleSettingPermission('offline-payments', 'read'),
$this->createModuleSettingPermission('offline-payments', 'update'),
$this->createModuleSettingPermission('offline-payments', 'delete'),
$this->alias . '-settings' => 'r,u,d',
]);
}
}

0 comments on commit 04f2872

Please sign in to comment.