rollerworks-graveyard / RollerworksMultiUserBundle

Multi user management for the FOSUserBundle - DISCONTINUED!!
MIT License
56 stars 21 forks source link

Resetting and custom registration form issues #85

Closed VolodymyrChornohal closed 9 years ago

VolodymyrChornohal commented 9 years ago

Hi guys, I'm running SF2.3.25 and installed RWMUB recently on it having 3 user-systems functioning ok with a number of exceptions:

These are seemingly not related issues, but may be it will bring some light on the depth of my cluelessness :)

  1. "Variable "form" does not exist in RollerworksMultiUserBundle:UserBundle/Resetting:request_content.html.twig at line 3"
  2. this happens when trying to access user/resetting/request, despite the "form" is actually present in the template.
  3. My user-systems' custom RegistrationFormType throws an exception when trying to return 'rollerworks_multi_user_registration' in getParent function:

What am I doing wrong if my configurations are as follows:

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.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_proxies: ~
    session:         ~
    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:        [RollerworksMultiUserBundle, AcmeUserBundle]
    #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 Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options: {}
    default_database: test_database
    document_managers:
        default:
            auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       smtp.gmail.com
    username:   *************
    password:   *************
#   spool: { type: memory }

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

#Sonata block configuration
sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
        # Your other blocks

#Sonata MongoDB Admin Configuration
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

rollerworks_multi_user:
    from_email:
        address: ******************
        sender_name: ************

acme_user:
    path: "^/user"

ondiplom_author:
    path: "^/author"

ondiplom_client:
    path: "^/client"
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        acme_user_bundle:
            id: acme_user.user_provider.username
        ondiplom_author_bundle:
            id: ondiplom_author.user_provider.username
        ondiplom_client_bundle:
            id: ondiplom_client.user_provider.username

    firewalls:
        moder:
            # Pattern should equal the pattern you configured for the bundle
            pattern: ^/user
            # Or if you use a custom request_matcher configure that one instead
            #request_matcher: some.service.id

            form_login:
                provider: acme_user_bundle
                check_path: /user/login_check
                login_path: /user/login
                csrf_provider: form.csrf_provider
            logout:
                path: /user/logout
            anonymous:    true
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        author:
            # Pattern should equal the pattern you configured for the bundle
            pattern: ^/author
            # Or if you use a custom request_matcher configure that one instead
            #request_matcher: some.service.id

            form_login:
                check_path: /author/login_check
                login_path: /author/login
                provider: ondiplom_author_bundle
                csrf_provider: form.csrf_provider
            logout:
                path: /author/logout
            anonymous:    true
        client:
            # Pattern should equal the pattern you configured for the bundle
            pattern: ^/client
            # Or if you use a custom request_matcher configure that one instead
            #request_matcher: some.service.id

            form_login:
                check_path: /client/login_check
                login_path: /client/login
                provider: ondiplom_client_bundle
                csrf_provider: form.csrf_provider
            logout:
                path: /client/logout
            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 }
# author bundle user access        
        - { path: ^/author/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/author/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/author/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
# client bundle user access        
        - { path: ^/client/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/client/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/client/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
# src/Ondiplom/Bundle/AuthorBundle/Form/Type/RegistrationFormType.php
namespace Ondiplom\Bundle\AuthorBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // add your custom field
        $builder
            ->add('name', null, array('label' => 'Name', 'translation_domain' => 'FOSUserBundle'))
            ->add('phone', null, array('label' => 'Phone', 'translation_domain' => 'FOSUserBundle'))
            ->add('education', null, array('label' => 'Edu', 'translation_domain' => 'FOSUserBundle'))
            ->add('country', null, array('label' => 'Country', 'translation_domain' => 'FOSUserBundle'))
            ->add('city', null, array('label' => 'City', 'translation_domain' => 'FOSUserBundle'))
        ;

    }

    public function getParent()
    {
        return 'rollerworks_multi_user_registration';
    }

    public function getName()
    {
        return 'ondiplom_author_registration';
    }
}

and dependency

# src/Ondiplom/Bundle/AuthorBundle/DependencyInjection/OndiplomAuthorExtension.php
namespace Ondiplom\Bundle\AuthorBundle\DependencyInjection;

use Rollerworks\Bundle\MultiUserBundle\DependencyInjection\Factory\UserServicesFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class OndiplomAuthorExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $factory = new UserServicesFactory($container);

        // The first parameter is the name of the user-system, the second is the configuration which internally
        // is normalized by the Symfony Config component.

        /*
            The `UserServicesFactory::create()` will also register the form-types you need,
            any form-type/name/class that belongs to the FOSUserBundle will be converted to an ready
            to use form-type. Remember form types and names start with the service-prefix of the user-system.
        'form' => array(
            'type' => 'fos_user_profile',
            'class' => 'FOS\UserBundle\Form\Type\ProfileFormType',
            'name' => 'fos_user_profile_form',
        ),
        ```

        Will internally get converted to an 'ondiplom_author_profile' form-type service definition.
        Never set the template namespace to FOSUserBundle or RollerworksMultiUserBundle as this will create an endless recursion!
    */

    $factory->create('ondiplom_author', array(
        array(
            /*
             * `path` and `host` are used by the `RequestListener` service for finding the correct user-type.
             * You can either set only a path or host, or both. Or you can choose to use your own matcher-service
             * by setting the `request_matcher`, and giving it the service-id.
             *
             * A request-matcher must always implement the `Symfony\Component\HttpFoundation\RequestMatcherInterface`.
             */

            'path' => '^/author', // path-regex, must match the firewall pattern
            'host' => null,
            'request_matcher' => null,

            // When not set this will inherit from the user-system name provided above
            'services_prefix' => 'ondiplom_author',
            'routes_prefix' => 'ondiplom_author',

            'db_driver' => 'mongodb', // can be either: orm, mongodb, couchdb or custom (Propel is not supported)
            'model_manager_name' => 'default',
            'use_listener' => true,

            'user_class' => 'Ondiplom\Bundle\AuthorBundle\Document\User',
            'firewall_name' => 'author', // this must equal to the firewall-name used for this user-system

            'use_username_form_type' => true,

            // When not set these will inherit from the system wide configuration
            'from_email' => array(
                'address' => null,
                'sender_name' => null,
            ),

            'security' => array(
                'login' => array(
                    'template' => 'RollerworksMultiUserBundle:UserBundle/Security:login.html.twig',
                ),
            ),

            'service' => array(
                'mailer' => 'fos_user.mailer.default',
                'email_canonicalizer' => 'fos_user.util.canonicalizer.default',
                'username_canonicalizer' => 'fos_user.util.canonicalizer.default',
                'user_manager' => 'fos_user.user_manager.default',
            ),

            'template' => array(
                'layout' => 'RollerworksMultiUserBundle::layout.html.twig',
            ),

            'profile' => array(
                'form' => array(
                    'class' => 'FOS\\UserBundle\\Form\\Type\\ProfileFormType',
                    'type' => 'fos_user_profile',
                    'name' => 'fos_user_profile_form',
                    'validation_groups' => array('Profile', 'Default'),
                ),
                'template' => array(
                    'edit' => 'RollerworksMultiUserBundle:UserBundle/Profile:edit.html.twig',
                    'show' => 'RollerworksMultiUserBundle:UserBundle/Profile:show.html.twig',
                ),
            ),

            'change_password' => array(
                'form' => array(
                    'class' => 'FOS\\UserBundle\\Form\\Type\\ChangePasswordFormType',
                    'type' => 'fos_user_change_password',
                    'name' => 'fos_user_change_password_form',
                    'validation_groups' => array('ChangePassword', 'Default'),
                ),
                'template' => array(
                    'change_password' => 'RollerworksMultiUserBundle:UserBundle/ChangePassword:changePassword.html.twig',
                ),
            ),

            'registration' => array(
                'confirmation' => array(
                    'enabled' => false,
                    'template' => array(
                        'email' => 'RollerworksMultiUserBundle:UserBundle/Registration:email.txt.twig',
                        'confirmed' => 'RollerworksMultiUserBundle:UserBundle/Registration:confirmed.html.twig',
                    ),
                    'from_email' => array(
                        'address' => null,
                        'sender_name' => null,
                    ),
                ),
                'form' => array(
                    'class' => 'Ondiplom\Bundle\AuthorBundle\Form\Type\RegistrationFormType',
                    'type' => 'ondiplom_author_registration',
                    'name' => 'ondiplom_author_registration_form',
                    'validation_groups' => array('Registration', 'Default'),
                ),
                'template' => array(
                    'register' => 'RollerworksMultiUserBundle:UserBundle/Registration:register.html.twig',
                    'check_email' => 'RollerworksMultiUserBundle:UserBundle/Registration:checkEmail.html.twig',
                ),
            ),

            'resetting' => array(
                'token_ttl' => 86400,
                'email' => array(
                    'from_email' => array(
                        'address' => null,
                        'sender_name' => null,
                    ),
                ),
                'form' => array(
                    'template' => null,
                    'class' => 'FOS\\UserBundle\\Form\\Type\\ResettingFormType',
                    'type' => 'fos_user_resetting',
                    'name' => 'fos_user_resetting_form',
                    'validation_groups' => array('ResetPassword', 'Default'),
                ),
                'template' => array(
                    'check_email' => 'RollerworksMultiUserBundle:UserBundle/Resetting:checkEmail.html.twig',
                    'email' => 'RollerworksMultiUserBundle:UserBundle/Resetting:email.txt.twig',
                    'password_already_requested' => 'RollerworksMultiUserBundle:UserBundle/Resetting:passwordAlreadyRequested.html.twig',
                    'request' => 'RollerworksMultiUserBundle:UserBundle/Resetting:request.html.twig',
                    'reset' => 'RollerworksMultiUserBundle:UserBundle/Resetting:reset.html.twig',
                ),
            ),

            // Optional
            /*
            'group' => array(
                'group_class' => 'Ondiplom\Bundle\AuthorBundle\Document\Group',
                'group_manager' => 'fos_user.group_manager.default',

                'form' => array(
                    'class' => 'FOS\\UserBundle\\Form\\Type\\GroupFormType',
                    'type' => 'fos_user_group',
                    'name' => 'fos_user_group_form',
                    'validation_groups' => array('Registration', 'Default'),
                ),
                'template' => array(
                    'edit' => 'RollerworksMultiUserBundle:UserBundle/Group:edit.html.twig',
                    'list' => 'RollerworksMultiUserBundle:UserBundle/Group:list.html.twig',
                    'new' => 'RollerworksMultiUserBundle:UserBundle/Group:new.html.twig',
                    'show' => 'RollerworksMultiUserBundle:UserBundle/Group:show.html.twig',
                ),
            ),
            */
        ),
        $config
    ));

    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('services.yml');
}

}

sstok commented 9 years ago

Please use the GitHub markdown code formatting. https://help.github.com/articles/markdown-basics/#code-formatting

I edited your post to make it more readable.

sstok commented 9 years ago

The rollerworks_multi_user_registration form doesn't exists (currenty), you can extend the base class (see https://github.com/rollerworks/RollerworksMultiUserBundle/blob/master/tests/Functional/Bundle/UserBundle/Form/Type/RegistrationFormType.php for en example) and overwrite buildForm with

public function buildForm(FormBuilderInterface $builder, array $options)
{
  parent::buildForm($builder, $options);

  // you form fields here
}

That should work for now, I need look at better way to handle this as it goes against the Symfony conventions.

VolodymyrChornohal commented 9 years ago

Thank you, Sebastiaan! :) It seems to be working with a small fix to db_driver.