dimanche 16 mai 2021

Laravel 8: Redirecting Forbidden Users To Their Respective Home Pages

Good Day

I'm new to laravel, and I'm building a website that has multiple users (Student, Supervisor, HOD). I am making use of the laratrust package to manage my users. If a student tries to access a url belonging to a supervisor, for example, laratrust throws an exception that says "USER DOES NOT HAVE ANY OF THE NECESSARY ACCESS RIGHTS". I would like to redirect the student back to their home page instead of this exception.

Here are my routes:

web.php

// Routes For Students
Route::group([
    'name' => 'student.',
    'prefix' => 'student',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:student']
], function () {
    Route::get('home', 'StudentController@index')->name('student-home');
    Route::get('proposal', 'StudentController@proposal')->name('student-proposal');
    Route::get('thesis', 'StudentController@thesis')->name('student-thesis');
});

// Routes For Supervisors
Route::group([
    'name' => 'supervisor.',
    'prefix' => 'supervisor',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:supervisor']
], function () {
    Route::get('home', 'supervisorController@index')->name('supervisor-home');
    Route::get('proposal', 'supervisorController@proposal')->name('supervisor-proposal');
    Route::get('thesis', 'supervisorController@thesis')->name('supervisor-thesis');
});

// Routes For HOD
Route::group([
    'name' => 'hod.',
    'prefix' => 'hod',
    'namespace' => 'App\Http\Controllers',
    'middleware' => ['auth', 'role:hod']
], function () {
    Route::get('home', 'hodController@index')->name('hod-home');
    Route::get('proposal', 'hodController@proposal')->name('hod-proposal');
    Route::get('thesis', 'hodController@thesis')->name('hod-thesis');
});

I have researched that in order to do this, I need to edit this specific part of the laratrust.php file:

    'middleware' => [

        'register' => true,

        'handling' => 'abort', // This needs to change to redirect

        'handlers' => [
            /**
             * Aborts the execution with a 403 code and allows you to provide the response text
             */
            'abort' => [
                'code' => 403,
                'message' => 'User does not have any of the necessary access rights.'
            ],

            // Redirect User To Their Respective Home Based On Their Role.
            'redirect' => [
                'url' => '/home',
                'message' => [
                    'key' => 'error',
                    'content' => ''
                ]
            ]
        ]
    ],

The home url is different based on the user type, so I want to know how I can check determine the type of user, then based on that assign their respective home url. Please let me know if you require more code or better explanation.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3yhlltZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire