Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a new class that inherits from User in Laravel 5.2?

I am pretty new to laravel (using 5.2 which is the latest version to date), therefore I have the following dilemma: I know that Laravel comes with a User class right out of the box, but I want to develop a system where I can have another two types of users called Researcher and Admin.

My main need to create completely different classes of users (Researcher and Admin), possibly inheriting from User because the business logic is almost 100% different amongst them and I would not want to create a column in the database to classify the type of user. Furthermore, there aren't many fields that overlap between the stock User, Admin and Researcher classes.

My main question would be: Will everything still work the same (Auth Controller, middleware enabled, etc...) if I inherit from User for my other 2 classes? I know the principles of OOP and by intuition I assume I should be fine if I do the following:

//'Stock' User class:
//
class User extends Authenticatable{
    //Any overlapping logic between Researcher and Admin.
}


class Researcher extends User{
    //My class definition here.
}

class Admin extends User{
    //My class definition here.
}

And then use both classes as I would normally use an instance of the class User. By that, I mean use all the methods and right out of the box User functionality.

QUESTION EXTENSION: While writing my question, I realize that the class User looks like so by default:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Could not I just do the following instead of inheriting from User to create my other 2 classes (Researcher & Admin):

//Researcher Class:
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Researcher extends Authenticatable
{
    //Class definition...
}

...

//Admin Class:
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    //Class definition
}

In this approach, I am trying to 'mimic' the User class. The thing is that I don't know if there is any logic in the Kernel that is hardcoded and is being referenced to the User class implicitly.

Any thought and help will be much appreciated. Sorry if this is silly to you. I am just getting started with the laravel framework.

Cheers!

like image 707
idelara Avatar asked Jan 07 '16 10:01

idelara


1 Answers

Yes, you can extend the class Illuminate\Foundation\Auth\User:

use Illuminate\Foundation\Auth\User as Authenticatable;

class Researcher extends Authenticatable {
    //My class definition here.
}

If you take a look at User class:

class User extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
}

you see that it extends the Model class and implements all the other useful interfaces for authentication handling

you can safely extend the User class, because Laravel is good enough to work with the Illuminate\Contracts\Auth\Authenticatable interface to handle the authentication and not directly with the User class

In fact if you check in:

Illuminate/Auth/SessionGuard.php

That is the main class for auth handling, you'll see that all the auth actions are made against the Illuminate\Contracts\Auth\Authenticatable interface, i.e:

/**
 * Log a user into the application.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  bool  $remember
 * @return void
 */
public function login(AuthenticatableContract $user, $remember = false)
{
    //other code
} 

I Think that your real problem would be: how to instantiate the classes Researcher and Admin instead of the User class during the authentication process?

If you use Eloquent as authentication driver, by default Laravel is going to use the Illuminate\Auth\EloquentUserProvider to create an instance of the Model. So, if you want to create an instance one of your classes instead of User, you should override this provider (or create one of your own) and here you could choose which class to instantiate

like image 128
Moppo Avatar answered Sep 23 '22 20:09

Moppo