HummingBirdAnimeClient/src/Aviat/AnimeClient/Auth/HummingbirdAuth.php

95 lines
1.6 KiB
PHP
Raw Normal View History

2015-09-17 23:11:18 -04:00
<?php
namespace Aviat\AnimeClient\Auth;
use Aviat\Ion\Di\ContainerInterface;
2015-10-14 09:20:52 -04:00
use Aviat\AnimeClient\Model\API;
2015-09-17 23:11:18 -04:00
2015-10-09 14:34:55 -04:00
/**
* Hummingbird API Authentication
*/
2015-09-17 23:11:18 -04:00
class HummingbirdAuth {
use \Aviat\Ion\Di\ContainerAware;
/**
* Anime API Model
*
2015-10-16 12:53:55 -04:00
* @var \Aviat\AnimeClient\Model\API
2015-09-17 23:11:18 -04:00
*/
protected $model;
/**
* Session object
*
* @var Aura\Session\Segment
*/
2015-10-14 09:20:52 -04:00
protected $segment;
2015-09-17 23:11:18 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
2015-10-14 09:20:52 -04:00
$this->segment = $container->get('session')
2015-09-17 23:11:18 -04:00
->getSegment(__NAMESPACE__);
2015-10-14 09:20:52 -04:00
$this->model = new API($container);
2015-09-17 23:11:18 -04:00
}
2015-10-09 14:34:55 -04:00
/**
* Make the appropriate authentication call,
* and save the resulting auth token if successful
*
* @param string $password
* @return boolean
*/
2015-10-14 09:20:52 -04:00
public function authenticate($password)
2015-09-17 23:11:18 -04:00
{
2015-10-16 12:53:55 -04:00
$username = $this->container->get('config')
->get('hummingbird_username');
2015-10-14 09:20:52 -04:00
$auth_token = $this->model->authenticate($username, $password);
if (FALSE !== $auth_token)
{
$this->segment->set('auth_token', $auth_token);
return TRUE;
}
return FALSE;
}
/**
* Check whether the current user is authenticated
2015-10-16 12:53:55 -04:00
*
2015-10-14 09:20:52 -04:00
* @return boolean
*/
public function is_authenticated()
{
return ($this->get_auth_token() !== FALSE);
}
/**
* Clear authentication values
*
* @return void
*/
2015-11-13 11:33:27 -05:00
public function logout()
2015-10-14 09:20:52 -04:00
{
$this->segment->clear();
2015-09-17 23:11:18 -04:00
}
2015-10-09 14:34:55 -04:00
/**
* Retrieve the authentication token from the session
*
2015-10-14 09:20:52 -04:00
* @return string|false
2015-10-09 14:34:55 -04:00
*/
2015-09-17 23:11:18 -04:00
public function get_auth_token()
{
2015-10-14 09:20:52 -04:00
return $this->segment->get('auth_token', FALSE);
2015-09-17 23:11:18 -04:00
}
}
// End of HummingbirdAuth.php