user3461461
user3461461

Reputation: 326

Symfony3 + FosUserBundle + EasyAdminBundle

I want to create a new form for creating new users. I created my own AdminController with these functions:

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);
}

But the password is not being encrypted.

This is my config.yml file:

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

In my security.yml file:

app/config/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 }

and this is the constructor from my User entity:

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

On another hand, when a user is added to the system, I need it with ROLE_USER role, but I don't know what to do for changing that.

Two Problems: Password is not encrypted and the role is not defined.

Upvotes: 2

Views: 2374

Answers (1)

Krzysztof Raciniewski
Krzysztof Raciniewski

Reputation: 4924

Everything you find in the documentation, I checked all instructions in my project and all works fine.

Generated form

Override EasyAdminController methods(create your own controller implementation):

<?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);
    }

}

My user entity name is "User", if your name is different change methods names.

Add this code to configuration file(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' } } }

Now open your routing.yaml, resources parameter should point to the new controller:

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

And my code in 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 }

Upvotes: 2

Related Questions