chore: Minor refactor

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2023-12-09 08:01:57 +01:00
parent 9d9fc63ebc
commit efea16c6f5
6 changed files with 106 additions and 116 deletions

View File

@@ -54,11 +54,11 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
use RepositoryItemTrait;
public function __construct(
DatabaseInterface $database,
ContextInterface $session,
QueryFactory $queryFactory,
EventDispatcherInterface $eventDispatcher,
private AccountFilterUserInterface $accountFilterUser
DatabaseInterface $database,
ContextInterface $session,
QueryFactory $queryFactory,
EventDispatcherInterface $eventDispatcher,
private readonly AccountFilterUserInterface $accountFilterUser
) {
parent::__construct($database, $session, $eventDispatcher, $queryFactory);
}
@@ -77,7 +77,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
}
/**
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -86,13 +86,13 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
$query = $this->accountFilterUser
->buildFilter()
->cols([
'Account.id,',
'Account.name',
'Account.login',
'Account.pass',
'Account.key',
'Account.parentId',
])
'Account.id,',
'Account.name',
'Account.login',
'Account.pass',
'Account.key',
'Account.parentId',
])
->where('Account.id = :id')
->bindValues(['id' => $accountId])
->limit(1);
@@ -101,7 +101,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
}
/**
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -110,14 +110,14 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
$query = $this->accountFilterUser
->buildFilterHistory()
->cols([
'AccountHistory.id,',
'AccountHistory.name',
'AccountHistory.login',
'AccountHistory.pass',
'AccountHistory.key',
'AccountHistory.parentId',
'AccountHistory.mPassHash',
])
'AccountHistory.id,',
'AccountHistory.name',
'AccountHistory.login',
'AccountHistory.pass',
'AccountHistory.key',
'AccountHistory.parentId',
'AccountHistory.mPassHash',
])
->where('AccountHistory.accountId = :accountId')
->bindValues(['accountId' => $accountId]);
@@ -127,7 +127,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Incrementa el contador de vista de clave de una cuenta en la BBDD
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
* @throws ConstraintException
@@ -192,7 +192,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Actualiza la clave de una cuenta en la BBDD.
*
* @param int $accountId
* @param int $accountId
* @param Account $account
*
* @return QueryResult
@@ -218,7 +218,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Actualiza la clave de una cuenta en la BBDD.
*
* @param int $accountId
* @param int $accountId
* @param EncryptedPassword $encryptedPassword
*
* @return QueryResult
@@ -242,7 +242,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Restaurar una cuenta desde el histórico.
*
* @param int $accountId
* @param int $accountId
* @param Account $account
*
* @return QueryResult
@@ -279,7 +279,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Elimina los datos de una cuenta en la BBDD.
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
* @throws ConstraintException
@@ -301,10 +301,10 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Updates an item
*
* @param int $accountId
* @param int $accountId
* @param Account $account
* @param bool $changeOwner
* @param bool $changeUserGroup
* @param bool $changeOwner
* @param bool $changeUserGroup
*
* @return QueryResult
* @throws ConstraintException
@@ -352,10 +352,10 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Updates an item for bulk action
*
* @param int $accountId
* @param int $accountId
* @param Account $account
* @param bool $changeOwner
* @param bool $changeUserGroup
* @param bool $changeOwner
* @param bool $changeUserGroup
*
* @return QueryResult
* @throws ConstraintException
@@ -400,7 +400,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Returns the item for given id with referential data
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -423,7 +423,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Returns the item for given id
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -461,7 +461,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Deletes all the items for given ids
*
* @param array $accountsId
* @param array $accountsId
*
* @return QueryResult
* @throws ConstraintException
@@ -486,7 +486,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Searches for items by a given filter
*
* @param ItemSearchData $itemSearchData
* @param ItemSearchData $itemSearchData
*
* @return QueryResult
*/
@@ -507,15 +507,15 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
->orWhere('userName LIKE :userName')
->orWhere('userGroupName LIKE :userGroupName');
$search = '%'.$itemSearchData->getSeachString().'%';
$search = '%' . $itemSearchData->getSeachString() . '%';
$query->bindValues([
'name' => $search,
'clientName' => $search,
'categoryName' => $search,
'userName' => $search,
'userGroupName' => $search,
]);
'name' => $search,
'clientName' => $search,
'categoryName' => $search,
'userName' => $search,
'userGroupName' => $search,
]);
}
return $this->db->doSelect(QueryData::buildWithMapper($query, AccountSearchView::class), true);
@@ -524,7 +524,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Incrementa el contador de visitas de una cuenta en la BBDD
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
* @throws ConstraintException
@@ -545,7 +545,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
/**
* Obtener los datos de una cuenta.
*
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -555,15 +555,15 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
->newSelect()
->from('Account')
->cols([
'Account.name',
'Account.login',
'Account.pass',
'Account.key',
'Account.url',
'Account.notes',
'Client.name AS clientName',
'Category.name AS categoryName',
])
'Account.name',
'Account.login',
'Account.pass',
'Account.key',
'Account.url',
'Account.notes',
'Client.name AS clientName',
'Category.name AS categoryName',
])
->join('INNER', 'Client', 'Account.clientId = Client.id')
->join('INNER', 'Category', 'Account.categoryId = Category.id')
->where('Account.id = :id')
@@ -575,7 +575,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
}
/**
* @param int|null $accountId
* @param int|null $accountId
*
* @return QueryResult
*/
@@ -584,10 +584,10 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
$query = $this->accountFilterUser
->buildFilter()
->cols([
'Account.id',
'Account.name',
'Client.name AS clientName',
])
'Account.id',
'Account.name',
'Client.name AS clientName',
])
->join('INNER', 'Client', 'Account.clientId = Client.id')
->orderBy(['Account.name ASC']);
@@ -602,7 +602,7 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
}
/**
* @param int $accountId
* @param int $accountId
*
* @return QueryResult
*/
@@ -611,10 +611,10 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
$query = $this->accountFilterUser
->buildFilter()
->cols([
'Account.id',
'Account.name',
'Client.name AS clientName',
])
'Account.id',
'Account.name',
'Client.name AS clientName',
])
->join('INNER', 'Client', 'Account.clientId = Client.id')
->where('Account.parentId = :parentId')
->bindValues(['parentId' => $accountId])
@@ -634,11 +634,11 @@ final class AccountRepository extends Repository implements AccountRepositoryInt
->newSelect()
->from('Account')
->cols([
'id',
'name',
'pass',
'key',
])
'id',
'name',
'pass',
'key',
])
->where('BIT_LENGTH(pass) > 0');
return $this->db->doSelect(QueryData::buildWithMapper($query, Account::class));

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -66,7 +66,7 @@ final class CategoryRepository extends Repository implements CategoryRepositoryI
$queryData->setParams([
$itemData->getName(),
$itemData->getDescription(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
]);
$queryData->setOnErrorMessage(__u('Error while creating the category'));
@@ -87,7 +87,7 @@ final class CategoryRepository extends Repository implements CategoryRepositoryI
$queryData = new QueryData();
$queryData->setQuery('SELECT id FROM Category WHERE `hash` = ? OR `name` = ?');
$queryData->setParams([
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getName(),
]);
@@ -122,7 +122,7 @@ final class CategoryRepository extends Repository implements CategoryRepositoryI
$queryData->setParams([
$itemData->getName(),
$itemData->getDescription(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getId(),
]);
$queryData->setOnErrorMessage(__u('Error while updating the category'));
@@ -144,7 +144,7 @@ final class CategoryRepository extends Repository implements CategoryRepositoryI
$queryData = new QueryData();
$queryData->setQuery('SELECT id FROM Category WHERE (`hash` = ? OR `name` = ?) AND id <> ?');
$queryData->setParams([
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getName(),
$itemData->getId(),
]);
@@ -187,7 +187,7 @@ final class CategoryRepository extends Repository implements CategoryRepositoryI
$queryData->setQuery('SELECT id, `name`, description FROM Category WHERE `name` = ? OR `hash` = ? LIMIT 1');
$queryData->setParams([
$name,
$this->makeItemHash($name, $this->db->getDbHandler()),
$this->makeItemHash($name),
]);
return $this->db->doSelect($queryData);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -76,7 +76,7 @@ final class ClientRepository extends Repository implements ClientRepositoryInter
$itemData->getName(),
$itemData->getDescription(),
$itemData->getIsGlobal(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
]);
$queryData->setOnErrorMessage(__u('Error while creating the client'));
@@ -96,7 +96,7 @@ final class ClientRepository extends Repository implements ClientRepositoryInter
{
$queryData = new QueryData();
$queryData->setQuery('SELECT id FROM Client WHERE `hash` = ? LIMIT 1');
$queryData->addParam($this->makeItemHash($itemData->getName(), $this->db->getDbHandler()));
$queryData->addParam($this->makeItemHash($itemData->getName()));
return $this->db->doQuery($queryData)->getNumRows() > 0;
}
@@ -131,7 +131,7 @@ final class ClientRepository extends Repository implements ClientRepositoryInter
$itemData->getName(),
$itemData->getDescription(),
$itemData->getIsGlobal(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getId(),
]);
$queryData->setOnErrorMessage(__u('Error while updating the client'));
@@ -153,7 +153,7 @@ final class ClientRepository extends Repository implements ClientRepositoryInter
$queryData = new QueryData();
$queryData->setQuery('SELECT id FROM Client WHERE (`hash` = ? OR `name` = ?) AND id <> ?');
$queryData->setParams([
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getName(),
$itemData->getId(),
]);
@@ -198,7 +198,7 @@ final class ClientRepository extends Repository implements ClientRepositoryInter
);
$queryData->setParams([
$name,
$this->makeItemHash($name, $this->db->getDbHandler()),
$this->makeItemHash($name),
]);
return $this->db->doSelect($queryData);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,9 +24,6 @@
namespace SP\Infrastructure\Common\Repositories;
use SP\Infrastructure\Database\DatabaseUtil;
use SP\Infrastructure\Database\DbStorageInterface;
/**
* Trait RepositoryItemTrait
*
@@ -40,17 +37,13 @@ trait RepositoryItemTrait
* Esta función crear un hash para detectar nombres de elementos duplicados mediante
* la eliminación de carácteres especiales y capitalización
*/
protected function makeItemHash(
string $name,
DbStorageInterface $DBStorage
): string {
protected function makeItemHash(string $name): string
{
$charsSrc = ['.', ' ', '_', ', ', '-', ';', '\'', '"', ':', '(', ')', '|', '/'];
$databaseUtil = new DatabaseUtil($DBStorage);
return md5(
return sha1(
strtolower(
str_replace($charsSrc, '', $databaseUtil->escape($name))
str_replace($charsSrc, '', $name)
)
);
}
@@ -58,15 +51,13 @@ trait RepositoryItemTrait
/**
* Devuelve una cadena con los parámetros para una consulta SQL desde un array
*
* @param array $items
* @param string $placeholder Cadena a utilizar para los parámetros
* @param array $items
* @param string $placeholder Cadena a utilizar para los parámetros
*
* @return string
*/
protected function buildParamsFromArray(
array $items,
string $placeholder = '?'
): string {
protected function buildParamsFromArray(array $items, string $placeholder = '?'): string
{
return implode(
',',
array_fill(0, count($items), $placeholder)

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -25,6 +25,7 @@
namespace SP\Infrastructure\Database;
use Exception;
use function SP\processException;
/**
@@ -66,14 +67,12 @@ class DatabaseUtil
'account_data_v',
'account_search_v',
];
private DbStorageInterface $DBStorage;
/**
* DatabaseUtil constructor.
*/
public function __construct(DbStorageInterface $DBStorage)
public function __construct(private readonly DbStorageInterface $dbStorage)
{
$this->DBStorage = $DBStorage;
}
/**
@@ -86,7 +85,7 @@ class DatabaseUtil
',',
array_map(
static function ($value) {
return '\''.$value.'\'';
return '\'' . $value . '\'';
},
self::TABLES
)
@@ -102,7 +101,7 @@ class DatabaseUtil
$tables
);
$numTables = $this->DBStorage
$numTables = $this->dbStorage
->getConnection()
->query($query)
->fetchColumn();
@@ -118,7 +117,7 @@ class DatabaseUtil
public function checkDatabaseConnection(): bool
{
try {
$this->DBStorage->getConnection();
$this->dbStorage->getConnection();
return true;
} catch (Exception $e) {
@@ -136,7 +135,7 @@ class DatabaseUtil
$dbinfo = [];
try {
$db = $this->DBStorage->getConnection();
$db = $this->dbStorage->getConnection();
$attributes = [
'SERVER_VERSION',
@@ -146,7 +145,7 @@ class DatabaseUtil
];
foreach ($attributes as $val) {
$dbinfo[$val] = $db->getAttribute(constant('PDO::ATTR_'.$val));
$dbinfo[$val] = $db->getAttribute(constant('PDO::ATTR_' . $val));
}
} catch (Exception $e) {
processException($e);
@@ -161,7 +160,7 @@ class DatabaseUtil
public function escape(string $str): string
{
try {
return $this->DBStorage->getConnection()->quote(trim($str));
return $this->dbStorage->getConnection()->quote(trim($str));
} catch (Exception $e) {
processException($e);
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -64,7 +64,7 @@ final class TagRepository extends Repository implements TagRepositoryInterface
$queryData->setQuery('INSERT INTO Tag SET `name` = ?, `hash` = ?');
$queryData->setParams([
$itemData->getName(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
]);
$queryData->setOnErrorMessage(__u('Error while creating the tag'));
@@ -86,7 +86,7 @@ final class TagRepository extends Repository implements TagRepositoryInterface
$queryData->setQuery('SELECT id FROM Tag WHERE `name` = ? OR `hash` = ?');
$queryData->setParams([
$itemData->getName(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
]);
return $this->db->doSelect($queryData)->getNumRows() > 0;
@@ -112,7 +112,7 @@ final class TagRepository extends Repository implements TagRepositoryInterface
$queryData->setQuery('UPDATE Tag SET `name` = ?, `hash` = ? WHERE id = ? LIMIT 1');
$queryData->setParams([
$itemData->getName(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getId(),
]);
$queryData->setOnErrorMessage(__u('Error while updating the tag'));
@@ -135,7 +135,7 @@ final class TagRepository extends Repository implements TagRepositoryInterface
$queryData->setQuery('SELECT `hash` FROM Tag WHERE (`name` = ? OR `hash` = ?) AND id <> ?');
$queryData->setParams([
$itemData->getName(),
$this->makeItemHash($itemData->getName(), $this->db->getDbHandler()),
$this->makeItemHash($itemData->getName()),
$itemData->getId(),
]);
@@ -175,7 +175,7 @@ final class TagRepository extends Repository implements TagRepositoryInterface
$queryData = new QueryData();
$queryData->setMapClassName(TagData::class);
$queryData->setQuery('SELECT id, `name` FROM Tag WHERE `hash` = ? OR `name` = ? ORDER BY `name` LIMIT 1');
$queryData->setParams([$this->makeItemHash($name, $this->db->getDbHandler()), $name]);
$queryData->setParams([$this->makeItemHash($name), $name]);
return $this->db->doSelect($queryData);
}