From 14713b0ec7ed93ca45578da069ad4e19a7d8addf Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 28 May 2016 19:48:22 -0400 Subject: Rename all models --- app/Model/LastLoginModel.php | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 app/Model/LastLoginModel.php (limited to 'app/Model/LastLoginModel.php') diff --git a/app/Model/LastLoginModel.php b/app/Model/LastLoginModel.php new file mode 100644 index 00000000..16821392 --- /dev/null +++ b/app/Model/LastLoginModel.php @@ -0,0 +1,92 @@ +cleanup($user_id); + + return $this->db + ->table(self::TABLE) + ->insert(array( + 'auth_type' => $auth_type, + 'user_id' => $user_id, + 'ip' => $ip, + 'user_agent' => substr($user_agent, 0, 255), + 'date_creation' => time(), + )); + } + + /** + * Cleanup login history + * + * @access public + * @param integer $user_id + */ + public function cleanup($user_id) + { + $connections = $this->db + ->table(self::TABLE) + ->eq('user_id', $user_id) + ->desc('id') + ->findAllByColumn('id'); + + if (count($connections) >= self::NB_LOGINS) { + $this->db->table(self::TABLE) + ->eq('user_id', $user_id) + ->notin('id', array_slice($connections, 0, self::NB_LOGINS - 1)) + ->remove(); + } + } + + /** + * Get the last connections for a given user + * + * @access public + * @param integer $user_id User id + * @return array + */ + public function getAll($user_id) + { + return $this->db + ->table(self::TABLE) + ->eq('user_id', $user_id) + ->desc('id') + ->columns('id', 'auth_type', 'ip', 'user_agent', 'date_creation') + ->findAll(); + } +} -- cgit v1.2.3