From 04f2760fe9677bdf7ce825b5e8b503c4dade39ef Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Mon, 16 Dec 2019 18:30:32 +0100 Subject: [PATCH] Updating the package * Adding PHP 7.4 support * Updating the `same` check for the impersonator and the impersonated --- .travis.yml | 5 +++-- src/Contracts/Impersonatable.php | 9 +++++++++ src/Exceptions/ImpersonationException.php | 22 ++++++++++++++++++++-- src/Impersonator.php | 5 +++-- src/ImpersonatorServiceProvider.php | 2 +- src/Traits/CanImpersonate.php | 17 +++++++++++++++++ 6 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0f8720f..d43d90e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 7.2 - 7.3 + - 7.4snapshot - nightly matrix: @@ -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 diff --git a/src/Contracts/Impersonatable.php b/src/Contracts/Impersonatable.php index a8cb4b1..bfb47e1 100644 --- a/src/Contracts/Impersonatable.php +++ b/src/Contracts/Impersonatable.php @@ -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); } diff --git a/src/Exceptions/ImpersonationException.php b/src/Exceptions/ImpersonationException.php index 541af81..45023c2 100644 --- a/src/Exceptions/ImpersonationException.php +++ b/src/Exceptions/ImpersonationException.php @@ -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(), + ]) ); } @@ -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.') ); } } diff --git a/src/Impersonator.php b/src/Impersonator.php index d4ffe2a..1ca072d 100644 --- a/src/Impersonator.php +++ b/src/Impersonator.php @@ -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(); + } } /** diff --git a/src/ImpersonatorServiceProvider.php b/src/ImpersonatorServiceProvider.php index 610c7df..6da5da6 100644 --- a/src/ImpersonatorServiceProvider.php +++ b/src/ImpersonatorServiceProvider.php @@ -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( diff --git a/src/Traits/CanImpersonate.php b/src/Traits/CanImpersonate.php index f500e0c..94f4fe8 100644 --- a/src/Traits/CanImpersonate.php +++ b/src/Traits/CanImpersonate.php @@ -1,6 +1,7 @@ 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(); + } }