From 6789d6c556a368307689da1737fafc79a53bb1c7 Mon Sep 17 00:00:00 2001 From: REJack Date: Wed, 14 Nov 2018 12:19:19 +0100 Subject: [PATCH] added LoginTokenModel --- application/Models/Aauth/LoginTokenModel.php | 140 +++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 application/Models/Aauth/LoginTokenModel.php diff --git a/application/Models/Aauth/LoginTokenModel.php b/application/Models/Aauth/LoginTokenModel.php new file mode 100644 index 0000000..6cfbe8f --- /dev/null +++ b/application/Models/Aauth/LoginTokenModel.php @@ -0,0 +1,140 @@ +config = new AauthConfig(); + $this->DBGroup = $this->config->dbProfile; + $this->table = $this->config->dbTableLoginTokens; + + if ($db instanceof ConnectionInterface) + { + $this->db = & $db; + } + else + { + $this->db = Database::connect($this->DBGroup); + } + + $this->request = Services::request(); + } + + /** + * Works with the current Query Builder instance to return + * all results, while optionally limiting them. + * + * @param intger $user_id + * @param boolean $expired + * + * @return array|null + */ + public function getTokens($user_id) + { + $builder = $this->builder(); + + $builder->where('is_expired', 0); + + $row = $builder->get(); + + $row = $row->getResult('array'); + + return $row['data']; + } + + public function insert($data) + { + $builder = $this->builder(); + + $data['created_at'] = date('Y-m-d H:i:s'); + $data['updated_at'] = date('Y-m-d H:i:s'); + + return $builder->insert($data); + } + + /** + * Deletes login attempt. + * + * @return BaseBuilder + */ + public function delete($tokenId) + { + $builder = $this->builder(); + $builder->where('id', $tokenId); + + 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; + } + +}