update roles

This commit is contained in:
Edward Yang 2022-08-02 10:55:06 +08:00
parent 26f0c2caa4
commit 19e5ecc29a
3 changed files with 80 additions and 47 deletions

View File

@ -12,12 +12,13 @@
namespace Beike\Admin\Repositories;
use Beike\Models\AdminUser;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
use Spatie\Permission\Models\Role;
class PermissionRepo
{
private $adminUser;
private $adminRole;
private AdminUser $adminUser;
private Role $adminRole;
public function setUser(AdminUser $user): PermissionRepo
{
@ -61,11 +62,11 @@ class PermissionRepo
private function getProductPermissions(): array
{
return [
['code' => 'product_list', 'name' => '商品列表', 'selected' => $this->hasPermission('product_list')],
['code' => 'product_create', 'name' => '商品创建', 'selected' => $this->hasPermission('product_create')],
['code' => 'product_show', 'name' => '商品详情', 'selected' => $this->hasPermission('product_show')],
['code' => 'product_update', 'name' => '商品编辑', 'selected' => $this->hasPermission('product_update')],
['code' => 'product_delete', 'name' => '商品删除', 'selected' => $this->hasPermission('product_delete')],
['code' => 'products_index', 'name' => '商品列表', 'selected' => $this->hasPermission('products_index')],
['code' => 'products_create', 'name' => '商品创建', 'selected' => $this->hasPermission('products_create')],
['code' => 'products_show', 'name' => '商品详情', 'selected' => $this->hasPermission('products_show')],
['code' => 'products_update', 'name' => '商品编辑', 'selected' => $this->hasPermission('products_update')],
['code' => 'products_delete', 'name' => '商品删除', 'selected' => $this->hasPermission('products_delete')],
];
}
@ -78,11 +79,11 @@ class PermissionRepo
private function getOrderPermissions(): array
{
return [
['code' => 'order_list', 'name' => '订单列表', 'selected' => $this->hasPermission('order_list')],
['code' => 'order_create', 'name' => '订单创建', 'selected' => $this->hasPermission('order_create')],
['code' => 'order_show', 'name' => '订单详情', 'selected' => $this->hasPermission('order_show')],
['code' => 'order_update', 'name' => '订单编辑', 'selected' => $this->hasPermission('order_update')],
['code' => 'order_delete', 'name' => '订单删除', 'selected' => $this->hasPermission('order_delete')],
['code' => 'orders_index', 'name' => '订单列表', 'selected' => $this->hasPermission('orders_index')],
['code' => 'orders_create', 'name' => '订单创建', 'selected' => $this->hasPermission('orders_create')],
['code' => 'orders_show', 'name' => '订单详情', 'selected' => $this->hasPermission('orders_show')],
['code' => 'orders_update', 'name' => '订单编辑', 'selected' => $this->hasPermission('orders_update')],
['code' => 'orders_delete', 'name' => '订单删除', 'selected' => $this->hasPermission('orders_delete')],
];
}
@ -95,10 +96,14 @@ class PermissionRepo
*/
private function hasPermission($permission): bool
{
if ($this->adminRole) {
return $this->adminRole->hasPermissionTo($permission);
} elseif ($this->adminUser) {
return $this->adminUser->hasPermissionTo($permission);
try {
if ($this->adminRole) {
return $this->adminRole->hasPermissionTo($permission);
} elseif ($this->adminUser) {
return $this->adminUser->can($permission);
}
} catch (PermissionDoesNotExist $exception) {
return false;
}
return false;
}

View File

@ -2,11 +2,13 @@
namespace Beike\Admin\View\Components;
use Beike\Models\AdminUser;
use Illuminate\View\Component;
class Header extends Component
{
public array $links = [];
private AdminUser $adminUser;
/**
* Create a new component instance.
@ -15,13 +17,10 @@ class Header extends Component
*/
public function __construct()
{
$this->addLink('管理首页', admin_route('home.index'), equal_route('admin.home.index'));
$this->addLink('订单管理', admin_route('orders.index'), equal_route('admin.orders.index'));
$this->addLink('商品管理', admin_route('products.index'), equal_route('admin.products.index'));
$this->addLink('会员管理', admin_route('customers.index'), equal_route('admin.customers.index'));
$this->addLink('系统设置', admin_route('settings.index'), equal_route('admin.settings.index'));
$this->adminUser = auth()->user();
}
/**
* Get the view / contents that represent the component.
*
@ -29,11 +28,31 @@ class Header extends Component
*/
public function render()
{
$this->addLink('管理首页', 'home.index', equal_route('admin.home.index'));
$this->addLink('订单管理', 'orders.index', equal_route('admin.orders.index'));
$this->addLink('商品管理', 'products.index', equal_route('admin.products.index'));
$this->addLink('会员管理', 'customers.index', equal_route('admin.customers.index'));
$this->addLink('系统设置', 'settings.index', equal_route('admin.settings.index'));
return view('admin::components.header');
}
private function addLink($title, $url, $active = false)
/**
* 添加后台顶部菜单链接
*
* @param $title
* @param $route
* @param false $active
*/
private function addLink($title, $route, bool $active = false)
{
$permissionRoute = str_replace('.', '_', $route);
if ($this->adminUser->cannot($permissionRoute) && $route != 'home.index') {
return;
}
$url = admin_route($route);
$this->links[] = [
'title' => $title,
'url' => $url,

View File

@ -2,6 +2,7 @@
namespace Beike\Admin\View\Components;
use Beike\Models\AdminUser;
use Illuminate\Support\Str;
use Illuminate\View\Component;
@ -10,6 +11,7 @@ class Sidebar extends Component
public array $links = [];
private string $adminName;
private string $routeNameWithPrefix;
private AdminUser $adminUser;
/**
* Create a new component instance.
@ -20,6 +22,7 @@ class Sidebar extends Component
{
$this->adminName = admin_name();
$this->routeNameWithPrefix = request()->route()->getName();
$this->adminUser = auth(AdminUser::AUTH_GUARD)->user();
}
/**
@ -34,40 +37,40 @@ class Sidebar extends Component
$routeName = str_replace($adminName . '.', '', $routeNameWithPrefix);
if (Str::startsWith($routeName, ['home.'])) {
$this->addLink('首页装修', admin_route('design.index'), 'fa fa-tachometer-alt', $this->equalRoute('design.index'), true);
$this->addLink('插件列表', admin_route('plugins.index'), 'fa fa-tachometer-alt', $this->equalRoute('plugins.index'));
$this->addLink('商品分类', admin_route('categories.index'), 'fa fa-tachometer-alt', $this->equalRoute('categories.index'));
$this->addLink('品牌管理', admin_route('brands.index'), 'fa fa-tachometer-alt', $this->equalRoute('brands.index'));
$this->addLink('税率设置', admin_route('tax_rates.index'), 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('语言管理', admin_route('tax_rates.index'), 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('货币管理', admin_route('currencies.index'), 'fa fa-tachometer-alt', $this->equalRoute('currencies.index'));
$this->addLink('首页装修', 'design.index', 'fa fa-tachometer-alt', $this->equalRoute('design.index'), true);
$this->addLink('插件列表', 'plugins.index', 'fa fa-tachometer-alt', $this->equalRoute('plugins.index'));
$this->addLink('商品分类', 'categories.index', 'fa fa-tachometer-alt', $this->equalRoute('categories.index'));
$this->addLink('品牌管理', 'brands.index', 'fa fa-tachometer-alt', $this->equalRoute('brands.index'));
$this->addLink('税率设置', 'tax_rates.index', 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('语言管理', 'tax_rates.index', 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('货币管理', 'currencies.index', 'fa fa-tachometer-alt', $this->equalRoute('currencies.index'));
}
if (Str::startsWith($routeName, ['products.', 'categories.', 'brands.'])) {
$this->addLink('商品分类', admin_route('categories.index'), 'fa fa-tachometer-alt', $this->equalRoute('categories.index'));
$this->addLink('商品列表', admin_route('products.index'), 'fa fa-tachometer-alt', $this->equalRoute('products.index'));
$this->addLink('品牌管理', admin_route('brands.index'), 'fa fa-tachometer-alt', $this->equalRoute('brands.index'));
$this->addLink('回收站', admin_route('products.index', ['trashed' => 1]), 'fa fa-tachometer-alt', false);
$this->addLink('商品分类', 'categories.index', 'fa fa-tachometer-alt', $this->equalRoute('categories.index'));
$this->addLink('商品列表', 'products.index', 'fa fa-tachometer-alt', $this->equalRoute('products.index'));
$this->addLink('品牌管理', 'brands.index', 'fa fa-tachometer-alt', $this->equalRoute('brands.index'));
$this->addLink('回收站', 'products.trashed', 'fa fa-tachometer-alt', $this->equalRoute('products.trashed'));
}
if (Str::startsWith($routeName, ['customers.', 'customer_groups.'])) {
$this->addLink('会员管理', admin_route('customers.index'), 'fa fa-tachometer-alt', $this->equalRoute('customers.index'));
$this->addLink('用户组', admin_route('customer_groups.index'), 'fa fa-tachometer-alt', $this->equalRoute('customer_groups.index'));
$this->addLink('会员管理', 'customers.index', 'fa fa-tachometer-alt', $this->equalRoute('customers.index'));
$this->addLink('用户组', 'customer_groups.index', 'fa fa-tachometer-alt', $this->equalRoute('customer_groups.index'));
}
if (Str::startsWith($routeName, ['orders.'])) {
$this->addLink('订单列表', admin_route('orders.index'), 'fa fa-tachometer-alt', $this->equalRoute('orders.index'));
$this->addLink('订单列表', 'orders.index', 'fa fa-tachometer-alt', $this->equalRoute('orders.index'));
}
if (Str::startsWith($routeName, ['settings.', 'admin_users.', 'admin_roles.', 'plugins.', 'tax_classes', 'tax_rates', 'regions', 'currencies'])) {
$this->addLink('系统设置', admin_route('settings.index'), 'fa fa-tachometer-alt', $this->equalRoute('settings.index'));
$this->addLink('后台用户', admin_route('admin_users.index'), 'fa fa-tachometer-alt', $this->equalRoute('admin_users.index'));
$this->addLink('插件列表', admin_route('plugins.index'), 'fa fa-tachometer-alt', $this->equalRoute('plugins.index'));
$this->addLink('区域分组', admin_route('regions.index'), 'fa fa-tachometer-alt', $this->equalRoute('regions.index'));
$this->addLink('税率设置', admin_route('tax_rates.index'), 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('税费类别', admin_route('tax_classes.index'), 'fa fa-tachometer-alt', $this->equalRoute('tax_classes.index'));
$this->addLink('货币管理', admin_route('currencies.index'), 'fa fa-tachometer-alt', $this->equalRoute('currencies.index'));
$this->addLink('首页装修', admin_route('design.index'), 'fa fa-tachometer-alt', $this->equalRoute('design.index'), true);
$this->addLink('系统设置', 'settings.index', 'fa fa-tachometer-alt', $this->equalRoute('settings.index'));
$this->addLink('后台用户', 'admin_users.index', 'fa fa-tachometer-alt', $this->equalRoute('admin_users.index'));
$this->addLink('插件列表', 'plugins.index', 'fa fa-tachometer-alt', $this->equalRoute('plugins.index'));
$this->addLink('区域分组', 'regions.index', 'fa fa-tachometer-alt', $this->equalRoute('regions.index'));
$this->addLink('税率设置', 'tax_rates.index', 'fa fa-tachometer-alt', $this->equalRoute('tax_rates.index'));
$this->addLink('税费类别', 'tax_classes.index', 'fa fa-tachometer-alt', $this->equalRoute('tax_classes.index'));
$this->addLink('货币管理', 'currencies.index', 'fa fa-tachometer-alt', $this->equalRoute('currencies.index'));
$this->addLink('首页装修', 'design.index', 'fa fa-tachometer-alt', $this->equalRoute('design.index'), true);
}
return view('admin::components.sidebar');
@ -75,16 +78,22 @@ class Sidebar extends Component
/**
* 添加链接
* 添加左侧菜单链接
*
* @param $title
* @param $url
* @param $route
* @param $icon
* @param $active
* @param false $newWindow
*/
public function addLink($title, $url, $icon, $active, bool $newWindow = false)
public function addLink($title, $route, $icon, $active, bool $newWindow = false)
{
$permissionRoute = str_replace('.', '_', $route);
if ($this->adminUser->cannot($permissionRoute)) {
return;
}
$url = admin_route($route);
$this->links[] = [
'title' => $title,
'url' => $url,