Fixing proper namespace for app/library
This commit is contained in:
parent
cb1e40ee0a
commit
aa37d10024
22 changed files with 78 additions and 62 deletions
127
app/library/Auth.php
Normal file
127
app/library/Auth.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Httpcb;
|
||||
|
||||
use Phalcon\Mvc\User\Component;
|
||||
use Model\Data\User;
|
||||
|
||||
class Auth extends Component
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_session_key = 'auth';
|
||||
|
||||
/**
|
||||
* Login using email/user + password combination.
|
||||
*
|
||||
* @param string $email_username
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
public function login($email_username, $password)
|
||||
{
|
||||
// Look for a user with this username/email.
|
||||
$user = User::findFirstByUsernameOrEmail($email_username);
|
||||
|
||||
if ($user) {
|
||||
// Verify password
|
||||
$hash = $user->getPassword();
|
||||
if (strlen($hash) > 1 && password_verify($password, $hash)) {
|
||||
$this->setIdentity($user->getId());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login using OAuth
|
||||
*
|
||||
* @param $auth
|
||||
*/
|
||||
public function loginOauth($auth)
|
||||
{
|
||||
$email = '';
|
||||
if (isset($auth['info']['email'])) {
|
||||
$email = $auth['info']['email'];
|
||||
}
|
||||
|
||||
// Look for a user with this email.
|
||||
$user = User::findFirstByEmail($email);
|
||||
|
||||
if (!$user) {
|
||||
// Did not find any user. create him.
|
||||
if (isset($auth['info']['nickname'])) {
|
||||
$name = $auth['info']['nickname'];
|
||||
} else if(isset($auth['info']['name'])) {
|
||||
$name = $auth['info']['name'];
|
||||
} else {
|
||||
$name = '';
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail($email)
|
||||
->setUsername($name);
|
||||
|
||||
$user->save();
|
||||
}
|
||||
|
||||
$this->setIdentity($user->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $identity
|
||||
* @return Auth
|
||||
*/
|
||||
public function setIdentity($identity)
|
||||
{
|
||||
$this->session->set($this->_session_key, $identity);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* return \Model\Data\User
|
||||
*/
|
||||
public function getIdentity()
|
||||
{
|
||||
$id = $this->session->get($this->_session_key);
|
||||
|
||||
if ($id !== null) {
|
||||
return \Model\Data\User::findFirst($id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* return \Model\Data\User
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
if ($this->hasIdentity()) {
|
||||
|
||||
$id = $this->session->get($this->_session_key);
|
||||
|
||||
return \Model\Data\User::findFirst($id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasIdentity()
|
||||
{
|
||||
return $this->getIdentity() !== NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Clears the identity information.
|
||||
*
|
||||
* @return Auth
|
||||
*/
|
||||
public function clearIdentity()
|
||||
{
|
||||
$this->session->remove($this->_session_key);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in a new issue