Archived
1
0
Fork 0
This repository has been archived on 2026-04-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
httpcb/app/library/Auth.php

124 lines
2.6 KiB
PHP

<?php
namespace Httpcb;
use Httpcb\OAuth\UserData\UserDataInterface;
use Phalcon\Mvc\User\Component;
use App\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());
$this->_eventsManager->fire('auth:onLogin', $this, 'password');
return true;
}
}
return false;
}
/**
* Login using OAuth
*
* @param UserDataInterface $data
* @return bool|\Phalcon\Mvc\Model\MessageInterface[]
*/
public function loginOauth(UserDataInterface $data)
{
$user = User::findFirstByOAuthID($data);
if (!$user) {
// Did not find any user. create him.
$user = User::createFromOAuthData($data);
if ($user->save() === false) {
return $user->getMessages();
}
}
$this->setIdentity($user->getId());
$this->_eventsManager->fire('auth:onLogin', $this,
"OAuth {$data->getProvider()}");
return true;
}
/**
* @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 User::findFirst($id);
}
return null;
}
/**
* return \Model\Data\User
*/
public function getUser()
{
if ($this->hasIdentity()) {
$id = $this->session->get($this->_session_key);
return 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;
}
}