From 1b780b8f595d9ff3bad6725882defbd3807faa1e Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 2 Oct 2019 23:37:15 +0100 Subject: [PATCH] Updating the package * Allow to install laravel 6 minor releases * Updating the service provider + defer * Cleaning/Refactoring & other minor changes --- README.md | 4 +- composer.json | 8 +-- helpers.php | 2 +- src/Contracts/Impersonatable.php | 2 +- src/Contracts/Impersonator.php | 4 +- src/Events/AbstractImpersonationEvent.php | 8 ++- src/Exceptions/ImpersonationException.php | 60 ++++++++++++++++++- src/Guard/SessionGuard.php | 6 +- src/Impersonator.php | 42 ++++++------- src/ImpersonatorServiceProvider.php | 15 +++-- .../AuthorizationServiceProvider.php | 2 +- src/Traits/CanImpersonate.php | 2 +- tests/ImpersonatorServiceProviderTest.php | 5 +- 13 files changed, 108 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b42ff77..4f5a6e7 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Feel free to check out the [releases](https://github.com/ARCANEDEV/LaravelImpers ### Features - * Laravel `5.4` to `6.0` are supported. + * Laravel `5.4` to `6.x` are supported. * Easy setup & configuration. * Well documented & IDE Friendly. * Well tested with maximum code quality. @@ -44,7 +44,7 @@ Any ideas are welcome. Feel free to submit any issues or pull requests, please c - [All Contributors][link-contributors] [badge_license]: https://img.shields.io/packagist/l/arcanedev/laravel-impersonator.svg?style=flat-square -[badge_laravel]: https://img.shields.io/badge/Laravel-5.4%20to%206.0-orange.svg?style=flat-square +[badge_laravel]: https://img.shields.io/badge/Laravel-5.4%20to%206.x-orange.svg?style=flat-square [badge_build]: https://img.shields.io/travis/ARCANEDEV/LaravelImpersonator.svg?style=flat-square [badge_coverage]: https://img.shields.io/scrutinizer/coverage/g/ARCANEDEV/LaravelImpersonator.svg?style=flat-square [badge_quality]: https://img.shields.io/scrutinizer/g/ARCANEDEV/LaravelImpersonator.svg?style=flat-square diff --git a/composer.json b/composer.json index 671a591..77a1034 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,12 @@ "license": "MIT", "require": { "php": ">=7.2.0", - "arcanedev/support": "~5.0.0" + "arcanedev/support": "^5.0" }, "require-dev": { - "orchestra/testbench": "~4.0.0", - "phpunit/phpcov": "~6.0", - "phpunit/phpunit": "~8.0" + "orchestra/testbench": "^4.0", + "phpunit/phpcov": "^6.0", + "phpunit/phpunit": "^8.0" }, "autoload": { "psr-4": { diff --git a/helpers.php b/helpers.php index aa853e6..8899cc7 100644 --- a/helpers.php +++ b/helpers.php @@ -8,7 +8,7 @@ * * @return \Arcanedev\LaravelImpersonator\Contracts\Impersonator */ - function impersonator() { + function impersonator(): Impersonator { return app(Impersonator::class); } } diff --git a/src/Contracts/Impersonatable.php b/src/Contracts/Impersonatable.php index e85e624..a8cb4b1 100644 --- a/src/Contracts/Impersonatable.php +++ b/src/Contracts/Impersonatable.php @@ -18,7 +18,7 @@ interface Impersonatable extends Authenticatable /** * Impersonate the given user. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated * * @return bool */ diff --git a/src/Contracts/Impersonator.php b/src/Contracts/Impersonator.php index a8a6083..8b0c41e 100644 --- a/src/Contracts/Impersonator.php +++ b/src/Contracts/Impersonator.php @@ -35,8 +35,8 @@ public function getImpersonatorId(); /** * Start the impersonation. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonater - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonater + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated * * @return bool */ diff --git a/src/Events/AbstractImpersonationEvent.php b/src/Events/AbstractImpersonationEvent.php index 21314c6..ac24836 100644 --- a/src/Events/AbstractImpersonationEvent.php +++ b/src/Events/AbstractImpersonationEvent.php @@ -18,7 +18,9 @@ abstract class AbstractImpersonationEvent | ----------------------------------------------------------------- */ - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable, + InteractsWithSockets, + SerializesModels; /* ----------------------------------------------------------------- | Properties @@ -39,8 +41,8 @@ abstract class AbstractImpersonationEvent /** * AbstractImpersonationEvent constructor. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonater - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonater + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated */ public function __construct(Impersonatable $impersonater, Impersonatable $impersonated) { diff --git a/src/Exceptions/ImpersonationException.php b/src/Exceptions/ImpersonationException.php index 7a4436b..541af81 100644 --- a/src/Exceptions/ImpersonationException.php +++ b/src/Exceptions/ImpersonationException.php @@ -1,9 +1,67 @@ */ -class ImpersonationException extends \Exception {} +class ImpersonationException extends \Exception +{ + /* ----------------------------------------------------------------- + | Main Methods + | ----------------------------------------------------------------- + */ + + /** + * Make a new exception. + * + * @param string $message + * + * @return static + */ + public static function make(string $message): self + { + return new static($message); + } + + /** + * Make an exception when the impersonater and impersonated are same person. + * + * @return static + */ + public static function selfImpersonation(): self + { + return static::make('The impersonater & impersonated with must be different.'); + } + + /** + * Make an exception when the impersonater cannot (or not allowed) impersonate. + * + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonater + * + * @return static + */ + public static function cannotImpersonate(Impersonatable $impersonater): self + { + return static::make( + "The impersonater with `{$impersonater->getAuthIdentifierName()}`=[{$impersonater->getAuthIdentifier()}] doesn't have the ability to impersonate." + ); + } + + /** + * Make an exception when the impersonated cannot be impersonated. + * + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * + * @return static + */ + public static function cannotBeImpersonated(Impersonatable $impersonated) + { + return static::make( + "The impersonated with `{$impersonated->getAuthIdentifierName()}`=[{$impersonated->getAuthIdentifier()}] cannot be impersonated." + ); + } +} diff --git a/src/Guard/SessionGuard.php b/src/Guard/SessionGuard.php index 75fc4b3..439139f 100644 --- a/src/Guard/SessionGuard.php +++ b/src/Guard/SessionGuard.php @@ -19,9 +19,9 @@ class SessionGuard extends BaseSessionGuard /** * Login the user into the app without firing the Login event. * - * @param \Illuminate\Contracts\Auth\Authenticatable $user + * @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user */ - public function silentLogin(Authenticatable $user) + public function silentLogin(Authenticatable $user): void { $this->updateSession($user->getAuthIdentifier()); $this->setUser($user); @@ -30,7 +30,7 @@ public function silentLogin(Authenticatable $user) /** * Logout the user without updating `remember_token` and without firing the Logout event. */ - public function silentLogout() + public function silentLogout(): void { $this->clearUserDataFromStorage(); diff --git a/src/Impersonator.php b/src/Impersonator.php index 7ab2ebf..d4ffe2a 100644 --- a/src/Impersonator.php +++ b/src/Impersonator.php @@ -1,6 +1,7 @@ mustBeEnabled(); $this->mustBeDifferentImpersonatable($impersonater, $impersonated); @@ -230,10 +231,10 @@ private function checkImpersonation(Impersonatable $impersonater, Impersonatable * * @throws \Arcanedev\LaravelImpersonator\Exceptions\ImpersonationException */ - private function mustBeEnabled() + private function mustBeEnabled(): void { if ( ! $this->isEnabled()) - throw new Exceptions\ImpersonationException( + throw new ImpersonationException( 'The impersonation is disabled.' ); } @@ -241,46 +242,41 @@ private function mustBeEnabled() /** * Check the impersonater and the impersonated are different. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonater - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonater + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated * * @throws \Arcanedev\LaravelImpersonator\Exceptions\ImpersonationException */ - private function mustBeDifferentImpersonatable(Impersonatable $impersonater, Impersonatable $impersonated) + private function mustBeDifferentImpersonatable(Impersonatable $impersonater, Impersonatable $impersonated): void { if ($impersonater->getAuthIdentifier() == $impersonated->getAuthIdentifier()) - throw new Exceptions\ImpersonationException( - 'The impersonater & impersonated with must be different.' - ); + throw ImpersonationException::selfImpersonation(); } /** * Check the impersonater. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonater + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonater * * @throws \Arcanedev\LaravelImpersonator\Exceptions\ImpersonationException */ - private function checkImpersonater(Impersonatable $impersonater) + private function checkImpersonater(Impersonatable $impersonater): void { if ( ! $impersonater->canImpersonate()) - throw new Exceptions\ImpersonationException( - "The impersonater with `{$impersonater->getAuthIdentifierName()}`=[{$impersonater->getAuthIdentifier()}] doesn't have the ability to impersonate." - ); + throw ImpersonationException::cannotImpersonate($impersonater); } /** * Check the impersonated. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated * * @throws \Arcanedev\LaravelImpersonator\Exceptions\ImpersonationException */ - private function checkImpersonated(Impersonatable $impersonated) + private function checkImpersonated(Impersonatable $impersonated): void { + if ( ! $impersonated->canBeImpersonated()) - throw new Exceptions\ImpersonationException( - "The impersonated with `{$impersonated->getAuthIdentifierName()}`=[{$impersonated->getAuthIdentifier()}] cannot be impersonated." - ); + throw ImpersonationException::cannotBeImpersonated($impersonated); } } diff --git a/src/ImpersonatorServiceProvider.php b/src/ImpersonatorServiceProvider.php index 70c525c..610c7df 100644 --- a/src/ImpersonatorServiceProvider.php +++ b/src/ImpersonatorServiceProvider.php @@ -1,8 +1,9 @@ */ -class ImpersonatorServiceProvider extends PackageServiceProvider +class ImpersonatorServiceProvider extends PackageServiceProvider implements DeferrableProvider { /* ----------------------------------------------------------------- | Properties @@ -32,7 +33,7 @@ class ImpersonatorServiceProvider extends PackageServiceProvider /** * Register the service provider. */ - public function register() + public function register(): void { parent::register(); @@ -47,10 +48,8 @@ public function register() /** * Boot the service provider. */ - public function boot() + public function boot(): void { - parent::boot(); - $this->publishConfig(); } @@ -59,7 +58,7 @@ public function boot() * * @return array */ - public function provides() + public function provides(): array { return [ Contracts\Impersonator::class, @@ -74,7 +73,7 @@ public function provides() /** * Extend the auth session driver. */ - private function extendAuthDriver() + private function extendAuthDriver(): void { /** @var \Illuminate\Auth\AuthManager $auth */ $auth = $this->app['auth']; diff --git a/src/Providers/AuthorizationServiceProvider.php b/src/Providers/AuthorizationServiceProvider.php index ad58e13..af8e1df 100644 --- a/src/Providers/AuthorizationServiceProvider.php +++ b/src/Providers/AuthorizationServiceProvider.php @@ -19,7 +19,7 @@ class AuthorizationServiceProvider extends ServiceProvider /** * Register any application authentication / authorization services. */ - public function boot() + public function boot(): void { parent::registerPolicies(); diff --git a/src/Traits/CanImpersonate.php b/src/Traits/CanImpersonate.php index f304f31..f500e0c 100644 --- a/src/Traits/CanImpersonate.php +++ b/src/Traits/CanImpersonate.php @@ -18,7 +18,7 @@ trait CanImpersonate /** * Impersonate the given user. * - * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated + * @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated * * @return bool */ diff --git a/tests/ImpersonatorServiceProviderTest.php b/tests/ImpersonatorServiceProviderTest.php index 1878d4a..d1d60b7 100644 --- a/tests/ImpersonatorServiceProviderTest.php +++ b/tests/ImpersonatorServiceProviderTest.php @@ -47,8 +47,9 @@ public function it_can_be_instantiated() { $expectations = [ \Illuminate\Support\ServiceProvider::class, - \Arcanedev\Support\ServiceProvider::class, - \Arcanedev\Support\PackageServiceProvider::class, + \Illuminate\Contracts\Support\DeferrableProvider::class, + \Arcanedev\Support\Providers\ServiceProvider::class, + \Arcanedev\Support\Providers\PackageServiceProvider::class, \Arcanedev\LaravelImpersonator\ImpersonatorServiceProvider::class, ];