简体   繁体   中英

Symfony log into by using FosRestBundle

I'm working on a API. To give User Access - for example by smartphone - I need to login users by rest.

Is there an existing module available? Actually, I'm using fosUserBundle. Maybe there is a possibility to get those two bundle work together?

The Users which will login by rest are already existing as "normal" fos users.

It would be grest if you could gomme some links, tips or hints cause I'm searching and searching and searching and for the reason that I am new in symfony it's not that easy :):)

rgrds

I use FOSUserBundle for login since a smartphone by the API.

APIBundle/Controller/UserController.php (the default route is /api)

   /** 
   * @Post("/user/login") 
   * @Template(engine="serializer") 
   */ 
   public function loginAction() 
   { 
     $request      = $this->get('request'); 

     $username = $request->request->get('username'); 
     $password = $request->request->get('password'); 

     return $this->container->get('myproject_user.user_service') 
                            ->login($username, $password); 
   } 

in this method, I call a personal service who manage the user's functions. (UserHandler.php)

UserBundle/Handler/UserHandler.php

        class UserHandler implements UserHandlerInterface
        {
          private $om;
          private $entityClass;
          private $repository;
          private $container;
          private $manager;

          public function   __construct(ObjectManager $om, Container $container, $entityClass)
          {
            $this->om = $om;
            $this->entityClass = $entityClass;
            $this->repository = $this->om->getRepository($this->entityClass);
            $this->container = $container;
            $this->manager = $this->container->get('fos_user.user_manager');
          }

          public function   login($username, $password)
          {
             $jsonErrorCreator = $this->container->get('myproject_api.create_error_json');
             $code = 0;

             // check the arguments here.          

             $user = $this->manager->findUserByUsername($username);

             if($user === null) $user = $this->manager->findUserByEmail($username);

             if($user === null)
             {
               $code = 224;
               return ($jsonErrorCreator->createErrorJson($code, $username));            
             }

             // check the user password      
             if($this->checkUserPassword($user, $password) === false)
             {                   
               $code = 225;
               return ($jsonErrorCreator->createErrorJson($code, null));
             }

             // log the user
             $this->loginUser($user);

             $jsonCreator = $this->container->get('myproject_api.create_json');
             $response = $jsonCreator->createJson(array('success'=>true, 'user'=>$user));
             return $response;
          }

          protected function    loginUser(User $user)
          {       
            $security = $this->container->get('security.context');                                 
            $providerKey = $this->container->getParameter('fos_user.firewall_name');    
            $roles = $user->getRoles();
            $token = new UsernamePasswordToken($user, null, $providerKey, $roles);      
            $security->setToken($token);                                                
          }

  protected function    checkUserPassword(User $user, $password)                         
  {       
    $factory = $this->container->get('security.encoder_factory');                          
    $encoder = $factory->getEncoder($user);                                     

    if(!$encoder)                                                              
      return false;

    return $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
  }

}

UserBundle/Handler/UserHandlerInterface.php

Interface UserHandlerInterface
{
  public function login($username, $password);
}

Don't forget to declare your service !

UserBundle/Resources/config/services.yml

myproject_user.user_service: 
    class: %myproject_user.user_handler.class% 
    arguments: [ @doctrine.orm.entity_manager, @service_container, %fos_user.model.user.class%] 

You can now login with your smartphone at the adresse api/user/login

I think I got the solution:

http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html

This seems pretty nice to me and paired with Guras inputit should work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM