codebot/laravel-usub
Composer 安装命令:
composer require codebot/laravel-usub
包简介
Laravel package for authenticated user substitution to login as other users.
README 文档
README
About laravel-usub
Laravel package for authenticated user substitution to login as other users. You will find this package useful when your client says "As an admin I want to be authenticated and act as chosen user without typing password, and want to back to my admin dashboard by single click.".
Installation
-
Install the package using composer - run command
composer require codebot/laravel-usub:0.1.* -
Publish vendors using
php artisan vendor:publish --tag=laravel-usubcommand. You will get published config file config/usub.php, middleware app/Http/Middleware/UsubSignIn.php, command app/Console/Commands/ClearUsubTokens.php, directory views/vendor/usub and migration to create usub_tokens table. -
Run
php artisan migrateto create usub_tokens table. -
If auto-discovery doesn't work for you then register service provider by adding
Usub\Core\UsubServiceProvider::classto providers in config/app.php file. -
Add the
UsubSignInmiddleware to the$routeMiddlewarearray in theApp\Http\Kernel.phpclass.
You need complete UsubSignIn middleware to implement permissions. For example:
public function handle( $request, Closure $next ) { if ( !$request->user()->hasRole( 'admin' ) ) { abort( 401 ); } return $next( $request ); }
Configuration
expiration - Usub token expiration time in minutes.
length - Length of generated usub token.
redirect_to_on_sign_in - Default URL where user will be redirected on sign in whenever it's not overridden by redirect_to_on_sign_in key in request, e.g. by hidden input field.
redirect_to_on_sign_out - Default URL where user will be redirected on sign out whenever it's not overridden by redirect_to_on_sign_out key in request, e.g. by hidden input field.
redirect_to_on_cookie_expiration - URL where user will be redirected when token cookie expired.
forget_cookies_on_sign_out - Array of cookie names that will be removed from browser on sign out and usub token expiration.
You can change the configuration in your .env file if you need to.
USUB_TOKEN_EXPIRATION=120
USUB_TOKEN_LENGTH=100
USUB_REDIRECT_TO_ON_SIGN_IN="/"
USUB_REDIRECT_TO_ON_SIGN_OUT="/"
USUB_REDIRECT_TO_ON_COOKIE_EXPIRATION="/"
Usage
Once you have package installed, following routes are registered:
-
POST /usub/sign-in- used to sign up as given user id.- Fields
- user2 (required)
- redirect_to_on_sign_in (optional, once set - overrides redirect_to_on_sign_in config variable )
- redirect_to_on_sign_out (optional, once set - overrides redirect_to_on_sign_out config variable )
- Fields
-
POST /usub/sign-out- used to "sign up back" to administrator account.- No field needs to be specified.
An example of html form that can be used to sign in as specific user:
@if( \Auth::user()->hasRole('admin') ) @include('vendor.usub.partials.sign_in', [ 'user_id' => $user->id, 'on_sign_in' => route('home'), 'on_sign_out' => route('backend.user.index') ]) @endif
If you won't specify on_sign_in and on_sign_out values, then it will use defaults from the config/usub.php config file.
An example of html form that can be used to sign out and back to admin dashboard (or whatever page you need):
@if( \Illuminate\Support\Facades\Cookie::get('usub_token') ) <li class="nav-item"> <form action="{{ route('usub.sign_out') }}" method="post"> @csrf <button type="submit" class="btn btn-primary">Back to Admin</button> </form> </li> @endif
Cleanup
To delete expired tokens from the usub_tokens database table, you can use php artisan usub:clear command.
codebot/laravel-usub 适用场景与选型建议
codebot/laravel-usub 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.25k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 codebot/laravel-usub 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codebot/laravel-usub 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-22