victoryoalli/laravel-multitenancy-impersonate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

victoryoalli/laravel-multitenancy-impersonate

最新稳定版本:v2.1.0

Composer 安装命令:

composer require victoryoalli/laravel-multitenancy-impersonate

包简介

Laravel Multitenancy Impersonation from landlord to tenant

README 文档

README

Laravel multitenancy impersonation from landlord to tenant.

This package is made to be used with Spatie Laravel Multitenancy.

Latest Version on Packagist Tests Total Downloads

This package allows you to impersonate users from a landlord (main) application into tenant applications. It generates secure, time-limited tokens that enable seamless authentication across tenant databases.

Requirements

Installation

You can install the package via composer:

composer require victoryoalli/laravel-multitenancy-impersonate

Publish Config and Migrations

php artisan vendor:publish --provider="VictorYoalli\MultitenancyImpersonate\MultitenancyImpersonateServiceProvider"

Configuration

After publishing, you can modify config/multitenancy-impersonate.php:

return [
    'ttl' => 60,                // Token lifetime in seconds
    'redirect_path' => '/home', // Default redirect after impersonation
    'auth_guard' => 'web',      // Authentication guard to use
    'rate_limit' => [
        'max_attempts' => 5,    // Max token validation attempts
        'decay_minutes' => 1,   // Minutes until attempts reset
    ],
];

Usage

Landlord Controller

The Landlord controller creates the token and redirects to the tenant for automatic login.

use VictorYoalli\MultitenancyImpersonate\Traits\CanImpersonate;

class ImpersonateController
{
    use CanImpersonate;

    public function store(Request $request)
    {
        $tenant = Tenant::find($request->get('tenant_id'));
        $redirect_url = "https://{$tenant->domain}/admin";

        // Create impersonation token in tenant's database
        $impersonate = $this->createToken($tenant, auth()->user(), $redirect_url);

        // Redirect to tenant's impersonation endpoint
        $tenant_url = "https://{$tenant->domain}/impersonate";

        return redirect("{$tenant_url}/{$impersonate->token}");
    }
}

Tenant Controller

Validates the token and logs in the specified user. The user will be redirected to the $redirect_url provided when creating the token.

use VictorYoalli\MultitenancyImpersonate\Traits\CanImpersonate;

class TenantImpersonateController
{
    use CanImpersonate;

    public function __invoke(Request $request, string $token)
    {
        $user = User::firstOrFail(); // Or find user by any criteria

        return $this->impersonate($token, $user);
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email victoryoalli@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 552
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-12