here my problem : I have two categories of users in my application (locataires and propriétaires), and i need one (or two) login form. I use PUGXMultiUserBundle to manage all my users.
here is the view for loggin "proprietaires" :
{% extends "::layout.html.twig" %}
{% block title %}
Nous contacter - {{ parent() }}
{% endblock %}
{# Contents #}
{% block body %}
<div class="row">
<div class="col-md-12">
<div class="well">
<form action="{{ path('proprietaire_login_check') }}" method="post">
<fieldset>
<legend><i class="fa fa-lock"></i> Secure Sign in</legend>
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="_username" value="" class="form-control"/>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="_password" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">
<i class="fa fa-sign-in"></i> Sign in
</button>
</fieldset>
</form>
</div>
</div>
</div>
{% endblock %}
My file app/config/config.yml :
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
service:
user_manager: pugx_user_manager
pugx_multi_user:
users:
proprietaire:
entity:
class: AppBundle\Entity\Proprietaire
# factory:
registration:
form:
type: AppBundle\Form\Type\RegistrationProprietaireFormType
name: fos_user_registration_form
validation_groups: [Registration, Default]
template: proprietaire.form.html.twig
profile:
form:
type: AppBundle\Form\Type\ProfileProprietaireFormType
name: fos_user_profile_form
validation_groups: [Profile, Default]
locataire:
entity:
class: AppBundle\Entity\Locataire
registration:
form:
type: AppBundle\Form\Type\RegistrationLocataireFormType
template: locataire.form.html.twig
profile:
form:
type: AppBundle\Form\Type\ProfileLocataireFormType
And my file app/config/security.yml :
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
FOS\UserBundle\Model\UserInterface: sha512
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
in_memory:
memory: ~
fos_userbundle:
id: fos_user.user_manager
proprietaire:
entity:
class: AppBundle:Proprietaire
property: username
locataire:
entity:
class: AppBundle:Locataire
property: username
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
# form_login:
# provider: fos_userbundle
# csrf_provider: security.csrf.token_manager # Use form.csrf_provider instead for Symfony <2.4
# logout:
# path: /logout
# target: /
anonymous: true
proprietaire_firewall:
pattern: .*
form_login:
# Soumet le formulaire de connection ici
provider: fos_userbundle
check_path: /proprietaire_login_check
logout:
path: /proprietaire_logout
target: /
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/profile, role: ROLE_USER }
I begin with symfony 2, and I don't understand how to create a login form for the users "proprietaires" and one other for the users "locataires". And how to configure the differents firewalls in the the file security.yml ?
Another question : in your opinion, I have to create differents "roles" in my security.yml file ?
Thanks very much.
The PUGXMultiUserBundle is build on top of the FOSUserBundle, it helps managing different types of users using doctrine table inheritance, looking at your database, you can see how there is a parent table "User" and two child tables "locataire" and "proprietaire". By different types of users is meant the point where there is a difference; for example: the registration of a user: there is a difference in the form's fields and the profile editing form is also different. All the rest where there is no difference between the users, the login to the website, the profile page, the logout action... are all handled, as usual directly by the FOSUserBundle.
SO concretely, yes you can use one login form for your two users( Actually I'm using a login form for three different users). You don't need a firewall per user, one firewall is enough. And yes, probably you need to define roles in access control section because you would have to secure an url relevant to the locataire only, for example: /locataire/pays/rent/1 , (the best way to give a user a role is in the class constructor like this:
)
So in your security.yml file:
I hope this helps you, I know how this does look to a newbie because I've been there. Take your time and I'm here if you need any help.