Skip to content

Commit

Permalink
- Added model trait Impersonate and its tests
Browse files Browse the repository at this point in the history
- Added travis and scrutinizer configuration
  • Loading branch information
MarceauKa committed Feb 11, 2017
1 parent 52d3774 commit 960a8b8
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,9 @@
checks:
php:
code_rating: true
filter:
paths: ["src/*.php"]
excluded_paths:
- "tests/"
- "config/"
- "migrations/"
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
language: php
php:
- 7.1
- 7.0
- 5.6
before_script: composer install
script: vendor/bin/phpunit -c phpunit.xml
6 changes: 6 additions & 0 deletions src/Controllers/ImpersonateController.php
Expand Up @@ -40,6 +40,12 @@ public function take(Request $request, $id)
abort(403);
}

// The user
if ($request->user()->canImpersonate())
{
abort(403);
}

if ($this->manager->take($request->user(), $this->manager->findUserById($id)))
{
return redirect()->to($this->manager->getTakeRedirectTo());
Expand Down
56 changes: 56 additions & 0 deletions src/Models/Impersonate.php
@@ -0,0 +1,56 @@
<?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;
}

/**
* Impersonate the given user.
*
* @param Model $user
* @return bool
*/
public function impersonate(Model $user)
{
return app(ImpersonateManager::class)->take($this, $user);
}

/**
* 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();
}
}
}
44 changes: 44 additions & 0 deletions tests/Stubs/ModelImpersonateTest.php
@@ -0,0 +1,44 @@
<?php

namespace Lab404\Tests;

use Lab404\Impersonate\Services\ImpersonateManager;

class ModelImpersonateTest extends TestCase
{
/** @test */
public function it_can_impersonate()
{
$user = $this->app['auth']->loginUsingId(1);
$this->assertTrue($user->canImpersonate());
}

/** @test */
public function it_cant_impersonate()
{
$user = $this->app['auth']->loginUsingId(2);
$this->assertFalse($user->canImpersonate());
}

/** @test */
public function it_impersonates()
{
$admin = $this->app['auth']->loginUsingId(1);
$this->assertFalse($admin->isImpersonated());
$user = $this->app[ImpersonateManager::class]->findUserById(2);
$admin->impersonate($user);
$this->assertTrue($user->isImpersonated());
$this->assertEquals($this->app['auth']->user()->id, 2);
}

/** @test */
public function it_can_leave_impersonation()
{
$admin = $this->app['auth']->loginUsingId(1);
$user = $this->app[ImpersonateManager::class]->findUserById(2);
$admin->impersonate($user);
$admin->leaveImpersonation();
$this->assertFalse($user->isImpersonated());
$this->assertNotEquals($this->app['auth']->user()->id, 2);
}
}
11 changes: 10 additions & 1 deletion tests/Stubs/Models/User.php
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Lab404\Impersonate\Models\Impersonate;

class User extends Authenticatable
{
use Notifiable;
use Notifiable, Impersonate;

/**
* @var array
Expand All @@ -22,4 +23,12 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

/**
* @return bool
*/
public function canImpersonate()
{
return $this->attributes['is_admin'] == 1;
}
}

0 comments on commit 960a8b8

Please sign in to comment.