access_control:
- { path: ^/backOffice, roles: ROLE_USER }
il est possible de rajouter cette condition dans une méthode
$this->denyAccessUnlessGranted('ROLE_USER');
documentationhttps://symfony.com/doc/master/bundles/FOSUserBundle/index.html
http://knpbundles.com/
https://packagist.org/packages/friendsofsymfony/user-bundle
à lire pour aller dans les détails http://symfony.com/doc/current/bundles/FOSUserBundle/index.html#next-steps
php composer.phar require friendsofsymfony/user-bundle "~2.0@dev"
new FOS\UserBundle\FOSUserBundle(),
security:
encoders: # type d'encodage
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy: #hierarchy des rôles
ROLE_ADMIN: ROLE_USER #ROLE_USER : authentifié
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers: #provider pour génerer les clés
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
access_control: #les routes à controller
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/backOffice, roles: ROLE_USER }
d’après la documentation,
rajouter dans le fichier app/config/routing/yml rajouter :
# référence à vendor/friendofsymfony/Resources/config/routing/all.xml
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
d’après la documentation,
rajouter dans le fichier app/config/config.yml :
# app/config/config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main # Attention correspond au main dans le security.yml (partie firewall)
user_class: projet2sdvBundle\Entity\User
d’après la documentation
<?php
// src/projet2sdvBundle/Entity/User.php
namespace projet2sdvBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Remarque : cette classe est dérivée de BaseUser
création de la table :
php bin/console doctrine:schema:update --force
aller voir la structure de la table
regarder le contenu de la table
php bin/console fos:user:promote --help
php bin/console fos:user:promote toto ROLE_ADMIN
php bin/console debug:router
{% if app.user is defined and app.user is not null %}
<li class="name" style="color: #d58512">Connecté en tant que : {{ app.user.username }} </li>
<li><a href="{{ path('fos_user_security_logout') }}" > Se déconnecter</a></li>
{% if app.user.hasRole('ROLE_ADMIN') %} administration {% endif %}
{% else %}
<li><a href="{{ path('fos_user_security_login') }}">Se connecter</a></li>
<li><a href="{{ path('fos_user_registration_register') }}">S'enregistrer</a></li>
{% endif %}
dans le layout.html.twig
{% if app.user is defined and app.user is not null %}
{% if app.user.hasRole('ROLE_ADMIN') %}
{% include "projet2sdvBundle::nav_backOff.html.twig" %}
{% else %}
{% include "projet2sdvBundle::nav_frontOff.html.twig" %}
{% endif%}
{% else %}
{% include "projet2sdvBundle::nav_home.html.twig" %}
{% endif%}
php bin/console fos:user:promote --help
php bin/console fos:user:promote toto ROLE_ADMIN
DROP TABLE IF EXISTS `fos_user`;
CREATE TABLE IF NOT EXISTS `fos_user` (
`id` int(11) NOT NULL,
`username` varchar(180) COLLATE utf8_unicode_ci NOT NULL,
`username_canonical` varchar(180) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(180) COLLATE utf8_unicode_ci NOT NULL,
`email_canonical` varchar(180) COLLATE utf8_unicode_ci NOT NULL,
`enabled` tinyint(1) NOT NULL,
`salt` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`last_login` datetime DEFAULT NULL,
`confirmation_token` varchar(180) COLLATE utf8_unicode_ci DEFAULT NULL,
`password_requested_at` datetime DEFAULT NULL,
`roles` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Contenu de la table `fos_user`
--
INSERT INTO `fos_user` (`id`, `username`, `username_canonical`, `email`, `email_canonical`, `enabled`, `salt`, `password`, `last_login`, `confirmation_token`, `password_requested_at`, `roles`) VALUES
(3, 'client', 'client', 'client@google.com', 'client@google.com', 1, NULL, '$2y$13$D9TE9B6itUgAhhfFxjoKQ.A9W.fDmA18JqMGzM.Xa6/Fz2mdfxxtO', '2017-01-27 12:14:04', NULL, NULL, 'a:1:{i:0;s:11:"ROLE_CLIENT";}'),
(4, 'client2', 'client2', 'client2@gmail.com', 'client2@gmail.com', 1, NULL, '$2y$13$bjSYGs6KJwXNm0M.ZCPHPez/jSbtOnW3V46Zzg9ThTFrDjH3plqTa', '2017-01-27 12:15:02', NULL, NULL, 'a:1:{i:0;s:11:"ROLE_CLIENT";}'),
(5, 'admin', 'admin', 'admin@gmail.com', 'admin@gmail.com', 1, NULL, '$2y$13$m9Rbx3m7XF2LH0lenspJSuDZCPeIrNXpYbsjLlTLrzZx0R0lS6ZVK', '2017-01-27 12:15:38', NULL, NULL, 'a:1:{i:0;s:10:"ROLE_ADMIN";}'),
(6, 'vendeur', 'vendeur', 'vendeur@gmai.com', 'vendeur@gmai.com', 1, NULL, '$2y$13$66bfuuYBv0NiGbWz1G8YzeHlE8Lnj/nBdCAJT/n4jfGI48jCscmTa', '2017-01-27 12:16:19', NULL, NULL, 'a:3:{i:0;s:11:"ROLE_CLIENT";i:1;s:12:"ROLE_VENDEUR";i:2;s:10:"ROLE_ADMIN";}'),
(7, 'vendeur2', 'vendeur2', 'vendeur2@gmai.com', 'vendeur2@gmai.com', 1, NULL, '$2y$13$66bfuuYBv0NiGbWz1G8YzeHlE8Lnj/nBdCAJT/n4jfGI48jCscmTa', '2017-01-27 12:16:19', NULL, NULL, 'a:3:{i:0;s:11:"ROLE_CLIENT";i:1;s:12:"ROLE_VENDEUR";i:2;s:10:"ROLE_ADMIN";}');
--
-- Index pour les tables exportées
--
--
-- Index pour la table `fos_user`
--
-- ALTER TABLE `fos_user`
-- ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `UNIQ_957A647992FC23A8` (`username_canonical`), ADD UNIQUE KEY `UNIQ_957A6479A0D96FBF` (`email_canonical`), ADD UNIQUE KEY `UNIQ_957A6479C05FB297` (`confirmation_token`);
<?php
// src/projet2sdvBundle/projet2sdvBundle.php
namespace projet2sdvBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class projet2sdvBundle extends Bundle
{
// PARTIE à rajouter dans le fichier existant
public function getParent()
{
return 'FOSUserBundle';
}
}
recopier vendor/friendsofsymfony/user-bundle/Resources/view/ sur un dossier dans src/projet2sdvBundle/Resources/views/exemplesLayout
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
{{ 'layout.logged_in_as'|trans({'%username%': app.user.username}, 'FOSUserBundle') }} |
<a href="{{ path('fos_user_security_logout') }}">
{{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
</a>
{% else %}
<a href="{{ path('fos_user_security_login') }}">{{ 'layout.login'|trans({}, 'FOSUserBundle') }}</a>
{% endif %}
</div>
{% for type, messages in app.session.flashBag.all %}
{% for message in messages %}
<div class="{{ type }}">
{{ message|trans({}, 'FOSUserBundle') }}
</div>
{% endfor %}
{% endfor %}
<div>
{% block fos_user_content %}
{% endblock fos_user_content %}
</div>
Hello !
<a href="{{ path('maRoute1SurPage1') }}"> mon lien sur la page 1</a>
</body>
</html>
recopier
- vendor/friendsofsymfony/user-bundle/Resources/view/
sur
-src
-test1AnnotBundle
-Resources
-views
layout.html.twig
- ChangePassword
- Security
login.html.twig
....