grocy/services/ApiKeyService.php

65 lines
1.2 KiB
PHP

<?php
namespace Grocy\Services;
class ApiKeyService extends BaseService
{
/**
* @return boolean
*/
public function IsValidApiKey($apiKey)
{
if ($apiKey === null || empty($apiKey))
{
return false;
}
else
{
$apiKeyRow = $this->Database->api_keys()->where('api_key = :1 AND expires > :2', $apiKey, date('Y-m-d H:i:s', time()))->fetch();
if ($apiKeyRow !== null)
{
$apiKeyRow->update(array(
'last_used' => date('Y-m-d H:i:s', time())
));
return true;
}
else
{
return false;
}
}
}
/**
* @return string
*/
public function CreateApiKey()
{
$newApiKey = $this->GenerateApiKey();
$apiKeyRow = $this->Database->api_keys()->createRow(array(
'api_key' => $newApiKey,
'expires' => '2999-12-31 23:59:59' // Default is that API keys expire never
));
$apiKeyRow->save();
return $newApiKey;
}
public function RemoveApiKey($apiKey)
{
$this->Database->api_keys()->where('api_key', $apiKey)->delete();
}
public function GetApiKeyId($apiKey)
{
$apiKey = $this->Database->api_keys()->where('api_key', $apiKey)->fetch();
return $apiKey->id;
}
private function GenerateApiKey()
{
return RandomString(50);
}
}