====== Пользователи и логин ======
===== Настройки =====
===== Контроллер =====
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
public function indexAction()
{
if (!$this->get('security.context')->isGranted(new Expression(
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
))) {
throw new AccessDeniedException();
}
// ...
}
Thanks to the SensioFrameworkExtraBundle, you can also secure your controller using annotations:
// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
/**
* @Security("has_role('ROLE_ADMIN')")
*/
public function helloAction($name)
{
// ...
}
public function indexAction()
{
$user = $this->get('security.context')->getToken()->getUser();
}
In a controller this can be shortcut to:
public function indexAction()
{
$user = $this->getUser();
}
===== Шаблоны =====
==== Форма логина ====
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
==== Общий шаблон ====
{% if is_granted('ROLE_ADMIN') %}
Delete
{% endif %}
{% if is_granted(expression(
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
)) %}
Delete
{% endif %}
{% if app.user %}
{% else %}
Username: {{ app.user.username }}
Full name: {{ app.user.fullname }}