mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-02 22:54:08 +01:00
332 lines
9.5 KiB
PHP
332 lines
9.5 KiB
PHP
<?php
|
|
/*
|
|
* sysPass
|
|
*
|
|
* @author nuxsmin
|
|
* @link https://syspass.org
|
|
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
|
|
*
|
|
* This file is part of sysPass.
|
|
*
|
|
* sysPass is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* sysPass is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace SP\Infrastructure\Client\Repositories;
|
|
|
|
use SP\Domain\Account\Ports\AccountFilterBuilder;
|
|
use SP\Domain\Client\Models\Client as ClientModel;
|
|
use SP\Domain\Client\Ports\ClientRepository;
|
|
use SP\Domain\Core\Dtos\ItemSearchDto;
|
|
use SP\Domain\Core\Exceptions\ConstraintException;
|
|
use SP\Domain\Core\Exceptions\QueryException;
|
|
use SP\Domain\Core\Exceptions\SPException;
|
|
use SP\Infrastructure\Common\Repositories\BaseRepository;
|
|
use SP\Infrastructure\Common\Repositories\DuplicatedItemException;
|
|
use SP\Infrastructure\Common\Repositories\RepositoryItemTrait;
|
|
use SP\Infrastructure\Database\QueryData;
|
|
use SP\Infrastructure\Database\QueryResult;
|
|
|
|
use function SP\__u;
|
|
|
|
/**
|
|
* Class Client
|
|
*
|
|
* @template T of ClientModel
|
|
*/
|
|
final class Client extends BaseRepository implements ClientRepository
|
|
{
|
|
use RepositoryItemTrait;
|
|
|
|
/**
|
|
* Creates an item
|
|
*
|
|
* @param ClientModel $client
|
|
*
|
|
* @return QueryResult
|
|
* @throws DuplicatedItemException
|
|
* @throws SPException
|
|
*/
|
|
public function create(ClientModel $client): QueryResult
|
|
{
|
|
if ($this->checkDuplicatedOnAdd($client)) {
|
|
throw new DuplicatedItemException(__u('Duplicated client'));
|
|
}
|
|
|
|
$query = $this->queryFactory
|
|
->newInsert()
|
|
->into(ClientModel::TABLE)
|
|
->cols($client->toArray(null, ['id', 'hash']))
|
|
->col('hash', $this->makeItemHash($client->getName()));
|
|
|
|
$queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while creating the client'));
|
|
|
|
return $this->db->runQuery($queryData);
|
|
}
|
|
|
|
/**
|
|
* Checks whether the item is duplicated on adding
|
|
*
|
|
* @param ClientModel $client
|
|
* @return bool
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
private function checkDuplicatedOnAdd(ClientModel $client): bool
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->cols(['id'])
|
|
->from(ClientModel::TABLE)
|
|
->where('hash = :hash')
|
|
->orWhere('name = :name')
|
|
->bindValues(
|
|
[
|
|
'hash' => $client->getHash(),
|
|
'name' => $client->getName()
|
|
]
|
|
);
|
|
|
|
return $this->db->runQuery(QueryData::build($query))->getNumRows() > 0;
|
|
}
|
|
|
|
/**
|
|
* Updates an item
|
|
*
|
|
* @param ClientModel $client
|
|
*
|
|
* @return int
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
* @throws DuplicatedItemException
|
|
*/
|
|
public function update(ClientModel $client): int
|
|
{
|
|
if ($this->checkDuplicatedOnUpdate($client)) {
|
|
throw new DuplicatedItemException(__u('Duplicated client'));
|
|
}
|
|
|
|
$query = $this->queryFactory
|
|
->newUpdate()
|
|
->table(ClientModel::TABLE)
|
|
->cols($client->toArray(null, ['id', 'hash']))
|
|
->where('id = :id')
|
|
->limit(1)
|
|
->bindValues(
|
|
[
|
|
'id' => $client->getId(),
|
|
'hash' => $this->makeItemHash($client->getName())
|
|
]
|
|
);
|
|
|
|
$queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while updating the client'));
|
|
|
|
return $this->db->runQuery($queryData)->getAffectedNumRows();
|
|
}
|
|
|
|
/**
|
|
* Checks whether the item is duplicated on updating
|
|
*
|
|
* @param ClientModel $client
|
|
*
|
|
* @return bool
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
private function checkDuplicatedOnUpdate(ClientModel $client): bool
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->cols(['id'])
|
|
->from(ClientModel::TABLE)
|
|
->where('(hash = :hash OR name = :name)')
|
|
->where('id <> :id')
|
|
->bindValues(
|
|
[
|
|
'id' => $client->getId(),
|
|
'hash' => $client->getHash(),
|
|
'name' => $client->getName(),
|
|
]
|
|
);
|
|
|
|
return $this->db->runQuery(QueryData::build($query))->getNumRows() > 0;
|
|
}
|
|
|
|
/**
|
|
* Returns the item for given id
|
|
*
|
|
* @param int $clientId
|
|
*
|
|
* @return QueryResult<T>
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function getById(int $clientId): QueryResult
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->from(ClientModel::TABLE)
|
|
->cols(ClientModel::getCols())
|
|
->where('id = :id')
|
|
->bindValues(['id' => $clientId])
|
|
->limit(1);
|
|
|
|
$queryData = QueryData::buildWithMapper($query, ClientModel::class);
|
|
|
|
return $this->db->runQuery($queryData);
|
|
}
|
|
|
|
/**
|
|
* Returns the item for given name
|
|
*
|
|
* @param string $name
|
|
*
|
|
* @return QueryResult<T>
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function getByName(string $name): QueryResult
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->from(ClientModel::TABLE)
|
|
->cols(ClientModel::getCols())
|
|
->where('(name = :name OR hash = :hash)')
|
|
->bindValues(['name' => $name, 'hash' => $this->makeItemHash($name)])
|
|
->limit(1);
|
|
|
|
$queryData = QueryData::buildWithMapper($query, ClientModel::class);
|
|
|
|
return $this->db->runQuery($queryData);
|
|
}
|
|
|
|
/**
|
|
* Returns all the items
|
|
*
|
|
* @return QueryResult<T>
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function getAll(): QueryResult
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->from(ClientModel::TABLE)
|
|
->cols(ClientModel::getCols());
|
|
|
|
return $this->db->runQuery(QueryData::buildWithMapper($query, ClientModel::class));
|
|
}
|
|
|
|
/**
|
|
* Deletes all the items for given ids
|
|
*
|
|
* @param array $clientIds
|
|
*
|
|
* @return QueryResult
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function deleteByIdBatch(array $clientIds): QueryResult
|
|
{
|
|
if (count($clientIds) === 0) {
|
|
return new QueryResult();
|
|
}
|
|
|
|
$query = $this->queryFactory
|
|
->newDelete()
|
|
->from(ClientModel::TABLE)
|
|
->where('id IN (:ids)', ['ids' => $clientIds]);
|
|
|
|
$queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while deleting the clients'));
|
|
|
|
return $this->db->runQuery($queryData);
|
|
}
|
|
|
|
/**
|
|
* Deletes an item
|
|
*
|
|
* @param int $id
|
|
*
|
|
* @return QueryResult
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function delete(int $id): QueryResult
|
|
{
|
|
$query = $this->queryFactory
|
|
->newDelete()
|
|
->from(ClientModel::TABLE)
|
|
->where('id = :id')
|
|
->bindValues(['id' => $id]);
|
|
|
|
$queryData = QueryData::build($query)->setOnErrorMessage(__u('Error while deleting the client'));
|
|
|
|
return $this->db->runQuery($queryData);
|
|
}
|
|
|
|
/**
|
|
* Searches for items by a given filter
|
|
*
|
|
* @param ItemSearchDto $itemSearchData
|
|
*
|
|
* @return QueryResult<T>
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function search(ItemSearchDto $itemSearchData): QueryResult
|
|
{
|
|
$query = $this->queryFactory
|
|
->newSelect()
|
|
->from(ClientModel::TABLE)
|
|
->cols(ClientModel::getCols(['hash']))
|
|
->orderBy(['name'])
|
|
->limit($itemSearchData->getLimitCount())
|
|
->offset($itemSearchData->getLimitStart());
|
|
|
|
if (!empty($itemSearchData->getSeachString())) {
|
|
$query->where('name LIKE :name OR description LIKE :description');
|
|
|
|
$search = '%' . $itemSearchData->getSeachString() . '%';
|
|
|
|
$query->bindValues(['name' => $search, 'description' => $search]);
|
|
}
|
|
|
|
$queryData = QueryData::build($query)->setMapClassName(ClientModel::class);
|
|
|
|
return $this->db->runQuery($queryData, true);
|
|
}
|
|
|
|
/**
|
|
* Return the clients visible for the current user
|
|
*
|
|
* @param AccountFilterBuilder $accountFilterUser
|
|
*
|
|
* @return QueryResult
|
|
* @throws ConstraintException
|
|
* @throws QueryException
|
|
*/
|
|
public function getAllForFilter(AccountFilterBuilder $accountFilterUser): QueryResult
|
|
{
|
|
$query = $accountFilterUser
|
|
->buildFilter()
|
|
->cols(['Client.id', 'Client.name'])
|
|
->join('right', 'Client', 'Account.clientId = Client.id')
|
|
->where('Account.clientId IS NULL')
|
|
->orWhere('Client.isGlobal = 1')
|
|
->groupBy(['id'])
|
|
->orderBy(['Client.name']);
|
|
|
|
return $this->db->runQuery(QueryData::build($query));
|
|
}
|
|
}
|