0
I got a problem here.
I’m on a project using symfony (very good Framework), but I’ve seen some complexities of how it authenticates. I was able to log in, however at the time of entering the home, as it is protected in the file of segirança for only ROLE_ADMIN access, I would like to know from you, in symfony3, how to recover this permission for my user, the user already has all implementations, however symfony does not see it as ROLE_ADMIN. Follows the code.
Controller
public function loginAction(Request $request)
{
if ($request->get('usuario')) {
$usuario = $request->get('usuario');
$senha = $request->get('senha');
$user_manager = $this->getDoctrine()->getRepository('AppBundle:Usuarios');
$factory = $this->get('security.encoder_factory');
$user = $user_manager->loadUserByUsername($usuario);
$encoder = $factory->getEncoder($user);
$salt = $user->getSalt();
if($encoder->isPasswordValid($user->getPassword(), $senha, $salt)) {
return $this->redirect('/home');
}
else {
$this->addFlash('error', 'Usuario ou senha não não encontrados!');
return $this->redirect('/');
}
}
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'login/index.html.twig',
array(
'last_username' => $lastUsername,
'error' => $error,
)
);
}
Repository
class UserRepository extends EntityRepository implements UserLoaderInterface
{
public function loadUserByUsername($username)
{
$user = $this->createQueryBuilder('u')
->where('u.usulogin = :usulogin')
->setParameter('usulogin', $username)
->getQuery()
->getOneOrNullResult();
if (null === $user) {
$message = sprintf(
'Unable to find an active admin AppBundle:User object identified by "%s".',
$username
);
throw new UsernameNotFoundException($message);
}
return $user;
}
}
Entity
class Usuarios implements UserInterface, \Serializable
{
//codigos não coloquei a classe interira, apenas a implemetação da interface, para ficar menor, porem garanto estar de acordo com a doc.
/**
* inicio das implementações da inteface
*/
public function getUsername()
{
return $this->usunom;
}
public function getRoles()
{
return array('ROLE_ADMIN');
}
public function getPassword()
{
return $this->ususenha;
}
public function getSalt()
{
return null;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->usuid,
$this->usunom,
$this->ususenha,
// ver la sección salt debajo
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->usuid,
$this->usunom,
$this->ususenha,
// ver la sección salt debajo
// $this->salt
) = unserialize($serialized);
}
}
Security.yml
# app/config/security.yml
security:
encoders:
AppBundle\Entity\Usuarios:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: AppBundle:Usuarios
firewalls:
main:
provider: our_db_provider
anonymous: true
form_login:
login_path: /login
check_path: /
logout:
path: /logout
target: /login
remember_me:
secret: '%secret%'
lifetime: 604800 # 1 week in seconds
path: /
access_control:
- { path: ^/home, roles: ROLE_ADMIN }
# ...
all Help is welcome...