IcingaHost: generate API keys

This commit is contained in:
Thomas Gelf 2017-07-06 16:18:35 +02:00
parent c1e056c749
commit 8a8540bc3e
1 changed files with 31 additions and 0 deletions

View File

@ -409,6 +409,37 @@ class IcingaHost extends IcingaObject
return $str;
}
/**
* @return string
*/
public function generateApiKey()
{
$key = sha1(
(string) microtime(false)
. $this->getObjectName()
. rand(1, 1000000)
);
if ($this->dbHasApiKey($key)) {
$key = $this->generateApiKey();
}
$this->set('api_key', $key);
return $key;
}
protected function dbHasApiKey($key)
{
$db = $this->getDb();
$query = $db->select()->from(
['o' => $this->getTableName()],
'o.api_key'
)->where('api_key = ?', $key);
return $db->fetchOne($query) === $key;
}
public static function loadWithApiKey($key, Db $db)
{
$query = $db->getDbAdapter()