使用FosRestBundle登录Symfony [英] Symfony log into by using FosRestBundle

查看:68
本文介绍了使用FosRestBundle登录Symfony的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​.要授予用户访问权限(例如通过智能手机),我需要休息一下来登录用户.

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

是否有可用的现有模块?实际上,我正在使用fosUserBundle.也许有可能使这两个捆绑在一起工作?

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

可以休息登录的用户已经作为普通" fos用户存在.

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

如果您可以链接一些链接,提示或提示,这会很遗憾,因为我正在搜索和搜索,并且由于我是symfony的新手,所以这并不容易:):)

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

推荐答案

自从API通过智能手机以来,我使用FOSUserBundle进行登录.

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

APIBundle/Controller/UserController.php (默认路由为/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); 
   } 

在这种方法中,我称个人服务负责管理用户的功能. (UserHandler.php)

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);
}

别忘了声明您的服务!

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%] 

您现在可以在adresse api/user/login上使用智能手机登录

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

这篇关于使用FosRestBundle登录Symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆