From af0e23a39c135fad72432d25ecbe98dece1091b5 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Tue, 16 Jun 2020 12:06:07 +0200 Subject: [PATCH] add methods for creating and getting api keys --- SeedDMS_Core/Core/inc.ClassDMS.php | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/SeedDMS_Core/Core/inc.ClassDMS.php b/SeedDMS_Core/Core/inc.ClassDMS.php index 06a6092f3..98be741df 100644 --- a/SeedDMS_Core/Core/inc.ClassDMS.php +++ b/SeedDMS_Core/Core/inc.ClassDMS.php @@ -465,6 +465,7 @@ class SeedDMS_Core_DMS { $this->classnames['documentcontent'] = 'SeedDMS_Core_DocumentContent'; $this->classnames['user'] = 'SeedDMS_Core_User'; $this->classnames['role'] = 'SeedDMS_Core_Role'; + $this->classnames['apikey'] = 'SeedDMS_Core_ApiKey'; $this->classnames['group'] = 'SeedDMS_Core_Group'; $this->classnames['attributedefinitiongroup'] = 'SeedDMS_Core_AttributeDefinitionGroup'; $this->classnames['transmittal'] = 'SeedDMS_Core_Transmittal'; @@ -2782,6 +2783,40 @@ class SeedDMS_Core_DMS { return $this->getRole($this->db->getInsertID('tblRoles')); } /* }}} */ + /** + * Get a apikey by its key name + * + * @param integer $name name of key + * @return object/boolean apikey or false if no role was found + */ + function getApiKeyByApiKey($name) { /* {{{ */ + $classname = $this->classnames['apikey']; + return $classname::getInstance($name, $this, 'apikey'); + } /* }}} */ + + /** + * Create a new apikey + * + * @param string $apikey name of key + * @return object/boolean instance of {@link SeedDMS_Core_ApiKey} or false in + * case of an error. + */ + function addApiKey($apikey, $user, $disabled=false, $expires=null) { /* {{{ */ + if (is_object($this->getApiKeyByApiKey($apikey))) { + return false; + } + + if ($this->classnames['user'] != get_class($user)) { + return false; + } + + $queryStr = "INSERT INTO `tblApiKeys` (`apikey`, `user`, `disabled`, `expires`) VALUES (".$this->db->qstr($apikey).", ".$user->getID().", ".($disabled ? 1 : 0).", ".($expires ? $this->db->qstr($expires) : "NULL").")"; + if (!$this->db->getResult($queryStr)) + return false; + + return $this->getApiKey($this->db->getInsertID('tblApiKeys')); + } /* }}} */ + /** * Get a transmittal by its id *