Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stenin-nikita committed Jan 26, 2017
2 parents ff1d148 + 5eeb54f commit 71a57ca
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 196 deletions.
86 changes: 43 additions & 43 deletions src/Impersonation/Middleware/CheckForImpersonating.php
@@ -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);
}
}
124 changes: 62 additions & 62 deletions src/Impersonation/Traits/Impersonable.php
@@ -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';
}
}
5 changes: 2 additions & 3 deletions tests/CheckForImpersonatingTest.php
Expand Up @@ -3,7 +3,6 @@
namespace LaraComponents\Impersonation\Test;

use LaraComponents\Impersonation\Middleware\CheckForImpersonating;
use LaraComponents\Impersonation\Test\TestCase;

class CheckForImpersonatingTest extends TestCase
{
Expand All @@ -20,7 +19,7 @@ public function setUp()
return $this->app['auth']->user();
});

$this->middleware = new CheckForImpersonating($this->app['auth']);
$this->middleware = new CheckForImpersonating($this->app['auth']);
$this->otherUser = TestUser::find(2);
}

Expand Down Expand Up @@ -51,7 +50,7 @@ public function it_user_without_impersonate()

protected function handleMiddleware()
{
return $this->middleware->handle($this->app['request'], function($request) {
return $this->middleware->handle($this->app['request'], function ($request) {
return $request->user();
});
}
Expand Down
38 changes: 18 additions & 20 deletions tests/ImpersonableTest.php
@@ -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());
}
}
101 changes: 50 additions & 51 deletions tests/TestCase.php
@@ -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']);
}
}

0 comments on commit 71a57ca

Please sign in to comment.