athome-solution/user-bundle

此包的最新版本(v2.2.0)没有可用的许可证信息。

用户管理包

v2.2.0 2021-08-23 08:43 UTC

README

AthomeUserBundle 允许您将简单的用户/认证过程添加到您的 Symfony 4 应用程序中。此包提供

  • 一个认证系统(表单登录)+ 登出
  • 注册过程(通过电子邮件确认或无需确认)
  • 忘记密码过程(通过发送电子邮件)
  • “编辑账户”功能

开始使用 AthomeUserBundle

配置

在 config/packages/athome_user.yaml 中

athome_user:
  user_class: App\Entity\User
  from_email: 'no-reply@email.com'
  from_name: 'Athome-Solution' #not mandatory : default to null
  enable_on_registration: false #not mandatory : default to true

在 config/routes.yaml 中

app_security:
  resource: '@AthomeUserBundle/Resources/config/routing/security.yaml'

在 config/bundles.php 中

Athome\UserBundle\AthomeUserBundle::class => ['all' => true],

在 config/packages/security.yaml 中

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
security:
    providers:
        users:
            entity:
                # the class of the entity that represents users
                class: 'App\Entity\User'
                # the property to query by - e.g. username, email, etc
                property: 'email'
security:
    access_control:
         - { path: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY }   # racine
         # user bundle
         - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
         - { path: ^/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
         - { path: ^/password, roles: IS_AUTHENTICATED_ANONYMOUSLY }
         - { path: ^/account, roles: IS_AUTHENTICATED_FULLY }
         # user bundle end

您可以使用此包的原始认证器或继承它。

firewalls:
    main:
        guard:
            authenticators: [ Athome\UserBundle\Security\Authenticator ]
        logout:
            path: user_bundle_security_logout
            target: user_bundle_security_login

在 config/packages/doctrine.yaml 中

doctrine:
    # ...
    orm:
        # ...
        auto_mapping: false
        mappings:
            # ...
            App:
                type: xml
                dir: '%kernel.project_dir%/src/Resources/config/doctrine'

扩展用户模型

<?php

namespace App\Entity;

use Athome\UserBundle\Model\User as BaseUser;

class User extends BaseUser
{
    public $property;
    
    public $anotherProperty;
    
    // ...

覆盖包

为了覆盖包的任何部分,您可以扩展基类或使用服务装饰

例如:(src/Security/Authenticator.php)

<?php


namespace App\Security;

use Athome\UserBundle\Security\Authenticator as BaseAuthenticator;

class Authenticator extends BaseAuthenticator
{
    // Inherit functions as needed
}

例如:(config/services.yaml)

App\Form\RegisterType:
    decorates: Athome\UserBundle\Form\Type\RegisterType

覆盖模板

只需遵循相同的目录结构

例如:要覆盖注册,在 templates/bundles/AthomeUserBundle/security/register.html.twig 中创建模板

事件

每个操作都会产生一个事件,该事件携带一个对象、请求和响应。

例如,如果您想保留包的默认注册行为,但想添加业务逻辑

  1. 创建一个订阅者(src/EventSubscriber/RegisterSubscriber.php)在注册后使用 RegisterEvent->setResponse() 进行重定向
    public static function getSubscribedEvents()
    {
        return [
            UserEvents::REGISTRATION_SUCCESSFUL => 'onRegistrationSuccessful'
        ];
    }

    /**
     * @param UserEvent $event
     */
    public function onRegistrationSuccessful(UserEvent $event)
    {
        $user = $event->getUser();

        // Authenticate user
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $this->tokenStorage->setToken($token);
        $this->session->set('_security_main', serialize($token));
        
        // Redirect user 
        $event->setResponse(new RedirectResponse($this->router->generate('homepage')));
    }

可用事件列表

REGISTRATION_SUCCESSFUL:注册完成时触发

REGISTRATION_CONFIRMED:账户激活时触发

PASSWORD_REQUEST_SUCCESSFUL:重置密码电子邮件已发送时触发

PASSWORD_RESET_SUCCESSFUL:密码已重置时触发

ACCOUNT_UPDATE_SUCCESSFUL:账户已更新时触发