vendor and env first commit
This commit is contained in:
+177
@@ -0,0 +1,177 @@
|
||||
# Laravel Impersonate Changelog
|
||||
|
||||
## 1.7.5
|
||||
|
||||
- Laravel 11.x support (thanks to [erikn69](https://github.com/erikn69), [#195](https://github.
|
||||
com/404labfr/laravel-impersonate/pull/195))
|
||||
|
||||
## 1.7.4
|
||||
|
||||
- Laravel 10.x support (thanks to [freekmurze](https://github.com/freekmurze), [#175](https://github.
|
||||
com/404labfr/laravel-impersonate/pull/175))
|
||||
|
||||
## 1.7.3
|
||||
|
||||
- Laravel 9.x support (thanks to [freekmurze](https://github.com/freekmurze), [#149](https://github.com/404labfr/laravel-impersonate/pull/149))
|
||||
|
||||
## 1.7.2
|
||||
|
||||
### Added
|
||||
|
||||
- Supports for PHP 8.0 (thanks to [pascalbaljet](https://github.com/pascalbaljet), [#121](https://github.com/404labfr/laravel-impersonate/pull/121))
|
||||
|
||||
### Fixed
|
||||
|
||||
- getImpersonator() when working with multiple guards (thanks to [carsso](https://github.com/carsso)), [#120](https://github.com/404labfr/laravel-impersonate/pull/120))
|
||||
- Can't leave impersonation when multi-guard is used (thanks to [ctf0](https://github.com/ctf0), [#116](https://github.com/404labfr/laravel-impersonate/pull/117))
|
||||
|
||||
### Changed
|
||||
|
||||
- Allow guard name in blade directives (thanks to [ctf0](https://github.com/ctf0), [#115](https://github.com/404labfr/laravel-impersonate/pull/117))
|
||||
- Documentation about multi-guard usage (thanks to [ctf0](https://github.com/ctf0), [#117](https://github.com/404labfr/laravel-impersonate/pull/117))
|
||||
|
||||
### Removed
|
||||
|
||||
- composer.lock
|
||||
|
||||
## 1.7.1
|
||||
|
||||
### Added
|
||||
|
||||
- Laravel 8.x support
|
||||
|
||||
## 1.7.0
|
||||
|
||||
### Added
|
||||
|
||||
- `ImpersonateManager@findUserById` will now throw a `MissingUserProvider` exception when guard has no user provider [baa722b](https://github.com/404labfr/laravel-impersonate/commit/baa722b1bde0aefd9efbd9454c699b7894ddc49b)
|
||||
- `ImpersonateManager@findUserById` will now throw a `InvalidUserProvider` exception when guard has an invalid user provider [baa722b](https://github.com/404labfr/laravel-impersonate/commit/baa722b1bde0aefd9efbd9454c699b7894ddc49b)
|
||||
|
||||
### Changed
|
||||
|
||||
- Helper `can_be_impersonated` now use model `getAuthIdentifier()` instead of hardcoded `id` column [#105](https://github.com/404labfr/laravel-impersonate/pull/105)
|
||||
- Git attributes [#108](https://github.com/404labfr/laravel-impersonate/pull/108)
|
||||
|
||||
## 1.6.0
|
||||
|
||||
### Added
|
||||
|
||||
- Laravel 7.x support
|
||||
|
||||
### Removed
|
||||
|
||||
- Laravel 5.x support
|
||||
|
||||
## 1.5.1
|
||||
|
||||
### Changed
|
||||
|
||||
- Use `Illuminate\Contracts\Auth\Authenticatable@getAuthIdentifier` instead of `Illuminate\Database\Eloquent\Model@getKey` [#96](https://github.com/404labfr/laravel-impersonate/pull/96)
|
||||
- PHPDoc updated
|
||||
|
||||
## 1.5.0
|
||||
|
||||
### Changed
|
||||
|
||||
- Events uses `Illuminate\Contracts\Auth\Authenticatable` instead of `Illuminate\Database\Eloquent\Model` [#92](https://github.com/404labfr/laravel-impersonate/pull/92)
|
||||
- PHPDoc and return values for `ImpersonateManager`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Security issue for `symfony/http-foundation` ([CVE-2019-18888](https://github.com/advisories/GHSA-xhh6-956q-4q69))
|
||||
|
||||
## 1.4.3
|
||||
|
||||
### Fixed
|
||||
|
||||
- `can_impersonate()` helper
|
||||
- Tests for Blade directives
|
||||
|
||||
## 1.4.2
|
||||
|
||||
### Added
|
||||
|
||||
- `is_impersonating()`, `can_impersonate()` and `can_be_impersonated()` helpers
|
||||
|
||||
### Changed
|
||||
|
||||
- Blade directives now use helpers
|
||||
|
||||
## 1.4.1
|
||||
|
||||
### Fixed
|
||||
|
||||
- Laravel 6.0 dependencies compatibility
|
||||
- dump() in ImpersonateManager.php
|
||||
|
||||
## 1.4.0
|
||||
|
||||
### Added
|
||||
|
||||
- Allows impersonation through multiple guards ([Pull 58](https://github.com/404labfr/laravel-impersonate/pull/58))
|
||||
- Added the public method `getImpersonator` to `ImpersonateManager` ([Pull 69](https://github.com/404labfr/laravel-impersonate/pull/69))
|
||||
|
||||
### Changed
|
||||
|
||||
- Laravel 6.0 compatibility (min version is 5.8)
|
||||
|
||||
### Fixed
|
||||
|
||||
- The user `remember_token` is now preserved ([Pull 71](https://github.com/404labfr/laravel-impersonate/pull/71))
|
||||
|
||||
## 1.3.0 (2019-02-28)
|
||||
|
||||
### Changed
|
||||
|
||||
- Laravel 5.8 compatibility (min version)
|
||||
|
||||
## 1.2.3 (2018-09-03)
|
||||
|
||||
### Changed
|
||||
|
||||
- Documentation
|
||||
- Use `getSessionKey()` in `take()` method
|
||||
|
||||
## 1.2.2 (2018-01-19)
|
||||
|
||||
### Changed
|
||||
|
||||
- Register Blade directives after resolving
|
||||
|
||||
### Fixed
|
||||
|
||||
- Blade directives documentation
|
||||
|
||||
## 1.2.1 (2017-09-03)
|
||||
|
||||
### Changed
|
||||
|
||||
- PHP version requirement
|
||||
- Laravel version requirement
|
||||
|
||||
## 1.2.0 (2017-07-28)
|
||||
|
||||
### Added
|
||||
|
||||
- Laravel 5.5 compatibility
|
||||
- Package auto-discovery
|
||||
|
||||
## 1.1.0 (2017-03-05)
|
||||
|
||||
### Added
|
||||
|
||||
- Custom Session guard driver based on the original Session Guard ([c76bb96](https://github.com/404labfr/laravel-impersonate/commit/c76bb96da9ca53b70fd3ce5d063722076ffcbcb4))
|
||||
|
||||
### Changed
|
||||
|
||||
- The Auth events `login`, `authenticated` and `logout` are not fired anymore when taking or leaving impersonation
|
||||
|
||||
### Fixed
|
||||
|
||||
- The user remember token is not touched when taking and leaving impersonation ([#11](https://github.com/404labfr/laravel-impersonate/issues/11))
|
||||
|
||||
## 1.0.11 (2017-03-05)
|
||||
|
||||
### Added
|
||||
|
||||
- New blade directive `canBeImpersonated` ([#12](https://github.com/404labfr/laravel-impersonate/issues/12))
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "lab404/laravel-impersonate",
|
||||
"description": "Laravel Impersonate is a plugin that allows to you to authenticate as your users.",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"laravel",
|
||||
"package",
|
||||
"plugin",
|
||||
"impersonate",
|
||||
"impersonation",
|
||||
"user",
|
||||
"auth",
|
||||
"laravel-package",
|
||||
"laravel-plugin"
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2 | ^8.0",
|
||||
"laravel/framework": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.5 | ^8.0 | ^9.0 | ^10.0",
|
||||
"mockery/mockery": "^1.3.3",
|
||||
"orchestra/testbench": "^4.0 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0"
|
||||
},
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Marceau Casals",
|
||||
"email": "marceau@casals.fr"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lab404\\Impersonate\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
]
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Lab404\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Lab404\\Impersonate\\ImpersonateServiceProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
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',
|
||||
|
||||
/**
|
||||
* The URI to redirect after taking an impersonation.
|
||||
*
|
||||
* Only used in the built-in controller.
|
||||
* * Use 'back' to redirect to the previous page
|
||||
*/
|
||||
'take_redirect_to' => '/',
|
||||
|
||||
/**
|
||||
* The URI to redirect after leaving an impersonation.
|
||||
*
|
||||
* Only used in the built-in controller.
|
||||
* Use 'back' to redirect to the previous page
|
||||
*/
|
||||
'leave_redirect_to' => '/',
|
||||
|
||||
];
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table)
|
||||
{
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->string('password');
|
||||
$table->boolean('is_admin')->default(0)->index();
|
||||
$table->boolean('can_be_impersonated')->default(1)->index();
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
DB::table('users')->insert([
|
||||
[
|
||||
'name' => 'Admin',
|
||||
'email' => 'admin@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 1,
|
||||
'can_be_impersonated' => 1,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
[
|
||||
'name' => 'User',
|
||||
'email' => 'user@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 0,
|
||||
'can_be_impersonated' => 1,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
[
|
||||
'name' => 'SuperAdmin',
|
||||
'email' => 'superadmin@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 1,
|
||||
'can_be_impersonated' => 0,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
}
|
||||
}
|
||||
Vendored
+67
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CreateOtherUsersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('other_users', function (Blueprint $table)
|
||||
{
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->string('password');
|
||||
$table->boolean('is_admin')->default(0)->index();
|
||||
$table->boolean('can_be_impersonated')->default(1)->index();
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
DB::table('other_users')->insert([
|
||||
[
|
||||
'name' => 'OtherAdmin',
|
||||
'email' => 'otheradmin@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 1,
|
||||
'can_be_impersonated' => 1,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
[
|
||||
'name' => 'OtherUser',
|
||||
'email' => 'otheruser@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 0,
|
||||
'can_be_impersonated' => 1,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
[
|
||||
'name' => 'OtherSuperAdmin',
|
||||
'email' => 'othersuperadmin@test.rocks',
|
||||
'password' => bcrypt('password'),
|
||||
'is_admin' => 1,
|
||||
'can_be_impersonated' => 0,
|
||||
'created_at' => Carbon::now()->toDateTimeString(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('other_users');
|
||||
}
|
||||
}
|
||||
+257
@@ -0,0 +1,257 @@
|
||||
# Laravel Impersonate
|
||||
|
||||
[](https://travis-ci.org/404labfr/laravel-impersonate) [](https://scrutinizer-ci.com/g/404labfr/laravel-impersonate/?branch=master)
|
||||
|
||||
**Laravel Impersonate** makes it easy to **authenticate as your users**. Add a simple **trait** to your **user model** and impersonate as one of your users in one click.
|
||||
|
||||
- [Requirements](#requirements)
|
||||
- [Installation](#installation)
|
||||
- [Simple usage](#simple-usage)
|
||||
- [Using the built-in controller](#using-the-built-in-controller)
|
||||
- [Advanced Usage](#advanced-usage)
|
||||
- [Defining impersonation authorization](#defining-impersonation-authorization)
|
||||
- [Using your own strategy](#using-your-own-strategy)
|
||||
- [Middleware](#middleware)
|
||||
- [Events](#events)
|
||||
- [Configuration](#configuration)
|
||||
- [Blade](#blade)
|
||||
- [Tests](#tests)
|
||||
- [Contributors](#contributors)
|
||||
- [Why Not Just Use loginAsId()?](#rationale)
|
||||
|
||||
## Requirements
|
||||
|
||||
- Laravel 6.x to 11.x
|
||||
- PHP >= 7.2 or >= 8.0
|
||||
|
||||
### Laravel support
|
||||
|
||||
| Version | Release |
|
||||
|:-------------:|:-------------:|
|
||||
| 6.x to 11.x | 1.7 |
|
||||
| 6.x, 7.x | 1.6 |
|
||||
| 5.8 | 1.5 |
|
||||
| 5.7, 5.6 | 1.2 |
|
||||
| 5.5, 5.4 | 1.1 |
|
||||
|
||||
## Installation
|
||||
|
||||
- Require it with Composer:
|
||||
```bash
|
||||
composer require lab404/laravel-impersonate
|
||||
```
|
||||
|
||||
- Add the service provider at the end of your `config/app.php`:
|
||||
```php
|
||||
'providers' => [
|
||||
// ...
|
||||
Lab404\Impersonate\ImpersonateServiceProvider::class,
|
||||
],
|
||||
```
|
||||
|
||||
- Add the trait `Lab404\Impersonate\Models\Impersonate` to your **User** model.
|
||||
|
||||
## Simple usage
|
||||
|
||||
Impersonate a user:
|
||||
```php
|
||||
Auth::user()->impersonate($other_user);
|
||||
// You're now logged as the $other_user
|
||||
```
|
||||
|
||||
Leave impersonation:
|
||||
```php
|
||||
Auth::user()->leaveImpersonation();
|
||||
// You're now logged as your original user.
|
||||
```
|
||||
|
||||
### Using the built-in controller
|
||||
|
||||
In your routes file, under web middleware, you must call the `impersonate` route macro.
|
||||
|
||||
```php
|
||||
Route::impersonate();
|
||||
```
|
||||
|
||||
Alternatively, you can execute this macro with your `RouteServiceProvider`.
|
||||
|
||||
```php
|
||||
namespace App\Providers;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function map() {
|
||||
Route::middleware('web')->group(function (Router $router) {
|
||||
$router->impersonate();
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```php
|
||||
// Where $id is the ID of the user you want impersonate
|
||||
route('impersonate', $id)
|
||||
|
||||
// Or in case of multi guards, you should also add `guardName` (defaults to `web`)
|
||||
route('impersonate', ['id' => $id, 'guardName' => 'admin'])
|
||||
|
||||
// Generate an URL to leave current impersonation
|
||||
route('impersonate.leave')
|
||||
```
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Defining impersonation authorization
|
||||
|
||||
By default all users can **impersonate** an user.
|
||||
You need to add the method `canImpersonate()` to your user model:
|
||||
|
||||
```php
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function canImpersonate()
|
||||
{
|
||||
// For example
|
||||
return $this->is_admin == 1;
|
||||
}
|
||||
```
|
||||
|
||||
By default all users can **be impersonated**.
|
||||
You need to add the method `canBeImpersonated()` to your user model to extend this behavior:
|
||||
|
||||
```php
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function canBeImpersonated()
|
||||
{
|
||||
// For example
|
||||
return $this->can_be_impersonated == 1;
|
||||
}
|
||||
```
|
||||
|
||||
### Using your own strategy
|
||||
|
||||
- Getting the manager:
|
||||
```php
|
||||
// With the app helper
|
||||
app('impersonate')
|
||||
// Dependency Injection
|
||||
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
|
||||
```
|
||||
|
||||
- Working with the manager:
|
||||
```php
|
||||
$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();
|
||||
```
|
||||
|
||||
### Middleware
|
||||
|
||||
**Protect From Impersonation**
|
||||
|
||||
You can use the middleware `impersonate.protect` to protect your routes against user impersonation.
|
||||
This middleware can be useful when you want to protect specific pages like users subscriptions, users credit cards, ...
|
||||
|
||||
```php
|
||||
Router::get('/my-credit-card', function() {
|
||||
echo "Can't be accessed by an impersonator";
|
||||
})->middleware('impersonate.protect');
|
||||
```
|
||||
|
||||
### Events
|
||||
|
||||
There are two events available that can be used to improve your workflow:
|
||||
- `TakeImpersonation` is fired when an impersonation is taken.
|
||||
- `LeaveImpersonation` is fired when an impersonation is leaved.
|
||||
|
||||
Each events returns two properties `$event->impersonator` and `$event->impersonated` containing User model instance.
|
||||
|
||||
## Configuration
|
||||
|
||||
The package comes with a configuration file.
|
||||
|
||||
Publish it with the following command:
|
||||
```bash
|
||||
php artisan vendor:publish --tag=impersonate
|
||||
```
|
||||
|
||||
Available options:
|
||||
```php
|
||||
// The session key used to store the original user id.
|
||||
'session_key' => 'impersonated_by',
|
||||
// Where to redirect after taking an impersonation.
|
||||
// Only used in the built-in controller.
|
||||
// You can use: an URI, the keyword back (to redirect back) or a route name
|
||||
'take_redirect_to' => '/',
|
||||
// Where to redirect after leaving an impersonation.
|
||||
// Only used in the built-in controller.
|
||||
// You can use: an URI, the keyword back (to redirect back) or a route name
|
||||
'leave_redirect_to' => '/'
|
||||
```
|
||||
|
||||
## Blade
|
||||
|
||||
There are three Blade directives available.
|
||||
|
||||
### When the user can impersonate
|
||||
|
||||
```blade
|
||||
@canImpersonate($guard = null)
|
||||
<a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
|
||||
@endCanImpersonate
|
||||
```
|
||||
|
||||
### When the user can be impersonated
|
||||
|
||||
This comes in handy when you have a user list and want to show an "Impersonate" button next to all the users.
|
||||
But you don\'t want that button next to the current authenticated user neither to that users which should not be able to impersonated according your implementation of `canBeImpersonated()` .
|
||||
|
||||
```blade
|
||||
@canBeImpersonated($user, $guard = null)
|
||||
<a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
|
||||
@endCanBeImpersonated
|
||||
```
|
||||
|
||||
### When the user is impersonated
|
||||
|
||||
```blade
|
||||
@impersonating($guard = null)
|
||||
<a href="{{ route('impersonate.leave') }}">Leave impersonation</a>
|
||||
@endImpersonating
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
```bash
|
||||
vendor/bin/phpunit
|
||||
```
|
||||
|
||||
## Contributors
|
||||
|
||||
- This package was created by [MarceauKa](https://github.com/MarceauKa) and [tghpow](https://github.com/tghpow). Many thanks to all of our [contributors](https://github.com/404labfr/laravel-impersonate/graphs/contributors).
|
||||
|
||||
## Rationale
|
||||
|
||||
### Why not just use `loginAsId()`?
|
||||
|
||||
This package adds broader functionality, including Blade directives to allow you to override analytics and other tracking events when impersonating, fire events based on impersonation status, and more. Brief discussion at [issues/5](https://github.com/404labfr/laravel-impersonate/issues/5)
|
||||
|
||||
## Licence
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Controllers;
|
||||
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Lab404\Impersonate\Services\ImpersonateManager;
|
||||
|
||||
class ImpersonateController extends Controller
|
||||
{
|
||||
/** @var ImpersonateManager */
|
||||
protected $manager;
|
||||
|
||||
/**
|
||||
* ImpersonateController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->manager = app()->make(ImpersonateManager::class);
|
||||
|
||||
$guard = $this->manager->getDefaultSessionGuard();
|
||||
$this->middleware('auth:' . $guard)->only('take');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string|null $guardName
|
||||
* @return RedirectResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function take(Request $request, $id, $guardName = null)
|
||||
{
|
||||
$guardName = $guardName ?? $this->manager->getDefaultSessionGuard();
|
||||
|
||||
// Cannot impersonate yourself
|
||||
if ($id == $request->user()->getAuthIdentifier() && ($this->manager->getCurrentAuthGuardName() == $guardName)) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
// Cannot impersonate again if you're already impersonate a user
|
||||
if ($this->manager->isImpersonating()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
if (!$request->user()->canImpersonate()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$userToImpersonate = $this->manager->findUserById($id, $guardName);
|
||||
|
||||
if ($userToImpersonate->canBeImpersonated()) {
|
||||
if ($this->manager->take($request->user(), $userToImpersonate, $guardName)) {
|
||||
$takeRedirect = $this->manager->getTakeRedirectTo();
|
||||
if ($takeRedirect !== 'back') {
|
||||
return redirect()->to($takeRedirect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function leave()
|
||||
{
|
||||
if (!$this->manager->isImpersonating()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$this->manager->leave();
|
||||
|
||||
$leaveRedirect = $this->manager->getLeaveRedirectTo();
|
||||
if ($leaveRedirect !== 'back') {
|
||||
return redirect()->to($leaveRedirect);
|
||||
}
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class LeaveImpersonation
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/** @var Authenticatable */
|
||||
public $impersonator;
|
||||
|
||||
/** @var Authenticatable */
|
||||
public $impersonated;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Authenticatable $impersonator, Authenticatable $impersonated)
|
||||
{
|
||||
$this->impersonator = $impersonator;
|
||||
$this->impersonated = $impersonated;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
|
||||
class TakeImpersonation
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/** @var Authenticatable */
|
||||
public $impersonator;
|
||||
|
||||
/** @var Authenticatable */
|
||||
public $impersonated;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Authenticatable $impersonator, Authenticatable $impersonated)
|
||||
{
|
||||
$this->impersonator = $impersonator;
|
||||
$this->impersonated = $impersonated;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Exceptions;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class InvalidUserProvider extends \Exception
|
||||
{
|
||||
public function __construct(string $guard, $message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct(sprintf('Invalid user provider for guard %s', $guard), $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Exceptions;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class MissingUserProvider extends \Exception
|
||||
{
|
||||
public function __construct(string $guard, $message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct(sprintf('Missing user provider for guard %s', $guard), $code, $previous);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Guard;
|
||||
|
||||
use Illuminate\Auth\SessionGuard as BaseSessionGuard;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class SessionGuard extends BaseSessionGuard
|
||||
{
|
||||
/**
|
||||
* Log a user into the application without firing the Login event.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
||||
* @return void
|
||||
*/
|
||||
public function quietLogin(Authenticatable $user)
|
||||
{
|
||||
$this->updateSession($user->getAuthIdentifier());
|
||||
|
||||
$this->setUser($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout the user without updating remember_token
|
||||
* and without firing the Logout event.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
public function quietLogout()
|
||||
{
|
||||
$this->clearUserDataFromStorage();
|
||||
|
||||
$this->user = null;
|
||||
|
||||
$this->loggedOut = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Lab404\Impersonate\Services\ImpersonateManager;
|
||||
|
||||
class Impersonate extends Facade
|
||||
{
|
||||
/**
|
||||
* Get the registered name of the component.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return ImpersonateManager::class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate;
|
||||
|
||||
use Illuminate\Auth\AuthManager;
|
||||
use Illuminate\Auth\Events\Login;
|
||||
use Illuminate\Auth\Events\Logout;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\View\Compilers\BladeCompiler;
|
||||
use Lab404\Impersonate\Guard\SessionGuard;
|
||||
use Lab404\Impersonate\Middleware\ProtectFromImpersonation;
|
||||
use Lab404\Impersonate\Services\ImpersonateManager;
|
||||
|
||||
/**
|
||||
* Class ServiceProvider
|
||||
*
|
||||
* @package Lab404\Impersonate
|
||||
*/
|
||||
class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
|
||||
{
|
||||
/** @var string $configName */
|
||||
protected $configName = 'laravel-impersonate';
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfig();
|
||||
|
||||
$this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
|
||||
|
||||
$this->app->singleton(ImpersonateManager::class, function ($app) {
|
||||
return new ImpersonateManager($app);
|
||||
});
|
||||
|
||||
$this->app->alias(ImpersonateManager::class, 'impersonate');
|
||||
|
||||
$this->registerRoutesMacro();
|
||||
$this->registerBladeDirectives();
|
||||
$this->registerMiddleware();
|
||||
$this->registerAuthDriver();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishConfig();
|
||||
|
||||
// We want to remove data from storage on real login and logout
|
||||
Event::listen(Login::class, function ($event) {
|
||||
app('impersonate')->clear();
|
||||
});
|
||||
Event::listen(Logout::class, function ($event) {
|
||||
app('impersonate')->clear();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register plugin blade directives.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
protected function registerBladeDirectives()
|
||||
{
|
||||
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
|
||||
$bladeCompiler->directive('impersonating', function ($guard = null) {
|
||||
return "<?php if (is_impersonating({$guard})) : ?>";
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('endImpersonating', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('canImpersonate', function ($guard = null) {
|
||||
return "<?php if (can_impersonate({$guard})) : ?>";
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('endCanImpersonate', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('canBeImpersonated', function ($expression) {
|
||||
$args = preg_split("/,(\s+)?/", $expression);
|
||||
$guard = $args[1] ?? null;
|
||||
|
||||
return "<?php if (can_be_impersonated({$args[0]}, {$guard})) : ?>";
|
||||
});
|
||||
|
||||
$bladeCompiler->directive('endCanBeImpersonated', function () {
|
||||
return '<?php endif; ?>';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register routes macro.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
protected function registerRoutesMacro()
|
||||
{
|
||||
$router = $this->app['router'];
|
||||
|
||||
$router->macro('impersonate', function () use ($router) {
|
||||
$router->get('/impersonate/take/{id}/{guardName?}',
|
||||
'\Lab404\Impersonate\Controllers\ImpersonateController@take')->name('impersonate');
|
||||
$router->get('/impersonate/leave',
|
||||
'\Lab404\Impersonate\Controllers\ImpersonateController@leave')->name('impersonate.leave');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
protected function registerAuthDriver()
|
||||
{
|
||||
/** @var AuthManager $auth */
|
||||
$auth = $this->app['auth'];
|
||||
|
||||
$auth->extend('session', function (Application $app, $name, array $config) use ($auth) {
|
||||
$provider = $auth->createUserProvider($config['provider']);
|
||||
|
||||
$guard = new SessionGuard($name, $provider, $app['session.store']);
|
||||
|
||||
if (method_exists($guard, 'setCookieJar')) {
|
||||
$guard->setCookieJar($app['cookie']);
|
||||
}
|
||||
|
||||
if (method_exists($guard, 'setDispatcher')) {
|
||||
$guard->setDispatcher($app['events']);
|
||||
}
|
||||
|
||||
if (method_exists($guard, 'setRequest')) {
|
||||
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));
|
||||
}
|
||||
|
||||
return $guard;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register plugin middleware.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
public function registerMiddleware()
|
||||
{
|
||||
$this->app['router']->aliasMiddleware('impersonate.protect', ProtectFromImpersonation::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge config file.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
protected function mergeConfig()
|
||||
{
|
||||
$configPath = __DIR__ . '/../config/' . $this->configName . '.php';
|
||||
|
||||
$this->mergeConfigFrom($configPath, $this->configName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish config file.
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
protected function publishConfig()
|
||||
{
|
||||
$configPath = __DIR__ . '/../config/' . $this->configName . '.php';
|
||||
|
||||
$this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Lab404\Impersonate\Services\ImpersonateManager;
|
||||
|
||||
class ProtectFromImpersonation
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$impersonate_manager = app()->make(ImpersonateManager::class);
|
||||
|
||||
if ($impersonate_manager->isImpersonating()) {
|
||||
return Redirect::back();
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Lab404\Impersonate\Services\ImpersonateManager;
|
||||
|
||||
trait Impersonate
|
||||
{
|
||||
/**
|
||||
* Return true or false if the user can impersonate an other user.
|
||||
*
|
||||
* @param void
|
||||
* @return bool
|
||||
*/
|
||||
public function canImpersonate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true or false if the user can be impersonate.
|
||||
*
|
||||
* @param void
|
||||
* @return bool
|
||||
*/
|
||||
public function canBeImpersonated()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Impersonate the given user.
|
||||
*
|
||||
* @param Model $user
|
||||
* @param string|null $guardName
|
||||
* @return bool
|
||||
*/
|
||||
public function impersonate(Model $user, $guardName = null)
|
||||
{
|
||||
return app(ImpersonateManager::class)->take($this, $user, $guardName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user is impersonated.
|
||||
*
|
||||
* @param void
|
||||
* @return bool
|
||||
*/
|
||||
public function isImpersonated()
|
||||
{
|
||||
return app(ImpersonateManager::class)->isImpersonating();
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave the current impersonation.
|
||||
*
|
||||
* @param void
|
||||
* @return bool
|
||||
*/
|
||||
public function leaveImpersonation()
|
||||
{
|
||||
if ($this->isImpersonated()) {
|
||||
return app(ImpersonateManager::class)->leave();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
namespace Lab404\Impersonate\Services;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Lab404\Impersonate\Events\LeaveImpersonation;
|
||||
use Lab404\Impersonate\Events\TakeImpersonation;
|
||||
use Lab404\Impersonate\Exceptions\InvalidUserProvider;
|
||||
use Lab404\Impersonate\Exceptions\MissingUserProvider;
|
||||
|
||||
class ImpersonateManager
|
||||
{
|
||||
const REMEMBER_PREFIX = 'remember_web';
|
||||
|
||||
/** @var Application $app */
|
||||
private $app;
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return \Illuminate\Contracts\Auth\Authenticatable
|
||||
* @throws MissingUserProvider
|
||||
* @throws InvalidUserProvider
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function findUserById($id, $guardName = null)
|
||||
{
|
||||
if (empty($guardName)) {
|
||||
$guardName = $this->app['config']->get('auth.default.guard', 'web');
|
||||
}
|
||||
|
||||
$providerName = $this->app['config']->get("auth.guards.$guardName.provider");
|
||||
|
||||
if (empty($providerName)) {
|
||||
throw new MissingUserProvider($guardName);
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var UserProvider $userProvider */
|
||||
$userProvider = $this->app['auth']->createUserProvider($providerName);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new InvalidUserProvider($guardName);
|
||||
}
|
||||
|
||||
if (!($modelInstance = $userProvider->retrieveById($id))) {
|
||||
$model = $this->app['config']->get("auth.providers.$providerName.model");
|
||||
|
||||
throw (new ModelNotFoundException())->setModel(
|
||||
$model,
|
||||
$id
|
||||
);
|
||||
}
|
||||
|
||||
return $modelInstance;
|
||||
}
|
||||
|
||||
public function isImpersonating(): bool
|
||||
{
|
||||
return session()->has($this->getSessionKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getImpersonatorId()
|
||||
{
|
||||
return session($this->getSessionKey(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Contracts\Auth\Authenticatable
|
||||
*/
|
||||
public function getImpersonator()
|
||||
{
|
||||
$id = session($this->getSessionKey(), null);
|
||||
|
||||
return is_null($id) ? null : $this->findUserById($id, $this->getImpersonatorGuardName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getImpersonatorGuardName()
|
||||
{
|
||||
return session($this->getSessionGuard(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getImpersonatorGuardUsingName()
|
||||
{
|
||||
return session($this->getSessionGuardUsing(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Contracts\Auth\Authenticatable $from
|
||||
* @param \Illuminate\Contracts\Auth\Authenticatable $to
|
||||
* @param string|null $guardName
|
||||
* @return bool
|
||||
*/
|
||||
public function take($from, $to, $guardName = null)
|
||||
{
|
||||
$this->saveAuthCookieInSession();
|
||||
|
||||
try {
|
||||
$currentGuard = $this->getCurrentAuthGuardName();
|
||||
session()->put($this->getSessionKey(), $from->getAuthIdentifier());
|
||||
session()->put($this->getSessionGuard(), $currentGuard);
|
||||
session()->put($this->getSessionGuardUsing(), $guardName);
|
||||
|
||||
$this->app['auth']->guard($currentGuard)->quietLogout();
|
||||
$this->app['auth']->guard($guardName)->quietLogin($to);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
unset($e);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->app['events']->dispatch(new TakeImpersonation($from, $to));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function leave(): bool
|
||||
{
|
||||
try {
|
||||
$impersonated = $this->app['auth']->guard($this->getImpersonatorGuardUsingName())->user();
|
||||
$impersonator = $this->findUserById($this->getImpersonatorId(), $this->getImpersonatorGuardName());
|
||||
|
||||
$this->app['auth']->guard($this->getCurrentAuthGuardName())->quietLogout();
|
||||
$this->app['auth']->guard($this->getImpersonatorGuardName())->quietLogin($impersonator);
|
||||
|
||||
$this->extractAuthCookieFromSession();
|
||||
|
||||
$this->clear();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
unset($e);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->app['events']->dispatch(new LeaveImpersonation($impersonator, $impersonated));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
session()->forget($this->getSessionKey());
|
||||
session()->forget($this->getSessionGuard());
|
||||
session()->forget($this->getSessionGuardUsing());
|
||||
}
|
||||
|
||||
public function getSessionKey(): string
|
||||
{
|
||||
return config('laravel-impersonate.session_key');
|
||||
}
|
||||
|
||||
public function getSessionGuard(): string
|
||||
{
|
||||
return config('laravel-impersonate.session_guard');
|
||||
}
|
||||
|
||||
public function getSessionGuardUsing(): string
|
||||
{
|
||||
return config('laravel-impersonate.session_guard_using');
|
||||
}
|
||||
|
||||
public function getDefaultSessionGuard(): string
|
||||
{
|
||||
return config('laravel-impersonate.default_impersonator_guard');
|
||||
}
|
||||
|
||||
public function getTakeRedirectTo(): string
|
||||
{
|
||||
try {
|
||||
$uri = route(config('laravel-impersonate.take_redirect_to'));
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$uri = config('laravel-impersonate.take_redirect_to');
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
public function getLeaveRedirectTo(): string
|
||||
{
|
||||
try {
|
||||
$uri = route(config('laravel-impersonate.leave_redirect_to'));
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$uri = config('laravel-impersonate.leave_redirect_to');
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getCurrentAuthGuardName()
|
||||
{
|
||||
$guards = array_keys(config('auth.guards'));
|
||||
|
||||
foreach ($guards as $guard) {
|
||||
if ($this->app['auth']->guard($guard)->check()) {
|
||||
return $guard;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function saveAuthCookieInSession(): void
|
||||
{
|
||||
$cookie = $this->findByKeyInArray($this->app['request']->cookies->all(), static::REMEMBER_PREFIX);
|
||||
$key = $cookie->keys()->first();
|
||||
$val = $cookie->values()->first();
|
||||
|
||||
if (!$key || !$val) {
|
||||
return;
|
||||
}
|
||||
|
||||
session()->put(static::REMEMBER_PREFIX, [
|
||||
$key,
|
||||
$val,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function extractAuthCookieFromSession(): void
|
||||
{
|
||||
if (!$session = $this->findByKeyInArray(session()->all(), static::REMEMBER_PREFIX)->first()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->app['cookie']->queue($session[0], $session[1]);
|
||||
session()->forget($session);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @param string $search
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
protected function findByKeyInArray(array $values, string $search)
|
||||
{
|
||||
return collect($values ?? session()->all())
|
||||
->filter(function ($val, $key) use ($search) {
|
||||
return strpos($key, $search) !== false;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
if (! function_exists('can_impersonate')) {
|
||||
|
||||
/**
|
||||
* Check whether the current user is authorized to impersonate.
|
||||
*
|
||||
* @param null $guard
|
||||
* @return bool
|
||||
*/
|
||||
function can_impersonate(string $guard = null): bool
|
||||
{
|
||||
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
|
||||
|
||||
return app('auth')->guard($guard)->check()
|
||||
&& app('auth')->guard($guard)->user()->canImpersonate();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('can_be_impersonated')) {
|
||||
|
||||
/**
|
||||
* Check whether the specified user can be impersonated.
|
||||
*
|
||||
* @param Authenticatable $user
|
||||
* @param string|null $guard
|
||||
* @return bool
|
||||
*/
|
||||
function can_be_impersonated(Authenticatable $user, string $guard = null): bool
|
||||
{
|
||||
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
|
||||
return app('auth')->guard($guard)->check()
|
||||
&& app('auth')->guard($guard)->user()->isNot($user)
|
||||
&& $user->canBeImpersonated();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('is_impersonating')) {
|
||||
|
||||
/**
|
||||
* Check whether the current user is being impersonated.
|
||||
*
|
||||
* @param string|null $guard
|
||||
* @return bool
|
||||
*/
|
||||
function is_impersonating(string $guard = null): bool
|
||||
{
|
||||
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
|
||||
|
||||
return app('auth')->guard($guard)->check()
|
||||
&& app('auth')->guard($guard)->user()->isImpersonated();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user