Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added model trait Impersonate and its tests
- Added travis and scrutinizer configuration
- Loading branch information
Showing
6 changed files
with
132 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
checks: | ||
php: | ||
code_rating: true | ||
filter: | ||
paths: ["src/*.php"] | ||
excluded_paths: | ||
- "tests/" | ||
- "config/" | ||
- "migrations/" |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: php | ||
php: | ||
- 7.1 | ||
- 7.0 | ||
- 5.6 | ||
before_script: composer install | ||
script: vendor/bin/phpunit -c phpunit.xml |
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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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