根据许可在 Laravel 侧边栏中管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45173016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:19:26  来源:igfitidea点击:

Adminlte in Laravel sidebar based on permission

phplaravelpermissionssidebaradminlte

提问by derrysan7

I have multiple users with multiple permission (admin and user). example: admin is able to see sidebar a,b,c but user can only see sidebar d,e,f.

我有多个具有多个权限的用户(管理员和用户)。例如:管理员可以看到侧边栏 a、b、c,但用户只能看到侧边栏 d、e、f。

Can you load the menu at adminlte.php based on the permission? I store the permission in permission column inside User table (isAdmin, isUser) Thank you

您可以根据权限在 adminlte.php 加载菜单吗?我将权限存储在用户表(isAdmin,isUser)内的权限列中,谢谢

Menu array format in adminlte.php:

adminlte.php 中的菜单数组格式:

'menu' => [
        [
            'text' => 'Dashboard',
            'url'  => '/dashboard',
            'icon' => 'dashboard',
        ],
],

The only solution that i found is for plain php http://seegatesite.com/how-to-create-user-permissions-view-to-dynamic-sidebar-menu-adminlte/

我发现的唯一解决方案是纯 php http://seegatesite.com/how-to-create-user-permissions-view-to-dynamic-sidebar-menu-adminlte/

I would prefer a native laravel solution using the built in adminlte.php

我更喜欢使用内置的 adminlte.php 的本地 laravel 解决方案

回答by Mustafa

I have integrate Laravel-AdminLTE

我已经集成了Laravel-AdminLTE

to my project. Using this plugin permission could be given like the following:

到我的项目。可以像下面这样授予使用此插件的权限:

'can' => 'add-blog-post'

Here is the example:

这是示例:

'menu' => [
    'MAIN NAVIGATION',    
    [
        'text' => 'Pages',
        'url' => 'admin/pages',
        'icon' => 'file',
        'can' => 'add-blog-post'
    ],      
],

回答by Phil

I think using database is wise choice. But we could avoid using raw database query and use Laravel built-ins.

我认为使用数据库是明智的选择。但是我们可以避免使用原始数据库查询并使用 Laravel 内置函数。

In sidebar.blade.php (or other template/view files you have), you could fetch permissions from database and then use them to show your specific menu like:

在 sidebar.blade.php(或您拥有的其他模板/视图文件)中,您可以从数据库中获取权限,然后使用它们来显示您的特定菜单,例如:

 @if(Auth::user()->permission_1)
     <li><a href=""><i class="fa fa-group"></i> menu-item_1</a></li>
 @endif
 @if(Auth::user()->permission_2)
     <li><a href=""><i class="fa fa-tasks"></i> menu-item_2</a></li>
 @endif

回答by derrysan7

I end up just hardcoding it in the page.blade.php using two different views for each permission

我最终只是在 page.blade.php 中对每个权限使用两个不同的视图进行硬编码

@if(config('adminlte.layout') != 'top-nav')
        <aside class="main-sidebar">
            <!-- sidebar: style can be found in sidebar.less -->
            <section class="sidebar">
                <!-- Sidebar Menu -->
                <ul class="sidebar-menu">
                    <!-- Left side column. contains the logo and sidebar -->
                    @if (Auth::user()->role == "isAdmin")
                        @include('layouts.sidebar_admin_menu')
                    @elseif (Auth::user()->role == "isNormalUser")
                        @each('adminlte::partials.menu-item', $adminlte->menu(), 'item')
                    @endif
                </ul>
                <!-- /.sidebar-menu -->
            </section>
            <!-- /.sidebar -->
        </aside>       
    @endif

which is not ideal, but it works well for the time being..

这并不理想,但暂时效果很好..

the ideal and tidier solution would be to store each permission (create, update, delete, read) into database with boolean value.

理想和更整洁的解决方案是将每个权限(创建、更新、删除、读取)存储到具有布尔值的数据库中。

what surprised me the most is that adminlte as one of the most popular sidebar framework out there doesn't have a native function for sidebar permission..

最让我惊讶的是,adminlte 作为最流行的侧边栏框架之一,没有用于侧边栏权限的本机功能..

回答by Adnan Boota

You can follow the 'Menu configuration at runtime' item on the README.

您可以按照自述文件中的“运行时菜单配置”项进行操作。

  1. Set 'menu' => [] inside config/adminlte.php Edit
  2. List item app\Providers\AppServiceProvider.php boot method
  1. 在 config/adminlte.php 中设置 'menu' => [] 编辑
  2. 列表项 app\Providers\AppServiceProvider.php 启动方法
    <?php

    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use Illuminate\Contracts\Events\Dispatcher;
    use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
    use \App\User;

    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot(Dispatcher $events)
        {
            $events->listen(BuildingMenu::class, function (BuildingMenu $event) {
                $event->menu->add('MAIN NAVIGATION');
                $event->menu->add([
                    'text'        => 'Users',
                    'url'         => 'admin/users',
                    'icon'        => 'users',
                    'label'       => User::count(),
                    'label_color' => 'success',
                ]);
            });
        }

        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    }

回答by Mateus Gon?alves

Good Morning.

早上好。

You must implement Gates. One of the menu options is 'can'. With it you create a Gate and this concentrates your permissions rule.

您必须实施盖茨。菜单选项之一是“可以”。使用它,您可以创建一个 Gate,这将集中您的权限规则。

An example:

一个例子:

/app/config/adminlte.php

/app/config/adminlte.php

[
    'text'        => 'Por Chassi',
    'url'         => 'consultarporchassi',
    'icon'        => 'search',
    'icon_color' => 'success',
    'can'         => 'consulta-chassi'
],

Gate: /app/Providers/AppServiceProvider.php: Gate must be deployed within the AppServiceProvider.php "boot" method

Gate: /app/Providers/AppServiceProvider.php: Gate 必须部署在 AppServiceProvider.php "boot" 方法中

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    $this->register();

\Gate::define('consulta-chassi', function ($user) {
    if ($user->priv_admin == 'Y') {
        return true;
    }
    return false;
});

}

Good luck, see you later

祝你好运,下期见

For more information here is the link: https://laravel.com/docs/5.7/authorization

有关更多信息,请访问以下链接:https: //laravel.com/docs/5.7/authorization

回答by DarkMode

Create your own CustomMenuFilterclass

创建您自己的CustomMenuFilter

<?php

namespace JeroenNoten\LaravelAdminLte\Menu\Filters;

use JeroenNoten\LaravelAdminLte\Menu\Builder;
use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;

class CustomMenuFilter implements FilterInterface
{
    public function transform($item, Builder $builder)
    {
      $urls = ['url_1', 'url_2', 'url_3'];
      if (isset($item['url']) && in_array($item['url'], $urls)) {
            return $item; //display menu if $item['url'] exists in $urls
      } else if (isset($item['submenu']) && count($item['submenu']) > 0) {
            return $item;
      }
      return false;
    }
}

In your adminlte.phpcomment out GateFilterclass so you can use your own menu filter implementation.

在您adminlte.php注释掉GateFilter类中,以便您可以使用自己的菜单过滤器实现。

 'filters' => [
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ HrefFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ SearchFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ ActiveFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ SubmenuFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ ClassesFilter::class,
        // JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ GateFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ CustomMenuFilter::class,
        JeroenNoten\ LaravelAdminLte\ Menu\ Filters\ LangFilter::class,
    ],

回答by Dharma Saputra

I think you should try this package to implement what you need: https://github.com/Zizaco/entrust. It's a laravel package for handling role based permission. And anyway you need to change some of your code if you are using this package.

我认为你应该试试这个包来实现你需要的:https://github.com/Zizaco/entrust。这是一个用于处理基于角色的权限的 laravel 包。无论如何,如果您使用这个包,您需要更改一些代码。

Hope it helps.

希望能帮助到你。