Step by Step Guide to Setup Sonata User bundle with MongoDB

622 views
Skip to first unread message

Pavel Borovich

unread,
Jul 22, 2014, 8:07:26 AM7/22/14
to sonata...@googlegroups.com
Hello!

When setup Sonata Admin Bundle + Sonata User Bundle + FOSUserBundle + MongoDB I got the following error when trying to pass the link http://[your_host_name]/login:

An exception has been thrown during the rendering of a template ("No mapping found for field 'username' in class 'Acme\StoreBundle\Document\User'.") in SonataUserBundle:Security:base_login.html.twig at line 86.

Where can I find a step by step guide to install Sonata User Bundle + MongoDB?

It's my documents:
<?php
// src/Acme/StoreBundle/Document/User.php

namespace Acme\StoreBundle\Document;

//use FOS\UserBundle\Document\User as BaseUser;
use Sonata\UserBundle\Document\BaseUser as BaseUser;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 *
@MongoDB\Document(collection="users")
 */

class User extends BaseUser
{
   
/**
     *
@MongoDB\Id(strategy="auto")
     */

   
protected $id;

   
/**
     *
@MongoDB\ReferenceMany(targetDocument="Group")
     */

   
protected $groups;

   
public function __construct()
   
{
        parent
::__construct();
       
// your own logic
   
}

   
/**
     * Get id
     */

   
public function getId()
   
{
       
return $this->id;
   
}
}


<?php
// src/Acme/StoreBundle/Document/Group.php
namespace Acme\StoreBundle\Document;

//use FOS\UserBundle\Document\Group as BaseGroup;
use Sonata\UserBundle\Document\BaseGroup as BaseGroup;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 *
@MongoDB\Document(collection="groups")
 */

class Group extends BaseGroup
{
   
/**
     *
@MongoDB\Id(strategy="auto")
     */

   
protected $id;

   
/**
     * Get id
     */

   
public function getId()
   
{
       
return $this->id;
   
}

}


# app/cache/config.yml
imports
:
   
- { resource: parameters.yml }
   
- { resource: security.yml }
   
- { resource: config_sonata_admin.yml }

framework
:
   
#esi:             ~
   
#translator:      { fallback: "%locale%" }
    translator
:      ~
    secret
:          
"%secret%"
    router
:
        resource
:
"%kernel.root_dir%/config/routing.yml"
        strict_requirements
: ~
    form
:            ~
    csrf_protection
: ~
    validation
:      { enable_annotations:
true }
    templating
:
        engines
: [
'twig']
       
#assets_version: SomeVersionScheme
    default_locale
:  
"%locale%"
    trusted_hosts
:   ~
    trusted_proxies
: ~
    session
:
       
# handler_id set to null will use default session handler from php.ini
        handler_id
:  ~
    fragments
:       ~
    http_method_override
:
true

# Twig Configuration
twig
:
    debug
:            
"%kernel.debug%"
    strict_variables
:
"%kernel.debug%"

# Assetic Configuration
assetic
:
    debug
:          
"%kernel.debug%"
    use_controller
:
false
    bundles
:        [ ]
   
#java: /usr/bin/java
    filters
:
        cssrewrite
: ~
       
#closure:
       
#    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
       
#yui_css:
       
#    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

doctrine_mongodb
:
    connections
:
       
default:
            server
: mongodb:
//%database_host%:%database_port%
            options
: {}
    default_database
: %database_name%
    document_managers
:
       
default:
            auto_mapping
:
true
            mappings
:
               
AcmeStoreBundle: ~
               
SonataUserBundle: ~
               
FOSUserBundle: ~

# Swiftmailer Configuration
swiftmailer
:
    transport
:
"%mailer_transport%"
    host
:      
"%mailer_host%"
    username
:  
"%mailer_user%"
    password
:  
"%mailer_password%"
    spool
:     { type: memory }


# app/config/config_sonata_admin.yml
fos_user:
   
db_driver: mongodb
   
firewall_name: main
   
user_class: Acme\StoreBundle\Document\User

   
group:
       
group_class: Acme\StoreBundle\Document\Group

   
profile: # Authentication Form
       
form:
           
type:               fos_user_profile
           
handler:            fos_user.profile.form.handler.default
           
name:               fos_user_profile_form
           
validation_groups:  [Authentication] # Please note : this is not the default value

sonata_user:
#    security_acl: false
   
manager_type: mongodb
   
class:
       
user:   Acme\StoreBundle\Document\User
       
group:  Acme\StoreBundle\Document\Group
   
profile:  # Profile Form (firstname, lastname, etc ...)
       
form:
           
type:               sonata_user_profile
           
handler:            sonata.user.profile.form.handler.default
           
name:               sonata_user_profile_form
           
validation_groups:  [Profile]

sonata_block:
   
default_contexts: [admin]
   
blocks:
        sonata
.admin.block.
admin_list:
           
contexts:   [admin]
        sonata
.admin.block.
search_result:
           
contexts:   [admin]
        sonata
.block.service.
text:
           
contexts:   [admin]
        sonata
.user.block.
menu:
           
contexts:   [admin]
        sonata
.user.block.
account:
           
contexts:   [admin]

services:
    sonata
.user.serializer.handler.
user:
       
class: Sonata\UserBundle\Serializer\UserSerializerHandler
       
tags:
           
- {
name: jms_serializer.subscribing_handler }
       
arguments:
           
- [ sonata.user.mongodb.user_manager ]

sonata_admin:
   
security:
       
handler: sonata.admin.security.handler.role

   
title:      Sonata Project
   
title_logo: /bundles/sonataadmin/logo_title.png

sonata_doctrine_mongo_db_admin:
   
templates:
       
form:
           
-
SonataDoctrineMongoDBAdminBundle:Form:form_admin_fields.html.twig
       
filter:
           
-
SonataDoctrineMongoDBAdminBundle:Form:filter_admin_fields.html.twig
       
types:
           
list:
               
array:      SonataAdminBundle:CRUD:list_array.html.twig
               
boolean:    SonataAdminBundle:CRUD:list_boolean.html.twig
               
date:       SonataAdminBundle:CRUD:list_date.html.twig
               
time:       SonataAdminBundle:CRUD:list_time.html.twig
               
datetime:   SonataAdminBundle:CRUD:list_datetime.html.twig
               
text:       SonataAdminBundle:CRUD:base_list_field.html.twig
               
trans:      SonataAdminBundle:CRUD:list_trans.html.twig
               
string:     SonataAdminBundle:CRUD:base_list_field.html.twig
               
smallint:   SonataAdminBundle:CRUD:base_list_field.html.twig
               
bigint:     SonataAdminBundle:CRUD:base_list_field.html.twig
               
integer:    SonataAdminBundle:CRUD:base_list_field.html.twig
               
decimal:    SonataAdminBundle:CRUD:base_list_field.html.twig
               
identifier: SonataAdminBundle:CRUD:base_list_field.html.twig

           
show:
               
array:      SonataAdminBundle:CRUD:show_array.html.twig
               
boolean:    SonataAdminBundle:CRUD:show_boolean.html.twig
               
date:       SonataAdminBundle:CRUD:show_date.html.twig
               
time:       SonataAdminBundle:CRUD:show_time.html.twig
               
datetime:   SonataAdminBundle:CRUD:show_datetime.html.twig
               
text:       SonataAdminBundle:CRUD:base_show_field.html.twig
               
trans:      SonataAdminBundle:CRUD:show_trans.html.twig
               
string:     SonataAdminBundle:CRUD:base_show_field.html.twig
               
smallint:   SonataAdminBundle:CRUD:base_show_field.html.twig
               
bigint:     SonataAdminBundle:CRUD:base_show_field.html.twig
               
integer:    SonataAdminBundle:CRUD:base_show_field.html.twig
               
decimal:    SonataAdminBundle:CRUD:base_show_field.html.twig

// app/AppKernel.php
$bundles
= array(
           
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
           
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
           
new Symfony\Bundle\TwigBundle\TwigBundle(),
           
new Symfony\Bundle\MonologBundle\MonologBundle(),
           
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
           
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
           
new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
           
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
           
new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),

           
new Sonata\CoreBundle\SonataCoreBundle(),
           
new Sonata\BlockBundle\SonataBlockBundle(),
           
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
           
new FOS\UserBundle\FOSUserBundle(),
           
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
           
new Sonata\AdminBundle\SonataAdminBundle(),
           
new Sonata\DoctrineMongoDBAdminBundle\SonataDoctrineMongoDBAdminBundle(),
           
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),

           
new Acme\StoreBundle\AcmeStoreBundle(),
       
);


// composer.json
"require"
: {
       
"php": ">=5.3.3",
       
"symfony/symfony": "2.5.*",
       
"
twig/extensions": "~1.0",
       
"
symfony/assetic-bundle": "~2.3",
       
"
symfony/swiftmailer-bundle": "~2.3",
       
"
symfony/monolog-bundle": "~2.4",
       
"
sensio/distribution-bundle": "~3.0",
       
"
sensio/framework-extra-bundle": "~3.0",
       
"
incenteev/composer-parameter-handler": "~2.0",

       
"
doctrine/mongodb-odm": "1.0.*@dev",
       
"
doctrine/mongodb-odm-bundle": "3.0.*@dev",
       
"
friendsofsymfony/user-bundle": "~1.3@dev",
       
"
sonata-project/user-bundle": "~2.3@dev",
       
"
sonata-project/admin-bundle": "~2.3@dev",
       
"
sonata-project/datagrid-bundle": "~2.2@dev",
       
"
sonata-project/doctrine-mongodb-admin-bundle": "~2.3@dev",
       
"
doctrine/doctrine-fixtures-bundle": "2.2.*",
       
"sonata-project/easy-extends-bundle": "dev-master"
   
},


# app/config/routing.yml
acme_store
:
    resource
:
"@AcmeStoreBundle/Resources/config/routing.yml"
    prefix
:   /

sonata:
    resource:
"routing_sonata_admin.yml"
    prefix:   /


# app/config/routing_sonata_admin.yml
sonata_user_security
:
    resource
:
"@SonataUserBundle/Resources/config/routing/sonata_security_1.xml"

sonata_user_resetting
:
    resource
:
"@SonataUserBundle/Resources/config/routing/sonata_resetting_1.xml"
    prefix
:
/resetting

sonata_user_profile:
    resource: "@SonataUserBundle/
Resources/config/routing/sonata_profile_1.xml"
    prefix: /profile

sonata_user_register:
    resource: "
@SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"
    prefix: /register

sonata_user_change_password:
    resource: "
@SonataUserBundle/Resources/config/routing/sonata_change_password_1.xml"
    prefix: /profile

sonata_user:
    resource: '
@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

admin:
    resource: '
@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin


# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true
            remember_me:
                key:      RememberYou
                lifetime: 36000
        # -> end custom configuration

        # default login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:             .*
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site

        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

Where I was wrong?


ss ss

unread,
Aug 7, 2015, 4:27:59 AM8/7/15
to sonata-users
Hey, did you ever find an answer to this issue?

Run through the installations twice and still get the same error !

Alex Timin

unread,
Aug 7, 2015, 7:51:26 AM8/7/15
to sonata-users
Hey,
 did you try to change template?  There is no username field, but it is called.


пятница, 7 августа 2015 г., 11:27:59 UTC+3 пользователь ss ss написал:

Alexey Devyatov

unread,
Oct 12, 2015, 10:04:29 AM10/12/15
to sonata-users
Look this - https://github.com/hats/symfony-sonata-mongodb-seed/tree/master/src/Acme/StoreBundle
You may extends from FOS\UserBundle\Document\User (as BaseUser) and add additional fields


пятница, 7 августа 2015 г., 11:27:59 UTC+3 пользователь ss ss написал:
Hey, did you ever find an answer to this issue?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages