secgin/phalcon-auth-plugin

Phalcon 框架授权插件

v1.0 2023-04-24 16:03 UTC

This package is auto-updated.

Last update: 2024-09-11 21:45:49 UTC


README

使用 Outurum bilgisi ile bir kullanıcının giriş yapıp yapmadığını kontrol eder.

  • Aynı anda sadece bir ip adresinden girişe izin verilir.
  • Kullanıcıların sayfalara erişim izni olup olmadığını kontrol edebilirsiniz.
  • Ayarlardan aktif edilirse sadece izin verilen ip adreslerinden erişim yapılmasını sağlayabilirsiniz.

注意:对于权限和ip地址权限,您应该有一个实现 'AuthDataServiceInterface' 类的类。用户的权限和允许的ip地址将通过此类获取。

安装

composer require secgin/phalcon-auth-plugin

应用步骤

服务注册

$di->register(new AuthProvider());

权限和ip地址(AuthDataServiceInterface)

创建一个实现 'AuthDataServiceInterface' 接口的类,并将其命名为 'authDataService' 以获取用户权限。

class AuthDataService implements AuthDataServiceInterface
{
    private array $permissions = [
        '100' => 7,
        '101' => 9,
        'user' => [
            '100' => 9,
            '101' => 9
        ],
        'customer' => [
            '100' => 9,
            '101' => 3
        ]
    ];

    private array $allowedIpAddresses = [
        '127.0.0.1',
        '::1',
        '192.168.1.42'
    ];

    public function getPermissionLevel(string $permissionCode, ?string $moduleName = null): ?int
    {
        return $moduleName != ''
            ? $this->permissions[$moduleName][$permissionCode] ?? null
            : $this->permissions[$permissionCode] ?? null;
    }

    public function isIpAddressAllowed(string $ipAddress): bool
    {
        return in_array($ipAddress, $this->allowedIpAddresses);
    }
}
$container->setShared('authDataService', AuthDataService::class);

检查页面权限的步骤

将实现类应用到 Dispatcher 服务的事件监听器中,以对打开的页面的权限进行控制。

class DispatcherEventHandler extends Injectable
{
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher): bool
    {
        $result = $this->auth->hasAllowed(
            $dispatcher->getControllerClass(),
            $dispatcher->getActiveMethod(),
            $dispatcher->getModuleName());

        switch ((string)$result)
        {
            case AuthInterface::NOT_LOGGED_IN:
                $this->response
                    ->redirect('user');
                return false;
            case AuthInterface::NOT_ALLOWED_IP_ADDRESS:
                $this->response
                    ->redirect('user/ip');
                return false;
            case AuthInterface::NOT_ALLOWED_RESOURCE:
                $dispatcher->forward([
                    'controller' => 'error',
                    'action' => 'show401'
                ]);
                return false;
        }

        return true;
    }
}

登录处理

验证用户的用户名和密码后,应开始登录过程。

try
{
    $auth->login([
        'id' => '1',
        'usernamea' => 'admin',
        'name' => 'Admin',
    ]);
}
catch (LoginRequiredFieldException $e)
{
    echo $e->getMessage();
}
catch (Exception $e)
{
    echo 'Giriş yapılmadı.';
}

额外说明(注解)权限设置

权限检查根据权限代码和级别值进行。

权限代码:为应用程序中允许的每个操作指定一个代码。这些代码通过实现 'AuthDataServiceInterface' 接口的类获取。

权限级别选项

  • 3: 读取
  • 5: 读取、写入
  • 7: 读取、写入、更新
  • 9: 读取、写入、更新、删除

额外说明

  • @Public: 表示页面或操作对所有人公开。例如,登录页面。
  • @Private: 用于有访问限制的页面。此注释是默认值,如果没有指定权限代码和级别,则仅允许用户登录即可访问页面。
  • @IpAllowed: 此注释允许用户从所有ip地址登录。例如,要为用户添加ip权限,可以在相应的页面上添加此注释。

私有使用

类:@Private(权限代码)

方法:@Private(权限代码,权限级别(可选))

/**
* @Private(100)
 */
class UserController extends Controller 
{
    /**
    * @Private(3, 100) 
     */
    public function listAction()
    {
    
    }
    
    /**
    * @Private(5) 
     */
    public function newAction()
    {
    
    }
    
    /**
    * @Private(7) 
     */
    public function editAction()
    {
    
    }
    
    /**
    * @Private(9) 
     */
    public function deleteAction()
    {
    
    }
}

@Private(3, 100) 这里第一个参数指定listAction权限级别至少为3,权限代码为100。

如果不写入权限代码,则使用类中定义的权限代码。

设置

在项目的config文件中创建一个名为auth的配置组。如果使用默认值处理,则以下选项可以不填写。

new Config([
    'application' => [
        'cacheDir' => BASE_PATH . '/var/cache/',
    ],
    'auth' => [
        'useAllowedIpAddress' => true,
        'defaultAction' => 0,
        'allowMultipleLogin' => true
    ]
]);
  • useAllowedIpAddress(bool[false]): 仅向允许的ip地址发送'true'以获取访问权限。
  • defaultAction(int): 接受0-1的值。默认情况下,所有页面都是private,1是public。
  • allowMultipleLogin(bool[false]): 用于允许在不同ip地址上登录。