From 2781cbd8de1f13abbbbb43d073dcce5916caaecd Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Tue, 16 Jun 2020 08:28:06 +0200 Subject: [PATCH] add class SeedDMS_Core_ApiKey --- SeedDMS_Core/Core/inc.ClassUser.php | 188 ++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/SeedDMS_Core/Core/inc.ClassUser.php b/SeedDMS_Core/Core/inc.ClassUser.php index ca1634e03..01521f15e 100644 --- a/SeedDMS_Core/Core/inc.ClassUser.php +++ b/SeedDMS_Core/Core/inc.ClassUser.php @@ -229,6 +229,194 @@ class SeedDMS_Core_Role { /* {{{ */ } /* }}} */ +/** + * Class to represent an apikey in the document management system + * + * @category DMS + * @package SeedDMS_Core + * @author Uwe Steinmann + * @copyright Copyright (C) 2016 Uwe Steinmann + * @version Release: @package_version@ + */ +class SeedDMS_Core_ApiKey { /* {{{ */ + /** + * @var integer id of apikey + * + * @access protected + */ + var $_id; + + /** + * @var string name of key + * + * @access protected + */ + var $_apikey; + + /** + * @var boolean disabled + * + * @access protected + */ + var $_disabled; + + /** + * @var date expires at end of this date + * + * @access protected + */ + var $_expires; + + /** + * @var object reference to the user this apikey belongs to + * + * @access protected + */ + var $_user; + + /** + * @var object reference to the dms instance this user belongs to + * + * @access protected + */ + var $_dms; + + function __construct($id, $apikey, $disabled=false, $expires=null) { /* {{{ */ + $this->_id = $id; + $this->_apikey = $apikey; + $this->_disabled = $disabled; + $this->_expires = $expires; + $this->_dms = null; + } /* }}} */ + + /** + * Create an instance of a role object + * + * @param string|integer $id Id, login name, or email of user, depending + * on the 3rd parameter. + * @param object $dms instance of dms + * @param string $by search by [name|email]. If 'name' is passed, the method + * will check for the 4th paramater and also filter by email. If this + * parameter is left empty, the user will be search by its Id. + * @param string $email optional email address if searching for name + * @return object instance of class SeedDMS_Core_User + */ + public static function getInstance($id, $dms, $by='') { /* {{{ */ + $db = $dms->getDB(); + + switch($by) { + case 'apikey': + $queryStr = "SELECT * FROM `tblApiKeys` WHERE `apikey` = ".$db->qstr($id); + break; + default: + $queryStr = "SELECT * FROM `tblApiKeys` WHERE id = " . (int) $id; + } + + $resArr = $db->getResultArray($queryStr); + if (is_bool($resArr) && $resArr == false) return false; + if (count($resArr) != 1) return false; + + $resArr = $resArr[0]; + + $apikey = new self($resArr["id"], $resArr["apikey"], $resArr["disabled"] == 1, $resArr['expires']); + $apikey->setDMS($dms); + return $apikey; + } /* }}} */ + + public static function getAllInstances($orderby, $dms) { /* {{{ */ + $db = $dms->getDB(); + + if($orderby == 'expires') + $queryStr = "SELECT * FROM `tblApiKeys` ORDER BY `expires`"; + else + $queryStr = "SELECT * FROM `tblApiKeys` ORDER BY `id`"; + $resArr = $db->getResultArray($queryStr); + + if (is_bool($resArr) && $resArr == false) + return false; + + $apikeys = array(); + + for ($i = 0; $i < count($resArr); $i++) { + $apikey = new self($resArr[$i]["id"], $resArr[$i]["apikey"], $resArr[$i]["disabled"]==1, $resArr[$i]['expires']); + $apikey->setDMS($dms); + $apikeys[$i] = $apikey; + } + + return $apikeys; +} /* }}} */ + + function setDMS($dms) { + $this->_dms = $dms; + } + + function getID() { return $this->_id; } + + function getApiKey() { return $this->_apikey; } + + function setName($newApiKey) { /* {{{ */ + $db = $this->_dms->getDB(); + + $queryStr = "UPDATE `tblApiKeys` SET `apikey` =".$db->qstr($newApiKey)." WHERE `id` = " . $this->_id; + $res = $db->getResult($queryStr); + if (!$res) + return false; + + $this->_name = $newApiKey; + return true; + } /* }}} */ + + function isDisabled() { return ($this->_disabled); } + + function getExpires() { return $this->_expires; } + + function setExpires($newexpires) { /* {{{ */ + $db = $this->_dms->getDB(); + + $queryStr = "UPDATE `tblApiKeys` SET `expires` = " . $newexpires . " WHERE `id` = " . $this->_id; + if (!$db->getResult($queryStr)) + return false; + + $this->_expires = $newexpires; + return true; + } /* }}} */ + + /** + * Delete apikey + * + * @return boolean true on success or false in case of an error + */ + function remove() { /* {{{ */ + $db = $this->_dms->getDB(); + + $queryStr = "DELETE FROM `tblApiKeys` WHERE `id` = " . $this->_id; + if (!$db->getResult($queryStr)) { + return false; + } + + return true; + } /* }}} */ + + function getUser() { /* {{{ */ + $db = $this->_dms->getDB(); + + if (!isset($this->_user)) { + $queryStr = "SELECT `b`.* FROM `tblApiKeys`i `a` LEFT JOIN `tblUsers` `b` ON `a`.`user`=`b`.`id` WHERE `a`.`id`=".$this->_id; + $resArr = $db->getResultArray($queryStr); + if (is_bool($resArr) && $resArr == false) + return false; + + $this->_user = null; + $row = $resArr[0]; + $classname = $this->_dms->getClassname('user'); + $this->_user = new $classname($row["id"], $row["login"], $row["pwd"], $row["fullName"], $row["email"], $row["language"], $row["theme"], $row["comment"], $role, $row['hidden']); + $this->_user->setDMS($this->_dms); + } + return $this->_user; + } /* }}} */ + +} /* }}} */ + /** * Class to represent a user in the document management system *