0

My current project for college is a school management system. It consists mainly of three "profiles" - one for students, another for teachers/professors and a third for coordinators/admins.

After authentication and leaving the login page, this is what I'd like to happen:

  • If users occupy only one role inside the application, they should be redirected directly to the correspondent profile (e.g.: a student goes straight to a student profile);
  • However, for those who occupy two or even all three roles (unlikely but possible), an identities page gives them a choice between their different profiles;
  • A user can only have access to pages and profiles as defined by their role(s);

What would be the best way to implement this within Laravel 5.6?

Note: I can currently authenticate users but no permissions and roles are in place. I'm using Laravel's default users table.

Julio Kirk
  • 41
  • 9
  • people told below but when i am using entrust i was so happy. Great package. After that i found the voyager admin package and my life getting easier. You can do anything you told. – Ali Özen Apr 10 '18 at 06:24
  • did u find solution? – Adam Kozlowski Apr 10 '18 at 07:22
  • @AdamKozlowski I haven't had time yet to test the solutions offered here, but I'll do it later today. I will update the post as soon as I do! – Julio Kirk Apr 10 '18 at 13:21

3 Answers3

0

@Julio Kirk You can use the Voyager package.

Voyager is simply an admin for your Laravel app. Whatever you want your app to do on the front-end is completely up to you. You are in control of your application and you can use Voyager to make your life easier by adding data, editing users, creating menus, and many other administrative tasks.

Your requirement is fully completed in this project.

AddWeb Solution Pvt Ltd
  • 20,310
  • 5
  • 23
  • 55
0

I recommend to use ENTRUST (Laravel 5 Package)

Simple and powerful.

In order to install Laravel 5 Entrust, just add the following to your composer.json. Then run composer update:

"zizaco/entrust": "5.2.x-dev"

Open your config/app.php and add the following to the providers array:

Zizaco\Entrust\EntrustServiceProvider::class,

In the same config/app.php and add the following to the aliases array:

'Entrust'   => Zizaco\Entrust\EntrustFacade::class,

Run the command below to publish the package config file config/entrust.php:

php artisan vendor:publish

Open your config/auth.php and add the following to it:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => Namespace\Of\Your\User\Model\User::class,
        'table' => 'users',
    ],
],

If you want to use Middleware (requires Laravel 5.1 or later) you also need to add the following:

'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,

to routeMiddleware array in app/Http/Kernel.php.

Read more and get the library: https://github.com/Zizaco/entrust

Adam Kozlowski
  • 4,952
  • 2
  • 31
  • 44
  • As in his example there are only 3 roles that means they can be used as permissions and therefore there is really no need for extra package for such simple task – NoOorZ24 Apr 10 '18 at 05:35
  • It is a complex library for this kind of task. If you have never used that - just try and see how it simple is. – Adam Kozlowski Apr 10 '18 at 05:36
  • I'm just thinking it's an overkill for him to add it and learn it (no matter how easy it is) if task is actually very simple – NoOorZ24 Apr 10 '18 at 05:40
  • Let him make his own decision – Adam Kozlowski Apr 10 '18 at 05:41
  • I chose your answer because it addresses the problem I have, even though I'd have to spend some time learning how to use it. I'll first try without a package, using policies and so on, since my roles system is quite simple. Thanks everyone for the answers! – Julio Kirk Apr 16 '18 at 23:23
0

As one user can have only one of three roles you don't really need to separate roles from permissions - just add role cell to your users table or if you can't modify it create new one with id - role pairs.

After that you simply use middleware with with parameter: https://laravel.com/docs/5.6/middleware#middleware-parameters

to determine if user is allowed to visit current route.

Besides that this post will be answer to you: Laravel 5.4 redirection to custom url after login

NoOorZ24
  • 2,370
  • 1
  • 11
  • 30
  • Actually, an user can have one, two or three roles. For example, it would be common for the admin to be a professor at the same time. In this case, I'd like the user to be able to choose which profile to view at the moment, Professor or Admin. This choice is presented on a page I call "identities" in the form of flexible boxes that are links to the profiles. – Julio Kirk Apr 10 '18 at 05:50