hashmatwaziri/laravel-multi-auth-impersonate 问题修复 & 功能扩展

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

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

hashmatwaziri/laravel-multi-auth-impersonate

Composer 安装命令:

composer require hashmatwaziri/laravel-multi-auth-impersonate

包简介

laravel-multi-auth-impersonate

README 文档

README

Latest Version on Packagist

Total Downloads

Requirements

  • Php > 7.3 or 8

Installation

You can install the package via composer:

composer require HashmatWaziri/laravel-multi-auth-impersonate

You can publish the config file with:

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

This is the contents of the published config file:

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',
];

Redirect URLs

take Redirect : when impersonating another user, you can add the method takeRedirectTo() to your model which is being impersonated:

example:

  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function takeRedirectTo(){

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

}

leave Redirect : when an impersonator ( the one who impersonated or logged in as another user) is leaving the impersonation, you can add the method leaveRedirectTo() to that model:

example:

  class Employee extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;



    public static function leaveRedirectTo(){

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

}

Usage

Impersonate a user:

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

Leave impersonation:

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

Routes

In your routes file, under web middleware, you must call the multiAuthImpersonate route macro with any route name you choose to be used for this package. This package let user decides on which package URL these routes should be registered

Route::multiAuthImpersonate('impersonation');

Alternatively, you can execute this macro with your 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')

Defining impersonation authorization

By default all users can impersonate an user.
You need to add the method canImpersonate() to your guard model: example:

  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;


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

        return true ;

    }

}

By default all users can be impersonated.
You need to add the method canBeImpersonated() to your guard model to extend this behavior:

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

Using your own strategy

  • Getting the manager:
// With the app helper
app('impersonate')
// Dependency Injection
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
  • Working with the manager:
$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();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固