0

In Laravel 5.4 I make one custom service providers for shows specific routes. My code is working properly but I am facing one problem. Problem is that if user ABC assign two routes(menu's) for example

user\list
user\form

Now if user ABC try to call other url like user\show_form so user ABC can access this routes(menu). So it possible if user ABC don't have rights to access user\show_form and user try to access this routes(menu) it's redirect to 404 page.

My Service Provider

    class UserNavigationServiceProvider extends ServiceProvider {

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

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

    /**
     * getUserNavigation() function use for after user login
     * fetch the rights
     * @return void
     */
    private function getUserNavigation() {
        $user_permission = user_permission();
        View::composer('admin.navbar', function($view) use ($user_permission)  {
            $auth_user = Auth::user();
            $user_id = $auth_user->id;
            $user_group = UserPermissionsGroup::select('user_permissions_id')
                    ->where('user_id', $user_id)
                    ->get()
                    ->toArray();
            $menu = array();
            foreach ($user_group as $k => $v) {
                foreach($v as $kv) {
                    $menu[$kv] = id_to_text($kv, $user_permission);
                }
            }
            $view->with('menu_list', $menu);
        });
    }

}

Please suggest me how to solve.

sandip kakade
  • 1,346
  • 5
  • 23
  • 49

1 Answers1

0

You can define Custom Request class and in it's authorize() method, check for permissions. Or you can add a middleware for authorising request and redirect to 404 from that middleware.

This question might help you. https://laracasts.com/discuss/channels/general-discussion/l5-middleware-or-authorize-method?page=1

sp11
  • 323
  • 1
  • 3
  • 6
  • how can I set `$menu` global variable in UserNavigationServiceProvider – sandip kakade Mar 06 '17 at 13:06
  • So `$menu` global variable I can access any controller – sandip kakade Mar 06 '17 at 13:07
  • You can create a facade with one setter method and a getter method. Set it's value from UserNavigationServiceProvider and use get methods in controller to get that value. Or you can set it in session. Hope this helps.. – sp11 Mar 07 '17 at 12:56