Skip to content

Commit

Permalink
Merge pull request #9 from ARCANEDEV/update-package
Browse files Browse the repository at this point in the history
Updating the package
  • Loading branch information
arcanedev-maroc authored Oct 2, 2019
2 parents 3c4a1c1 + 1b780b8 commit eb23e0a
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 52 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @return \Arcanedev\LaravelImpersonator\Contracts\Impersonator
*/
function impersonator() {
function impersonator(): Impersonator {
return app(Impersonator::class);
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Impersonatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Impersonator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Events/AbstractImpersonationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ abstract class AbstractImpersonationEvent
| -----------------------------------------------------------------
*/

use Dispatchable, InteractsWithSockets, SerializesModels;
use Dispatchable,
InteractsWithSockets,
SerializesModels;

/* -----------------------------------------------------------------
| Properties
Expand All @@ -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)
{
Expand Down
60 changes: 59 additions & 1 deletion src/Exceptions/ImpersonationException.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
<?php namespace Arcanedev\LaravelImpersonator\Exceptions;

use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;

/**
* Class ImpersonationException
*
* @package Arcanedev\LaravelImpersonator\Exceptions
* @author ARCANEDEV <[email protected]>
*/
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."
);
}
}
6 changes: 3 additions & 3 deletions src/Guard/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand Down
42 changes: 19 additions & 23 deletions src/Impersonator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Arcanedev\LaravelImpersonator;

use Arcanedev\LaravelImpersonator\Contracts\Impersonatable;
use Arcanedev\LaravelImpersonator\Exceptions\ImpersonationException;
use Exception;
use Illuminate\Contracts\Foundation\Application;

Expand Down Expand Up @@ -108,8 +109,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
*/
Expand Down Expand Up @@ -214,10 +215,10 @@ public function isEnabled()
/**
* Check 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
*/
private function checkImpersonation(Impersonatable $impersonater, Impersonatable $impersonated)
private function checkImpersonation(Impersonatable $impersonater, Impersonatable $impersonated): void
{
$this->mustBeEnabled();
$this->mustBeDifferentImpersonatable($impersonater, $impersonated);
Expand All @@ -230,57 +231,52 @@ 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.'
);
}

/**
* 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);
}
}
15 changes: 7 additions & 8 deletions src/ImpersonatorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php namespace Arcanedev\LaravelImpersonator;

use Arcanedev\Support\PackageServiceProvider;
use Arcanedev\Support\Providers\PackageServiceProvider;
use Illuminate\Auth\SessionGuard as IlluminateSessionGuard;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;

/**
* Class ImpersonatorServiceProvider
*
* @package Arcanedev\LaravelImpersonator
* @author ARCANEDEV <[email protected]>
*/
class ImpersonatorServiceProvider extends PackageServiceProvider
class ImpersonatorServiceProvider extends PackageServiceProvider implements DeferrableProvider
{
/* -----------------------------------------------------------------
| Properties
Expand All @@ -32,7 +33,7 @@ class ImpersonatorServiceProvider extends PackageServiceProvider
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
parent::register();

Expand All @@ -47,10 +48,8 @@ public function register()
/**
* Boot the service provider.
*/
public function boot()
public function boot(): void
{
parent::boot();

$this->publishConfig();
}

Expand All @@ -59,7 +58,7 @@ public function boot()
*
* @return array
*/
public function provides()
public function provides(): array
{
return [
Contracts\Impersonator::class,
Expand All @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/AuthorizationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AuthorizationServiceProvider extends ServiceProvider
/**
* Register any application authentication / authorization services.
*/
public function boot()
public function boot(): void
{
parent::registerPolicies();

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanImpersonate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 3 additions & 2 deletions tests/ImpersonatorServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down

0 comments on commit eb23e0a

Please sign in to comment.