onix-systems-php/hyperf-impersonate

Hyperf Impersonate 是一个插件,允许您以用户身份进行认证

v1.0.0 2024-02-16 09:47 UTC

This package is auto-updated.

Last update: 2024-09-16 10:57:09 UTC


README

包含以下类

  • 合同
    • Impersonatable;
  • 控制器
    • ImpersonateController;
  • 数据传输对象
    • ImpersonateInfoDTO;
    • ImpersonateLeaveDTO;
    • ImpersonateTakeDTO;
  • 事件
    • LeaveImpersonation
    • TakeImpersonation
  • 异常
    • AlreadyImpersonatingException;
    • CantBeImpersonatedException;
    • CantImpersonateException;
    • CantImpersonateSelfException;
    • NotImpersonatingException;
    • ProtectedAgainstImpersonationException;
  • 中间件
    • ProtectFromImpersonation;
  • 资源
    • ResourceInfoImpersonate;
    • ResourceLeaveImpersonate;
    • ResourceTakeImpersonate;
  • 服务
    • ImpersonateService;
  • 特质
    • Impersonatable

安装

composer require onix-systems-php/hyperf-impersonate

发布配置

php bin/hyperf.php vendor:publish onix-systems-php/hyperf-impersonate -i config

发布翻译

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

导入伪装路由

require_once './vendor/onix-systems-php/hyperf-auth/publish/routes.php';

config/autoload/swagger.php 中添加行以用于 Swagger

'vendor/onix-systems-php/hyperf-impersonate/src/',

基本用法

use OnixSystemsPHP\HyperfImpersonate\Contract\Impersonatable as ImpersonatableInterface;
use OnixSystemsPHP\HyperfImpersonate\Trait\Impersonatable;

class User extends ... implements ImpersonatableInterface
{
    use Impersonatable;
}

高级用法

定义伪装授权

默认情况下,所有用户都可以伪装成其他用户。您需要将方法 canImpersonate() 添加到您的用户模型中

public function canImpersonate(): bool
{
    return in_array($this->getRole(), UserRoles::GROUP_ADMINS);
}

默认情况下,所有用户都可以被伪装。您需要将方法 canBeImpersonated() 添加到您的用户模型中来扩展此行为

public function canBeImpersonated(): bool
{
    return in_array($this->getRole(), UserRoles::GROUP_USERS);
}