Browse Source

updated LoginAttemptModel, LoginTokenModel & UserVariableModel

v3-dev
REJack 7 years ago
parent
commit
ef4fc7d98a
  1. 91
      application/Models/Aauth/LoginAttemptModel.php
  2. 36
      application/Models/Aauth/LoginTokenModel.php
  3. 75
      application/Models/Aauth/UserVariableModel.php

91
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;
} }
else
$table = empty($table) ? $this->table : $table;
// Ensure we have a good db connection
if (! $this->db instanceof BaseConnection)
{ {
$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 * Inserts or Updates Login Attempt
* @param array $data Data array
* *
* @return BaseBuilder * @return boolean
*/ */
public function update(int $id = null, array $data = null) public function save()
{ {
$builder = $this->builder();
$ipAddress = $this->request->getIPAddress(); $ipAddress = $this->request->getIPAddress();
$builder = $this->builder();
$builder->where('ip_address', $ipAddress); $builder->where('ip_address', $ipAddress);
$builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); $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['count'] = 1;
$data['created_at'] = date('Y-m-d H:i:s'); $data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s');
$builder->insert($data); $builder->insert($data);
return true; return true;
@ -152,6 +148,7 @@ class LoginAttemptModel
{ {
$data['count'] = $row->count + 1; $data['count'] = $row->count + 1;
$data['updated_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s');
$builder->update($data, ['id' => $row->id]); $builder->update($data, ['id' => $row->id]);
if ($data['count'] > $this->config->loginAttemptLimit) 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(); $builder = $this->builder();
$ipAddress = $this->request->getIPAddress(); $builder->where('ip_address', $this->request->getIPAddress());
$builder->where('ip_address', $ipAddress);
$builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); $builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod)));
if ($builder->countAllResults() !== 0) return $builder->delete();
{
$row = $builder->get()->getFirstRow();
return $row->count;
}
else
{
return 0;
}
} }
/** /**
* 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 * @return BaseBuilder
*/ */
public function delete() protected function builder(string $table = null)
{ {
$builder = $this->builder(); if ($this->builder instanceof BaseBuilder)
$ipAddress = $this->request->getIPAddress(); {
$builder->where('ip_address', $ipAddress); return $this->builder;
$builder->where('updated_at >=', date('Y-m-d H:i:s', strtotime('-' . $this->config->loginAttemptLimitTimePeriod))); }
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;
} }
} }

36
application/Models/Aauth/LoginTokenModel.php

@ -68,18 +68,18 @@ class LoginTokenModel
* *
* @var BaseConfig * @var BaseConfig
*/ */
protected $config; protected $config;
/** /**
* Constructor * Constructor
* *
* @param ConnectionInterface $db * @param ConnectionInterface $db Database object
*/ */
public function __construct(ConnectionInterface &$db = null) public function __construct(ConnectionInterface &$db = null)
{ {
$this->config = new AauthConfig(); $this->config = new AauthConfig();
$this->DBGroup = $this->config->dbProfile; $this->DBGroup = $this->config->dbProfile;
$this->table = $this->config->dbTableLoginTokens; $this->table = $this->config->dbTableLoginTokens;
if ($db instanceof ConnectionInterface) if ($db instanceof ConnectionInterface)
{ {
@ -94,33 +94,29 @@ class LoginTokenModel
} }
/** /**
* Works with the current Query Builder instance to return * Get all Login Tokens by User ID
* all results, while optionally limiting them.
* *
* @param integer $user_id * @param integer $userId User id
* @param boolean $expired
* *
* @return array|null * @return array|null
*/ */
public function getAllByUserId($userId) public function findAllByUserId(int $userId)
{ {
$builder = $this->builder(); $builder = $this->builder();
$builder->select('id, user_id, random_hash, selector_hash, expires_at'); $builder->select('id, user_id, random_hash, selector_hash, expires_at');
$builder->where('user_id', $userId); $builder->where('user_id', $userId);
$row = $builder->get()->getResult('array'); return $builder->get()->getResult('array');
return $row;
} }
/** /**
* Updates Login Token * Updates Login Token
* *
* @param array $data array with data * @param array $data Array with data
* *
* @return BaseBuilder * @return BaseBuilder
*/ */
public function insert($data) public function insert(array $data)
{ {
$builder = $this->builder(); $builder = $this->builder();
@ -133,9 +129,11 @@ class LoginTokenModel
/** /**
* Updates Login Token by tokenId * Updates Login Token by tokenId
* *
* @param integer $tokenId Login Token id
*
* @return BaseBuilder * @return BaseBuilder
*/ */
public function update($tokenId) public function update(int $tokenId)
{ {
$builder = $this->builder(); $builder = $this->builder();
$builder->where('id', $tokenId); $builder->where('id', $tokenId);
@ -149,9 +147,11 @@ class LoginTokenModel
/** /**
* Deletes expired Login Tokens by userId. * Deletes expired Login Tokens by userId.
* *
* @param integer $userId User id
*
* @return BaseBuilder * @return BaseBuilder
*/ */
public function delete($userId) public function deleteExpired(int $userId)
{ {
$builder = $this->builder(); $builder = $this->builder();
$builder->where('user_id', $userId); $builder->where('user_id', $userId);
@ -163,7 +163,7 @@ class LoginTokenModel
/** /**
* Provides a shared instance of the Query Builder. * Provides a shared instance of the Query Builder.
* *
* @param string $table * @param string $table Table Name
* *
* @return BaseBuilder * @return BaseBuilder
*/ */
@ -177,7 +177,7 @@ class LoginTokenModel
$table = empty($table) ? $this->table : $table; $table = empty($table) ? $this->table : $table;
// Ensure we have a good db connection // Ensure we have a good db connection
if ( ! $this->db instanceof BaseConnection) if (! $this->db instanceof BaseConnection)
{ {
$this->db = Database::connect($this->DBGroup); $this->db = Database::connect($this->DBGroup);
} }

75
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 * Find user varialbe
* *
@ -131,7 +105,6 @@ class UserVariableModel
public function find(int $userId, string $dataKey, bool $system = null) public function find(int $userId, string $dataKey, bool $system = null)
{ {
$builder = $this->builder(); $builder = $this->builder();
$builder->select('data_value'); $builder->select('data_value');
$builder->where('user_id', $userId); $builder->where('user_id', $userId);
$builder->where('data_key', $dataKey); $builder->where('data_key', $dataKey);
@ -204,6 +177,8 @@ class UserVariableModel
*/ */
public function insert(int $userId, string $dataKey, string $dataValue, bool $system = null) public function insert(int $userId, string $dataKey, string $dataValue, bool $system = null)
{ {
$builder = $this->builder();
$data['user_id'] = $userId; $data['user_id'] = $userId;
$data['data_key'] = $dataKey; $data['data_key'] = $dataKey;
$data['data_value'] = $dataValue; $data['data_value'] = $dataValue;
@ -211,8 +186,6 @@ class UserVariableModel
$data['created_at'] = date('Y-m-d H:i:s'); $data['created_at'] = date('Y-m-d H:i:s');
$data['updated_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); return $builder->insert($data);
} }
@ -239,4 +212,48 @@ class UserVariableModel
return $builder->set($data)->update(); 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;
}
} }

Loading…
Cancel
Save