Skip to content

Commit

Permalink
Updating the package
Browse files Browse the repository at this point in the history
* Adding PHP 7.4 support
* Updating the `same` check for the impersonator and the impersonated
  • Loading branch information
arcanedev-maroc committed Dec 16, 2019
1 parent eb23e0a commit 04f2760
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.2
- 7.3
- 7.4snapshot
- nightly

matrix:
Expand All @@ -19,5 +20,5 @@ script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- if [ "$TRAVIS_PHP_VERSION" != "7.4snapshot" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "7.4snapshot" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
9 changes: 9 additions & 0 deletions src/Contracts/Impersonatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public function canBeImpersonated();
* @return bool
*/
public function isImpersonated();

/**
* Check if the two persons are the same.
*
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated
*
* @return bool
*/
public function isSamePerson($impersonated);
}
22 changes: 20 additions & 2 deletions src/Exceptions/ImpersonationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public static function selfImpersonation(): self
public static function cannotImpersonate(Impersonatable $impersonater): self
{
return static::make(
"The impersonater with `{$impersonater->getAuthIdentifierName()}`=[{$impersonater->getAuthIdentifier()}] doesn't have the ability to impersonate."
__("The impersonater with `:impersonator_name`=[:impersonator_id] doesn't have the ability to impersonate.", [
'impersonator_name' => $impersonater->getAuthIdentifierName(),
'impersonator_id' => $impersonater->getAuthIdentifier(),
])
);
}

Expand All @@ -61,7 +64,22 @@ public static function cannotImpersonate(Impersonatable $impersonater): self
public static function cannotBeImpersonated(Impersonatable $impersonated)
{
return static::make(
"The impersonated with `{$impersonated->getAuthIdentifierName()}`=[{$impersonated->getAuthIdentifier()}] cannot be impersonated."
__('The impersonated with `:impersonated_name`=[:impersonated_id] cannot be impersonated.', [
'impersonated_name' => $impersonated->getAuthIdentifierName(),
'impersonated_id' => $impersonated->getAuthIdentifier()
])
);
}

/**
* Make an exception when the impersonator and the impersonated are the same person.
*
* @return static
*/
public static function impersonaterAndImpersonatedAreSame()
{
return static::make(
__('The impersonater & impersonated with must be different.')
);
}
}
5 changes: 3 additions & 2 deletions src/Impersonator.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ private function mustBeEnabled(): void
*/
private function mustBeDifferentImpersonatable(Impersonatable $impersonater, Impersonatable $impersonated): void
{
if ($impersonater->getAuthIdentifier() == $impersonated->getAuthIdentifier())
throw ImpersonationException::selfImpersonation();
if ($impersonater->isSamePerson($impersonated)) {
throw Exceptions\ImpersonationException::impersonaterAndImpersonatedAreSame();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ImpersonatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function extendAuthDriver(): void
/** @var \Illuminate\Auth\AuthManager $auth */
$auth = $this->app['auth'];

$this->app['auth']->extend('session', function (Application $app, $name, array $config) use ($auth) {
$auth->extend('session', function (Application $app, $name, array $config) use ($auth) {
$provider = $auth->createUserProvider($config['provider']);

return tap(
Expand Down
17 changes: 17 additions & 0 deletions src/Traits/CanImpersonate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\LaravelImpersonator\Traits;

use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
use Illuminate\Database\Eloquent\Model;

/**
* Trait HasImpersonation
Expand Down Expand Up @@ -66,4 +67,20 @@ public function isImpersonated()
{
return impersonator()->isImpersonating();
}

/**
* Check if the two persons are the same.
*
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated
*
* @return bool
*/
public function isSamePerson($impersonated)
{
if ($this instanceof Model && $impersonated instanceof Model) {
return $this->is($impersonated);
}

return $this->getAuthIdentifier() == $impersonated->getAuthIdentifier();
}
}

0 comments on commit 04f2760

Please sign in to comment.