使用Symfony 3在FOSUserBundle注册表上显示下拉角色

时间:2016-06-28 11:33:11

标签: twig fosuserbundle symfony-forms symfony

我使用FOSUserBundle使用组和角色实现了一个登录系统。我想在FOSUserBundle的用户注册表单上显示角色名称下拉列表(角色:投标人和角色:驱动程序),新用户可以选择要应用的角色,我该怎么做? 这是我的代码:

用户实体

<?php

namespace RemovalsUK\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="RemovalsUK\UserBundle\Repository\UserRepository")
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="RemovalsUK\UserBundle\Entity\Group")
     * @ORM\JoinTable(name="fos_user_user_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

    /**
     * @var array
     * @ORM\OneToMany(targetEntity="RemovalsUK\RemovalsBundle\Entity\Post",
     *     mappedBy="author")
     */
    protected $posts;

    public function __construct()
    {
        parent::__construct();
        // your own logic
        $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add post
     *
     * @param \RemovalsUK\RemovalsBundle\Entity\Post $post
     *
     * @return User
     */
    public function addPost(\RemovalsUK\RemovalsBundle\Entity\Post $post)
    {
        $this->posts[] = $post;

        return $this;
    }

    /**
     * Remove post
     *
     * @param \RemovalsUK\RemovalsBundle\Entity\Post $post
     */
    public function removePost(\RemovalsUK\RemovalsBundle\Entity\Post $post)
    {
        $this->posts->removeElement($post);
    }

    /**
     * Get posts
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getPosts()
    {
        return $this->posts;
    }
}

群组实体

<?php
/**
 * Created by PhpStorm.
 * User: Ezequias
 * Date: 25/06/2016
 * Time: 19:09
 */

namespace RemovalsUK\UserBundle\Entity;


use FOS\UserBundle\Model\Group as BaseGroup;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_group")
 */
class Group extends BaseGroup
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

}

Security.yml

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
            ROLE_ADMIN:       ROLE_USER
            ROLE_SUPER_ADMIN: ROLE_ADMIN

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        fos_userbundle:
                    id: fos_user.user_provider.username
        in_memory:
            memory: ~

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login:
                    pattern:  ^/demo/secured/login$
                    security: false
        main:
            pattern: ^/
            form_login:
                     provider: fos_userbundle
                     csrf_token_generator: security.csrf.token_manager
            logout:    true
            anonymous: true

    access_control:
            - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/admin/, role: ROLE_ADMIN }
            - { path: ^/create, role: ROLE_USER }
            - { path: ^/EDIT, role: ROLE_USER }
            - { path: ^/delete, role: ROLE_USER }
            # activate different ways to authenticate

            # http_basic: ~
            # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            #http://symfony.com/doc/current/cookbook/security/form_login_setup.html

观察 登录工作完全正常,我只想让用户选择例如:角色:投标人或角色:驱动程序,当他们注册时。如果有人能帮忙解决这个问题,我会很感激。 提前谢谢

1 个答案:

答案 0 :(得分:-1)

您需要覆盖默认的注册表单,如文档here所示。