diff --git a/application/Models/Aauth/LoginAttemptModel.php b/application/Models/Aauth/LoginAttemptModel.php index 80e805b..5079427 100644 --- a/application/Models/Aauth/LoginAttemptModel.php +++ b/application/Models/Aauth/LoginAttemptModel.php @@ -97,44 +97,39 @@ class LoginAttemptModel } /** - * Provides a shared instance of the Query Builder. + * Get Login Attempt * - * @param string $table Table name + * Get login attempt based on time and ip address * - * @return BaseBuilder + * @return integer */ - protected function builder(string $table = null) + public function find() { - if ($this->builder instanceof BaseBuilder) + $builder = $this->builder(); + $builder->where('ip_address', $this->request->getIPAddress()); + $builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); + + if ($builder->countAllResults() !== 0) { - return $this->builder; + return $builder->get()->getFirstRow()->count; } - - $table = empty($table) ? $this->table : $table; - - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) + else { - $this->db = Database::connect($this->DBGroup); + return 0; } - - $this->builder = $this->db->table($table); - - return $this->builder; } /** - * Update Login Attempt + * Save Login Attempt * - * @param integer $id Login attempt id - * @param array $data Data array + * Inserts or Updates Login Attempt * - * @return BaseBuilder + * @return boolean */ - public function update(int $id = null, array $data = null) + public function save() { - $builder = $this->builder(); $ipAddress = $this->request->getIPAddress(); + $builder = $this->builder(); $builder->where('ip_address', $ipAddress); $builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); @@ -144,6 +139,7 @@ class LoginAttemptModel $data['count'] = 1; $data['created_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s'); + $builder->insert($data); return true; @@ -152,6 +148,7 @@ class LoginAttemptModel { $data['count'] = $row->count + 1; $data['updated_at'] = date('Y-m-d H:i:s'); + $builder->update($data, ['id' => $row->id]); if ($data['count'] > $this->config->loginAttemptLimit) @@ -166,44 +163,46 @@ class LoginAttemptModel } /** - * Get Login Attempt + * Delete login attempt. * - * Get login attempt based on time and ip address + * Delete login attempt based on time and ip address * - * @return integer + * @return BaseBuilder */ - public function get() + public function delete() { - $builder = $this->builder(); - $ipAddress = $this->request->getIPAddress(); - $builder->where('ip_address', $ipAddress); + $builder = $this->builder(); + $builder->where('ip_address', $this->request->getIPAddress()); $builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); - if ($builder->countAllResults() !== 0) - { - $row = $builder->get()->getFirstRow(); - return $row->count; - } - else - { - return 0; - } + return $builder->delete(); } /** - * Delete login attempt. + * Provides a shared instance of the Query Builder. * - * Delete login attempt based on time and ip address + * @param string $table Table name * * @return BaseBuilder */ - public function delete() + protected function builder(string $table = null) { - $builder = $this->builder(); - $ipAddress = $this->request->getIPAddress(); - $builder->where('ip_address', $ipAddress); - $builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); + if ($this->builder instanceof BaseBuilder) + { + return $this->builder; + } - return $builder->delete(); + $table = empty($table) ? $this->table : $table; + + // Ensure we have a good db connection + if (! $this->db instanceof BaseConnection) + { + $this->db = Database::connect($this->DBGroup); + } + + $this->builder = $this->db->table($table); + + return $this->builder; } + } diff --git a/application/Models/Aauth/LoginTokenModel.php b/application/Models/Aauth/LoginTokenModel.php index f285636..d884a12 100644 --- a/application/Models/Aauth/LoginTokenModel.php +++ b/application/Models/Aauth/LoginTokenModel.php @@ -68,18 +68,18 @@ class LoginTokenModel * * @var BaseConfig */ - protected $config; + protected $config; /** * Constructor * - * @param ConnectionInterface $db + * @param ConnectionInterface $db Database object */ public function __construct(ConnectionInterface &$db = null) { - $this->config = new AauthConfig(); + $this->config = new AauthConfig(); $this->DBGroup = $this->config->dbProfile; - $this->table = $this->config->dbTableLoginTokens; + $this->table = $this->config->dbTableLoginTokens; if ($db instanceof ConnectionInterface) { @@ -94,33 +94,29 @@ class LoginTokenModel } /** - * Works with the current Query Builder instance to return - * all results, while optionally limiting them. + * Get all Login Tokens by User ID * - * @param integer $user_id - * @param boolean $expired + * @param integer $userId User id * * @return array|null */ - public function getAllByUserId($userId) + public function findAllByUserId(int $userId) { $builder = $this->builder(); $builder->select('id, user_id, random_hash, selector_hash, expires_at'); $builder->where('user_id', $userId); - $row = $builder->get()->getResult('array'); - - return $row; + return $builder->get()->getResult('array'); } /** * Updates Login Token * - * @param array $data array with data + * @param array $data Array with data * * @return BaseBuilder */ - public function insert($data) + public function insert(array $data) { $builder = $this->builder(); @@ -133,9 +129,11 @@ class LoginTokenModel /** * Updates Login Token by tokenId * + * @param integer $tokenId Login Token id + * * @return BaseBuilder */ - public function update($tokenId) + public function update(int $tokenId) { $builder = $this->builder(); $builder->where('id', $tokenId); @@ -149,9 +147,11 @@ class LoginTokenModel /** * Deletes expired Login Tokens by userId. * + * @param integer $userId User id + * * @return BaseBuilder */ - public function delete($userId) + public function deleteExpired(int $userId) { $builder = $this->builder(); $builder->where('user_id', $userId); @@ -163,7 +163,7 @@ class LoginTokenModel /** * Provides a shared instance of the Query Builder. * - * @param string $table + * @param string $table Table Name * * @return BaseBuilder */ @@ -177,7 +177,7 @@ class LoginTokenModel $table = empty($table) ? $this->table : $table; // Ensure we have a good db connection - if ( ! $this->db instanceof BaseConnection) + if (! $this->db instanceof BaseConnection) { $this->db = Database::connect($this->DBGroup); } diff --git a/application/Models/Aauth/UserVariableModel.php b/application/Models/Aauth/UserVariableModel.php index de6986b..99fb564 100644 --- a/application/Models/Aauth/UserVariableModel.php +++ b/application/Models/Aauth/UserVariableModel.php @@ -91,32 +91,6 @@ class UserVariableModel } } - /** - * Provides a shared instance of the Query Builder. - * - * @param string $table Table name - * - * @return BaseBuilder - */ - protected function builder(string $table = null) - { - if ($this->builder instanceof BaseBuilder) - { - return $this->builder; - } - - $table = empty($table) ? $this->table : $table; - - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - - $this->builder = $this->db->table($table); - - return $this->builder; - } - /** * Find user varialbe * @@ -131,7 +105,6 @@ class UserVariableModel public function find(int $userId, string $dataKey, bool $system = null) { $builder = $this->builder(); - $builder->select('data_value'); $builder->where('user_id', $userId); $builder->where('data_key', $dataKey); @@ -204,6 +177,8 @@ class UserVariableModel */ public function insert(int $userId, string $dataKey, string $dataValue, bool $system = null) { + $builder = $this->builder(); + $data['user_id'] = $userId; $data['data_key'] = $dataKey; $data['data_value'] = $dataValue; @@ -211,8 +186,6 @@ class UserVariableModel $data['created_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s'); - $builder = $this->builder(); - return $builder->insert($data); } @@ -239,4 +212,48 @@ class UserVariableModel return $builder->set($data)->update(); } + /** + * Delete User Variable + * + * @param integer $userId User id + * @param string $dataKey Key of variable + * @param boolean $system Whether system variable + * + * @return BaseBuilder + */ + public function delete(int $userId, string $dataKey, bool $system) + { + $builder = $this->builder(); + $builder->where('user_id', $userId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + + return $builder->delete(); + } + + /** + * Provides a shared instance of the Query Builder. + * + * @param string $table Table name + * + * @return BaseBuilder + */ + protected function builder(string $table = null) + { + if ($this->builder instanceof BaseBuilder) + { + return $this->builder; + } + + $table = empty($table) ? $this->table : $table; + + if (! $this->db instanceof BaseConnection) + { + $this->db = Database::connect($this->DBGroup); + } + + $this->builder = $this->db->table($table); + + return $this->builder; + } }