Adminlte in Laravel sidebar based on permission

13,721

Solution 1

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

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:

 @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

Solution 2

I have integrate 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'
    ],      
],

Solution 3

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.

An example:

/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

/**
 * 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

Solution 4

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
    <?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()
        {
            //
        }
    }

Solution 5

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

@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..

Share:
13,721

Related videos on Youtube

derrysan7
Author by

derrysan7

Just a Curious Software Engineer

Updated on June 04, 2022

Comments

  • derrysan7
    derrysan7 almost 2 years

    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.

    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

    Menu array format in 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/

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

    • Ronald Araújo
      Ronald Araújo about 6 years
      Did you find any solutions?
  • derrysan7
    derrysan7 almost 7 years
    Thank you, but I don't want to use another package to solve this problem... I'd like to do things native if possible