Search code examples
phpsymfony1sfguard

Authenticate with sfguarduser without http login


I'm trying to develop a webservice in a symfony based application, we are using a sfguarduser plugin, and i want to get user credentials authenticated in a single shot request to check user permissions and then execute webservice action, is there any way to perform login action without html form login?


Solution

  • In your action do the following:

    $this->getUser()->signin($sfGuardUserObject);
    

    Which you must call with an object of sfGuardUser. You could put this code in a preExecute() method of the module, to check on each request.

    I did something similar when I was doing an "activate account" action. An email with a token is sent after account creating, it contains a link back to the site. A token is sent in the URL and matched in the route to the action. The action then checks if a user has that token and then logs the user in. This is what I did:

    public function executeActivate(sfWebRequest $request)
    {
      // if token is not present, forward to 404
      $this->forward404Unless($request->getParameter('token'), 'page', 'index');
    
      $this->profile = sfGuardUserProfilePeer::activateProfileByToken($request->getParameter('token'));
    
      // if someone was activated, return the mobile activation form and success page
      if (!$this->profile)
      {
        $this->redirect404();
      }
    
      // sign the user in
      $this->getUser()->signin($this->profile->getsfGuardUser());
    
      // set the $user variable for the template
      $this->user = $this->profile->getsfGuardUser();
    
    }
    

    Hope it helps.