onix-systems-php / hyperf-impersonate
Hyperf Impersonate is a plugin that allows to you to authenticate as your users
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:extension
Requires
- php: >=8.1
- ext-json: *
- ext-openssl: *
- onix-systems-php/hyperf-auth: >=1.2.3
- onix-systems-php/hyperf-core: >=1.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: >=7.0
- swoole/ide-helper: ^4.5|^5.0
Suggests
- swow/swow: Required to create swow components.
README
Includes the following classes:
- Contract:
- Impersonatable;
- Controller:
- ImpersonateController;
- DTO:
- ImpersonateInfoDTO;
- ImpersonateLeaveDTO;
- ImpersonateTakeDTO;
- Event:
- LeaveImpersonation
- TakeImpersonation
- Exception:
- AlreadyImpersonatingException;
- CantBeImpersonatedException;
- CantImpersonateException;
- CantImpersonateSelfException;
- NotImpersonatingException;
- ProtectedAgainstImpersonationException;
- Middleware:
- ProtectFromImpersonation;
- Resource:
- ResourceInfoImpersonate;
- ResourceLeaveImpersonate;
- ResourceTakeImpersonate;
- Service:
- ImpersonateService;
- Trait
- Impersonatable
Install:
composer require onix-systems-php/hyperf-impersonate
Publish config:
php bin/hyperf.php vendor:publish onix-systems-php/hyperf-impersonate -i config
Publish translations:
php bin/hyperf.php vendor:publish onix-systems-php/hyperf-impersonate -i en_us_translation php bin/hyperf.php vendor:publish onix-systems-php/hyperf-impersonate -i ua_uk_translation
Import impersonate routes:
require_once './vendor/onix-systems-php/hyperf-auth/publish/routes.php';
Add line to config/autoload/swagger.php
for swagger:
'vendor/onix-systems-php/hyperf-impersonate/src/',
Basic Usage
use OnixSystemsPHP\HyperfImpersonate\Contract\Impersonatable as ImpersonatableInterface; use OnixSystemsPHP\HyperfImpersonate\Trait\Impersonatable; class User extends ... implements ImpersonatableInterface { use Impersonatable; }
Advanced Usage
Defining impersonation authorization
By default all users can impersonate an user.
You need to add the method canImpersonate()
to your user model:
public function canImpersonate(): bool { return in_array($this->getRole(), UserRoles::GROUP_ADMINS); }
By default all users can be impersonated.
You need to add the method canBeImpersonated()
to your user model to extend this behavior:
public function canBeImpersonated(): bool { return in_array($this->getRole(), UserRoles::GROUP_USERS); }