I have a Symfony 3.3 application with users stored in db with a normal User Entity as per manual.
I need to be able to login as "admin" (which is always the user with id=2), but with a fixed "master" password which will be hardcoded (its hash, not in plain text).
So this is the idea: if the username is tech and the password is xyz, the user admin must be logged in, and a custom role must be set.
I've tried as described here, and I did:
public function loadUserByUsername($username)
{
if($username == 'TECH') {
/** @var $user User */
$user = $this->createQueryBuilder('u')
->where('u.username = :username')
->setParameter('username', 'admin')
->getQuery()
->getOneOrNullResult();
$user->setUsername('⭐ TECH');
$user->setPassword('$2y$13$u...');
$user->setIsTech(true);
return $user;
}
return $this->createQueryBuilder('u')
->where('u.username = :username')
->setParameter('username', $username)
->getQuery()
->getOneOrNullResult();
}
And in the User entity:
public function getRoles() : array
{
if($this->isAdmin()) {
if($this->isTech) {
return ['ROLE_TECH', 'ROLE_ADMIN', ...];
}
return ['ROLE_ADMIN', ...];
} else {
return ['ROLE_USER'];
}
}
But it's not working, the user is logged in as admin, but the ROLE_TECH role is not granted, and the custom name is not set (I don't know if that is possible, if not it's not so important)