Answer the question
In order to leave comments, you need to log in
How to setup authorization in symfony2?
Welcome all. Please help me understand and find the error. I'm trying to set up authorization for site admins. I keep admins in the database. I do everything according to the documentation, but I can't log in. When I try to log in, the form is submitted and nothing happens.
#security.yml
security:
encoders:
AppMainBundle\Entity\User:
algorithm: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
main_db_provider:
entity:
class: AppMainBundle:User
property: username
manager_name: ~
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login_firewall:
pattern: ^/admin/login$
anonymous: ~
main:
pattern: ^/
form_login:
check_path: app_main_admin_login
login_path: app_main_admin_login
always_use_default_target_path: true
default_target_path: app_main_admin_products_list
logout:
path: main_admin_logout
target: app_main_homepage
anonymous: ~
provider: main_db_provider
access_control:
- { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin, roles: ROLE_ADMIN }
<?php
namespace App\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\MainBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* User constructor.
*/
public function __construct()
{
$this->isActive = true;
}
/**
* @return mixed
*/
public function getUsername()
{
return $this->username;
}
/**
* @return null
*/
public function getSalt()
{
return null;
}
/**
* @return mixed
*/
public function getPassword()
{
return $this->password;
}
/**
* @return array
*/
public function getRoles()
{
return array('ROLE_ADMIN');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
) = unserialize($serialized);
}
}
<?php
namespace App\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @var AuthenticationUtils
*/
private $authenticationUtils;
/**
* SecurityController constructor.
* @param AuthenticationUtils $authenticationUtils
*/
public function __construct(AuthenticationUtils $authenticationUtils)
{
$this->authenticationUtils = $authenticationUtils;
}
/**
* @Template("AppMainBundle:Auth:login.html.twig")
*
* @param Request $request
* @return array
*/
public function loginAction(Request $request)
{
$error = $this->authenticationUtils->getLastAuthenticationError();
$lastUsername = $this->authenticationUtils->getLastUsername();
return [
'last_username' => $lastUsername,
'error' => $error,
];
}
}
{% extends 'AppMainBundle::layout.html.twig' %}
{% block content %}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<div class="content login">
<form action="{{ path('app_main_admin_login') }}" method="post" class="login__form">
<div class="login__inner">
<label class="login__form__label text" for="username">Логин</label><!--
--><input class="login__form__inp"
type="text"
id="username"
name="_username"
placeholder="Логин"
value="{{ last_username }}"/>
</div>
<div class="login__inner">
<label class="login__form__label text" for="password">Пароль</label><!--
--><input class="login__form__inp"
type="password"
id="password"
placeholder="Пароль"
name="_password"/>
</div>
<div class="login__inner">
<button class="login__form__btn" type="submit">Войти</button>
</div>
</form>
</div>
{% endblock %}
app_main_admin_login:
path: /admin/login
defaults: { _controller: app_main.security_controller:loginAction }
Answer the question
In order to leave comments, you need to log in
Your check_path and login_path are the same.
check_path: app_main_admin_login
login_path: app_main_admin_login
app_main_admin_login:
path: /admin/login
defaults: { _controller: app_main.security_controller:loginAction }
app_main_admin_check:
path: /admin/check
app_main_admin_logout:
path: /admin/logout
check_path: app_main_admin_check
login_path: app_main_admin_login
get FOS User Bundle and don't worry
or use http_basic+ in_memory
# app/config/security.yml
security:
# ...
providers:
default_provider:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
firewalls:
# ...
default:
anonymous: ~
http_basic: ~
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question