From 31b25cd07f4c3cab7a783ae76702f41f1ee0eeb6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 21 Jul 2016 21:03:17 +0200 Subject: [PATCH] Fixed authentication --- Account/Account.php | 5 +- Account/AccountManager.php | 71 +++++++++++++++++- Account/AccountMapper.php | 111 ++++++++++++++++++++++++++++ DataStorage/Session/HttpSession.php | 8 ++ 4 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 Account/AccountMapper.php diff --git a/Account/Account.php b/Account/Account.php index 44eb88318..8e26001e5 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -150,12 +150,15 @@ class Account implements ArrayableInterface, \JsonSerializable /** * Constructor. * + * @param int $id Account id + * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct() + public function __construct(int $id) { $this->createdAt = new \DateTime('now'); + $this->id = $id; } /** diff --git a/Account/AccountManager.php b/Account/AccountManager.php index 465177971..81c12d142 100644 --- a/Account/AccountManager.php +++ b/Account/AccountManager.php @@ -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 */ - 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 */ - 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 + */ + 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. * diff --git a/Account/AccountMapper.php b/Account/AccountMapper.php new file mode 100644 index 000000000..c8d16a3dc --- /dev/null +++ b/Account/AccountMapper.php @@ -0,0 +1,111 @@ + + * @author Dennis Eichhorn + * @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 + * @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 + */ + 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 + */ + 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); + } +} diff --git a/DataStorage/Session/HttpSession.php b/DataStorage/Session/HttpSession.php index 086bd76c5..3aa0c9651 100644 --- a/DataStorage/Session/HttpSession.php +++ b/DataStorage/Session/HttpSession.php @@ -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. *