fire event before authorizing menu item
This commit is contained in:
parent
c45d7f596f
commit
7ddc9a6995
20
app/Events/Menu/ItemAuthorizing.php
Normal file
20
app/Events/Menu/ItemAuthorizing.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Menu;
|
||||||
|
|
||||||
|
use App\Abstracts\Event;
|
||||||
|
|
||||||
|
class ItemAuthorizing extends Event
|
||||||
|
{
|
||||||
|
public $item;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @param $item
|
||||||
|
*/
|
||||||
|
public function __construct($item)
|
||||||
|
{
|
||||||
|
$this->item = $item;
|
||||||
|
}
|
||||||
|
}
|
@ -3,9 +3,12 @@
|
|||||||
namespace App\Listeners\Menu;
|
namespace App\Listeners\Menu;
|
||||||
|
|
||||||
use App\Events\Menu\AdminCreated as Event;
|
use App\Events\Menu\AdminCreated as Event;
|
||||||
|
use App\Traits\Permissions;
|
||||||
|
|
||||||
class AddAdminItems
|
class AddAdminItems
|
||||||
{
|
{
|
||||||
|
use Permissions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the event.
|
* Handle the event.
|
||||||
*
|
*
|
||||||
@ -16,15 +19,15 @@ class AddAdminItems
|
|||||||
{
|
{
|
||||||
$menu = $event->menu;
|
$menu = $event->menu;
|
||||||
|
|
||||||
$user = user();
|
|
||||||
$attr = ['icon' => ''];
|
$attr = ['icon' => ''];
|
||||||
|
|
||||||
// Dashboards
|
// Dashboards
|
||||||
if ($user->can('read-common-dashboards')) {
|
$title = trim(trans_choice('general.dashboards', 2));
|
||||||
$dashboards = $user->dashboards()->enabled()->get();
|
if ($this->canAccessMenuItem($title, 'read-common-dashboards')) {
|
||||||
|
$dashboards = user()->dashboards()->enabled()->get();
|
||||||
|
|
||||||
if ($dashboards->count() > 1) {
|
if ($dashboards->count() > 1) {
|
||||||
$menu->dropdown(trim(trans_choice('general.dashboards', 2)), function ($sub) use ($user, $attr, $dashboards) {
|
$menu->dropdown($title, function ($sub) use ($attr, $dashboards) {
|
||||||
foreach ($dashboards as $key => $dashboard) {
|
foreach ($dashboards as $key => $dashboard) {
|
||||||
if (session('dashboard_id') != $dashboard->id) {
|
if (session('dashboard_id') != $dashboard->id) {
|
||||||
$sub->route('dashboards.switch', $dashboard->name, ['dashboard' => $dashboard->id], $key, $attr);
|
$sub->route('dashboards.switch', $dashboard->name, ['dashboard' => $dashboard->id], $key, $attr);
|
||||||
@ -34,7 +37,7 @@ class AddAdminItems
|
|||||||
}
|
}
|
||||||
}, 10, [
|
}, 10, [
|
||||||
'url' => '/' . company_id(),
|
'url' => '/' . company_id(),
|
||||||
'title' => trans_choice('general.dashboards', 2),
|
'title' => $title,
|
||||||
'icon' => 'fa fa-tachometer-alt',
|
'icon' => 'fa fa-tachometer-alt',
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
@ -48,87 +51,104 @@ class AddAdminItems
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Items
|
// Items
|
||||||
if ($user->can('read-common-items')) {
|
$title = trim(trans_choice('general.items', 2));
|
||||||
$menu->route('items.index', trans_choice('general.items', 2), [], 20, ['icon' => 'fa fa-cube']);
|
if ($this->canAccessMenuItem($title, 'read-common-items')) {
|
||||||
|
$menu->route('items.index', $title, [], 20, ['icon' => 'fa fa-cube']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sales
|
// Sales
|
||||||
if ($user->canAny(['read-sales-invoices', 'read-sales-revenues', 'read-sales-customers'])) {
|
$title = trim(trans_choice('general.sales', 2));
|
||||||
$menu->dropdown(trim(trans_choice('general.sales', 2)), function ($sub) use ($user, $attr) {
|
if ($this->canAccessMenuItem($title, ['read-sales-invoices', 'read-sales-revenues', 'read-sales-customers'])) {
|
||||||
if ($user->can('read-sales-invoices')) {
|
$menu->dropdown($title, function ($sub) use ($attr) {
|
||||||
$sub->route('invoices.index', trans_choice('general.invoices', 2), [], 10, $attr);
|
$title = trim(trans_choice('general.invoices', 2));
|
||||||
|
if ($this->canAccessMenuItem($title, 'read-sales-invoices')) {
|
||||||
|
$sub->route('invoices.index', $title, [], 10, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-sales-revenues')) {
|
$title = trim(trans_choice('general.revenues', 2));
|
||||||
$sub->route('revenues.index', trans_choice('general.revenues', 2), [], 20, $attr);
|
if ($this->canAccessMenuItem($title, 'read-sales-revenues')) {
|
||||||
|
$sub->route('revenues.index', $title, [], 20, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-sales-customers')) {
|
$title = trim(trans_choice('general.customers', 2));
|
||||||
$sub->route('customers.index', trans_choice('general.customers', 2), [], 30, $attr);
|
if ($this->canAccessMenuItem($title, 'read-sales-customers')) {
|
||||||
|
$sub->route('customers.index', $title, [], 30, $attr);
|
||||||
}
|
}
|
||||||
}, 30, [
|
}, 30, [
|
||||||
'title' => trans_choice('general.sales', 2),
|
'title' => $title,
|
||||||
'icon' => 'fa fa-money-bill',
|
'icon' => 'fa fa-money-bill',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Purchases
|
// Purchases
|
||||||
if ($user->canAny(['read-purchases-bills', 'read-purchases-payments', 'read-purchases-vendors'])) {
|
$title = trim(trans_choice('general.purchases', 2));
|
||||||
$menu->dropdown(trim(trans_choice('general.purchases', 2)), function ($sub) use ($user, $attr) {
|
if ($this->canAccessMenuItem($title, ['read-purchases-bills', 'read-purchases-payments', 'read-purchases-vendors'])) {
|
||||||
if ($user->can('read-purchases-bills')) {
|
$menu->dropdown($title, function ($sub) use ($attr) {
|
||||||
$sub->route('bills.index', trans_choice('general.bills', 2), [], 10, $attr);
|
$title = trim(trans_choice('general.bills', 2));
|
||||||
|
if ($this->canAccessMenuItem($title, 'read-purchases-bills')) {
|
||||||
|
$sub->route('bills.index', $title, [], 10, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-purchases-payments')) {
|
$title = trim(trans_choice('general.payments', 2));
|
||||||
$sub->route('payments.index', trans_choice('general.payments', 2), [], 20, $attr);
|
if ($this->canAccessMenuItem($title, 'read-purchases-payments')) {
|
||||||
|
$sub->route('payments.index', $title, [], 20, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-purchases-vendors')) {
|
$title = trim(trans_choice('general.vendors', 2));
|
||||||
$sub->route('vendors.index', trans_choice('general.vendors', 2), [], 30, $attr);
|
if ($this->canAccessMenuItem($title, 'read-purchases-vendors')) {
|
||||||
|
$sub->route('vendors.index', $title, [], 30, $attr);
|
||||||
}
|
}
|
||||||
}, 40, [
|
}, 40, [
|
||||||
'title' => trans_choice('general.purchases', 2),
|
'title' => $title,
|
||||||
'icon' => 'fa fa-shopping-cart',
|
'icon' => 'fa fa-shopping-cart',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Banking
|
// Banking
|
||||||
if ($user->canAny(['read-banking-accounts', 'read-banking-transfers', 'read-banking-transactions', 'read-banking-reconciliations'])) {
|
$title = trim(trans('general.banking'));
|
||||||
$menu->dropdown(trim(trans('general.banking')), function ($sub) use ($user, $attr) {
|
if ($this->canAccessMenuItem($title, ['read-banking-accounts', 'read-banking-transfers', 'read-banking-transactions', 'read-banking-reconciliations'])) {
|
||||||
if ($user->can('read-banking-accounts')) {
|
$menu->dropdown($title, function ($sub) use ($attr) {
|
||||||
$sub->route('accounts.index', trans_choice('general.accounts', 2), [], 10, $attr);
|
$title = trim(trans_choice('general.accounts', 2));
|
||||||
|
if ($this->canAccessMenuItem($title, 'read-banking-accounts')) {
|
||||||
|
$sub->route('accounts.index', $title, [], 10, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-banking-transfers')) {
|
$title = trim(trans_choice('general.transfers', 2));
|
||||||
$sub->route('transfers.index', trans_choice('general.transfers', 2), [], 20, $attr);
|
if ($this->canAccessMenuItem($title, 'read-banking-transfers')) {
|
||||||
|
$sub->route('transfers.index', $title, [], 20, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-banking-transactions')) {
|
$title = trim(trans_choice('general.transactions', 2));
|
||||||
$sub->route('transactions.index', trans_choice('general.transactions', 2), [], 30, $attr);
|
if ($this->canAccessMenuItem($title, 'read-banking-transactions')) {
|
||||||
|
$sub->route('transactions.index', $title, [], 30, $attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->can('read-banking-reconciliations')) {
|
$title = trim(trans_choice('general.reconciliations', 2));
|
||||||
$sub->route('reconciliations.index', trans_choice('general.reconciliations', 2), [], 40, $attr);
|
if ($this->canAccessMenuItem($title, 'read-banking-reconciliations')) {
|
||||||
|
$sub->route('reconciliations.index', $title, [], 40, $attr);
|
||||||
}
|
}
|
||||||
}, 50, [
|
}, 50, [
|
||||||
'title' => trans('general.banking'),
|
'title' => $title,
|
||||||
'icon' => 'fa fa-briefcase',
|
'icon' => 'fa fa-briefcase',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reports
|
// Reports
|
||||||
if ($user->can('read-common-reports')) {
|
$title = trim(trans_choice('general.reports', 2));
|
||||||
$menu->route('reports.index', trans_choice('general.reports', 2), [], 60, ['icon' => 'fa fa-chart-pie']);
|
if ($this->canAccessMenuItem($title, 'read-common-reports')) {
|
||||||
|
$menu->route('reports.index', $title, [], 60, ['icon' => 'fa fa-chart-pie']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settings
|
// Settings
|
||||||
if ($user->can('read-settings-settings')) {
|
$title = trim(trans_choice('general.settings', 2));
|
||||||
$menu->route('settings.index', trans_choice('general.settings', 2), [], 70, ['icon' => 'fa fa-cog']);
|
if ($this->canAccessMenuItem($title, 'read-settings-settings')) {
|
||||||
|
$menu->route('settings.index', $title, [], 70, ['icon' => 'fa fa-cog']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apps
|
// Apps
|
||||||
if ($user->can('read-modules-home')) {
|
$title = trim(trans_choice('general.modules', 2));
|
||||||
$menu->route('apps.home.index', trans_choice('general.modules', 2), [], 80, ['icon' => 'fa fa-rocket']);
|
if ($this->canAccessMenuItem($title, 'read-modules-home')) {
|
||||||
|
$menu->route('apps.home.index', $title, [], 80, ['icon' => 'fa fa-rocket']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ use App\Traits\SearchString;
|
|||||||
use App\Utilities\Reports;
|
use App\Utilities\Reports;
|
||||||
use App\Utilities\Widgets;
|
use App\Utilities\Widgets;
|
||||||
use Illuminate\Routing\Route;
|
use Illuminate\Routing\Route;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
trait Permissions
|
trait Permissions
|
||||||
@ -473,4 +474,17 @@ trait Permissions
|
|||||||
$this->middleware('permission:update-' . $controller)->only('update', 'enable', 'disable');
|
$this->middleware('permission:update-' . $controller)->only('update', 'enable', 'disable');
|
||||||
$this->middleware('permission:delete-' . $controller)->only('destroy');
|
$this->middleware('permission:delete-' . $controller)->only('destroy');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canAccessMenuItem($title, $permissions)
|
||||||
|
{
|
||||||
|
$permissions = Arr::wrap($permissions);
|
||||||
|
|
||||||
|
$item = new \stdClass();
|
||||||
|
$item->title = $title;
|
||||||
|
$item->permissions = $permissions;
|
||||||
|
|
||||||
|
event(new \App\Events\Menu\ItemAuthorizing($item));
|
||||||
|
|
||||||
|
return user()->canAny($item->permissions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user