chaplean/user-bundle

创建用户账户并登录(来自 FOSUserBundle)

安装次数: 1,565

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放性问题: 0

类型:symfony-bundle


README

先决条件

此版本的包需要 Symfony 3.4+。

安装

1. Composer

composer require chaplean/user-bundle

2. AppKernel.php

添加

            new Chaplean\Bundle\UserBundle\ChapleanUserBundle(),
            new FOS\UserBundle\FOSUserBundle(),

注意:在 SecurityBundle 之后添加

3. 定义用户实体

创建一个包含 doctrine 信息的用户类。

<?php
//...

use Chaplean\Bundle\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="<YourTableName>")
 */
class User extends BaseUser {
//...
}

4. 最小配置

app/config/config.yml 中定义用户实体命名空间

chaplean_user:
    entity:
        user:
            class: '<NamespaceUserEntity>'

app/config/config.yml 中导入默认配置

imports:
    - { resource: '@ChapleanUserBundle/Resources/config/config.yml' }

app/config/config.yml 中定义索引路径的路由名称

chaplean_user:
    entity:
        user:
            class: '<NamespaceUserEntity>'
    controller:
        index_route: <YourRouteNameForIndex>
        login_route: <YourRouteNameForLogin>
        register_password_route: <Route to set password on register> # default: 'chaplean_user_password_set_password'
        resetting_password_route: <Route to set password on resetting> # default: null and use register_password_route

自定义电子邮件模板:在 app/config/config.yml

chaplean_user:
    # ...
    emailing:
        register:
            subject: '<Translation key>'
            body: '<template twig>'
        resetting:
            subject: '<Translation key>'
            body: '<template twig>'

5. 配置安全性

app/config/security.yml

imports:
    - { resource: '@ChapleanUserBundle/Resources/config/security.yml' }

如果您愿意,也可以覆盖默认设置

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    firewalls:
        main:
            pattern: ^/
            form_login:
                login_path: /login
                check_path: /api/login
                use_forward: false
                remember_me: true
                use_referer: true
                success_handler: chaplean_user.authentication.handler_json
                failure_handler: chaplean_user.authentication.handler_json
                csrf_token_generator: security.csrf.token_manager
            logout:
                path: /logout
                target: /
            anonymous:    true

6. 导入 routing.yml

然后您应该为您的登录页面创建一个控制器操作。使此控制器继承 LoginController 以获取 checkAction 和 logoutAction 操作。最后,在您的路由中创建一个路由。

在您的控制器中

<?php

namespace App\Bundle\FrontBundle\Controller;

use Chaplean\Bundle\UserBundle\Controller\LoginController as BaseController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\SecurityUtilityTest\Csrf\CsrfTokenManagerInterface;

/**
 * Class LoginController.
 */
class LoginController extends BaseController
{
    /**
     * @var CsrfTokenManagerInterface
     */
    protected $tokenManager;

    /**
     * LoginController constructor.
     *
     * @param CsrfTokenManagerInterface $tokenManager
     */
    public function __construct(CsrfTokenManagerInterface $tokenManager)
    {
        $this->tokenManager = $tokenManager;
    }

    /**
     * Renders the login page.
     *
     * @Route("/connexion-of")
     *
     * @return Response
     */
    public function loginAction()
    {
        return $this->render(
            'Login/login.html.twig',
            [
                'csrf_token' => $this->tokenManager->getToken('authenticate')->getValue()
            ]
        );
    }
}

app/config/routing.yml

chaplean_user_login_check:
    path: /api/login
    defaults: { _controller: AppFrontBundle:Login:check }
    methods: 'POST'

chaplean_user_logout:
    path: /logout
    defaults: { _controller: AppFrontBundle:Login:logout }

chaplean_user:
    resource: '@ChapleanUserBundle/Resources/config/routing.yml'

chaplean_user_api:
    type: rest
    resource: '@ChapleanUserBundle/Resources/config/routing_rest.yml'
    prefix:   /api/

app_front:
    type: annotation
    resource: '@AppFrontBundle/Controller/'
    prefix: /

验证器

最小密码要求

Chaplean\Bundle\UserBundle\Validator\Constraints\MinimalPasswordRequirements 有 2 个选项

  • minLength,默认:6
  • atLeastOneSpecialCharacter,默认:true

事件

UserBundle 定义了一些事件,允许您钩入自己的逻辑

  • ChapleanUserCreatedEvent:在创建用户后触发。使用 getUser() 获取实体。
  • ChapleanUserDeletedEvent:在删除用户之前触发。使用 getUser() 获取实体。