diff --git a/application/Models/Aauth/LoginAttemptModel.php b/application/Models/Aauth/LoginAttemptModel.php index 8a7559f..80e805b 100644 --- a/application/Models/Aauth/LoginAttemptModel.php +++ b/application/Models/Aauth/LoginAttemptModel.php @@ -1,4 +1,23 @@ + * @author Raphael Jackstadt + * @copyright 2014-2017 Emre Akay + * @copyright 2018 Magefly + * @license https://opensource.org/licenses/MIT MIT License + * @link https://github.com/magefly/CodeIgniter-Aauth + */ + namespace App\Models\Aauth; use Config\Aauth as AauthConfig; @@ -8,6 +27,11 @@ use CodeIgniter\Database\BaseBuilder; use CodeIgniter\Database\BaseConnection; use CodeIgniter\Database\ConnectionInterface; +/** + * Login Attempt caseModel + * + * @package CodeIgniter-Aauth + */ class LoginAttemptModel { @@ -45,13 +69,20 @@ class LoginAttemptModel * * @var BaseConfig */ - protected $config; + protected $config; + /** + * Constructor + * + * @param ConnectionInterface $db Database connection + * + * @return void + */ public function __construct(ConnectionInterface &$db = null) { - $this->config = new AauthConfig(); + $this->config = new AauthConfig(); $this->DBGroup = $this->config->dbProfile; - $this->table = $this->config->dbTableLoginAttempts; + $this->table = $this->config->dbTableLoginAttempts; if ($db instanceof ConnectionInterface) { @@ -65,28 +96,63 @@ class LoginAttemptModel $this->request = Services::request(); } - public function update($id = null, $data = null) + /** + * 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; + + // 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; + } + + /** + * Update Login Attempt + * + * @param integer $id Login attempt id + * @param array $data Data array + * + * @return BaseBuilder + */ + public function update(int $id = null, array $data = null) { - $builder = $this->builder(); - $ip_address = $this->request->getIPAddress(); - $builder->where('ip_address', $ip_address); - $builder->where('updated_at >=', date("Y-m-d H:i:s", strtotime("-".$this->config->loginAttemptLimitTimePeriod))); - if ( ! $row = $builder->get()->getFirstRow()) + $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 (! $row = $builder->get()->getFirstRow()) { - $data = []; - $data['ip_address'] = $ip_address; - $data['count'] = 1; + $data['ip_address'] = $ipAddress; + $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; } else { - $data = array(); - $data['count'] = $row->count + 1; + $data['count'] = $row->count + 1; $data['updated_at'] = date('Y-m-d H:i:s'); - $builder->update($data, array('id' => $row->id)); + $builder->update($data, ['id' => $row->id]); if ($data['count'] > $this->config->loginAttemptLimit) { @@ -99,14 +165,21 @@ class LoginAttemptModel } } + /** + * Get Login Attempt + * + * Get login attempt based on time and ip address + * + * @return integer + */ public function get() { - $builder = $this->builder(); - $ip_address = $this->request->getIPAddress(); - $builder->where('ip_address', $ip_address); - $builder->where('updated_at >=', date("Y-m-d H:i:s", strtotime("-".$this->config->loginAttemptLimitTimePeriod))); + $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 ($builder->countAllResults() != 0) + if ($builder->countAllResults() !== 0) { $row = $builder->get()->getFirstRow(); return $row->count; @@ -118,45 +191,19 @@ class LoginAttemptModel } /** - * Deletes login attempt. + * Delete login attempt. + * + * Delete login attempt based on time and ip address * * @return BaseBuilder */ public function delete() { - $builder = $this->builder(); - $ip_address = $this->request->getIPAddress(); - $builder->where('ip_address', $ip_address); - $builder->where('updated_at >=', date("Y-m-d H:i:s", strtotime("-".$this->config->loginAttemptLimitTimePeriod))); + $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))); return $builder->delete(); } - - /** - * Provides a shared instance of the Query Builder. - * - * @param string $table - * - * @return BaseBuilder - */ - protected function builder(string $table = null) - { - if ($this->builder instanceof BaseBuilder) - { - return $this->builder; - } - - $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 70adf96..f285636 100644 --- a/application/Models/Aauth/LoginTokenModel.php +++ b/application/Models/Aauth/LoginTokenModel.php @@ -1,4 +1,23 @@ + * @author Raphael Jackstadt + * @copyright 2014-2017 Emre Akay + * @copyright 2018 Magefly + * @license https://opensource.org/licenses/MIT MIT License + * @link https://github.com/magefly/CodeIgniter-Aauth + */ + namespace App\Models\Aauth; use Config\Aauth as AauthConfig; @@ -7,6 +26,11 @@ use CodeIgniter\Database\BaseBuilder; use CodeIgniter\Database\BaseConnection; use CodeIgniter\Database\ConnectionInterface; +/** + * Login Token Model + * + * @package CodeIgniter-Aauth + */ class LoginTokenModel { @@ -46,6 +70,11 @@ class LoginTokenModel */ protected $config; + /** + * Constructor + * + * @param ConnectionInterface $db + */ public function __construct(ConnectionInterface &$db = null) { $this->config = new AauthConfig(); @@ -68,7 +97,7 @@ class LoginTokenModel * Works with the current Query Builder instance to return * all results, while optionally limiting them. * - * @param intger $user_id + * @param integer $user_id * @param boolean $expired * * @return array|null diff --git a/application/Models/Aauth/UserModel.php b/application/Models/Aauth/UserModel.php index ae43e12..f2b5bfd 100644 --- a/application/Models/Aauth/UserModel.php +++ b/application/Models/Aauth/UserModel.php @@ -1,31 +1,105 @@ + * @author Raphael Jackstadt + * @copyright 2014-2017 Emre Akay + * @copyright 2018 Magefly + * @license https://opensource.org/licenses/MIT MIT License + * @link https://github.com/magefly/CodeIgniter-Aauth + */ + namespace App\Models\Aauth; use CodeIgniter\Model; use Config\Aauth as AauthConfig; +/** + * User Model + * + * @package CodeIgniter-Aauth + */ class UserModel extends Model { + /** + * If this model should use "softDeletes" and + * simply set a flag when rows are deleted, or + * do hard deletes. + * + * @var boolean + */ protected $useSoftDeletes = true; - protected $useTimestamps = true; - protected $createdField = 'created_at'; - protected $updatedField = 'updated_at'; - protected $allowedFields = ['email', 'username', 'password']; - protected $beforeInsert = ['hashPassword']; - protected $beforeUpdate = ['hashPassword']; + /** + * If true, will set created_at, and updated_at + * values during insert and update routines. + * + * @var boolean + */ + protected $useTimestamps = true; + + /** + * An array of field names that are allowed + * to be set by the user in inserts/updates. + * + * @var array + */ + protected $allowedFields = [ + 'email', + 'username', + 'password', + ]; + + /** + * Callbacks. Each array should contain the method + * names (within the model) that should be called + * when those events are triggered. With the exception + * of 'afterFind', all methods are passed the same + * items that are given to the update/insert method. + * 'afterFind' will also include the results that were found. + * + * @var array + */ + protected $beforeInsert = ['hashPassword']; + + /** + * Callbacks. Each array should contain the method + * names (within the model) that should be called + * when those events are triggered. With the exception + * of 'afterFind', all methods are passed the same + * items that are given to the update/insert method. + * 'afterFind' will also include the results that were found. + * + * @var array + */ + protected $beforeUpdate = ['hashPassword']; + + /** + * Constructor + */ public function __construct() { parent::__construct(); - $this->config = new AauthConfig(); - $this->table = $this->config->dbTableUsers; + $this->config = new AauthConfig(); + $this->table = $this->config->dbTableUsers; $this->DBGroup = $this->config->dbProfile; - $this->validationRules['email'] = 'required|if_exist|valid_email|is_unique['.$this->table.'.email,id,{id}]'; - $this->validationRules['password'] = 'required|if_exist|min_length['.$this->config->passwordMin.']|max_length['.$this->config->passwordMax.']'; - $this->validationRules['username'] = 'if_exist|is_unique['.$this->table.'.username,id,{id}]|alpha_numeric_space|min_length[3]'; + + $this->validationRules['email'] = 'required|if_exist|valid_email|is_unique[' . $this->table . '.email,id,{id}]'; + $this->validationRules['password'] = 'required|if_exist|min_length[' . $this->config->passwordMin . ']|max_length[' . $this->config->passwordMax . ']'; + $this->validationRules['username'] = 'if_exist|is_unique[' . $this->table . '.username,id,{id}]|alpha_numeric_space|min_length[3]'; + $this->validationMessages = [ - 'email' => [ - 'is_unique' => lang('Aauth.existsAlreadyEmail'), + 'email' => [ + 'is_unique' => lang('Aauth.existsAlreadyEmail'), 'valid_email' => lang('Aauth.invalidEmail'), ], 'password' => [ @@ -33,38 +107,58 @@ class UserModel extends Model 'max_length' => lang('Aauth.invalidPassword'), ], 'username' => [ - 'is_unique' => lang('Aauth.existsAlreadyUsername'), + 'is_unique' => lang('Aauth.existsAlreadyUsername'), 'min_length' => lang('Aauth.invalidUsername'), ], ]; if ($this->config->loginUseUsername) { - $this->validationRules['username'] = 'is_unique['.$this->table.'.username,id,{id}]|required|alpha_numeric_space|min_length[3]'; + $this->validationRules['username'] = 'is_unique[' . $this->table . '.username,id,{id}]|required|alpha_numeric_space|min_length[3]'; + $this->validationMessages['username']['required'] = lang('Aauth.requiredUsername'); } } - public function updateLastLogin(int $id) + /** + * Update last login by User ID + * + * @param integer $userId User id + * + * @return void + */ + public function updateLastLogin(int $userId) { $builder = $this->builder(); - $data = array(); - $data['last_login'] = $this->setDate(); - $data['last_activity'] = $this->setDate(); - $builder->update($data, array('id' => $id)); + $data['last_login'] = $this->setDate(); + $data['last_activity'] = $this->setDate(); + $builder->update($data, [$this->primaryKey => $userId]); } - public function updateLastActivity(int $id) + /** + * Update Last Activity by User ID + * + * @param integer $userId User id + * + * @return void + */ + public function updateLastActivity(int $userId) { $builder = $this->builder(); - $data = array(); - $data['last_activity'] = $this->setDate(); - $builder->update($data, array('id' => $id)); + $data['last_activity'] = $this->setDate(); + $builder->update($data, [$this->primaryKey => $userId]); } - public function isBanned(int $id) + /** + * Checks if user is banned + * + * @param integer $userId User id + * + * @return boolean + */ + public function isBanned(int $userId) { $builder = $this->builder(); @@ -73,12 +167,26 @@ class UserModel extends Model $builder->where($this->deletedField, 0); } - $builder->where($this->primaryKey, $id); - $builder->where('banned', 1); - return $builder->countAllResults(); + $builder->select('banned'); + $builder->where($this->primaryKey, $userId); + // $builder->where('banned', 1); + + if ($user = $builder->get()->getFirstRow()) + { + return $user['banned']; + } + + return false; } - public function existsById(int $id) + /** + * Checks if user exist by user id + * + * @param integer $userId User id + * + * @return boolean + */ + public function existsById(int $userId) { $builder = $this->builder(); @@ -87,10 +195,17 @@ class UserModel extends Model $builder->where($this->deletedField, 0); } - $builder->where($this->primaryKey, $id); - return $builder->countAllResults(); + $builder->where($this->primaryKey, $userId); + return ($builder->countAllResults() ? true : false); } + /** + * Checks if user exist by email + * + * @param string $email Email address + * + * @return boolean + */ public function existsByEmail(string $email) { $builder = $this->builder(); @@ -101,9 +216,16 @@ class UserModel extends Model } $builder->where('email', $email); - return $builder->countAllResults(); + return ($builder->countAllResults() ? true : false); } + /** + * Checks if user exist by username + * + * @param string $username Username + * + * @return boolean + */ public function existsByUsername(string $username) { $builder = $this->builder(); @@ -114,12 +236,19 @@ class UserModel extends Model } $builder->where('username', $username); - return $builder->countAllResults(); + return ($builder->countAllResults() ? true : false); } + /** + * Hash Password Callback + * + * @param array $data Data array + * + * @return array + */ protected function hashPassword(array $data) { - if ( ! isset($data['data']['password'])) + if (! isset($data['data']['password'])) { return $data; } diff --git a/application/Models/Aauth/UserVariableModel.php b/application/Models/Aauth/UserVariableModel.php index ddff8e2..de6986b 100644 --- a/application/Models/Aauth/UserVariableModel.php +++ b/application/Models/Aauth/UserVariableModel.php @@ -1,39 +1,242 @@ + * @author Raphael Jackstadt + * @copyright 2014-2017 Emre Akay + * @copyright 2018 Magefly + * @license https://opensource.org/licenses/MIT MIT License + * @link https://github.com/magefly/CodeIgniter-Aauth + */ + namespace App\Models\Aauth; -use CodeIgniter\Model; use Config\Aauth as AauthConfig; +use Config\Database; +use CodeIgniter\Database\BaseBuilder; +use CodeIgniter\Database\BaseConnection; +use CodeIgniter\Database\ConnectionInterface; -class UserVariableModel extends Model +/** + * User Variable Model. + * + * @package CodeIgniter-Aauth + */ +class UserVariableModel { - protected $useSoftDeletes = false; - protected $useTimestamps = true; - protected $createdField = 'created_at'; - protected $updatedField = 'updated_at'; - protected $protectFields = false; - public function __construct() + /** + * Database Connection + * + * @var ConnectionInterface + */ + protected $db; + + /** + * Query Builder object + * + * @var BaseBuilder + */ + protected $builder; + + /** + * Name of database table + * + * @var string + */ + protected $table; + + /** + * The Database connection group that + * should be instantiated. + * + * @var string + */ + protected $DBGroup; + + /** + * Aauth Config object + * + * @var BaseConfig + */ + protected $config; + + /** + * Constructor + * + * @param ConnectionInterface $db Database object + */ + public function __construct(ConnectionInterface &$db = null) { - parent::__construct(); - $this->config = new AauthConfig(); - $this->table = $this->config->dbTableUserVariables; + $this->config = new AauthConfig(); $this->DBGroup = $this->config->dbProfile; + $this->table = $this->config->dbTableUserVariables; + + if ($db instanceof ConnectionInterface) + { + $this->db = & $db; + } + else + { + $this->db = Database::connect($this->DBGroup); + } } - public function get($userId, $dataKey, $system = 0) + /** + * 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 Variable by userId, dataKey & optional system + * + * @param integer $userId User id + * @param string $dataKey Key of variable + * @param boolean $system Whether system variable + * + * @return string|boolean + */ + 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); - if ($builder->countAllResults() != 0) + $builder->where('system', ($system ? 1 : 0)); + + if ($row = $builder->get()->getFirstRow('array')) { - return $builder->get()->getFirstRow(); + return $row['data_value']; + } + + return false; + } + + /** + * Find all user variables + * + * @param integer $userId User id + * @param boolean $system Whether system variable + * + * @return object + */ + public function findAll(int $userId, bool $system = null) + { + $builder = $this->builder(); + $builder->where('user_id', $userId); + $builder->where('system', ($system ? 1 : 0)); + + return $builder->get()->getResult(); + } + + /** + * Update/Insert User Variable + * + * @param integer $userId User id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + * + * @return BaseBuilder + */ + public function save(int $userId, string $dataKey, string $dataValue, bool $system = null) + { + $builder = $this->builder(); + $builder->where('user_id', $userId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + + if ($builder->countAllResults()) + { + $response = $this->update($userId, $dataKey, $dataValue, $system); } else { - return false; + $response = $this->insert($userId, $dataKey, $dataValue, $system); } + + return $response; + } + + /** + * Inserts User Variable + * + * @param integer $userId User id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + + * @return BaseBuilder + */ + public function insert(int $userId, string $dataKey, string $dataValue, bool $system = null) + { + $data['user_id'] = $userId; + $data['data_key'] = $dataKey; + $data['data_value'] = $dataValue; + $data['system'] = ($system ? 1 : 0); + $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); + } + + /** + * Update User Variable + * + * @param integer $userId User id + * @param string $dataKey Key of variable + * @param string $dataValue Value of variable + * @param boolean $system Whether system variable + * + * @return BaseBuilder + */ + public function update(int $userId, string $dataKey, string $dataValue, bool $system = null) + { + $builder = $this->builder(); + $builder->where('user_id', $userId); + $builder->where('data_key', $dataKey); + $builder->where('system', ($system ? 1 : 0)); + + $data['data_value'] = $dataValue; + $data['updated_at'] = date('Y-m-d H:i:s'); + + return $builder->set($data)->update(); } + }