omarpre/impersonate 问题修复 & 功能扩展

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

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

omarpre/impersonate

Composer 安装命令:

composer create-project omarpre/impersonate

包简介

Impersonate users easily in Laravel apps. (Copy of original package as it is now abandoned)

README 文档

README

A lightweight, yet useful package for user impersonation.

Impersonation can be a nice way to track down issues / bugs beyond our tests. With this package, it's easy to implement it, without touching the core of our app.

Configure the package as you need and it's ready to go!

Getting Started

You can install the package with composer, using the composer require omarpre/impersonate command.

Laravel 5.5 or higher

If you are using version 5.5, there is nothing else to do. Since the package supports autodiscovery, Laravel will register the service provider automatically, behind the scenes.

Laravel 5.4 and below

You have to register the service provider manually. Go to the config/app.php file and add the Pine\Impersonate\ImpersonateServiceProvider::class to the providers array.

Disable the autodiscovery for the package

In some cases you may disable autodiscovery for this package. You can add the provider class to the dont-discover array to disable it.

Then you need to register it manually again.

Configuration

You may override the default configurations. To do that, first you have to publish the config file with the following command: php artisan vendor:publish --provider=Pine\Impersonate\ImpersonateServiceProvider. Then you can find the config file, at config/impersonate.php.

Middlewares

By default the impersonating routes have with two middlewares, the web and the auth. But of course, it's definetly not enough. In the config file, at the middlewares section, you can add more middlewares, what you registered in your Kernel.php before, or you can add fully qualified classnames as well.

Routes

The package brings two routes. One for imopersonating a user and one for revert to the original one.

The impersonate route's properties are the following:

  • Path: /impersonate/{user}
  • Name: impersonate.impersonate
  • Controller@action: Pine\Impersonate\Http\Controllers\ImpersonateController@impersonate

The revert route's properties are the following:

  • Path: /impersonate/revert
  • Name: impersonate.revert
  • Controller@action: Pine\Impersonate\Http\Controllers\ImpersonateController@revert

As you see, when we navigate a user to these routes, the action has to hit the server. That means, if you use something like Vue Router, please take care to use a normal link for these instead of the router's.

<!-- Bad, it will hit the router -->
<router-link to="/impersonate/revert">Revert</router-link>

<!-- Good, it will hit the server -->
<a href="/impersonate/revert">Revert</a>

Redirection

After impersonating a user, or reverting we return with a redirection response. By default both action redirects to the /home path. You can configure both routes individually in the config file, at the redirect section.

Events

Both actions trigger an event when we hit them. You can setup your own listeners for these events, in the EventServiceProvider. For example:

protected $listen = [
    \Pine\Impersonate\Events\ChangedToUser::class => [
        // Listeners
    ],
    \Pine\Impersonate\Events\Reverted::class => [
        // Listeners
    ],
];

Also, you can access the impersonated user via the ChangedToUser event. Here is an example listener for that:

public function handle(ChangedToUser $event)
{
    // $event->user
}

Blade Directive

The package brings a blade directive to check easily if the user is in an impersonation mode or not. It's like a basic if statement.

@impersonate
    // You are impersonating
@else
    // You are in your own profile
@endimpersonate

Working with JS

Since the whole process is session based, we have the information about the current state only on the server-side. That means, if we want to integrate the package with an SPA, we need to extract the info and make it accessible in the front-end.

At the first load, we have the chance to fetch the current status to the window object. For example:

<script>
    @impersonate
        window.isImpersonating = true;
    @else
        window.isImpersonating = false;
    @endimpersonate
</script>

Contribute

If you found a bug or you have an idea connecting the package, feel free to open an issue.

omarpre/impersonate 适用场景与选型建议

omarpre/impersonate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.51k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 omarpre/impersonate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 omarpre/impersonate 我们能提供哪些服务?
定制开发 / 二次开发

基于 omarpre/impersonate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-14