diff --git a/app/Http/Controllers/Common/Dashboards.php b/app/Http/Controllers/Common/Dashboards.php index 39fdae997..93f20c636 100644 --- a/app/Http/Controllers/Common/Dashboards.php +++ b/app/Http/Controllers/Common/Dashboards.php @@ -10,7 +10,6 @@ use App\Jobs\Common\UpdateDashboard; use App\Models\Common\Company; use App\Models\Common\Dashboard; use App\Models\Common\Widget; -use App\Models\Module\Module; use App\Traits\DateTime; use App\Traits\Users; use App\Utilities\Widgets; @@ -69,13 +68,7 @@ class Dashboards extends Controller } $widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) { - if ($alias = Widgets::getModuleAlias($widget->class)) { - if (!Module::alias($alias)->enabled()->first()) { - return false; - } - } - - return Widgets::canRead($widget->class); + return Widgets::canShow($widget->class); }); $financial_start = $this->getFinancialStart()->format('Y-m-d'); diff --git a/app/Http/Controllers/Common/Reports.php b/app/Http/Controllers/Common/Reports.php index bfb075f36..93a5fcbf5 100644 --- a/app/Http/Controllers/Common/Reports.php +++ b/app/Http/Controllers/Common/Reports.php @@ -25,7 +25,7 @@ class Reports extends Controller $reports = Report::orderBy('name')->get(); foreach ($reports as $report) { - if (!Utility::canRead($report->class)) { + if (!Utility::canShow($report->class)) { continue; } @@ -56,7 +56,7 @@ class Reports extends Controller */ public function show(Report $report) { - if (!Utility::canRead($report->class)) { + if (!Utility::canShow($report->class)) { abort(403); } @@ -203,7 +203,7 @@ class Reports extends Controller */ public function print(Report $report) { - if (!Utility::canRead($report->class)) { + if (!Utility::canShow($report->class)) { abort(403); } @@ -218,7 +218,7 @@ class Reports extends Controller */ public function export(Report $report) { - if (!Utility::canRead($report->class)) { + if (!Utility::canShow($report->class)) { abort(403); } @@ -263,7 +263,7 @@ class Reports extends Controller public function clear() { Report::all()->each(function ($report) { - if (!Utility::canRead($report->class)) { + if (!Utility::canShow($report->class)) { return; } diff --git a/app/Utilities/Reports.php b/app/Utilities/Reports.php index 8e6b26727..62bb8ba82 100644 --- a/app/Utilities/Reports.php +++ b/app/Utilities/Reports.php @@ -56,6 +56,11 @@ class Reports return new $class($model, $load_data); } + public static function canShow($class) + { + return (static::isModuleEnabled($class) && static::canRead($class)); + } + public static function canRead($class) { return user()->can(static::getPermission($class)); @@ -68,8 +73,8 @@ class Reports $prefix = 'read-'; // Add module - if (strtolower($arr[0]) == 'modules') { - $prefix .= Str::kebab($arr[1]) . '-'; + if ($alias = static::getModuleAlias($arr)) { + $prefix .= $alias . '-'; } $prefix .= 'reports-'; @@ -85,4 +90,35 @@ class Reports { return (new $class())->getDefaultName(); } + + public static function isModuleEnabled($class) + { + if (!$alias = static::getModuleAlias($class)) { + return true; + } + + if (Module::alias($alias)->enabled()->first()) { + return true; + } + + return false; + } + + public static function isModule($class) + { + $arr = is_array($class) ? $class : explode('\\', $class); + + return (strtolower($arr[0]) == 'modules'); + } + + public static function getModuleAlias($class) + { + if (!static::isModule($class)) { + return false; + } + + $arr = is_array($class) ? $class : explode('\\', $class); + + return Str::kebab($arr[1]); + } } diff --git a/app/Utilities/Widgets.php b/app/Utilities/Widgets.php index aaad77430..2a858626b 100644 --- a/app/Utilities/Widgets.php +++ b/app/Utilities/Widgets.php @@ -88,6 +88,11 @@ class Widgets return $class->show(...$arguments); } + public static function canShow($class) + { + return (static::isModuleEnabled($class) && static::canRead($class)); + } + public static function canRead($class) { return user()->can(static::getPermission($class)); @@ -100,7 +105,7 @@ class Widgets $prefix = 'read-'; // Add module - if ($alias = Widgets::getModuleAlias($class)) { + if ($alias = static::getModuleAlias($arr)) { $prefix .= $alias . '-'; } @@ -118,9 +123,22 @@ class Widgets return (new $class())->getDefaultName(); } + public static function isModuleEnabled($class) + { + if (!$alias = static::getModuleAlias($class)) { + return true; + } + + if (Module::alias($alias)->enabled()->first()) { + return true; + } + + return false; + } + public static function isModule($class) { - $arr = explode('\\', $class); + $arr = is_array($class) ? $class : explode('\\', $class); return (strtolower($arr[0]) == 'modules'); } @@ -131,7 +149,7 @@ class Widgets return false; } - $arr = explode('\\', $class); + $arr = is_array($class) ? $class : explode('\\', $class); return Str::kebab($arr[1]); }