Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Perforce Swarm
- *
- * @copyright 2012 Perforce Software. All rights reserved.
- * @license Please see LICENSE.txt in top-level folder of this distribution.
- * @version <release>/<patch>
- */
- namespace Users\Authentication;
- use P4\Connection\Connection;
- use P4\Connection\ConnectionInterface;
- use P4\Connection\LoginException;
- use P4\Connection\Exception\CommandException;
- use Zend\Authentication\Adapter\AdapterInterface;
- use Zend\Authentication\Result;
- class Adapter implements AdapterInterface
- {
- protected $user;
- protected $password;
- protected $p4;
- protected $userP4;
- /**
- * Sets username, password and connection for authentication
- *
- * @return void
- */
- public function __construct($user, $password, ConnectionInterface $p4)
- {
- $this->user = $user;
- $this->password = $password;
- $this->p4 = $p4;
- }
- /**
- * Performs an authentication attempt
- *
- * @return \Zend\Authentication\Result
- * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface
- * If authentication cannot be performed
- */
- public function authenticate()
- {
- // authenticate against current p4 server.
- $this->userP4 = Connection::factory(
- $this->p4->getPort(),
- $this->user,
- null,
- $this->password
- );
- // if the password looks like it may be a ticket;
- // test it for that case first
- if (preg_match('/^[A-Z0-9]{32}$/', $this->password)) {
- try {
- $this->userP4->run('login', '-s');
- return new Result(
- Result::SUCCESS,
- array('id' => $this->user, 'ticket' => $this->password)
- );
- } catch (CommandException $e) {
- // looks like it isn't a ticket, no problem we'll try to login
- }
- }
- // try to login using the password
- try {
- $ticket = $this->userP4->login();
- return new Result(
- Result::SUCCESS,
- array('id' => $this->user, 'ticket' => $ticket)
- );
- } catch (LoginException $e) {
- return new Result(
- $e->getCode(),
- null,
- array($e->getMessage())
- );
- }
- }
- /**
- * Get the connection instance most recently used to authenticate the user.
- *
- * @return Connection|null connection used for login or null if no auth attempted
- */
- public function getUserP4()
- {
- return $this->userP4;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement