Search code examples
laravellaravel-5cartalyst-sentinel

Laravel Sentinel setModel accessing the wrong table


I am attempting to use multiple tables for my authentication based on a subdomain. Therefore, I am attempting to use Sentinel's setModel function during runtime to set the model instance.

So in my routes file I have some simple logic:

if (Request::getHost() == $url) {
    Sentinel::getUserRepository()->setModel('App\Client');
} else {
    Sentinel::getUserRepository()->setModel('App\User');
}

The logic being one URL uses one model and the other uses the other model. Now, when I am attempting to load the App\Client by doing a dd(Sentinel::getUserRepository()); I get a return such as:

IlluminateUserRepository {#113 ▼
  #hasher: NativeHasher {#114}
  #model: "App\Client"
  #dispatcher: Dispatcher {#23 ▶}
  #dispatcherStatus: true
}

This is great, however, when I call Sentinel::getUser();, I am left with the following output:

EloquentUser {#230 ▼
  #table: "users"
  #fillable: array:5 [▶]
  #hidden: array:1 [▶]
  #persistableKey: "user_id"
  #persistableRelationship: "persistences"
  #loginNames: array:1 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:12 [▶]
  #original: array:12 [▶]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #permissionsInstance: null
}

This is referring to the App\User model rather than my App\Client model.

I don't understand why, but I can't seem to get functions of Sentinel to look at the model I am trying to reference.

Here is my Client model as a point of reference.

namespace App;

class Client extends \Cartalyst\Sentinel\Users\EloquentUser
{
    /**
     * The fields for sentinel to authenticate (login)
     *
     * @var array
     */
    protected $loginNames = ['email'];

    /**
    * The table associated with model.
    *
    * @var String
    */
    protected $table = 'clients';

    /**
    * Used for mass assignment fields.
    *
    * @var Array
    */
    protected $fillable = ['email', 'password', 'permissions', 'last_login', 'first_name', 'last_name'];
}

I have used this guide from the Github Wiki, however as you can see Im not achieving what I am trying to do.

How can I use this approach of setting the model during runtime and use the correct model with all of sentinels commands?


Solution

  • To anyone who encounters the same issue, i flagged this as an issue in the cartalyst/sentinel repository and some feedback was provided.

    The solution is to not only set the user repository but also define the model on the persistence repository:

    Sentinel::getUserRepository()->setUsersModel('App\Client');
    Sentinel::getPersistenceRepository()->setUsersModel('App\Client');
    

    Through this methods such as Sentinel::getUser() will access the model specified above.

    Please see the full response here: https://github.com/cartalyst/sentinel/issues/367