tarunn's avatar
Level 12

After authenticate how to change the Namespace

I have following scope here. I have 3 types of users. 1) Super Admin 2) Service Provider Admin and 3) End User. Super admin will have only website. Service provider admin will have website as well as app. End users will have only app.

Super admin will have main domain. eg domain.com Service providers can login in web with there subdomains. Eg. provider1.domain.com, provider3.domain.com.

I will be saving both superadmin and serviceprovider admin in single table USER, and will define there type. Based on Roles and permission service provider will have different views. That I will be handling with Entrust. After googling I decided to make different controller and views for Admin and Service providers by creating two different name spaces in my controller/views like: Admin and Serprovider. Now my doubt is: as I am using single table USER for both type of users, I need to used single AUTH file? If I use single AUTH file, then how to I manage different login for website.com/login AND provider1.domain.com/login?

I am using Laravel 5.2. Any help/suggestion will be highly appreciated.

0 likes
6 replies
martinbean's avatar
Level 80

@tarunn You’re storing your users in one table, which is good. If you’re using Entrust to assign users roles, then you can create some middleware classes that restricts routes to users with certain roles:

Route::group(['middleware' => ['role:admin']], function () {
    // Admin-only routes
});

Route::group(['middleware' => ['role:service_provider']], function () {
    // Service provider-only roles
});

Route::group(['middleware' => ['role:customer']], function () {
    // Customer-only roles
});

In your middleware class, check the role on the current user and either pass the request on, or abort if they don’t have the required role:

class VerifyUserHasRole
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if ($request->user()->hasRole($role)) {
            return $next($request);
        }

        // Throw “Forbidden” exception. User does not have required role.
        abort(403);
    }
}

Make sure to add the middleware class to your app/Http/Kernel.php file:

protected $routeMiddleware = [
    // Existing middleware
    'role' => \App\Http\Middleware\VerifyUserHasRole::class,
];
tarunn's avatar
Level 12

@martinbean Thanks for the prompt reply. But as I mention, I need different views for Super admin and SP admin! How can I manage that from this logic? I might have ReportsController for both users, but the information and presentation will be different. Can we manage that too?

One more case is there, I forgot to mention, the ServiceProvider Admin will have sub-admins and based on there roles, they will be assigned methods which they will manage by logging into provider1.domain.com?

I am not very good in laravel, I have intermediate knowledge so I am not sure how to manage sub-domain logins and superadmin logins along with providing them (Service providers admin) 2 login system i.e. web and token based. Awaiting for your guidance/suggestion. Thanks in advance.

tarunn's avatar
Level 12

I had changed the approach of sub-domain and just following common login structure. Thanks @martinbean

tarunn's avatar
Level 12

I have this in my route.php, but it gives 403 for all the routes, what I am doing wrong, please help

Route::group(['middleware' => ['web']], function(){

    Route::get('/', function () {
        return view('welcome');
    });
    
    Route::auth();
    
    /**
     * Route for the Super Admin
     */
    Route::group(['middleware' => ['role:super_admin|super_sub']], function () {

        Route::get('/dashboard', 'HomeController@index');
        
        Route::get('store/getstr', 'StoreController@providerDashboard'); 
        Route::resource('user', 'UserController');
        Route::resource('pet', 'PetController');
        Route::resource('provider', 'ProviderController');
        Route::resource('store', 'StoreController');
        Route::resource('content', 'ContentController');
        Route::resource('categories', 'CategoriesController');
    });


    /**
     * Route for the Providers
     */
    Route::group(['middleware' => ['role:provider_admin|provider_sub']], function () {

        Route::get('/pdashboard', 'HomeController@providerindex');
        
    });
});

Any guide/suggestion would be much appreciated. Thanks

tarunn's avatar
Level 12

Hi @martinbean ,

I need your help again. As per you guide, I had written following code in Kernel.php

/**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
        'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
        'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
        'role' => \App\Http\Middleware\VerifyUserHasRole::class,
    ];

The custom role which we created and the Zizaco role, both I have added. Then how to manage these middleware in routes.php? As they have same name, it gives me error on server while its working fine in local.

Please or to participate in this conversation.