2

我想创建一个新表单来创建新用户。AdminController我用这些函数创建了自己的:

public function createNewUsersEntity()
{
   return $this->container->get('fos_user.user_manager')->createUser();
}

public function prePersistUsersEntity(User $user)
{
   $this->get('fos_user.user_manager')->updatePassword($user);
   $this->container->get('fos_user.user_manager')->updateUser($user, false);
}

public function preUpdateUsersEntity(User $user)
{
 $this->get('fos_user.user_manager')->updatePassword($user);
   $this->container->get('fos_user.user_manager')->updateUser($user, false);
}

但是密码没有被加密

这是我的config.yml文件:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: AppBundle\Entity\User
    use_listener: false

在我的security.yml文件中:

应用程序/配置/security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle: 
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                always_use_default_target_path: true
                default_target_path: /admin
                failure_path: /
                # if you are using Symfony < 2.8, use the following config instead:
                # csrf_provider: form.csrf_provider

            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 }

这是我的用户实体的构造函数:

public function __construct()
{
     parent::__construct();
}

另一方面,当一个用户被添加到系统中时,我需要它的ROLE_USER角色,但我不知道该怎么做才能改变它。

问题二:密码未加密角色未定义。

4

1 回答 1

2

您在文档中找到的所有内容,我都检查了项目中的所有说明,一切正常。

生成的表格

覆盖 EasyAdminController 方法(创建您自己的控制器实现):

<?php

namespace AdminPanelBundle\Controller;

use JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController as EasyAdminController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

class AdminController extends EasyAdminController
{
    /**
     * @Route("/", name="easyadmin")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function indexAction(Request $request)
    {
        return parent::indexAction($request);
    }

    public function createNewUserEntity()
    {
        return $this->get('fos_user.user_manager')->createUser();
    }

    public function prePersistUserEntity($user)
    {
        $this->get('fos_user.user_manager')->updateUser($user, false);
    }

    public function preUpdateUserEntity($user)
    {
        $this->get('fos_user.user_manager')->updateUser($user, false);
    }

}

我的用户实体名称是“用户”,如果您的名称不同,请更改方法名称。

将此代码添加到配置文件(config.yml):

easy_admin:
    entities:
        User:
            class: AppBundle\Entity\User
            form:
                fields:
                    - username
                    - email
                    - enabled
                    - lastLogin
                    # if administrators are allowed to edit users' passwords and roles, add this:
                    - { property: 'plainPassword', type: 'text', type_options: { required: false } }
                    - { property: 'roles', type: 'choice', type_options: { multiple: true, choices: { 'ROLE_USER': 'ROLE_USER', 'ROLE_ADMIN': 'ROLE_ADMIN' } } }

现在打开你的 routing.yaml,resources 参数应该指向新的控制器:

admin_panel:
    resource: "@AdminPanelBundle/Controller/"
    type:     annotation
    prefix:   /admin

还有我在security.yml中的代码:

security:
  encoders:                                    #
      AppNg\Symfony\AuthBundle\Entity\User:    # Try add this...
          algorithm: 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

  firewalls:
      # disables authentication for assets and the profiler, adapt it according to your needs
      dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

      admin:
        pattern: ^/admin
        logout:
          path: /admin/logout
          target: /admin/login
        anonymous: ~
        form_login:
          provider: fos_userbundle
          login_path: fos_user_security_login
          check_path: fos_user_security_check
          always_use_default_target_path: true
          default_target_path: '/admin'
          csrf_token_generator: security.csrf.token_manager

  access_control:
    - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/logout, role: ROLE_ADMIN }
    - { path: ^/admin, role: ROLE_ADMIN }
于 2016-10-13T11:51:00.080 回答