longitude-one / banned-bundle

禁止用户(非)认证

1.1.0 2022-06-25 20:19 UTC

This package is auto-updated.

Last update: 2024-09-22 16:26:47 UTC


README

LongitudeOne\BannedBundle 是一个为 Symfony 框架设计的非常小的包。被禁止的用户将无法登录您的应用程序。

安装

请确保全局安装了 Composer,具体安装步骤请参阅 Composer 文档中的安装章节

使用 Symfony Flex 的应用程序

打开命令行,进入您的项目目录,然后执行以下命令

$ composer require longitude-one/banned-bundle

未使用 Symfony Flex 的应用程序

步骤 1:下载 Bundle

打开命令行,进入您的项目目录,并执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require longitude-one/banned-bundle

步骤 2:启用 Bundle

然后,将 Bundle 添加到项目 config/bundles.php 文件中已注册的 Bundle 列表,以启用该 Bundle

// config/bundles.php

return [
    // ...
    LongitudeOne\BannedBundle\LongitudeOneBannedBundle::class => ['all' => true],
];

配置

首先,您的用户类应该实现 BannedInterface 接口,然后添加 isBanned 模板方法。

示例

// src/Entity/User.php

namespace App\Entity;

// declare the interface
use LongitudeOne\BannedBundle\Entity\BannedInterface;
use Symfony\Component\Security\Core\User\UserInterface;

// add the interface
class User implements BannedInterface, UserInterface
{
    //Add a private property
    private bool $banned = false;

    //Your getter can be improved to avoid that an admin bans another one.        
    public function isBanned(): bool
    {
        //In this example admins cannot be banned
        return $this->isBanned() and !in_array('ROLE_ADMIN', $this->getRoles());
    }
    
    //Add a setter if needed
    public function setBanned(bool $banned): self
    {
        $this->banned = $banned;
        
        return $this;
    }
    
    //...    
}

步骤 2,配置安全层

将 UserChecker 服务添加到您的安全配置中

# config/security.yaml
security:
  ...
  firewalls:
    ...
    main:
      ...
      user_checker: lo_banned.user_checker

任务完成!