Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitenant not working with User model #87

Open
subhra44 opened this issue Dec 17, 2017 · 4 comments
Open

Multitenant not working with User model #87

subhra44 opened this issue Dec 17, 2017 · 4 comments

Comments

@subhra44
Copy link

subhra44 commented Dec 17, 2017

Multi tenant is not working with User Model. But it is working fine with all other models.

App/User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use HipsterJazzbo\Landlord\BelongsToTenants;

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}
@subhra44 subhra44 reopened this Dec 17, 2017
@videni
Copy link

videni commented Mar 14, 2018

@ subhra44, The SessionGuard may load your user from database, I guess you want to resolve tenant from your user table, at this moment , you user model try to scope your user table, but you don't set tenant yet ,so your user model is differed , you should boot the differed models after login , that is purpose of the applyTenantScopesToDeferredModels method I guess.

@gkarugi
Copy link

gkarugi commented Mar 14, 2018

@subhra44 is the trait BelongsToTenants not scoping the users?

@booni3
Copy link

booni3 commented Apr 18, 2018

Can anyone suggest where the applyTenantScopesToDeferredModels() function should be called? I find the only place it works is before each user model call inside the controller. i.e

Landlord::applyTenantScopesToDeferredModels();
$users = User::all();

This does mean it needs to be repeated quite a lot though. Any ideas why it does not work inside the UserController constructor?

@randarp
Copy link

randarp commented Oct 31, 2018

I know this is an old issue, but I ran into this exact issue and this post helped me come up with a fairly good solution.

We have some middleware that is responsible for tenancy. It is the code that eventually calls \Landlord::addTenant if we decide it's a valid user.

If we call addTenant, we also call Landlord::applyTenantScopesToDeferredModels() right after.

This seems to be a solution that seems to work across the entire system without spreading this across all our controllers. But it means you have to push your logic up from the controllers to middleware, which may not be a simple change to make to an existing system, but something to consider for new systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants