Advertisement
p4geoff

/module/Users/src/Users/Authentication/Adapter.php

May 9th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Perforce Swarm
  4.  *
  5.  * @copyright   2012 Perforce Software. All rights reserved.
  6.  * @license     Please see LICENSE.txt in top-level folder of this distribution.
  7.  * @version     <release>/<patch>
  8.  */
  9.  
  10. namespace Users\Authentication;
  11.  
  12. use P4\Connection\Connection;
  13. use P4\Connection\ConnectionInterface;
  14. use P4\Connection\LoginException;
  15. use P4\Connection\Exception\CommandException;
  16. use Zend\Authentication\Adapter\AdapterInterface;
  17. use Zend\Authentication\Result;
  18.  
  19. class Adapter implements AdapterInterface
  20. {
  21.     protected $user;
  22.     protected $password;
  23.     protected $p4;
  24.     protected $userP4;
  25.  
  26.     /**
  27.      * Sets username, password and connection for authentication
  28.      *
  29.      * @return void
  30.      */
  31.     public function __construct($user, $password, ConnectionInterface $p4)
  32.     {
  33.         $this->user     = $user;
  34.         $this->password = $password;
  35.         $this->p4       = $p4;
  36.     }
  37.  
  38.     /**
  39.      * Performs an authentication attempt
  40.      *
  41.      * @return \Zend\Authentication\Result
  42.      * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface
  43.      *               If authentication cannot be performed
  44.      */
  45.     public function authenticate()
  46.     {
  47.         // authenticate against current p4 server.
  48.         $this->userP4 = Connection::factory(
  49.             $this->p4->getPort(),
  50.             $this->user,
  51.             null,
  52.             $this->password
  53.         );
  54.  
  55.         // if the password looks like it may be a ticket;
  56.         // test it for that case first
  57.         if (preg_match('/^[A-Z0-9]{32}$/', $this->password)) {
  58.             try {
  59.                 $this->userP4->run('login', '-s');
  60.  
  61.                 return new Result(
  62.                     Result::SUCCESS,
  63.                     array('id' => $this->user, 'ticket' => $this->password)
  64.                 );
  65.             } catch (CommandException $e) {
  66.                 // looks like it isn't a ticket, no problem we'll try to login
  67.             }
  68.         }
  69.  
  70.         // try to login using the password
  71.         try {
  72.             $ticket = $this->userP4->login();
  73.  
  74.             return new Result(
  75.                 Result::SUCCESS,
  76.                 array('id' => $this->user, 'ticket' => $ticket)
  77.             );
  78.         } catch (LoginException $e) {
  79.             return new Result(
  80.                 $e->getCode(),
  81.                 null,
  82.                 array($e->getMessage())
  83.             );
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * Get the connection instance most recently used to authenticate the user.
  89.      *
  90.      * @return  Connection|null     connection used for login or null if no auth attempted
  91.      */
  92.     public function getUserP4()
  93.     {
  94.         return $this->userP4;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement