hashmatwaziri/laravel-multi-auth-impersonate

laravel-multi-auth-impersonate

v1.0 2021-01-20 01:04 UTC

This package is auto-updated.

Last update: 2024-09-06 04:27:22 UTC


README

Latest Version on Packagist

Total Downloads

需求

  • Php > 7.3 或 8

安装

您可以通过 composer 安装此包

composer require HashmatWaziri/laravel-multi-auth-impersonate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="HashmatWaziri\LaravelMultiAuthImpersonate\LaravelMultiAuthImpersonateServiceProvider" --tag="multiAuthImpersonate"

这是发布配置文件的内容

return [
 /**
     * The session key used to store the original user id.
     */
    'session_key' => 'impersonated_by',

    /**
     * The session key used to stored the original user guard.
     */
    'session_guard' => 'impersonator_guard',

    /**
     * The session key used to stored what guard is impersonator using.
     */
    'session_guard_using' => 'impersonator_guard_using',

    /**
     * The default impersonator guard used.
     */
    'default_impersonator_guard' => 'web',
];

重定向 URL

获取重定向:当模拟其他用户时,您可以在被模拟的模型上添加方法 takeRedirectTo()

示例

  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function takeRedirectTo(){

        return url('/after-being-impersonated');
    }

}

离开重定向:当模拟者(模拟或登录为其他用户的人)离开模拟时,您可以在该模型上添加方法 leaveRedirectTo()

示例

  class Employee extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function leaveRedirectTo(){

        return url('/workplace/dashboard');
    }

}

使用方法

模拟用户

$other_user = App\Student::find(1);
Auth::user()->impersonate($other_user);
// You're now logged as the $other_user

离开模拟

Auth::user()->leaveImpersonation();
// You're now logged as your original user.

路由

在您的路由文件中,在 web 中间件下,您必须调用任何您选择的用于此包的路由名称的 multiAuthImpersonate 路由宏。此包允许用户决定这些路由应在哪个 URL 上注册

Route::multiAuthImpersonate('impersonation');

或者,您也可以通过您的 RouteServiceProvider 执行此宏。

namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
	// here you can supply an array of guards ex ['web','employee','etc'] so that each can impersonate other
        Route::middleware('web')->group(function (Router $router) {
            $router->multiAuthImpersonate('impersonation');
        });
    }
}
// Where $id is the ID of the user you want impersonate
route('impersonate', $id)

// You should also add `guardName`
route('impersonate', ['id' => $id, 'guardName' => 'admin'])

// Generate an URL to leave current impersonation
route('impersonate.leave')

定义模拟授权

默认情况下,所有用户都可以 模拟 用户。
您需要将方法 canImpersonate() 添加到您的守卫模型中:示例

  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;


//    /**
//     * @return bool
//     */
    public function canImpersonate()
    {

        return true ;

    }

}

默认情况下,所有用户都可以 被模拟
您需要将方法 canBeImpersonated() 添加到您的守卫模型中,以扩展此行为

    /**
     * @return bool
     */
    public function canBeImpersonated()
    {
        // For example
        return $this->can_be_impersonated == 1;
    }

使用您自己的策略

  • 获取管理器
// With the app helper
app('impersonate')
// Dependency Injection
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
  • 与管理者合作
$manager = app('impersonate');

// Find an user by its ID
$manager->findUserById($id);

// TRUE if your are impersonating an user.
$manager->isImpersonating();

// Impersonate an user. Pass the original user and the user you want to impersonate
$manager->take($from, $to);

// Leave current impersonation
$manager->leave();

// Get the impersonator ID
$manager->getImpersonatorId();

测试

composer test

变更日志

请参阅 变更日志 了解最近更改的更多信息。

贡献

请参阅 贡献指南 了解详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。