improved permissions trait

This commit is contained in:
denisdulici
2020-02-06 17:08:20 +03:00
parent 9fd64ad696
commit ac4fd86ab3
7 changed files with 262 additions and 131 deletions

View File

@ -2,18 +2,17 @@
namespace Akaunting\Module\Commands;
use App\Models\Auth\Permission;
use App\Models\Auth\Role;
use App\Models\Module\Module;
use App\Models\Module\ModuleHistory;
use App\Utilities\Reports;
use App\Utilities\Widgets;
use App\Traits\Permissions;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
class InstallCommand extends Command
{
use Permissions;
/**
* The name and signature of the console command.
*
@ -68,9 +67,7 @@ class InstallCommand extends Command
event(new \App\Events\Module\Installed($alias, $company_id));
if (!empty($module->get('reports')) || !empty($module->get('widgets')) || !empty($module->get('settings'))) {
$this->updatePermissions($module);
}
$this->attachDefaultModulePermissions($module);
session()->forget('company_id');
@ -93,76 +90,4 @@ class InstallCommand extends Command
array('company_id', InputArgument::REQUIRED, 'Company ID.'),
);
}
protected function updatePermissions($module)
{
$permissions = [];
if (!empty($module->get('reports'))) {
foreach ($module->get('reports') as $class) {
if (!class_exists($class)) {
continue;
}
$name = Reports::getPermission($class);
$display_name = (new $class())->getDefaultName();
$permissions[] = Permission::firstOrCreate([
'name' => $name
], [
'display_name' => 'Read ' . $module->getName() . ' Reports ' . $display_name,
'description' => 'Read ' . $module->getName() . ' Reports ' . $display_name,
]);
}
}
if (!empty($module->get('widgets'))) {
foreach ($module->get('widgets') as $class) {
if (!class_exists($class)) {
continue;
}
$name = Widgets::getPermission($class);
$display_name = (new $class())->getDefaultName();
$permissions[] = Permission::firstOrCreate([
'name' => $name
], [
'display_name' => 'Read ' . $module->getName() . ' Widgets ' . $display_name,
'description' => 'Read ' . $module->getName() . ' Widgets ' . $display_name,
]);
}
}
if (!empty($module->get('settings'))) {
$permissions[] = Permission::firstOrCreate([
'name' => 'read-' . $module->getAlias() . '-settings'
], [
'display_name' => 'Read ' . $module->getName() . ' Settings',
'description' => 'Read ' . $module->getName() . ' Settings',
]);
$permissions[] = Permission::firstOrCreate([
'name' => 'update-' . $module->getAlias() . '-settings'
], [
'display_name' => 'Update ' . $module->getName() . ' Settings',
'description' => 'Update ' . $module->getName() . ' Settings',
]);
}
// Attach permission to roles
$roles = Role::all()->filter(function ($r) {
return $r->hasPermission('read-admin-panel');
});
foreach ($roles as $role) {
foreach ($permissions as $permission) {
if ($role->hasPermission($permission->name)) {
continue;
}
$role->attachPermission($permission);
}
}
}
}