Monday, March 6, 2017

Laravel service providers validate the routes

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.



via dhanashri

Advertisement