Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/LaraComponents/impersonation
- Loading branch information
Showing
6 changed files
with
192 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
|
||
class CheckForImpersonating | ||
{ | ||
/** | ||
* The authentication factory implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\Factory | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Factory $auth | ||
* @return void | ||
*/ | ||
public function __construct(AuthFactory $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if ($request->user() && $request->user()->isImpersonating()) { | ||
$this->auth->onceUsingId($request->user()->getImpersonatingId()); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Factory as AuthFactory; | ||
|
||
class CheckForImpersonating | ||
{ | ||
/** | ||
* The authentication factory implementation. | ||
* | ||
* @var \Illuminate\Contracts\Auth\Factory | ||
*/ | ||
protected $auth; | ||
|
||
/** | ||
* Create a new middleware instance. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Factory $auth | ||
* @return void | ||
*/ | ||
public function __construct(AuthFactory $auth) | ||
{ | ||
$this->auth = $auth; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if ($request->user() && $request->user()->isImpersonating()) { | ||
$this->auth->onceUsingId($request->user()->getImpersonatingId()); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Traits; | ||
|
||
use Illuminate\Support\Facades\Session; | ||
|
||
trait Impersonable | ||
{ | ||
/** | ||
* Start impersonating this user | ||
* | ||
* @return $this | ||
*/ | ||
public function impersonate() | ||
{ | ||
Session::put($this->getImpersonatingKey(), $this->getKey()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Stop impersonating | ||
* | ||
* @return $this | ||
*/ | ||
public function unimpersonate() | ||
{ | ||
Session::forget($this->getImpersonatingKey()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Check if user is impersonating | ||
* | ||
* @return boolean | ||
*/ | ||
public function isImpersonating() | ||
{ | ||
return Session::has($this->getImpersonatingKey()); | ||
} | ||
|
||
/** | ||
* Get impersonating user id | ||
* | ||
* @return int | ||
*/ | ||
public function getImpersonatingId() | ||
{ | ||
return Session::get($this->getImpersonatingKey()); | ||
} | ||
|
||
/** | ||
* Get impersonating key | ||
* | ||
* @return string | ||
*/ | ||
public function getImpersonatingKey() | ||
{ | ||
return 'impersonate_id'; | ||
} | ||
} | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Traits; | ||
|
||
use Illuminate\Support\Facades\Session; | ||
|
||
trait Impersonable | ||
{ | ||
/** | ||
* Start impersonating this user. | ||
* | ||
* @return $this | ||
*/ | ||
public function impersonate() | ||
{ | ||
Session::put($this->getImpersonatingKey(), $this->getKey()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Stop impersonating. | ||
* | ||
* @return $this | ||
*/ | ||
public function unimpersonate() | ||
{ | ||
Session::forget($this->getImpersonatingKey()); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Check if user is impersonating. | ||
* | ||
* @return bool | ||
*/ | ||
public function isImpersonating() | ||
{ | ||
return Session::has($this->getImpersonatingKey()); | ||
} | ||
|
||
/** | ||
* Get impersonating user id. | ||
* | ||
* @return int | ||
*/ | ||
public function getImpersonatingId() | ||
{ | ||
return Session::get($this->getImpersonatingKey()); | ||
} | ||
|
||
/** | ||
* Get impersonating key. | ||
* | ||
* @return string | ||
*/ | ||
public function getImpersonatingKey() | ||
{ | ||
return 'impersonate_id'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Test; | ||
|
||
use LaraComponents\Impersonation\Test\TestCase; | ||
|
||
class ImpersonableTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_user_impersonate() | ||
{ | ||
$this->assertFalse($this->testUser->isImpersonating()); | ||
$this->testUser->impersonate(); | ||
$this->assertTrue($this->testUser->isImpersonating()); | ||
$this->assertEquals(1, $this->testUser->getImpersonatingId()); | ||
$this->testUser->unimpersonate(); | ||
$this->assertFalse($this->testUser->isImpersonating()); | ||
$this->assertEquals(null, $this->testUser->getImpersonatingId()); | ||
} | ||
} | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Test; | ||
|
||
class ImpersonableTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_user_impersonate() | ||
{ | ||
$this->assertFalse($this->testUser->isImpersonating()); | ||
$this->testUser->impersonate(); | ||
$this->assertTrue($this->testUser->isImpersonating()); | ||
$this->assertEquals(1, $this->testUser->getImpersonatingId()); | ||
$this->testUser->unimpersonate(); | ||
$this->assertFalse($this->testUser->isImpersonating()); | ||
$this->assertEquals(null, $this->testUser->getImpersonatingId()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,50 @@ | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Test; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use LaraComponents\Impersonation\Test\TestUser; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
abstract class TestCase extends Orchestra | ||
{ | ||
/** | ||
* @var \LaraComponents\Impersonation\Test\TestUser | ||
*/ | ||
protected $testUser; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->setUpDatabase($this->app); | ||
$this->testUser = TestUser::find(1); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Foundation\Application $app | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('auth.providers.users.model', TestUser::class); | ||
$app['config']->set('database.default', 'sqlite'); | ||
$app['config']->set('database.connections.sqlite', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
$app['config']->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm'); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Foundation\Application $app | ||
*/ | ||
protected function setUpDatabase($app) | ||
{ | ||
$app['db']->connection()->getSchemaBuilder()->create('test_users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
}); | ||
TestUser::create(['id' => 1, 'name' => 'user 1']); | ||
TestUser::create(['id' => 2, 'name' => 'user 2']); | ||
} | ||
} | ||
<?php | ||
|
||
namespace LaraComponents\Impersonation\Test; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
abstract class TestCase extends Orchestra | ||
{ | ||
/** | ||
* @var \LaraComponents\Impersonation\Test\TestUser | ||
*/ | ||
protected $testUser; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->setUpDatabase($this->app); | ||
$this->testUser = TestUser::find(1); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Foundation\Application $app | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('auth.providers.users.model', TestUser::class); | ||
$app['config']->set('database.default', 'sqlite'); | ||
$app['config']->set('database.connections.sqlite', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
$app['config']->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm'); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Foundation\Application $app | ||
*/ | ||
protected function setUpDatabase($app) | ||
{ | ||
$app['db']->connection()->getSchemaBuilder()->create('test_users', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
}); | ||
TestUser::create(['id' => 1, 'name' => 'user 1']); | ||
TestUser::create(['id' => 2, 'name' => 'user 2']); | ||
} | ||
} |
Oops, something went wrong.