chore(tests): UT for Config service & repository

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-01-14 10:42:45 +01:00
parent 28dc47fe58
commit 739f40ba5d
61 changed files with 1304 additions and 690 deletions

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,7 +27,7 @@ namespace SP\Modules\Cli\Commands;
use Exception;
use Psr\Log\LoggerInterface;
use RuntimeException;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Export\Ports\FileBackupServiceInterface;
use SP\Domain\Export\Services\FileBackupService;
use Symfony\Component\Console\Input\InputInterface;
@@ -57,8 +57,9 @@ final class BackupCommand extends CommandBase
public function __construct(
FileBackupServiceInterface $fileBackupService,
LoggerInterface $logger,
ConfigInterface $config)
LoggerInterface $logger,
ConfigFileService $config
)
{
$this->fileBackupService = $fileBackupService;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -26,8 +26,8 @@ namespace SP\Modules\Cli\Commands;
use Psr\Log\LoggerInterface;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@@ -40,12 +40,12 @@ abstract class CommandBase extends Command
{
public static array $envVarsMapping = [];
protected LoggerInterface $logger;
protected ConfigFileService $config;
protected ConfigFile $config;
protected ConfigDataInterface $configData;
public function __construct(
LoggerInterface $logger,
ConfigInterface $config
ConfigFileService $config
)
{
$this->logger = $logger;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -29,11 +29,14 @@ use Psr\Log\LoggerInterface;
use RuntimeException;
use SP\Domain\Account\Ports\AccountServiceInterface;
use SP\Domain\Account\Services\AccountService;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Services\ConfigService;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Config\Services\Config;
use SP\Domain\Crypt\Ports\MasterPassServiceInterface;
use SP\Domain\Crypt\Services\MasterPassService;
use SP\Domain\Crypt\Services\UpdateMasterPassRequest;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Modules\Cli\Commands\CommandBase;
use SP\Modules\Cli\Commands\Validators;
use SP\Util\Util;
@@ -65,16 +68,16 @@ final class UpdateMasterPasswordCommand extends CommandBase
* @var string
*/
protected static $defaultName = 'sp:crypt:update-master-password';
private \SP\Domain\Crypt\Ports\MasterPassServiceInterface $masterPassService;
private ConfigService $configService;
private MasterPassServiceInterface $masterPassService;
private Config $configService;
private AccountService $accountService;
public function __construct(
\SP\Domain\Crypt\Ports\MasterPassServiceInterface $masterPassService,
MasterPassServiceInterface $masterPassService,
AccountServiceInterface $accountService,
ConfigServiceInterface $configService,
LoggerInterface $logger,
ConfigInterface $config
ConfigService $configService,
LoggerInterface $logger,
ConfigFileService $config
) {
$this->masterPassService = $masterPassService;
$this->accountService = $accountService;
@@ -273,8 +276,8 @@ final class UpdateMasterPasswordCommand extends CommandBase
}
/**
* @throws \SP\Domain\Common\Services\ServiceException
* @throws \SP\Infrastructure\Common\Repositories\NoSuchItemException
* @throws ServiceException
* @throws NoSuchItemException
*/
private function checkMasterPassword(string $password): void
{

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,7 +27,7 @@ namespace SP\Modules\Cli\Commands;
use Exception;
use Psr\Log\LoggerInterface;
use SP\Core\Language;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Exceptions\InstallError;
use SP\Domain\Core\Exceptions\InvalidArgumentException;
use SP\Domain\Install\Adapters\InstallData;
@@ -71,8 +71,8 @@ final class InstallCommand extends CommandBase
private InstallerService $installer;
public function __construct(
LoggerInterface $logger,
ConfigInterface $config,
LoggerInterface $logger,
ConfigFileService $config,
InstallerServiceInterface $installer
) {
parent::__construct($logger, $config);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -31,7 +31,7 @@ use SP\Core\Application;
use SP\Core\Crypt\Hash;
use SP\Core\Events\Event;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
@@ -56,13 +56,13 @@ final class SaveController extends SimpleControllerBase
use JsonTrait;
private MasterPassServiceInterface $masterPassService;
private ConfigServiceInterface $configService;
private ConfigService $configService;
public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
MasterPassServiceInterface $masterPassService,
ConfigServiceInterface $configService
ConfigService $configService
) {
parent::__construct($application, $simpleControllerHelper);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -30,8 +30,8 @@ use RuntimeException;
use SP\Core\Application;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Config\Ports\ConfigBackupServiceInterface;
use SP\Domain\Config\Services\ConfigBackupService;
use SP\Domain\Config\Ports\ConfigBackupService;
use SP\Domain\Config\Services\ConfigBackup;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
@@ -46,12 +46,12 @@ final class DownloadConfigBackup extends SimpleControllerBase
{
use JsonTrait;
private ConfigBackupServiceInterface $configBackupService;
private ConfigBackupService $configBackupService;
public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
ConfigBackupServiceInterface $configBackupService
ConfigBackupService $configBackupService
) {
parent::__construct($application, $simpleControllerHelper);
@@ -76,7 +76,7 @@ final class DownloadConfigBackup extends SimpleControllerBase
);
if ($type === 'json') {
$data = ConfigBackupService::configToJson($this->configBackupService->getBackup());
$data = ConfigBackup::configToJson($this->configBackupService->getBackup());
} else {
throw new RuntimeException('Not implemented');
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -35,7 +35,7 @@ use SP\Core\Events\Event;
use SP\Core\Language;
use SP\Domain\Account\Ports\AccountServiceInterface;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\AppInfoInterface;
use SP\Domain\Core\Exceptions\CheckException;
@@ -78,9 +78,9 @@ final class IndexController extends ControllerBase
private UserGroupServiceInterface $userGroupService;
private UserProfileServiceInterface $userProfileService;
private MimeTypesInterface $mimeTypes;
private DatabaseUtil $databaseUtil;
private ConfigServiceInterface $configService;
private AccountServiceInterface $accountService;
private DatabaseUtil $databaseUtil;
private ConfigService $configService;
private AccountServiceInterface $accountService;
private PluginManager $pluginManager;
public function __construct(
@@ -92,7 +92,7 @@ final class IndexController extends ControllerBase
UserProfileServiceInterface $userProfileService,
MimeTypesInterface $mimeTypes,
DatabaseUtil $databaseUtil,
ConfigServiceInterface $configService,
ConfigService $configService,
AccountServiceInterface $accountService,
PluginManager $pluginManager
) {

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -34,7 +34,7 @@ use SP\Core\Events\EventDispatcher;
use SP\DataModel\ProfileData;
use SP\Domain\Auth\Services\AuthException;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Acl\AclInterface;
use SP\Domain\Core\Context\SessionContextInterface;
use SP\Domain\Core\Exceptions\FileNotFoundException;
@@ -63,9 +63,9 @@ abstract class ControllerBase
protected const ERR_UNAVAILABLE = 0;
protected EventDispatcher $eventDispatcher;
protected ConfigInterface $config;
protected SessionContextInterface $session;
protected EventDispatcher $eventDispatcher;
protected ConfigFileService $config;
protected SessionContextInterface $session;
protected ThemeInterface $theme;
protected AclInterface $acl;
protected ConfigDataInterface $configData;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,7 +27,7 @@ namespace SP\Modules\Web\Controllers\Helpers;
use SP\Core\Application;
use SP\Core\Events\EventDispatcher;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Http\RequestInterface;
use SP\Http\Request;
@@ -43,9 +43,9 @@ abstract class HelperBase
protected TemplateInterface $view;
protected ConfigDataInterface $configData;
protected ContextInterface $context;
protected EventDispatcher $eventDispatcher;
protected ConfigFileService $config;
protected Request $request;
protected EventDispatcher $eventDispatcher;
protected ConfigFile $config;
protected Request $request;
/**
* Constructor

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -29,7 +29,7 @@ use SP\Core\Application;
use SP\Core\Events\EventDispatcher;
use SP\Core\PhpExtensionChecker;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Exceptions\SessionTimeout;
@@ -48,9 +48,9 @@ abstract class SimpleControllerBase
{
use WebControllerTrait;
protected EventDispatcher $eventDispatcher;
protected ConfigFileService $config;
protected ContextInterface $session;
protected EventDispatcher $eventDispatcher;
protected ConfigFile $config;
protected ContextInterface $session;
protected ThemeInterface $theme;
protected Acl $acl;
protected Request $request;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -28,7 +28,7 @@ use Exception;
use JsonException;
use SP\Core\Bootstrap\BootstrapBase;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Http\JsonMessage;
use SP\Util\Util;
@@ -48,8 +48,8 @@ trait ConfigTrait
*/
protected function saveConfig(
ConfigDataInterface $configData,
ConfigInterface $config,
callable $onSuccess = null
ConfigFileService $config,
callable $onSuccess = null
): bool {
try {
if ($configData->isDemoEnabled()) {

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -26,7 +26,7 @@ namespace SP\Modules\Web\Forms;
use SP\Core\Application;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Http\RequestInterface;
@@ -37,7 +37,7 @@ use SP\Domain\Http\RequestInterface;
*/
abstract class FormBase
{
protected ConfigFileService $config;
protected ConfigFile $config;
protected ConfigDataInterface $configData;
protected ContextInterface $context;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,7 +24,7 @@
namespace SP\Core;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Context\SessionContextInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
@@ -37,18 +37,18 @@ final class Application
/**
* Module constructor.
*
* @param ConfigInterface $config
* @param ConfigFileService $config
* @param EventDispatcherInterface $eventDispatcher
* @param SessionContextInterface|ContextInterface $context
*/
public function __construct(
private readonly ConfigInterface $config,
private readonly ConfigFileService $config,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly SessionContextInterface|ContextInterface $context
) {
}
public function getConfig(): ConfigInterface
public function getConfig(): ConfigFileService
{
return $this->config;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -51,9 +51,9 @@ use SP\Domain\Auth\Ports\LdapActionsInterface;
use SP\Domain\Auth\Ports\LdapAuthInterface;
use SP\Domain\Auth\Ports\LdapConnectionInterface;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Services\ConfigBackupService;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Services\ConfigBackup;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Acl\ActionsInterface;
use SP\Domain\Core\Bootstrap\UriContextInterface;
use SP\Domain\Core\Context\ContextInterface;
@@ -130,16 +130,16 @@ final class CoreDefinitions
UriContextInterface::class => autowire(UriContext::class),
ContextInterface::class =>
static fn() => ContextFactory::getForModule(APP_MODULE),
ConfigInterface::class => create(ConfigFileService::class)
ConfigFileService::class => create(ConfigFile::class)
->constructor(
create(XmlHandler::class)
->constructor(create(FileHandler::class)->constructor(CONFIG_FILE)),
create(FileCache::class)->constructor(ConfigFileService::CONFIG_CACHE_FILE),
create(FileCache::class)->constructor(ConfigFile::CONFIG_CACHE_FILE),
get(ContextInterface::class),
autowire(ConfigBackupService::class)
autowire(ConfigBackup::class)
),
ConfigDataInterface::class =>
static fn(ConfigInterface $config) => $config->getConfigData(),
static fn(ConfigFileService $config) => $config->getConfigData(),
DatabaseConnectionData::class => factory([DatabaseConnectionData::class, 'getFromConfig']),
DbStorageInterface::class => autowire(MysqlHandler::class),
ActionsInterface::class =>

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -25,7 +25,7 @@
namespace SP\Core;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Bootstrap\ModuleInterface;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
@@ -35,8 +35,8 @@ use SP\Domain\Core\Events\EventDispatcherInterface;
*/
abstract class ModuleBase implements ModuleInterface
{
protected ConfigFileService $config;
protected ConfigDataInterface $configData;
protected ConfigFile $config;
protected ConfigDataInterface $configData;
protected ContextInterface $context;
private EventDispatcherInterface $eventDispatcher;

View File

@@ -1,87 +0,0 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2021, 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\DataModel;
/**
* Class ConfigData
*
* @package SP\DataModel
*/
class ConfigData
{
/**
* @var string
*/
public $parameter;
/**
* @var string
*/
public $value;
/**
* ConfigData constructor.
*
* @param string $param
* @param string $value
*/
public function __construct($param, $value)
{
$this->parameter = $param;
$this->value = $value;
}
/**
* @return string
*/
public function getParameter()
{
return $this->parameter;
}
/**
* @param string $parameter
*/
public function setParameter($parameter)
{
$this->parameter = $parameter;
}
/**
* @return string
*/
public function getValue()
{
return $this->value;
}
/**
* @param string $value
*/
public function setValue($value)
{
$this->value = $value;
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,43 +24,25 @@
namespace SP\DataModel\Dto;
/**
* Class ConfigRequest
*
* @package SP\DataModel\Dto
*/
class ConfigRequest
{
/**
* @var array
*/
private $data = [];
private array $data = [];
/**
* @param $param
* @param $value
*/
public function add($param, $value)
public function add(string $param, string $value): void
{
$this->data[$param] = $value;
}
/**
* @param $param
*
* @return mixed|null
*/
public function get($param)
public function get(string $param): ?string
{
return $this->data[$param] ?? null;
}
/**
* @return array
*/
public function getData()
public function getData(): array
{
return $this->data;
}
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -49,7 +49,7 @@ use SP\Domain\Account\Ports\AccountToUserRepositoryInterface;
use SP\Domain\Common\Models\Simple;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\NoSuchPropertyException;
use SP\Domain\Core\Exceptions\QueryException;
@@ -79,7 +79,7 @@ final class AccountService extends Service implements AccountServiceInterface
private AccountHistoryServiceInterface $accountHistoryService,
private AccountItemsServiceInterface $accountItemsService,
private AccountPresetServiceInterface $accountPresetService,
private ConfigServiceInterface $configService,
private ConfigService $configService,
private AccountCryptServiceInterface $accountCryptService
) {
parent::__construct($application);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -38,7 +38,7 @@ use SP\Domain\Common\Models\Simple;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Common\Services\ServiceItemTrait;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Crypt\CryptInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\CryptException;
@@ -211,7 +211,7 @@ final class PublicLinkService extends Service implements PublicLinkServiceInterf
/**
* Devolver el tiempo de caducidad del enlace
*/
public static function calcDateExpire(ConfigInterface $config): int
public static function calcDateExpire(ConfigFileService $config): int
{
return time() + $config->getConfigData()->getPublinksMaxTime();
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -36,7 +36,6 @@ use SP\Domain\Auth\Ports\AuthTokenRepositoryInterface;
use SP\Domain\Auth\Ports\AuthTokenServiceInterface;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Common\Services\ServiceItemTrait;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Crypt\CryptInterface;
use SP\Domain\Core\Crypt\VaultInterface;
@@ -52,14 +51,12 @@ use SP\Util\PasswordUtil;
use function SP\__u;
/**
* Class AuthTokenService
* Class AuthToken
*
* @template T of AuthTokenModel
*/
final class AuthToken extends Service implements AuthTokenServiceInterface
{
use ServiceItemTrait;
private const SECURED_ACTIONS = [
AclActionsInterface::ACCOUNT_VIEW_PASS,
AclActionsInterface::ACCOUNT_EDIT_PASS,

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -32,7 +32,6 @@ use SP\Domain\Category\Ports\CategoryRepositoryInterface;
use SP\Domain\Category\Ports\CategoryServiceInterface;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Common\Services\ServiceItemTrait;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
@@ -49,8 +48,6 @@ use function SP\__u;
*/
final class Category extends Service implements CategoryServiceInterface
{
use ServiceItemTrait;
public function __construct(
Application $application,
private readonly CategoryRepositoryInterface $categoryRepository

View File

@@ -66,12 +66,12 @@ final class ClientAdapter extends Adapter implements ClientAdapterInterface
public function transform(Client $data): array
{
return [
'id' => $data->getId(),
'name' => $data->getName(),
'description' => $data->getDescription(),
'isGlobal' => $data->isGlobal,
'id' => $data->getId(),
'name' => $data->getName(),
'description' => $data->getDescription(),
'isGlobal' => $data->getIsGlobal(),
'customFields' => null,
'links' => [
'links' => [
[
'rel' => 'self',
'uri' => Link::getDeepLink(

View File

@@ -32,7 +32,7 @@ use SP\Domain\Common\Models\Model;
*/
class Client extends Model implements DataModelInterface
{
public ?int $isGlobal = null;
protected ?int $isGlobal = null;
protected ?int $id = null;
protected ?string $name = null;
protected ?string $description = null;

View File

@@ -33,7 +33,6 @@ use SP\Domain\Client\Ports\ClientServiceInterface;
use SP\Domain\Common\Models\Simple;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Common\Services\ServiceItemTrait;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
@@ -50,8 +49,6 @@ use function SP\__u;
*/
final class Client extends Service implements ClientServiceInterface
{
use ServiceItemTrait;
public function __construct(
Application $application,
private readonly ClientRepositoryInterface $clientRepository,

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -80,12 +80,12 @@ abstract class Model implements JsonSerializable, ArrayAccess
/**
* @param array|null $only Include only these properties
* @param array|null $filter Filter out these properties
* @param array|null $exclude Filter out these properties
* @param bool $includeOuter Whether to include non-class properties
*
* @return array
*/
final public function toArray(?array $only = null, ?array $filter = null, bool $includeOuter = false): array
final public function toArray(?array $only = null, ?array $exclude = null, bool $includeOuter = false): array
{
$fields = $this->getClassProperties();
@@ -97,8 +97,8 @@ abstract class Model implements JsonSerializable, ArrayAccess
$fields = array_intersect_key($fields, array_flip($only));
}
if (null !== $filter) {
$fields = array_diff_key($fields, array_flip($filter));
if (null !== $exclude) {
$fields = array_diff_key($fields, array_flip($exclude));
}
return $fields;
@@ -107,15 +107,15 @@ abstract class Model implements JsonSerializable, ArrayAccess
/**
* Get columns name for this model
*
* @param array|null $filter
* @param array|null $exclude The columns to filter out from this model
*
* @return array
*/
final public static function getCols(?array $filter = null): array
final public static function getCols(?array $exclude = null): array
{
$self = new static();
return array_keys($self->toArray(null, $filter, false));
return array_keys($self->toArray(null, $exclude));
}
/**

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -29,10 +29,9 @@ use SP\Core\Application;
use SP\Core\Context\ContextException;
use SP\Core\Context\SessionContext;
use SP\Core\Crypt\Session;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
use SP\Domain\Core\Exceptions\CryptException;
use function SP\__u;
@@ -47,8 +46,8 @@ abstract class Service
{
protected const STATUS_INTERNAL_ERROR = 1000;
protected ConfigInterface $config;
protected ContextInterface $context;
protected ConfigFileService $config;
protected ContextInterface $context;
protected EventDispatcherInterface $eventDispatcher;
public function __construct(Application $application)

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -28,6 +28,7 @@ namespace SP\Domain\Common\Services;
* Trait ServiceItemTrait
*
* @package SP\Domain\Common\Services
* @deprecated
*/
trait ServiceItemTrait
{

View File

@@ -0,0 +1,46 @@
<?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\Domain\Config\Models;
use SP\Domain\Common\Models\Model;
/**
* Class Config
*/
class Config extends Model
{
protected ?string $parameter = null;
protected ?string $value = null;
public function getParameter(): ?string
{
return $this->parameter;
}
public function getValue(): ?string
{
return $this->value;
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -28,11 +28,9 @@ use SP\Domain\Common\Services\ServiceException;
use SP\Infrastructure\File\FileException;
/**
* Class ConfigBackupService
*
* @package SP\Domain\Config\Services
* Interface ConfigBackupService
*/
interface ConfigBackupServiceInterface
interface ConfigBackupService
{
/**
* Backs up the config data into the database
@@ -43,10 +41,10 @@ interface ConfigBackupServiceInterface
* @throws FileException
* @throws ServiceException
*/
public function restore(ConfigInterface $config): ConfigDataInterface;
public function restore(ConfigFileService $configFile): ConfigDataInterface;
/**
* @throws \SP\Domain\Common\Services\ServiceException
* @throws ServiceException
*/
public function getBackup(): string;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -30,7 +30,7 @@ use SP\Infrastructure\File\FileException;
/**
* Esta clase es responsable de leer y escribir la configuración del archivo config.php
*/
interface ConfigInterface
interface ConfigFileService
{
/**
* Cargar el archivo de configuración
@@ -45,18 +45,18 @@ interface ConfigInterface
* @param ConfigDataInterface $configData
* @param bool|null $backup
*
* @return ConfigInterface
* @return ConfigFileService
* @throws FileException
*/
public function saveConfig(
ConfigDataInterface $configData,
?bool $backup = true
): ConfigInterface;
): ConfigFileService;
/**
* Commits a config data
*/
public function updateConfig(ConfigDataInterface $configData): ConfigInterface;
public function updateConfig(ConfigDataInterface $configData): ConfigFileService;
/**
* Cargar la configuración desde el contexto
@@ -74,5 +74,5 @@ interface ConfigInterface
* @throws FileException
* @throws EnvironmentIsBrokenException
*/
public function generateUpgradeKey(): ConfigInterface;
public function generateUpgradeKey(): ConfigFileService;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,70 +24,51 @@
namespace SP\Domain\Config\Ports;
use SP\DataModel\ConfigData;
use SP\Domain\Common\Ports\RepositoryInterface;
use SP\Domain\Config\Models\Config as ConfigModel;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Database\QueryResult;
/**
* Class ConfigRepository
* Interface ConfigRepository
*
* @package SP\Infrastructure\Common\Repositories\Config
* @template T of ConfigModel
*/
interface ConfigRepositoryInterface
interface ConfigRepository extends RepositoryInterface
{
/**
* @param ConfigData $configData
*
* @return bool
* @throws ConstraintException
* @throws QueryException
*/
public function update(ConfigData $configData): bool;
/**
* @param ConfigData $configData
*
* @return int
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigData $configData): int;
/**
* Obtener un array con la configuración almacenada en la BBDD.
* @param ConfigModel $config
*
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function getAll(): QueryResult;
public function update(ConfigModel $config): QueryResult;
/**
* @param string $param
*
* @param ConfigModel $config
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigModel $config): QueryResult;
/**
* @param string $param
*
* @return QueryResult<T>
* @throws ConstraintException
* @throws QueryException
*/
public function getByParam(string $param): QueryResult;
/**
* @param string $param
* @param string $param
*
* @return bool
* @throws ConstraintException
* @throws QueryException
*/
public function has(string $param): bool;
/**
* @param string $param
*
* @return int
* @throws ConstraintException
* @throws QueryException
*/
public function deleteByParam(string $param): int;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -24,31 +24,29 @@
namespace SP\Domain\Config\Ports;
use SP\DataModel\ConfigData;
use SP\DataModel\Dto\ConfigRequest;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Models\Config;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
/**
* Class ConfigService
*
* @package SP\Domain\Config\Services
* Interface ConfigService
*/
interface ConfigServiceInterface
interface ConfigService
{
/**
* @throws NoSuchItemException
* @throws ServiceException
*/
public function getByParam(string $param, $default = null);
public function getByParam(string $param, $default = null): ?string;
/**
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigData $configData): int;
public function create(Config $config): int;
/**
* @throws ServiceException
@@ -59,21 +57,5 @@ interface ConfigServiceInterface
* @throws ConstraintException
* @throws QueryException
*/
public function save(string $param, $value): bool;
/**
* Obtener un array con la configuración almacenada en la BBDD.
*
* @return ConfigData[]
* @throws ConstraintException
* @throws QueryException
*/
public function getAll(): array;
/**
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function deleteByParam(string $param): void;
public function save(string $param, string $value): bool;
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -26,39 +26,40 @@ namespace SP\Domain\Config\Services;
use Exception;
use SP\Core\Application;
use SP\DataModel\ConfigData;
use SP\DataModel\Dto\ConfigRequest;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigRepositoryInterface;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Models\Config as ConfigModel;
use SP\Domain\Config\Ports\ConfigRepository;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Config\Repositories\ConfigRepository;
use function SP\__;
use function SP\processException;
/**
* Class ConfigService
*
* @package SP\Domain\Config\Services
* Class Config
*/
final class ConfigService extends Service implements ConfigServiceInterface
final class Config extends Service implements ConfigService
{
private ConfigRepository $configRepository;
public function __construct(Application $application, ConfigRepositoryInterface $configRepository)
public function __construct(Application $application, private readonly ConfigRepository $configRepository)
{
parent::__construct($application);
$this->configRepository = $configRepository;
}
/**
* @param string $param
* @param null $default
* @return string|null
* @throws NoSuchItemException
* @throws SPException
* @throws ServiceException
*/
public function getByParam(string $param, $default = null)
public function getByParam(string $param, $default = null): ?string
{
try {
$result = $this->configRepository->getByParam($param);
@@ -73,20 +74,13 @@ final class ConfigService extends Service implements ConfigServiceInterface
}
if ($result->getNumRows() === 0) {
if ($default === null) {
throw new NoSuchItemException(
sprintf(__('Parameter not found (%s)'), $param)
);
}
return $default;
if ($result->getNumRows() === 0 && $default === null) {
throw new NoSuchItemException(
sprintf(__('Parameter not found (%s)'), $param)
);
}
/** @var ConfigData $data */
$data = $result->getData();
return empty($data->value) ? $default : $data->value;
return $result->getData(ConfigModel::class)->getValue() ?? $default;
}
/**
@@ -97,12 +91,13 @@ final class ConfigService extends Service implements ConfigServiceInterface
public function saveBatch(ConfigRequest $configRequest): void
{
try {
$this->transactionAware(
$this->configRepository->transactionAware(
function () use ($configRequest) {
foreach ($configRequest->getData() as $param => $value) {
$this->save($param, $value);
}
}
},
$this
);
} catch (Exception $e) {
processException($e);
@@ -121,47 +116,23 @@ final class ConfigService extends Service implements ConfigServiceInterface
* @throws ConstraintException
* @throws QueryException
*/
public function save(string $param, $value): bool
public function save(string $param, string $value): bool
{
$config = new ConfigModel(['parameter' => $param, 'value' => $value]);
if (!$this->configRepository->has($param)) {
return $this->configRepository->create(new ConfigData($param, $value));
return $this->configRepository->create($config)->getLastId() > 0;
}
return $this->configRepository->update(new ConfigData($param, $value));
return $this->configRepository->update($config)->getAffectedNumRows() === 1;
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigData $configData): int
public function create(ConfigModel $config): int
{
return $this->configRepository->create($configData);
}
/**
* Obtener un array con la configuración almacenada en la BBDD.
*
* @return ConfigData[]
* @throws ConstraintException
* @throws QueryException
*/
public function getAll(): array
{
return $this->configRepository->getAll()->getDataAsArray();
}
/**
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
*/
public function deleteByParam(string $param): void
{
if ($this->configRepository->deleteByParam($param) === 0) {
throw new NoSuchItemException(
sprintf(__('Parameter not found (%s)'), $param)
);
}
return $this->configRepository->create($config)->getLastId();
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,29 +27,27 @@ namespace SP\Domain\Config\Services;
use Exception;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Adapters\ConfigData;
use SP\Domain\Config\Ports\ConfigBackupServiceInterface;
use SP\Domain\Config\Ports\ConfigBackupService;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\SPException;
use SP\Http\JsonResponse;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\File\FileException;
use SP\Util\Util;
use function SP\__u;
use function SP\processException;
/**
* Class ConfigBackupService
*
* @package SP\Domain\Config\Services
* Class ConfigBackup
*/
class ConfigBackupService implements ConfigBackupServiceInterface
class ConfigBackup implements ConfigBackupService
{
private ConfigService $configService;
public function __construct(ConfigService $configService)
public function __construct(private readonly ConfigService $configService)
{
$this->configService = $configService;
}
/**
@@ -82,9 +80,9 @@ class ConfigBackupService implements ConfigBackupServiceInterface
* @throws FileException
* @throws ServiceException
*/
public function restore(ConfigInterface $config): ConfigDataInterface
public function restore(ConfigFileService $configFile): ConfigDataInterface
{
return $config->saveConfig(
return $configFile->saveConfig(
Util::unserialize(ConfigData::class, $this->getBackup())
)->getConfigData();
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,9 +27,9 @@ namespace SP\Domain\Config\Services;
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use Exception;
use SP\Domain\Config\Adapters\ConfigData;
use SP\Domain\Config\Ports\ConfigBackupServiceInterface;
use SP\Domain\Config\Ports\ConfigBackupService;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\AppInfoInterface;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Exceptions\ConfigException;
@@ -45,31 +45,31 @@ use function SP\processException;
defined('APP_ROOT') || die();
/**
* Esta clase es responsable de leer y escribir la configuración del archivo config.php
* Read and write the settings in the definex config file
*/
class ConfigFileService implements ConfigInterface
class ConfigFile implements ConfigFileService
{
/**
* Cache file name
*/
public const CONFIG_CACHE_FILE = CACHE_PATH.DIRECTORY_SEPARATOR.'config.cache';
public const CONFIG_CACHE_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'config.cache';
private static int $timeUpdated;
private static ?ConfigDataInterface $configData = null;
private bool $configLoaded = false;
private ContextInterface $context;
private XmlFileStorageInterface $fileStorage;
private FileCacheInterface $fileCache;
private ConfigBackupServiceInterface $configBackupService;
private static int $timeUpdated;
private static ?ConfigDataInterface $configData = null;
private bool $configLoaded = false;
private ContextInterface $context;
private XmlFileStorageInterface $fileStorage;
private FileCacheInterface $fileCache;
private ConfigBackupService $configBackupService;
/**
* @throws ConfigException
*/
public function __construct(
XmlFileStorageInterface $fileStorage,
FileCacheInterface $fileCache,
ContextInterface $context,
ConfigBackupServiceInterface $configBackupService
FileCacheInterface $fileCache,
ContextInterface $context,
ConfigBackupService $configBackupService
) {
$this->fileCache = $fileCache;
$this->fileStorage = $fileStorage;
@@ -159,12 +159,11 @@ class ConfigFileService implements ConfigInterface
$configData = new ConfigData();
foreach ($items as $item => $value) {
$methodName = 'set'.ucfirst($item);
$methodName = 'set' . ucfirst($item);
if (method_exists($configData, $methodName)) {
$configData->$methodName($value);
}
}
return $configData;
@@ -174,12 +173,12 @@ class ConfigFileService implements ConfigInterface
* Guardar la configuración
*
* @param ConfigDataInterface $configData
* @param bool|null $backup
* @param bool|null $backup
*
* @return ConfigInterface
* @return ConfigFileService
* @throws FileException
*/
public function saveConfig(ConfigDataInterface $configData, ?bool $backup = true): ConfigInterface
public function saveConfig(ConfigDataInterface $configData, ?bool $backup = true): ConfigFileService
{
if ($backup) {
$this->configBackupService->backup($configData);
@@ -209,7 +208,7 @@ class ConfigFileService implements ConfigInterface
/**
* Commits a config data
*/
public function updateConfig(ConfigDataInterface $configData): ConfigInterface
public function updateConfig(ConfigDataInterface $configData): ConfigFileService
{
$configData->setConfigDate(time());
$configData->setConfigSaver($this->context->getUserData()->getLogin());
@@ -262,7 +261,7 @@ class ConfigFileService implements ConfigInterface
* @throws FileException
* @throws EnvironmentIsBrokenException
*/
public function generateUpgradeKey(): ConfigInterface
public function generateUpgradeKey(): ConfigFileService
{
if (empty(self::$configData->getUpgradeKey())) {
logger('Generating upgrade key');

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -30,8 +30,8 @@ use SP\Core\Crypt\Hash;
use SP\Domain\Account\Ports\AccountCryptServiceInterface;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Services\ConfigService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Config\Services\Config;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Crypt\Ports\MasterPassServiceInterface;
@@ -49,15 +49,15 @@ final class MasterPassService extends Service implements MasterPassServiceInterf
public const PARAM_MASTER_PASS_TIME = 'lastupdatempass';
public const PARAM_MASTER_PASS_HASH = 'masterPwd';
protected ConfigService $configService;
protected Config $configService;
protected AccountCryptServiceInterface $accountCryptService;
protected CustomFieldCryptService $customFieldCryptService;
public function __construct(
Application $application,
ConfigServiceInterface $configService,
AccountCryptServiceInterface $accountCryptService,
CustomFieldCryptServiceInterface $customFieldCryptService
Application $application,
ConfigService $configService,
AccountCryptServiceInterface $accountCryptService,
CustomFieldCryptServiceInterface $customFieldCryptService
) {
parent::__construct($application);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -35,7 +35,7 @@ use SP\Core\Messages\MailMessage;
use SP\DataModel\Dto\ConfigRequest;
use SP\Domain\Common\Services\Service;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\AppInfoInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
@@ -67,14 +67,14 @@ final class TemporaryMasterPassService extends Service
public const PARAM_MAX_TIME = 'tempmaster_maxtime';
public const PARAM_ATTEMPTS = 'tempmaster_attempts';
private ConfigServiceInterface $configService;
private UserServiceInterface $userService;
private ConfigService $configService;
private UserServiceInterface $userService;
private MailServiceInterface $mailService;
private ?int $maxTime = null;
public function __construct(
Application $application,
ConfigServiceInterface $configService,
Application $application,
ConfigService $configService,
UserServiceInterface $userService,
MailServiceInterface $mailService
) {

View File

@@ -5,7 +5,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -32,7 +32,7 @@ use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\AppInfoInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
use SP\Domain\Core\Exceptions\CheckException;
@@ -59,7 +59,7 @@ final class FileBackupService implements FileBackupServiceInterface
'#^(?:[A-Z]:)?(?:/(?!(\.git|backup|cache|temp|vendor|tests))[^/]+)+/[^/]+\.\w+$#Di';
private EventDispatcherInterface $eventDispatcher;
private ConfigInterface $config;
private ConfigFileService $config;
private ConfigDataInterface $configData;
private ?string $backupPath = null;

View File

@@ -5,7 +5,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -28,7 +28,7 @@ namespace SP\Domain\Import\Services;
use Exception;
use SP\Core\Application;
use SP\Domain\Common\Services\Service;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Import\Ports\ImportServiceInterface;
use SP\Infrastructure\Database\DatabaseInterface;
@@ -54,14 +54,14 @@ final class ImportService extends Service implements ImportServiceInterface
private ?ImportParams $importParams = null;
private ?FileImportInterface $fileImport = null;
private Application $application;
private ImportHelper $importHelper;
private ConfigServiceInterface $configService;
private DatabaseInterface $database;
private ImportHelper $importHelper;
private ConfigService $configService;
private DatabaseInterface $database;
public function __construct(
Application $application,
ImportHelper $importHelper,
ConfigServiceInterface $configService,
Application $application,
ImportHelper $importHelper,
ConfigService $configService,
DatabaseInterface $database
) {
parent::__construct($application);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -25,7 +25,7 @@
namespace SP\Domain\Import\Services;
use SP\Core\Application;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\SPException;
defined('APP_ROOT') || die();
@@ -40,19 +40,19 @@ final class XmlImport implements XmlImportInterface
{
private XmlFileImport $xmlFileImport;
private ImportParams $importParams;
private ImportHelper $importHelper;
private ConfigServiceInterface $configService;
private Application $application;
private ImportHelper $importHelper;
private ConfigService $configService;
private Application $application;
/**
* XmlImport constructor.
*/
public function __construct(
Application $application,
ImportHelper $importHelper,
ConfigServiceInterface $configService,
XmlFileImportInterface $xmlFileImport,
ImportParams $importParams
Application $application,
ImportHelper $importHelper,
ConfigService $configService,
XmlFileImportInterface $xmlFileImport,
ImportParams $importParams
) {
$this->application = $application;
$this->importHelper = $importHelper;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -29,7 +29,7 @@ use DOMElement;
use SP\Core\Application;
use SP\Core\Events\EventDispatcher;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\SPException;
/**
@@ -43,19 +43,19 @@ abstract class XmlImportBase
protected XmlFileImportInterface $xmlFileImport;
protected DOMDocument $xmlDOM;
protected EventDispatcher $eventDispatcher;
protected ConfigServiceInterface $configService;
protected ConfigDataInterface $configData;
protected EventDispatcher $eventDispatcher;
protected ConfigService $configService;
protected ConfigDataInterface $configData;
/**
* ImportBase constructor.
*/
public function __construct(
Application $application,
ImportHelper $importHelper,
ConfigServiceInterface $configService,
Application $application,
ImportHelper $importHelper,
ConfigService $configService,
XmlFileImportInterface $xmlFileImport,
ImportParams $importParams
ImportParams $importParams
) {
$this->eventDispatcher = $application->getEventDispatcher();
$this->xmlFileImport = $xmlFileImport;

View File

@@ -5,7 +5,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,14 +27,14 @@ namespace SP\Domain\Install\Services;
use Exception;
use SP\Core\Crypt\Hash;
use SP\DataModel\ConfigData;
use SP\DataModel\ProfileData;
use SP\DataModel\UserData;
use SP\DataModel\UserGroupData;
use SP\DataModel\UserProfileData;
use SP\Domain\Config\Models\Config;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\InvalidArgumentException;
use SP\Domain\Core\Exceptions\QueryException;
@@ -65,25 +65,25 @@ final class InstallerService implements InstallerServiceInterface
public const VERSION_TEXT = '4.0';
public const BUILD = 21031301;
private RequestInterface $request;
private ConfigInterface $config;
private UserServiceInterface $userService;
private UserGroupServiceInterface $userGroupService;
private UserProfileServiceInterface $userProfileService;
private ConfigServiceInterface $configService;
private DatabaseConnectionData $databaseConnectionData;
private DatabaseSetupInterface $databaseSetup;
private ?InstallData $installData = null;
private RequestInterface $request;
private ConfigFileService $config;
private UserServiceInterface $userService;
private UserGroupServiceInterface $userGroupService;
private UserProfileServiceInterface $userProfileService;
private ConfigService $configService;
private DatabaseConnectionData $databaseConnectionData;
private DatabaseSetupInterface $databaseSetup;
private ?InstallData $installData = null;
public function __construct(
RequestInterface $request,
ConfigInterface $config,
UserServiceInterface $userService,
UserGroupServiceInterface $userGroupService,
UserProfileServiceInterface $userProfileService,
ConfigServiceInterface $configService,
DatabaseConnectionData $databaseConnectionData,
DatabaseSetupInterface $databaseSetup
RequestInterface $request,
ConfigFileService $config,
UserServiceInterface $userService,
UserGroupServiceInterface $userGroupService,
UserProfileServiceInterface $userProfileService,
ConfigService $configService,
DatabaseConnectionData $databaseConnectionData,
DatabaseSetupInterface $databaseSetup
) {
$this->request = $request;
$this->config = $config;
@@ -155,7 +155,8 @@ final class InstallerService implements InstallerServiceInterface
}
if (IS_TESTING
&& empty($this->installData->getDbAdminPass())) {
&& empty($this->installData->getDbAdminPass())
) {
throw new InvalidArgumentException(
__u('Please, enter the database password'),
SPException::ERROR,
@@ -203,7 +204,12 @@ final class InstallerService implements InstallerServiceInterface
$this->saveMasterPassword();
$this->createAdminAccount();
$this->configService->create(new ConfigData('version', VersionUtil::getVersionStringNormalized()));
$this->configService->create(
new Config([
'parameter' => 'version',
'value' => VersionUtil::getVersionStringNormalized()
])
);
$configData->setInstalled(true);
@@ -281,7 +287,7 @@ final class InstallerService implements InstallerServiceInterface
}
/**
* @param ConfigDataInterface $configData
* @param ConfigDataInterface $configData
*/
private function setupDb(ConfigDataInterface $configData): void
{
@@ -318,10 +324,15 @@ final class InstallerService implements InstallerServiceInterface
{
try {
$this->configService->create(
new ConfigData('masterPwd', Hash::hashKey($this->installData->getMasterPassword()))
new Config(
[
'parameter' => 'masterPwd',
'value' => Hash::hashKey($this->installData->getMasterPassword())
]
)
);
$this->configService->create(
new ConfigData('lastupdatempass', time())
new Config(['parameter' => 'lastupdatempass', 'value' => time()])
);
} catch (Exception $e) {
processException($e);
@@ -345,12 +356,12 @@ final class InstallerService implements InstallerServiceInterface
$userProfileData = new UserProfileData(['name' => 'Admin', 'profile' => new ProfileData()]);
$userData = new UserData([
'userGroupId' => $this->userGroupService->create($userGroupData),
'userProfileId' => $this->userProfileService->create($userProfileData),
'login' => $this->installData->getAdminLogin(),
'name' => 'sysPass Admin',
'isAdminApp' => 1,
]);
'userGroupId' => $this->userGroupService->create($userGroupData),
'userProfileId' => $this->userProfileService->create($userProfileData),
'login' => $this->installData->getAdminLogin(),
'name' => 'sysPass Admin',
'isAdminApp' => 1,
]);
$id = $this->userService->createWithMasterPass(
$userData,

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -25,7 +25,7 @@
namespace SP\Domain\Upgrade\Services;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Infrastructure\File\FileException;
use SP\Util\VersionUtil;
@@ -55,7 +55,7 @@ final class UpgradeUtil
/**
* @throws FileException
*/
public static function fixAppUpgrade(ConfigDataInterface $configData, ConfigInterface $config): void
public static function fixAppUpgrade(ConfigDataInterface $configData, ConfigFileService $config): void
{
// Fixes bug in 3.0.X version where some updates weren't applied
// when upgrading from v2

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -31,8 +31,8 @@ use SP\Core\Crypt\Hash;
use SP\DataModel\UserLoginData;
use SP\Domain\Common\Services\Service;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
@@ -60,13 +60,13 @@ final class UserPassService extends Service implements UserPassServiceInterface
public const MPASS_CHECKOLD = 4;
private ConfigDataInterface $configData;
private UserRepository $userRepository;
private ConfigServiceInterface $configService;
private UserRepository $userRepository;
private ConfigService $configService;
public function __construct(
Application $application,
UserRepositoryInterface $userRepository,
ConfigServiceInterface $configService
ConfigService $configService
) {
parent::__construct($application);
@@ -161,7 +161,7 @@ final class UserPassService extends Service implements UserPassServiceInterface
public function makeKeyForUser(string $userLogin, string $userPass): string
{
// Use always the most recent config data
if (ConfigFileService::getTimeUpdated() > $this->configData->getConfigDate()) {
if (ConfigFile::getTimeUpdated() > $this->configData->getConfigDate()) {
return trim($userPass.$userLogin.$this->config->getConfigData()->getPasswordSalt());
}

View File

@@ -0,0 +1,129 @@
<?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\Config\Repositories;
use SP\Domain\Config\Models\Config as ConfigModel;
use SP\Domain\Config\Ports\ConfigRepository;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Common\Repositories\Repository;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
/**
* Class Config
*
* @template T of ConfigModel
*/
final class Config extends Repository implements ConfigRepository
{
public const TABLE = 'Config';
/**
* @param ConfigModel $config
*
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function update(ConfigModel $config): QueryResult
{
$query = $this->queryFactory
->newUpdate()
->table(self::TABLE)
->cols($config->toArray(['value']))
->where('parameter = :parameter')
->limit(1)
->bindValues(
[
'value' => $config->getValue(),
'parameter' => $config->getParameter()
]
);
$queryData = QueryData::build($query);
return $this->db->doQuery($queryData);
}
/**
* @param ConfigModel $config
*
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigModel $config): QueryResult
{
$query = $this->queryFactory
->newInsert()
->into(self::TABLE)
->cols($config->toArray());
$queryData = QueryData::build($query);
return $this->db->doQuery($queryData);
}
/**
* @param string $param
*
* @return QueryResult<T>
*/
public function getByParam(string $param): QueryResult
{
$query = $this->queryFactory
->newSelect()
->from(self::TABLE)
->cols(ConfigModel::getCols())
->where('parameter = :parameter')
->bindValues(['parameter' => $param])
->limit(1);
$queryData = QueryData::buildWithMapper($query, ConfigModel::class);
return $this->db->doSelect($queryData);
}
/**
* @param string $param
*
* @return bool
*/
public function has(string $param): bool
{
$query = $this->queryFactory
->newSelect()
->from(self::TABLE)
->cols(ConfigModel::getCols(['value']))
->where('parameter = :parameter')
->bindValues(['parameter' => $param])
->limit(1);
$queryData = QueryData::build($query);
return $this->db->doSelect($queryData)->getNumRows() === 1;
}
}

View File

@@ -1,136 +0,0 @@
<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2022, 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\Config\Repositories;
use SP\DataModel\ConfigData;
use SP\Domain\Config\Ports\ConfigRepositoryInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Common\Repositories\Repository;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
/**
* Class ConfigRepository
*
* @package SP\Infrastructure\Common\Repositories\Config
*/
final class ConfigRepository extends Repository implements ConfigRepositoryInterface
{
/**
* @param ConfigData $configData
*
* @return bool
* @throws ConstraintException
* @throws QueryException
*/
public function update(ConfigData $configData): bool
{
$queryData = new QueryData();
$queryData->setQuery('UPDATE Config SET `value` = ? WHERE parameter = ?');
$queryData->setParams([$configData->getValue(), $configData->getParameter()]);
return $this->db->doQuery($queryData)->getAffectedNumRows();
}
/**
* @param ConfigData $configData
*
* @return int
* @throws ConstraintException
* @throws QueryException
*/
public function create(ConfigData $configData): int
{
$queryData = new QueryData();
$queryData->setQuery('INSERT INTO Config SET parameter = ?, `value` = ?');
$queryData->setParams([$configData->getParameter(), $configData->getValue()]);
return $this->db->doQuery($queryData)->getAffectedNumRows();
}
/**
* Obtener un array con la configuración almacenada en la BBDD.
*
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function getAll(): QueryResult
{
$queryData = new QueryData();
$queryData->setQuery('SELECT parameter, `value` FROM Config ORDER BY parameter');
return $this->db->doSelect($queryData);
}
/**
* @param string $param
*
* @return QueryResult
* @throws ConstraintException
* @throws QueryException
*/
public function getByParam(string $param): QueryResult
{
$queryData = new QueryData();
$queryData->setQuery('SELECT parameter, `value` FROM Config WHERE parameter = ? LIMIT 1');
$queryData->addParam($param);
return $this->db->doSelect($queryData);
}
/**
* @param string $param
*
* @return bool
* @throws ConstraintException
* @throws QueryException
*/
public function has(string $param): bool
{
$queryData = new QueryData();
$queryData->setQuery('SELECT parameter FROM Config WHERE parameter = ? LIMIT 1');
$queryData->addParam($param);
return $this->db->doSelect($queryData)->getNumRows() === 1;
}
/**
* @param string $param
*
* @return int
* @throws ConstraintException
* @throws QueryException
*/
public function deleteByParam(string $param): int
{
$queryData = new QueryData();
$queryData->setQuery('DELETE FROM Config WHERE parameter = ? LIMIT 1');
$queryData->addParam($param);
return $this->db->doQuery($queryData)->getAffectedNumRows();
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -25,7 +25,7 @@
namespace SP\Providers;
use SP\Core\Application;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
@@ -36,8 +36,8 @@ use SP\Domain\Core\Events\EventDispatcherInterface;
*/
abstract class Provider implements ProviderInterface
{
protected readonly ConfigInterface $config;
protected readonly ContextInterface $context;
protected readonly ConfigFileService $config;
protected readonly ContextInterface $context;
protected readonly EventDispatcherInterface $eventDispatcher;
protected bool $initialized = false;

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -31,8 +31,8 @@ use DI\NotFoundException;
use PHPUnit\Framework\TestCase;
use SP\Core\Context\ContextException;
use SP\Domain\Config\Adapters\ConfigData;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Infrastructure\File\FileException;
use function SPT\getResource;
@@ -85,11 +85,11 @@ class ConfigTest extends TestCase
* @throws DependencyException
* @throws NotFoundException
*/
public function testLoadClass(): ConfigInterface
public function testLoadClass(): ConfigFileService
{
$config = self::$dic->get(ConfigFileService::class);
$config = self::$dic->get(ConfigFile::class);
$this->assertInstanceOf(ConfigFileService::class, $config);
$this->assertInstanceOf(ConfigFile::class, $config);
$this->assertFileExists(CONFIG_FILE);
return $config;
@@ -100,11 +100,11 @@ class ConfigTest extends TestCase
*
* @depends testLoadClass
*
* @param ConfigInterface $config
* @param ConfigFileService $config
*
* @throws FileException
*/
public function testSaveConfig(ConfigInterface $config)
public function testSaveConfig(ConfigFileService $config)
{
$config->saveConfig($config->getConfigData(), false);
@@ -117,9 +117,9 @@ class ConfigTest extends TestCase
*
* @depends testLoadClass
*
* @param ConfigInterface $config
* @param ConfigFileService $config
*/
public function testLoadConfig(ConfigInterface $config)
public function testLoadConfig(ConfigFileService $config)
{
$this->assertInstanceOf(ConfigData::class, $config->loadConfig());
}
@@ -129,13 +129,13 @@ class ConfigTest extends TestCase
*
* @depends testLoadClass
*
* @param ConfigInterface $config
* @param ConfigFileService $config
*/
public function testUpdateConfig(ConfigInterface $config)
public function testUpdateConfig(ConfigFileService $config)
{
$config->updateConfig($config->getConfigData());
$this->assertEquals(ConfigFileService::getTimeUpdated(), $config->getConfigData()->getConfigDate());
$this->assertEquals(ConfigFile::getTimeUpdated(), $config->getConfigData()->getConfigDate());
}
/**
@@ -143,12 +143,12 @@ class ConfigTest extends TestCase
*
* @depends testLoadClass
*
* @param ConfigInterface $config
* @param ConfigFileService $config
*
* @throws EnvironmentIsBrokenException
* @throws FileException
*/
public function testGenerateUpgradeKey(ConfigInterface $config)
public function testGenerateUpgradeKey(ConfigFileService $config)
{
$config->generateUpgradeKey();

View File

@@ -37,7 +37,7 @@ trait AccountSearchTokenizerDataTrait
$faker = Factory::create();
$id = $faker->numberBetween(1);
$name = $faker->userName;
$file = sprintf('%s.%s', $faker->name(), $faker->fileExtension);
$file = sprintf('%s.%s', $faker->name(), $faker->fileExtension());
$conditions = [
sprintf('id:%d', $id),
@@ -46,7 +46,7 @@ trait AccountSearchTokenizerDataTrait
sprintf('file:"%s"', $file),
sprintf('owner:"%s"', $name),
sprintf('maingroup:"%s"', $name),
sprintf('clientService:"%s"', $name),
sprintf('client:"%s"', $name),
sprintf('category:"%s"', $name),
sprintf('name_regex:"^%s$"', $name),
];
@@ -64,14 +64,14 @@ trait AccountSearchTokenizerDataTrait
[
implode(' ', $conditions),
[
AccountSearchConstants::FILTER_ACCOUNT_ID => $id,
AccountSearchConstants::FILTER_USER_NAME => $name,
AccountSearchConstants::FILTER_GROUP_NAME => $name,
AccountSearchConstants::FILTER_FILE_NAME => $file,
AccountSearchConstants::FILTER_OWNER => $name,
AccountSearchConstants::FILTER_MAIN_GROUP => $name,
AccountSearchConstants::FILTER_CLIENT_NAME => $name,
AccountSearchConstants::FILTER_CATEGORY_NAME => $name,
AccountSearchConstants::FILTER_ACCOUNT_ID => $id,
AccountSearchConstants::FILTER_USER_NAME => $name,
AccountSearchConstants::FILTER_GROUP_NAME => $name,
AccountSearchConstants::FILTER_FILE_NAME => $file,
AccountSearchConstants::FILTER_OWNER => $name,
AccountSearchConstants::FILTER_MAIN_GROUP => $name,
AccountSearchConstants::FILTER_CLIENT_NAME => $name,
AccountSearchConstants::FILTER_CATEGORY_NAME => $name,
AccountSearchConstants::FILTER_ACCOUNT_NAME_REGEX => sprintf('^%s$', $name),
],
],
@@ -97,8 +97,8 @@ trait AccountSearchTokenizerDataTrait
{
$conditions = [
'test string' => null,
'op:and' => 'and',
'op:or' => 'or',
'op:and' => 'and',
'op:or' => 'or',
];
return [
@@ -116,9 +116,9 @@ trait AccountSearchTokenizerDataTrait
$faker->streetAddress,
$faker->name,
$faker->userName,
$faker->catchPhrase,
$faker->sentence,
$faker->ipv4,
$faker->bankAccountNumber,
$faker->iban,
$faker->companyEmail,
$faker->domainName,
];

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -207,7 +207,7 @@ class AccountSearchServiceTest extends UnitaryTestCase
$this->accountSearchService->getByFilter($accountSearchFilter);
}
private function buildExpectationForCondition(string $condition)
private function buildExpectationForCondition(string $condition): void
{
switch ($condition) {
case AccountSearchConstants::FILTER_IS_EXPIRED:
@@ -241,17 +241,21 @@ class AccountSearchServiceTest extends UnitaryTestCase
$userService = $this->createMock(UserServiceInterface::class);
$userService
->method('getByLogin')
->willReturn(new UserData([
'id' => self::$faker->randomNumber(),
'userGroupId' => self::$faker->randomNumber(),
]));
->willReturn(
new UserData([
'id' => self::$faker->randomNumber(),
'userGroupId' => self::$faker->randomNumber(),
])
);
$userGroupService = $this->createMock(UserGroupServiceInterface::class);
$userGroupService
->method('getByName')
->willReturn(new UserGroupData([
'id' => self::$faker->randomNumber(),
]));
->willReturn(
new UserGroupData([
'id' => self::$faker->randomNumber(),
])
);
$this->accountSearchRepository = $this->createMock(AccountSearchRepositoryInterface::class);
$this->accountSearchDataBuilder = $this->createMock(AccountSearchDataBuilderInterface::class);
@@ -264,5 +268,4 @@ class AccountSearchServiceTest extends UnitaryTestCase
$this->accountSearchDataBuilder
);
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -44,7 +44,7 @@ use SP\Domain\Account\Ports\AccountToUserRepositoryInterface;
use SP\Domain\Account\Services\AccountService;
use SP\Domain\Common\Models\Simple;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
@@ -70,7 +70,7 @@ class AccountServiceTest extends UnitaryTestCase
private AccountToTagRepositoryInterface|MockObject $accountToTagRepository;
private ItemPresetServiceInterface|MockObject $itemPresetService;
private AccountHistoryServiceInterface|MockObject $accountHistoryService;
private ConfigServiceInterface|MockObject $configService;
private ConfigService|MockObject $configService;
private AccountCryptServiceInterface|MockObject $accountCryptService;
private AccountPresetServiceInterface|MockObject $accountPresetService;
private AccountItemsServiceInterface|MockObject $accountItemsService;
@@ -1332,7 +1332,7 @@ class AccountServiceTest extends UnitaryTestCase
$this->accountToTagRepository = $this->createMock(AccountToTagRepositoryInterface::class);
$this->itemPresetService = $this->createMock(ItemPresetServiceInterface::class);
$this->accountHistoryService = $this->createMock(AccountHistoryServiceInterface::class);
$this->configService = $this->createMock(ConfigServiceInterface::class);
$this->configService = $this->createMock(ConfigService::class);
$this->accountCryptService = $this->createMock(AccountCryptServiceInterface::class);
$this->accountItemsService = $this->createMock(AccountItemsServiceInterface::class);
$this->accountPresetService = $this->createMock(AccountPresetServiceInterface::class);

View File

@@ -0,0 +1,222 @@
<?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 SPT\Domain\Config\Services;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Adapters\ConfigData;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Config\Services\ConfigBackup;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\File\FileException;
use SPT\UnitaryTestCase;
/**
* Class ConfigBackupTest
*
* @group unitary
*/
class ConfigBackupTest extends UnitaryTestCase
{
private ConfigService|MockObject $configService;
private ConfigBackup $configBackupService;
public function testBackup()
{
$this->configService
->expects(self::exactly(2))
->method('save')
->with(
...
self::withConsecutive(
['config_backup', self::isType('string')],
['config_backup_date', self::isType('string')]
)
);
$this->configBackupService->backup($this->config->getConfigData());
}
public function testBackupError()
{
$this->configService
->expects(self::once())
->method('save')
->with('config_backup', self::anything())
->willThrowException(new SPException('test'));
$this->configBackupService->backup($this->config->getConfigData());
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
*/
public function testRestore()
{
$configData = new ConfigData();
$configFile = $this->createMock(ConfigFileService::class);
$configFile->expects(self::once())
->method('saveConfig')
->with($configData)
->willReturn($configFile);
$hexConfigData = bin2hex(gzcompress(serialize($configData)));
$this->configService->expects(self::once())
->method('getByParam')
->with('config_backup')
->willReturn($hexConfigData);
$configFile->expects(self::once())
->method('getConfigData')
->willReturn($configData);
$out = $this->configBackupService->restore($configFile);
$this->assertEquals($configData, $out);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
*/
public function testRestoreWithNullData()
{
$configFile = $this->createMock(ConfigFileService::class);
$configFile->expects(self::never())
->method('saveConfig');
$this->configService->expects(self::once())
->method('getByParam')
->with('config_backup')
->willReturn(null);
$configFile->expects(self::never())
->method('getConfigData');
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Unable to restore the configuration');
$this->configBackupService->restore($configFile);
}
/**
* @throws Exception
* @throws ServiceException
* @throws FileException
*/
public function testRestoreWithMissingParam()
{
$configFile = $this->createMock(ConfigFileService::class);
$configFile->expects(self::never())
->method('saveConfig');
$this->configService->expects(self::once())
->method('getByParam')
->willThrowException(new NoSuchItemException('test'));
$configFile->expects(self::never())
->method('getConfigData');
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Unable to restore the configuration');
$this->configBackupService->restore($configFile);
}
/**
* @throws SPException
*/
public function testConfigToJson()
{
$this->assertNotEmpty(ConfigBackup::configToJson(serialize($this->config->getConfigData())));
}
/**
* @throws ServiceException
*/
public function testGetBackup()
{
$configData = new ConfigData();
$hexConfigData = bin2hex(gzcompress(serialize($configData)));
$this->configService->expects(self::once())
->method('getByParam')
->with('config_backup')
->willReturn($hexConfigData);
$out = unserialize($this->configBackupService->getBackup());
$this->assertEquals($configData, $out);
}
public function testGetBackupWithNullData()
{
$this->configService->expects(self::once())
->method('getByParam')
->with('config_backup')
->willReturn(null);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Unable to restore the configuration');
$this->configBackupService->getBackup();
}
public function testGetBackupWithMissingParam()
{
$this->configService->expects(self::once())
->method('getByParam')
->willThrowException(new NoSuchItemException('test'));
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Unable to restore the configuration');
$this->configBackupService->getBackup();
}
protected function setUp(): void
{
parent::setUp();
$this->configService = $this->createMock(ConfigService::class);
$this->configBackupService = new ConfigBackup($this->configService);
}
}

View File

@@ -0,0 +1,286 @@
<?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 SPT\Domain\Config\Services;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
use RuntimeException;
use SP\DataModel\Dto\ConfigRequest;
use SP\Domain\Common\Services\ServiceException;
use SP\Domain\Config\Ports\ConfigRepository;
use SP\Domain\Config\Services\Config;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Infrastructure\Common\Repositories\NoSuchItemException;
use SP\Infrastructure\Database\QueryResult;
use SPT\Generators\ConfigGenerator;
use SPT\UnitaryTestCase;
/**
* Class ConfigTest
*
* @group unitary
*/
class ConfigTest extends UnitaryTestCase
{
private MockObject|ConfigRepository $configRepository;
private Config $configService;
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testCreate()
{
$config = ConfigGenerator::factory()->buildConfig();
$queryResult = new QueryResult();
$queryResult->setLastId(self::$faker->randomNumber());
$this->configRepository
->expects(self::once())
->method('create')
->with($config)
->willReturn($queryResult);
$out = $this->configService->create($config);
$this->assertEquals($queryResult->getLastId(), $out);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testSaveWithNoExistingParameter()
{
$config = ConfigGenerator::factory()->buildConfig();
$this->configRepository
->expects(self::once())
->method('has')
->with($config->getParameter())
->willReturn(false);
$queryResult = new QueryResult();
$queryResult->setLastId(self::$faker->randomNumber());
$this->configRepository
->expects(self::once())
->method('create')
->with($config)
->willReturn($queryResult);
$this->configService->save($config->getParameter(), $config->getValue());
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testSaveWithExistingParameter()
{
$config = ConfigGenerator::factory()->buildConfig();
$this->configRepository
->expects(self::once())
->method('has')
->with($config->getParameter())
->willReturn(true);
$this->configRepository
->expects(self::once())
->method('update')
->with($config)
->willReturn(new QueryResult([1]));
$this->configService->save($config->getParameter(), $config->getValue());
}
/**
* @throws NoSuchItemException
* @throws ServiceException
* @throws SPException
*/
public function testGetByParam()
{
$parameter = self::$faker->colorName();
$config = ConfigGenerator::factory()->buildConfig();
$this->configRepository
->expects(self::once())
->method('getByParam')
->with($parameter)
->willReturn(new QueryResult([$config]));
$out = $this->configService->getByParam($parameter);
$this->assertEquals($config->getValue(), $out);
}
/**
* @throws NoSuchItemException
* @throws ServiceException
* @throws SPException
*/
public function testGetByParamWithDefaultValue()
{
$parameter = self::$faker->colorName();
$config = ConfigGenerator::factory()->buildConfig()->mutate(['value' => null]);
$this->configRepository
->expects(self::once())
->method('getByParam')
->with($parameter)
->willReturn(new QueryResult([$config]));
$out = $this->configService->getByParam($parameter, 'test');
$this->assertEquals('test', $out);
}
/**
* @throws NoSuchItemException
* @throws ServiceException
* @throws SPException
*/
public function testGetByParamWithNoFound()
{
$parameter = self::$faker->colorName();
$this->configRepository
->expects(self::once())
->method('getByParam')
->with($parameter)
->willReturn(new QueryResult([]));
$this->expectException(NoSuchItemException::class);
$this->expectExceptionMessage(sprintf('Parameter not found (%s)', $parameter));
$this->configService->getByParam($parameter);
}
/**
* @throws NoSuchItemException
* @throws ServiceException
* @throws SPException
*/
public function testGetByParamWithException()
{
$parameter = self::$faker->colorName();
$this->configRepository
->expects(self::once())
->method('getByParam')
->with($parameter)
->willThrowException(new RuntimeException('test'));
$this->expectException(SPException::class);
$this->expectExceptionMessage('test');
$this->configService->getByParam($parameter);
}
/**
* @throws ServiceException
*/
public function testSaveBatch()
{
$this->configRepository
->expects(self::once())
->method('transactionAware')
->with(
new Callback(function (callable $closure) {
$closure();
return true;
})
);
$this->configRepository
->expects(self::exactly(3))
->method('has')
->with(self::anything())
->willReturn(true);
$this->configRepository
->expects(self::exactly(3))
->method('update')
->with(self::anything())
->willReturn(new QueryResult([1]));
$configRequest = new ConfigRequest();
$configRequest->add(self::$faker->colorName, self::$faker->text);
$configRequest->add(self::$faker->colorName, self::$faker->text);
$configRequest->add(self::$faker->colorName, self::$faker->text);
$this->configService->saveBatch($configRequest);
}
/**
* @throws ServiceException
*/
public function testSaveBatchWithException()
{
$this->configRepository
->expects(self::once())
->method('transactionAware')
->with(self::anything())
->willThrowException(new RuntimeException('test'));
$this->configRepository
->expects(self::never())
->method('has')
->with(self::anything());
$this->configRepository
->expects(self::never())
->method('update')
->with(self::anything());
$configRequest = new ConfigRequest();
$configRequest->add(self::$faker->colorName, self::$faker->text);
$configRequest->add(self::$faker->colorName, self::$faker->text);
$configRequest->add(self::$faker->colorName, self::$faker->text);
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('test');
$this->configService->saveBatch($configRequest);
}
protected function setUp(): void
{
parent::setUp();
$this->configRepository = $this->createMock(ConfigRepository::class);
$this->configService = new Config($this->application, $this->configRepository);
}
}

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,7 +27,7 @@ namespace SPT\Domain\Install\Services;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use SP\Domain\Config\Ports\ConfigServiceInterface;
use SP\Domain\Config\Ports\ConfigService;
use SP\Domain\Core\Exceptions\InvalidArgumentException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Http\RequestInterface;
@@ -63,7 +63,7 @@ class InstallerTest extends UnitaryTestCase
*/
private $request;
/**
* @var MockObject|ConfigServiceInterface
* @var MockObject|ConfigService
*/
private $configService;
/**
@@ -440,7 +440,7 @@ class InstallerTest extends UnitaryTestCase
$this->databaseSetup = $this->createMock(DatabaseSetupInterface::class);
$this->userService = $this->createMock(UserServiceInterface::class);
$this->request = $this->createStub(RequestInterface::class);
$this->configService = $this->createMock(ConfigServiceInterface::class);
$this->configService = $this->createMock(ConfigService::class);
$this->userGroupService = $this->createMock(UserGroupServiceInterface::class);
$this->userProfileService = $this->createMock(UserProfileServiceInterface::class);

View File

@@ -0,0 +1,47 @@
<?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 SPT\Generators;
use SP\Domain\Config\Models\Config;
/**
* Class ConfigGenerator
*/
final class ConfigGenerator extends DataGenerator
{
public function buildConfig(): Config
{
return new Config($this->configProperties());
}
private function configProperties(): array
{
return [
'parameter' => $this->faker->colorName(),
'value' => $this->faker->text()
];
}
}

View File

@@ -81,7 +81,7 @@ class ClientRepositoryTest extends UnitaryTestCase
}
);
$callbackUpdate = new Callback(
$callbackCreate = new Callback(
static function (QueryData $arg) use ($client) {
$query = $arg->getQuery();
$params = $query->getBindValues();
@@ -99,7 +99,7 @@ class ClientRepositoryTest extends UnitaryTestCase
$this->database
->expects(self::exactly(2))
->method('doQuery')
->with(...self::withConsecutive([$callbackDuplicate], [$callbackUpdate]))
->with(...self::withConsecutive([$callbackDuplicate], [$callbackCreate]))
->willReturn(new QueryResult([]), new QueryResult([1]));
$this->clientRepository->create($client);

View File

@@ -0,0 +1,185 @@
<?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 SPT\Infrastructure\Config\Repositories;
use Aura\SqlQuery\Common\InsertInterface;
use Aura\SqlQuery\Common\SelectInterface;
use Aura\SqlQuery\Common\UpdateInterface;
use Aura\SqlQuery\QueryFactory;
use PHPUnit\Framework\Constraint\Callback;
use SP\Domain\Common\Models\Simple;
use SP\Domain\Config\Models\Config as ConfigModel;
use SP\Domain\Core\Exceptions\ConstraintException;
use SP\Domain\Core\Exceptions\QueryException;
use SP\Infrastructure\Config\Repositories\Config;
use SP\Infrastructure\Database\DatabaseInterface;
use SP\Infrastructure\Database\QueryData;
use SP\Infrastructure\Database\QueryResult;
use SPT\Generators\ConfigGenerator;
use SPT\UnitaryTestCase;
/**
* Class ConfigTest
*
* @group unitary
*/
class ConfigTest extends UnitaryTestCase
{
private Config $configRepository;
public function testGetByParam()
{
$param = self::$faker->colorName();
$callback = new Callback(
static function (QueryData $arg) use ($param) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['parameter'] === $param
&& $arg->getMapClassName() === ConfigModel::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doSelect')
->with($callback);
$this->configRepository->getByParam($param);
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testCreate()
{
$config = ConfigGenerator::factory()->buildConfig();
$callback = new Callback(
static function (QueryData $arg) use ($config) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 2
&& $params['parameter'] === $config->getParameter()
&& $params['value'] === $config->getValue()
&& is_a($query, InsertInterface::class)
&& !empty($query->getStatement());
}
);
$queryResult = new QueryResult([1]);
$this->database
->expects(self::exactly(1))
->method('doQuery')
->with($callback)
->willReturn($queryResult);
$out = $this->configRepository->create($config);
$this->assertEquals($queryResult, $out);
}
public function testHas()
{
$param = self::$faker->colorName();
$callback = new Callback(
static function (QueryData $arg) use ($param) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 1
&& $params['parameter'] === $param
&& $arg->getMapClassName() === Simple::class
&& is_a($query, SelectInterface::class)
&& !empty($query->getStatement());
}
);
$this->database
->expects(self::once())
->method('doSelect')
->with($callback)
->willReturn(new QueryResult([1]));
$this->assertTrue($this->configRepository->has($param));
}
/**
* @throws ConstraintException
* @throws QueryException
*/
public function testUpdate()
{
$config = ConfigGenerator::factory()->buildConfig();
$callback = new Callback(
static function (QueryData $arg) use ($config) {
$query = $arg->getQuery();
$params = $query->getBindValues();
return count($params) === 2
&& $params['parameter'] === $config->getParameter()
&& $params['value'] === $config->getValue()
&& is_a($query, UpdateInterface::class)
&& !empty($query->getStatement());
}
);
$queryResult = new QueryResult([1]);
$this->database
->expects(self::exactly(1))
->method('doQuery')
->with($callback)
->willReturn($queryResult);
$out = $this->configRepository->update($config);
$this->assertEquals($queryResult, $out);
}
protected function setUp(): void
{
parent::setUp();
$this->database = $this->createMock(DatabaseInterface::class);
$queryFactory = new QueryFactory('mysql');
$this->configRepository = new Config(
$this->database,
$this->context,
$this->application->getEventDispatcher(),
$queryFactory,
);
}
}

View File

@@ -42,7 +42,7 @@ use SP\Domain\Api\Services\ApiRequest;
use SP\Domain\Auth\Models\AuthToken;
use SP\Domain\Auth\Services\AuthToken;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Exceptions\ConstraintException;
@@ -166,7 +166,9 @@ abstract class ApiTestCase extends TestCase
},
DbStorageInterface::class => create(MysqlHandler::class)
->constructor($databaseConnectionData),
ConfigDataInterface::class => static function (ConfigInterface $config) use ($databaseConnectionData) {
ConfigDataInterface::class => static function (ConfigFileService $config) use (
$databaseConnectionData
) {
$configData = $config->getConfigData()
->setDbHost($databaseConnectionData->getDbHost())
->setDbName($databaseConnectionData->getDbName())

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -26,7 +26,7 @@ namespace SPT\Modules\Cli\Commands;
use DI\DependencyException;
use DI\NotFoundException;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Domain\Core\Exceptions\FileNotFoundException;
use SP\Domain\Export\Services\BackupFiles;
use SP\Modules\Cli\Commands\BackupCommand;
@@ -94,7 +94,7 @@ class BackupCommandTest extends CliTestCase
private function checkBackupFilesAreCreated(): void
{
$configData = self::$dic->get(ConfigFileService::class)->getConfigData();
$configData = self::$dic->get(ConfigFile::class)->getConfigData();
$this->assertFileExists(
BackupFiles::getAppBackupFilename(

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -27,7 +27,7 @@ namespace SPT\Modules\Cli\Commands;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use SP\Domain\Config\Services\ConfigFileService;
use SP\Domain\Config\Services\ConfigFile;
use SP\Infrastructure\Database\DatabaseException;
use SP\Modules\Cli\Commands\InstallCommand;
use SPT\DatabaseUtil;
@@ -188,7 +188,7 @@ class InstallCommandTest extends CliTestCase
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Installation finished', $output);
$configData = self::$dic->get(ConfigFileService::class)->getConfigData();
$configData = self::$dic->get(ConfigFile::class)->getConfigData();
// Cleanup database
DatabaseUtil::dropDatabase(self::$commandInputData['databaseName']);
@@ -222,7 +222,7 @@ class InstallCommandTest extends CliTestCase
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Installation finished', $output);
$configData = self::$dic->get(ConfigFileService::class)->getConfigData();
$configData = self::$dic->get(ConfigFile::class)->getConfigData();
$this->assertEquals($configData->getSiteLang(), $inputData['--language']);
@@ -269,7 +269,7 @@ class InstallCommandTest extends CliTestCase
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Installation finished', $output);
$configData = self::$dic->get(ConfigFileService::class)->getConfigData();
$configData = self::$dic->get(ConfigFile::class)->getConfigData();
$this->assertEquals($configData->getDbUser(), $databaseUser);
$this->assertEquals($configData->getDbPass(), $databasePassword);

View File

@@ -4,7 +4,7 @@
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -32,7 +32,7 @@ use SP\Core\Application;
use SP\Core\Context\ContextException;
use SP\Core\Context\StatelessContext;
use SP\DataModel\ProfileData;
use SP\Domain\Config\Ports\ConfigInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Context\ContextInterface;
use SP\Domain\Core\Events\EventDispatcherInterface;
use SP\Domain\User\Services\UserLoginResponse;
@@ -45,9 +45,9 @@ abstract class UnitaryTestCase extends TestCase
{
use PHPUnitHelper;
protected static Generator $faker;
protected ConfigInterface $config;
protected Application $application;
protected static Generator $faker;
protected ConfigFileService $config;
protected Application $application;
protected ContextInterface $context;
public static function setUpBeforeClass(): void
@@ -94,7 +94,7 @@ abstract class UnitaryTestCase extends TestCase
$configData = ConfigDataGenerator::factory()->buildConfigData();
$config = $this->createStub(ConfigInterface::class);
$config = $this->createStub(ConfigFileService::class);
$config->method('getConfigData')->willReturn($configData);
return new Application(