37 lines
1.1 KiB
PHP
37 lines
1.1 KiB
PHP
<?php
|
|
namespace Makai\SSHKeys;
|
|
|
|
use Index\Db\{DbConnection,DbStatementCache};
|
|
|
|
class SSHKeys {
|
|
private DbStatementCache $cache;
|
|
|
|
public function __construct(DbConnection $dbConn) {
|
|
$this->cache = new DbStatementCache($dbConn);
|
|
}
|
|
|
|
public function getKeys(
|
|
?int $minLevel = null,
|
|
?bool $deprecated = null,
|
|
): iterable {
|
|
$hasMinLevel = $minLevel !== null;
|
|
$hasDeprecated = $deprecated !== null;
|
|
|
|
$args = 0;
|
|
$query = 'SELECT key_id, key_level, key_algo, key_body, key_comment, UNIX_TIMESTAMP(key_created), UNIX_TIMESTAMP(key_deprecated) FROM fm_public_keys';
|
|
if($hasMinLevel) {
|
|
++$args;
|
|
$query .= ' WHERE key_level >= ?';
|
|
}
|
|
if($hasDeprecated)
|
|
$query .= sprintf(' %s key_deprecated %s NULL', ++$args > 1 ? 'AND' : 'WHERE', $deprecated ? 'IS NOT' : 'IS');
|
|
$query .= ' ORDER BY key_level DESC, key_id ASC';
|
|
|
|
$stmt = $this->cache->get($query);
|
|
if($hasMinLevel)
|
|
$stmt->addParameter(1, $minLevel);
|
|
$stmt->execute();
|
|
|
|
return $stmt->getResult()->getIterator(SSHKeyInfo::fromResult(...));
|
|
}
|
|
}
|