Fixed authentication

This commit is contained in:
Dennis Eichhorn 2016-07-21 21:03:17 +02:00
parent 8238a60357
commit 31b25cd07f
4 changed files with 192 additions and 3 deletions

View File

@ -150,12 +150,15 @@ class Account implements ArrayableInterface, \JsonSerializable
/**
* Constructor.
*
* @param int $id Account id
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct()
public function __construct(int $id)
{
$this->createdAt = new \DateTime('now');
$this->id = $id;
}
/**

View File

@ -15,6 +15,10 @@
*/
namespace phpOMS\Account;
use phpOMS\Auth\Auth;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Session\SessionInterface;
/**
* Account manager class.
*
@ -37,14 +41,44 @@ class AccountManager implements \Countable
*/
private $accounts = [];
/**
* Session.
*
* @var SessionInterface
* @since 1.0.0
*/
private $session = null;
/**
* Database connection instance.
*
* @var ConnectionAbstract
* @since 1.0.0
*/
private $connection = null;
/**
* Authenticator.
*
* @var Auth
* @since 1.0.0
*/
private $auth = null;
/**
* Constructor.
*
* @param ConnectionAbstract $connection Database connection
* @param SessionInterface $session Session
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct()
public function __construct(ConnectionAbstract $connection, SessionInterface $session)
{
$this->connection = $connection;
$this->session = $session;
$this->auth = new Auth($this->connection, $this->session);
}
/**
@ -57,11 +91,44 @@ class AccountManager implements \Countable
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function get(int $id) : Account
public function get(int $id = 0) : Account
{
if ($id === 0) {
$account = new Account($this->auth->authenticate());
if (!isset($this->accounts[$account->getId()])) {
$this->accounts[$account->getId()] = $account;
}
return $account;
}
return $this->accounts[$id] ?? new NullAccount();
}
/**
* Login user.
*
* @param int $account Account id
* @param string $login Username
* @param string $password Password
*
* @return int
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function login(int $account, string $login, string $password) : int
{
if (!isset($this->accounts[$account])) {
throw new \Exception('Account not loaded');
}
return $this->auth->login($login, $password);
}
/**
* Set account.
*

111
Account/AccountMapper.php Normal file
View File

@ -0,0 +1,111 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Account;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\Database\Query\Column;
use phpOMS\DataStorage\Database\RelationType;
class AccountMapper extends DataMapperAbstract
{
/**
* Columns.
*
* @var array<string, array>
* @since 1.0.0
*/
protected static $columns = [
'account_id' => ['name' => 'account_id', 'type' => 'int', 'internal' => 'id'],
'account_status' => ['name' => 'account_status', 'type' => 'int', 'internal' => 'status'],
'account_type' => ['name' => 'account_type', 'type' => 'int', 'internal' => 'type'],
'account_login' => ['name' => 'account_login', 'type' => 'string', 'internal' => 'login'],
'account_name1' => ['name' => 'account_name1', 'type' => 'string', 'internal' => 'name1'],
'account_name2' => ['name' => 'account_name2', 'type' => 'string', 'internal' => 'name2'],
'account_name3' => ['name' => 'account_name3', 'type' => 'string', 'internal' => 'name3'],
'account_email' => ['name' => 'account_email', 'type' => 'string', 'internal' => 'email'],
'account_lactive' => ['name' => 'account_lactive', 'type' => 'DateTime', 'internal' => 'lastActive'],
'account_created_at' => ['name' => 'account_created', 'type' => 'DateTime', 'internal' => 'createdAt'],
];
protected static $hasMany = [
];
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
protected static $table = 'account';
/**
* Created at.
*
* @var string
* @since 1.0.0
*/
protected static $createdAt = 'account_created_at';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static $primaryField = 'account_id';
/**
* Create object.
*
* @param mixed $obj Object
* @param int $relations Behavior for relations creation
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function create($obj, int $relations = RelationType::ALL)
{
try {
$objId = parent::create($obj, $relations);
} catch (\Exception $e) {
return false;
}
return $objId;
}
/**
* Find.
*
* @param array $columns Columns to select
*
* @return Builder
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function find(...$columns) : Builder
{
return parent::find(...$columns)->from('account_permission')
->where('account_permission.account_permission_for', '=', 'task')
->where('account_permission.account_permission_id1', '=', 1)
->where('task.task_id', '=', new Column('account_permission.account_permission_id2'))
->where('account_permission.account_permission_r', '=', 1);
}
}

View File

@ -39,7 +39,15 @@ class HttpSession implements SessionInterface
* @since 1.0.0
*/
private static $isLocked = false;
/**
* Raw session data.
*
* @var array
* @since 1.0.0
*/
private $sessionData = [];
/**
* Session ID.
*