symfony2 login by username or email - symfony

I'm using the standard authentication mechanism of Symfony2 and I want to let the user use either his username or email to login, but I can't find out why it's not working. I've tested the repository class and it works as expected. I've followed this how-to.
Here's my user provider class:
<?php
namespace My\UserBundle\Entity;
use Doctrine\ORM\EntityRepository ,
Symfony\Component\Security\Core\User\UserProviderInterface ,
Symfony\Component\Security\Core\User\UserInterface;
/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository implements UserProviderInterface
{
function loadUserByUsername($username)
{
$qb = $this->createQueryBuilder('u') ;
return
$qb->select('u')
->where(
$qb->expr()->orx(
$qb->expr()->like('u.username' ,':username') ,
$qb->expr()->like('u.email' ,':username')
)
)
//->andWhere($qb->expr()->eq('u.enabled' ,'true') )
->setParameters(array('username' =>$username ) )
->getQuery()
->getResult() ;
}
function refreshUser(UserInterface $user)
{
return $this->loadUserByUsername($user->getUsername() );
}
function supportsClass($class)
{
return $class === 'My\UserBundle\Entity\User';
}
}

I propose a more simple approach that only requires to edit security.yml file.
You must to create two diferent security providers but both using the same User class. The first has username as property and the second has email as property.
Then you create a chain_provider that includes the two providers and use it in your firewall section
security:
providers:
chain_provider:
chain:
providers: [db_username, db_email]
db_username:
entity:
class: MyUserBundle:User
property: username
db_email:
entity:
class: MyUserBundle:User
property: email
firewalls:
default:
anonymous: ~
provider: chain_provider
form_login:
login_path: /login
check_path: /login
I don't know if this approach is a clean practice, but is fast, simple and it works ok.

well guys the thing is in my security.yml i had this
providers:
main:
entity: { class: My\UserBundle\Entity\User ,property : username}
so i had to take off that parameter property :username

Taking off the property: username lets the UserProviderInterface load the user as expected when it logs in, but does not call the refreshUser() method as expected. I put in checks to see if it gets called but it doesn't.
The class that reloads the user on each access is ContextListener::refreshUser(TokenInterface $token) method. In this the interface iterates through the UserProviders and calls the refreshUser that first returns a non-null user.
I could make sure of this because, in the original load, I combine all different entities to make one SQL call instead of 7. And when the user reloads, it calls 7 times.
Also the method EntityUserProvider::refreshUser() doesn't call the repository's method and instead reloads from the database directly.

Your provider class is correct, and you are correct that the problem is in security.yml, however, your solution is incorrect.
According to the documentation, your security.yml file should look like this:
security:
# ...
providers:
administrators:
entity: { class: MyUserBundle:User }
Notice that the class is defined as the Bundle, not the direct class.
The way you have your code right now, symfony is completly ignoring your repository class until you define your security.yml correctly. And as #Anand pointed out, just removing the property does not invoke refreshUser. However, it looks like if you are using your own Repository, you do not need to define the property (since it's being defined in your query).

Related

Symfony access decision manager strategy is always affirmative

I try to change the access control decision strategy in a Symfony project but it doesn't seem to work.
I have the following in my security.yaml:
security:
access_decision_manager:
strategy: unanimous
allow_if_all_abstain: false
access_control:
- { path: ^/dev/test, roles: [COMPLEX_TEST, ROLE_OTHER] }
I need both COMPLEX_TEST and ROLE_OTHER to be granted for the route to be accessible (COMPLEX_TEST is tested by a custom role voter).
But when I try to access the route, only the COMPLEXT_TEST voter is called, and that's because it allows the access and the strategy is still affirmative.
I can say this because when debugging the code, I can see that the value in Symfony\Component\Security\Core\Authorization\AccessDecisionManager is always affirmative, no matter what I set in the security.yaml.
For now I've created a compiler pass that forces the strategy to unanimous, but it's kind of a hack:
class FixAccessDecisionManagerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->hasDefinition('security.access.decision_manager')) {
$definition = $container->getDefinition('security.access.decision_manager');
$definition->setArgument('$strategy', AccessDecisionManager::STRATEGY_UNANIMOUS);
}
}
}
With the compiler pass I can see that the value in Symfony\Component\Security\Core\Authorization\AccessDecisionManager is correcly set to unanimous and the access control works as expected.
Do you see what I am missing? Or is it a bug in Symfony? My Symfony version is 4.4.7.
Thank you for your time.

User password not encrypted when using FOSUserBundle + EasyAdminBundle with Symfony 3.4

I use Symfony 3.4 with FOSUserBundle and EasyAdminBundle.
I've been stuck for a while on the following problem: when I create a new user via EasyAdmin, the password entered is not hashed, it remains clear in the database and in the edit form of the created user (in EasyAdmin), while there is no problem when I create a user via the form generated by FOSUserBundle (register).
My User entity :
<?php
// src/Repas/UserBundle/Entity/User.php
namespace Repas\UserBundle\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;
}
My AdminController.php file :
<?php
namespace Repas\MenusBundle\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class AdminController extends BaseAdminController
{
public function createNewUserEntity()
{
return $this->get('fos_user.user_manager')->createUser();
}
public function persistUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
parent::persistEntity($user);
}
public function updateUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
parent::updateEntity($user);
}
}
In my config.yml file :
easy_admin:
entities:
User:
class: Repas\UserBundle\Entity\User
export_path: '%kernel.root_dir/../var/export/user'
password_encoding: { algorithm: 'bcrypt', cost: 12 }
In my security.yml file :
encoders:
Repas\UserBundle\Entity\User: bcrypt
In my routing.yml file :
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"
easy_admin_bundle:
resource: "#RepasMenusBundle/Controller/AdminController.php"
type: annotation
prefix: /admin
I've been through many forums, as well as the official docs, I think I followed everything properly but I certainly missed something.
Thank you for your help.
The EasyAdminBundle doesn't define an option to encrypt the password, it only provides options to save the entities (a crud) which you can extend by defining custom actions based on routes or actions inside an overrided AdminController in order to integrate with FOSUserBundle.
Example
easy_admin:
entities:
User:
list:
actions:
- { name: 'create_user', type: 'route' } //or nothing on type to use the action option
At this point you already have either a defined controller accessible by route or an overriden controller which handles the specified User actions. You only need to use the FOSUser methods to encrypt the password properly, as read in this doc.
Hope it helps!
Ok, I guess my mistake is that in the form generated by Easyadmin to create a new user, Easyadmin generates a field named "password" instead of "plainPassword" which is the one FOSUser uses to encrypt the entered password. So I think I just have to create a new "plainPassword" field in my "Easyadmin new user" form and enter the user password in that field to encrypt it. Then the encrypted password will be stored in "password" field of the database.
I will tell you if that is the solution.

Symfony2 How to authenticate a user with two different user names to access different url

I am working on a symfony project and I have a question in the authentication of users that I would like you to solve, since I am new to this.
My project is about a school, where there are teacher type users that have the username and password attributes in the database. The user name consists of the letter t followed by the personal identification number, for example t48945110. In addition, teachers have a Boolean attribute to indicate which of them is the school principal (there is only one).
The security settings in my project are as follows:
Secutiry.yml
security:
firewalls:
intranet:
pattern: ^/
anonymous: ~
provider: teachers
form_login:
login_path: /login
check_path: /login_check
# use_referer: true
default_target_path: /teacher
logout: ~
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: admin/, roles: ROLE_ADMIN }
- { path: /teacher, roles: ROLE_TEACHER }
providers:
teachers:
entity: { class: School\BackendBundle\Entity\Teacher, property: username}
encoders:
School\BackendBundle\Entity\Teacher: { algorithm: sha512 }
With the above, the teacher can accerder to his private part through the login form, but the problem I have when I want to access with the director (teacher with the attribute (director=1) in the database). I would like this teacher to be able to access with another username, but not save it in the database, just changing the main letter, and then in the authentication look for the normal username of the teacher and check if the director to redirect it to / admin instead of /teacher. For example, the head of the school who can access his personal area as a teacher by the user name t48945110 and also can access the administration area of ​​the school with the user name d48945110.
This idea is not to create two different login forms, but to access all the users of the application (students, teachers, director ...).
I do not know if it could be done through Events listeners or otherwise. I appreciate your help for help.
I think you might be overcomplicating things. I'm pretty sure your director does not want to login with different usernames to enter the separate sections. Without knowing more details, what I would recommend is changing the getRoles() in your School\BackendBundle\Entity\Teacher to something like this:
public function getRoles()
{
$roles = array('ROLE_TEACHER');
if ($this->director == 1) {
$roles[] = 'ROLE_DIRECTOR';
$roles[] = 'ROLE_ADMIN';
}
return $roles;
}
This is probably the easiest way to have the user be both a teacher having access to their secured area as well as director having access to the admin area without having to switch accounts.
If for some reason this does not work you could have a look at Security Voters. This could look something like this:
class TeacherIsDirectorVoter extends Voter
{
protected function supports($attribute, $subject)
{
// This voter will always be used
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// This voter will allow access if the currently logged in user is teacher and is a director
return ($user instanceof Teacher && $user->isDirector());
}
}
Again this will grant access for a teacher who is a director without requiring them to log in as a different user. Be careful with this voter as it will always grant access to all sections of your site to the teacher having director=1 with the default voter strategy. There are ways to change this or you could add checks based on the provided attributes and the subject being voted on.
I think for your use case the first option (updating the roles) is probably the easiest and will work well for you.
If you want students, teachers and director to be redirected to different pages after login you could do a little trick. In your SecurityController (or wherever you have your loginAction) create a new targetAction() that is used as target after successful login. In your security.yml assign the form_login's default_target_path to that route, instead of /teacher. That action should be accessible by all 3 user groups. Now just redirect, based on whatever roles your user has:
public function targetAAction()
{
$user = $this->getUser();
if (in_array('ROLE_ADMIN', $user->getRoles())) {
return $this->redirectToRoute('intranet_admin_page');
}
if (in_array('ROLE_TEACHER', $user->getRoles())) {
return $this->redirectToRoute('intranet_teacher_page');
}
return $this->redirectToRoute('generic_page_for_users');
}

Login in symfony2

I'm trying to implement very basic authentication in Symfony2. Here are main parts of the code I really don't see any problem
EDIT
complete security.yml
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls:
login:
pattern: ^/login
anonymous: ~
secured_area:
pattern: ^/
stateless: true
form_login:
login_path: /login
check_path: /login_check
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
This works fine, anonymous user is always redirected to loginAction controller.
EDIT
Here is the complete code
<?php
namespace AcmeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class SecurityController extends Controller {
public function loginAction() {
$providerKey = 'secured_area';
$token = new UsernamePasswordToken('test', 'test', $providerKey, array('ROLE_USER'));
$this->container->get('security.context')->setToken($token);
return $this->redirect($this->generateUrl('fronthomepage'));
}
}
I don't see any problem, anonymous user is redirected to loginAction, there is created authenticated user, saved to token and than redirected to secured area as an authenticated user. Unfortunately my code ends with redirect loop which looks like security firewall doesn't accept user as authenticated. Do you see any problem?
Well, your controller job is to render just form but not to populate security context. Symfony2 security firewall will do that for you automatically. You don't need to handle it unless you want to build you own custom authentication.
In other words, your job is to display the login form and any login
errors that may have occurred, but the security system itself takes
care of checking the submitted username and password and
authenticating the user.
Please read this document for clear picture.
If you want to do some custom stuff when a user logs in, in Symfony2 you have to add an event listener that will fire after the user successfully logged in. The event that is fired is security.interactive_login and to hook to it you have to specify this in services.yml file form your bundle Resources/config directory:
Pretty sure you need an actual user object before setting an authenticated user. I did something like this:
class BaseController
protected function setUser($userName)
{
if (is_object($userName)) $user = $userName;
else
{
$userProvider = $this->get('zayso_core.user.provider');
// Need try/catch here
$user = $userProvider->loadUserByUsername($userName);
}
$providerKey = 'secured_area';
$providerKey = $this->container->getParameter('zayso_core.provider.key'); // secured_area
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
$this->get('security.context')->setToken($token);
return $user;
}
However doing something like this bypasses much of the security system and is not recommended. I also wanted to use a 3rd party authentication system (Janrain). I looked at the authentication system and initially could not make heads or tails out of it. This was before the cookbook entry existed.
I know it seems overkill but once you work through things then it starts to make more sense. And you get access to a bunch of nifty security functions. It took me quite some time to start to understand the authentication system but it was worth it in the end.
Hints:
1. Work through the cook book backward. I had a real hard time understanding what was going on but I started with adding a new firewall to security.yml and then adding the alias for my security factory. I then sort of traced through what the factory was being asked to do. From there I got the listener to fire up and again traced through the calls. Finally the authentication manager comes into play. Again, time consuming, but worth it in the end. Learned a lot.
One thing that drove me crazy is that classes are scattered all over the place. And the naming leaves something to be desired. Very hard to get an overview. I ended up making my own authentication bundle then putting everything under security.
If you want another example of a working bundle then take a look at: https://github.com/cerad/cerad/tree/master/src/Cerad/Bundle/JanrainBundle

FOSFacebookBundle : Choose username before registration

I installed FOSUserBundle and FOSFacebookBundle using this method : How to make a separate url for signin via Facebook using FOSFacebookBundle in Symfony2. Actually I didn't really understand the whole security thing process, but it is working fine now.
When someone is using facebook signup, I would like the possibility to choose an username before being registred (instead of the facebook id as username)... what I do is that I send a POST parameter to the facebook login route but I can't find the controller where the registration is being processed.
What would be the best practise ? Where should I retrieve the username (the POST param) and set it ?
Here is my configuration in security.yml :
firewalls:
public:
pattern: ^/
fos_facebook:
app_url: "http://www.appName.com"
server_url: "http://local.appName.com/app_dev.php/"
login_path: /login
check_path: /login_check/facebook
provider: appName.facebook.provider
form_login:
login_path: /login
check_path: /login_check/form
provider: fos_userbundle
csrf_provider: form.csrf_provider
and here is the routing I use to signup with facebook :
_security_check_facebook:
pattern: /login_check/facebook
There is no controller processing authentication, ever authentication method add a listener that will trigger on check_path, and do the trick.
Authentication process shouldn't be responsible to add custom user data. If you followed FOSFacebookBundle documentation you should have a custom user provider that store the user on database, this may confuse a bit but this is not a registration step, is an authentication step. Adding a username is much similar to a profile editing.
To help you i should see your implementation (how do you send post parameter ? ) but you could try something like this:
send post parameter to a custom controller (implemented by you)
temporary store username (maybe user session data ?)
trigger authentication process (this depend on your implementation but essentially we are talking about authenticate on facebook and then post to login_check path)
add username at the authenticated user
This could work, but I never did something like this.
Old question, but I was working on a similar problem. I wanted to add a role to a user when they were registered on the first time they connected with Facebook.
If you follower these steps from the FOSFacebookBundle documentation, then you can tap into the Facebook connected user's "registration", take a look at the FacebookProvider class.
In loadUserByUsername method there is this part:
if (!empty($fbdata)) {
if (empty($user)) {
$user = $this->userManager->createUser();
$user->setEnabled(true);
$user->setPassword('');
}
$user->setFBData($fbdata);
...
After the setPassword within the if is where I added the role for my user.
For your original problem of choosing the username... notice that call to setFBData?
In the User entity the method looks like this:
public function setFBData($fbdata)
{
if (isset($fbdata['id'])) {
$this->setFacebookId($fbdata['id']);
$this->addRole('ROLE_FACEBOOK');
}
if (isset($fbdata['first_name'])) {
$this->setFirstname($fbdata['first_name']);
}
if (isset($fbdata['last_name'])) {
$this->setLastname($fbdata['last_name']);
}
if (isset($fbdata['email'])) {
$this->setEmail($fbdata['email']);
}
}
and setFacebookId() looks like this:
public function setFacebookId($facebookId)
{
$this->facebookId = $facebookId;
$this->setUsername($facebookId);
}
So that's where the username comes from. I guess that is a flow where you could plug your chosen username into and set it instead of the $facebookId.
I did another way for this.
I used GraphAPI.
public function setFacebookId($facebookId)
{
$this->facebookId = $facebookId;
$username = json_decode(file_get_contents('http://graph.facebook.com/'.$this->facebookId))->username;
$this->setUsername($username);
}
It takes the Facebook username, an unique one.

Resources