首页 > 解决方案 > Laravel - 如何根据组织映射路线与登录用户

问题描述

在我的 Laravel-5.8 中,我正在开发多组织应用程序而不使用任何包。我也在使用 Azure Socialite Login。

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/dashboard';
    protected $username = 'username';

    public function redirectToProvider()
    {
        return Socialite::with('azure')->redirect();
    }

    public function handleProviderCallback()
    {
         $azureUser = Socialite::with('azure')->user();
        try 
        {
            $user = User::where('email', $azureUser->email)->orWhere('username', $azureUser->user['mailNickname'])->first();
            if($user)
            {
                if(Auth::loginUsingId($user->id))
                {
                    $user->update([
                         'last_login_at' => now(),
                    ]);
                   return redirect()->intended('/dashboard');
                }
            }
        } 
        catch(\Exception $e) 
        {
            session()->flash("error", "Authentication failed, kindly contact the Administrator!");
            return redirect(route('login'));
        }
    }   
}

我有这些模型:

class Organization extends Model
{
  protected $table = 'organizations';
  protected $fillable = [
     'id',
     'org_name',
     'subdomain',   
 ];
}

class User extends Authenticatable
{
    protected $fillable = [
        'name', 
        'username',
        'email', 
        'password', 
        'organization_id', 
    ]; 

    public function organization()
    {
        return $this->belongsTo('App\Models\Organization');
    }
}

我创建了一个中间件,以便将路由映射到组织子域。

class ConfirmDomain
{
    public function handle($request, Closure $next)
    {
        $domain == "laraapp";
        $path = $request->getPathInfo(); // should return /laraapp.net or /organization1.laraapp.net
        if (strpos($path, ".") !== false) { // if path has dot.
            list($subdomain, $main) = explode('.', $path);
            if(strcmp($domain, $main) !== 0){
                abort(404); 
            }
        } else{
            if(strcmp($domain, $path) !== 0){
                abort(404); // if domain is not myapp then throw 404 page error
            }
            $subdomain = ""; // considering for main domain value is empty string.
        }

        $org = OrgCompany::where('subdomain', $subdomain)->firstOrFail(); // if not found then will throw 404

        $request->session()->put('subdomain', $org); //store it in session

        return $next($request);
    }
}

组织表

id | org_name           |    subdomain
1  | Main               |
2  | Organization1      |    organization1

我想添加主域(https://laraapp.net)和子域(https://organization1.laraapp.net),每个都将根据每个表中的组织 ID 看到数据

https://laraapp.net
https://organization1.laraapp.net

用户可以退出多个组织。

当用户使用 azure socialite 登录时,我希望应用程序检查域和子域路由并使用它来获取组织 ID,然后将其与用户表中的组织 ID 映射。

例如,如果用户使用https://laraapp.net登录,应用程序使用它来获取 user 表中的 organization_id,如果是https://organization1.laraapp.net,它也会这样做,依此类推。

用户只能看到基于其组织的配置文件。也应该只看到它的组织数据。

我如何从路由和 LoginController 中实现这一点?

标签: laravel

解决方案


推荐阅读