diff --git a/app/modules/api/Controllers/UserGroup/UserGroupBase.php b/app/modules/api/Controllers/UserGroup/UserGroupBase.php index a2049941..fef8cd89 100644 --- a/app/modules/api/Controllers/UserGroup/UserGroupBase.php +++ b/app/modules/api/Controllers/UserGroup/UserGroupBase.php @@ -29,7 +29,7 @@ use SP\Core\Application; use SP\Domain\Api\Ports\ApiService; use SP\Domain\Core\Acl\AclInterface; use SP\Domain\Core\Exceptions\InvalidClassException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Modules\Api\Controllers\ControllerBase; use SP\Modules\Api\Controllers\Help\TagHelp; @@ -38,7 +38,7 @@ use SP\Modules\Api\Controllers\Help\TagHelp; */ abstract class UserGroupBase extends ControllerBase { - protected UserGroupServiceInterface $userGroupService; + protected UserGroupService $userGroupService; /** * @throws InvalidClassException @@ -48,7 +48,7 @@ abstract class UserGroupBase extends ControllerBase Klein $router, ApiService $apiService, AclInterface $acl, - UserGroupServiceInterface $groupService + UserGroupService $groupService ) { parent::__construct($application, $router, $apiService, $acl); diff --git a/app/modules/web/Controllers/AccessManager/IndexController.php b/app/modules/web/Controllers/AccessManager/IndexController.php index 48838af7..03e60206 100644 --- a/app/modules/web/Controllers/AccessManager/IndexController.php +++ b/app/modules/web/Controllers/AccessManager/IndexController.php @@ -33,7 +33,7 @@ use SP\Domain\Auth\Ports\AuthTokenService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Html\DataGrid\DataGridTab; @@ -61,25 +61,25 @@ final class IndexController extends ControllerBase private AuthTokenGrid $authTokenGrid; private PublicLinkGrid $publicLinkGrid; private UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; + private UserGroupService $userGroupService; private UserProfileServiceInterface $userProfileService; private AuthTokenService $authTokenService; private PublicLinkService $publicLinkService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - TabsGridHelper $tabsGridHelper, - UserGrid $userGrid, - UserGroupGrid $userGroupGrid, - UserProfileGrid $userProfileGrid, - AuthTokenGrid $authTokenGrid, - PublicLinkGrid $publicLinkGrid, - UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService, + Application $application, + WebControllerHelper $webControllerHelper, + TabsGridHelper $tabsGridHelper, + UserGrid $userGrid, + UserGroupGrid $userGroupGrid, + UserProfileGrid $userProfileGrid, + AuthTokenGrid $authTokenGrid, + PublicLinkGrid $publicLinkGrid, + UserServiceInterface $userService, + UserGroupService $userGroupService, UserProfileServiceInterface $userProfileService, - AuthTokenService $authTokenService, - PublicLinkService $publicLinkService + AuthTokenService $authTokenService, + PublicLinkService $publicLinkService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/AccountManager/BulkEditController.php b/app/modules/web/Controllers/AccountManager/BulkEditController.php index 49122aae..156b4149 100644 --- a/app/modules/web/Controllers/AccountManager/BulkEditController.php +++ b/app/modules/web/Controllers/AccountManager/BulkEditController.php @@ -37,7 +37,7 @@ use SP\Domain\Client\Ports\ClientService; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\Tag\Ports\TagService; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; @@ -66,7 +66,7 @@ final class BulkEditController extends ControllerBase private ClientService $clientService; private TagService $tagService; private UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; + private UserGroupService $userGroupService; public function __construct( Application $application, @@ -75,7 +75,7 @@ final class BulkEditController extends ControllerBase ClientService $clientService, TagService $tagService, UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService + UserGroupService $userGroupService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/ConfigManager/IndexController.php b/app/modules/web/Controllers/ConfigManager/IndexController.php index 3222ea22..ecfc58d2 100644 --- a/app/modules/web/Controllers/ConfigManager/IndexController.php +++ b/app/modules/web/Controllers/ConfigManager/IndexController.php @@ -48,7 +48,7 @@ use SP\Domain\Crypt\Services\TemporaryMasterPass; use SP\Domain\Export\Services\BackupFileHelper; use SP\Domain\Export\Services\XmlExport; use SP\Domain\Task\Services\Task; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; @@ -75,7 +75,7 @@ final class IndexController extends ControllerBase { protected TabsHelper $tabsHelper; private UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; + private UserGroupService $userGroupService; private UserProfileServiceInterface $userProfileService; private MimeTypesService $mimeTypes; private DatabaseUtil $databaseUtil; @@ -88,7 +88,7 @@ final class IndexController extends ControllerBase WebControllerHelper $webControllerHelper, TabsHelper $tabsHelper, UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService, + UserGroupService $userGroupService, UserProfileServiceInterface $userProfileService, MimeTypesService $mimeTypes, DatabaseUtil $databaseUtil, diff --git a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php index 9e413e3c..216f575b 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountHelper.php @@ -55,7 +55,7 @@ use SP\Domain\Http\RequestInterface; use SP\Domain\ItemPreset\Ports\ItemPresetInterface; use SP\Domain\ItemPreset\Ports\ItemPresetService; use SP\Domain\Tag\Ports\TagService; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Domain\User\Services\UpdatedMasterPassException; use SP\Infrastructure\Common\Repositories\NoSuchItemException; @@ -84,9 +84,9 @@ final class AccountHelper extends AccountHelperBase private CustomFieldDataService $customFieldService; private ?AccountPermission $accountAcl = null; private ?int $accountId = null; - private UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; - private TagService $tagService; + private UserServiceInterface $userService; + private UserGroupService $userGroupService; + private TagService $tagService; public function __construct( Application $application, @@ -94,18 +94,18 @@ final class AccountHelper extends AccountHelperBase RequestInterface $request, AclInterface $acl, AccountService $accountService, - AccountHistoryService $accountHistoryService, - PublicLinkService $publicLinkService, - ItemPresetService $itemPresetService, - MasterPassService $masterPassService, - AccountActionsHelper $accountActionsHelper, - AccountAclService $accountAclService, - CategoryService $categoryService, - ClientService $clientService, - CustomFieldDataService $customFieldService, - UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService, - TagService $tagService + AccountHistoryService $accountHistoryService, + PublicLinkService $publicLinkService, + ItemPresetService $itemPresetService, + MasterPassService $masterPassService, + AccountActionsHelper $accountActionsHelper, + AccountAclService $accountAclService, + CategoryService $categoryService, + ClientService $clientService, + CustomFieldDataService $customFieldService, + UserServiceInterface $userService, + UserGroupService $userGroupService, + TagService $tagService ) { parent::__construct($application, $template, $request, $acl, $accountActionsHelper, $masterPassService); diff --git a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php index 1ab7f9ef..437a9422 100644 --- a/app/modules/web/Controllers/Helpers/ItemPresetHelper.php +++ b/app/modules/web/Controllers/Helpers/ItemPresetHelper.php @@ -35,7 +35,7 @@ use SP\Domain\Core\Exceptions\NoSuchPropertyException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Http\RequestInterface; use SP\Domain\ItemPreset\Models\ItemPreset; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Mvc\View\Components\SelectItemAdapter; @@ -51,15 +51,15 @@ final class ItemPresetHelper extends HelperBase private ?SelectItemAdapter $users = null; private ?SelectItemAdapter $userGroups = null; private UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; + private UserGroupService $userGroupService; private UserProfileServiceInterface $userProfileService; public function __construct( - Application $application, - TemplateInterface $template, - RequestInterface $request, + Application $application, + TemplateInterface $template, + RequestInterface $request, UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService, + UserGroupService $userGroupService, UserProfileServiceInterface $userProfileService ) { diff --git a/app/modules/web/Controllers/User/UserViewBase.php b/app/modules/web/Controllers/User/UserViewBase.php index ed8d552b..b6e3ce33 100644 --- a/app/modules/web/Controllers/User/UserViewBase.php +++ b/app/modules/web/Controllers/User/UserViewBase.php @@ -34,7 +34,7 @@ use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\User\Models\User; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Modules\Web\Controllers\ControllerBase; @@ -49,16 +49,16 @@ abstract class UserViewBase extends ControllerBase { use ItemTrait; - protected UserServiceInterface $userService; - private UserGroupServiceInterface $userGroupService; + protected UserServiceInterface $userService; + private UserGroupService $userGroupService; private UserProfileServiceInterface $userProfileService; private CustomFieldDataService $customFieldService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - UserServiceInterface $userService, - UserGroupServiceInterface $userGroupService, + Application $application, + WebControllerHelper $webControllerHelper, + UserServiceInterface $userService, + UserGroupService $userGroupService, UserProfileServiceInterface $userProfileService, CustomFieldDataService $customFieldService ) { diff --git a/app/modules/web/Controllers/UserGroup/SearchController.php b/app/modules/web/Controllers/UserGroup/SearchController.php index a0d09fd4..18392951 100644 --- a/app/modules/web/Controllers/UserGroup/SearchController.php +++ b/app/modules/web/Controllers/UserGroup/SearchController.php @@ -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\Core\Application; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Html\DataGrid\DataGridInterface; use SP\Http\JsonMessage; use SP\Modules\Web\Controllers\ControllerBase; @@ -47,14 +47,14 @@ final class SearchController extends ControllerBase use ItemTrait; use JsonTrait; - private UserGroupServiceInterface $userGroupService; - private UserGroupGrid $userGroupGrid; + private UserGroupService $userGroupService; + private UserGroupGrid $userGroupGrid; public function __construct( - Application $application, + Application $application, WebControllerHelper $webControllerHelper, - UserGroupServiceInterface $userGroupService, - UserGroupGrid $userGroupGrid + UserGroupService $userGroupService, + UserGroupGrid $userGroupGrid ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/UserGroup/UserGroupSaveBase.php b/app/modules/web/Controllers/UserGroup/UserGroupSaveBase.php index cd22f5f7..7cd88cad 100644 --- a/app/modules/web/Controllers/UserGroup/UserGroupSaveBase.php +++ b/app/modules/web/Controllers/UserGroup/UserGroupSaveBase.php @@ -27,7 +27,7 @@ namespace SP\Modules\Web\Controllers\UserGroup; use SP\Core\Application; use SP\Domain\CustomField\Ports\CustomFieldDataService; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Modules\Web\Controllers\ControllerBase; use SP\Modules\Web\Forms\UserGroupForm; use SP\Mvc\Controller\WebControllerHelper; @@ -37,14 +37,14 @@ use SP\Mvc\Controller\WebControllerHelper; */ abstract class UserGroupSaveBase extends ControllerBase { - protected UserGroupServiceInterface $userGroupService; + protected UserGroupService $userGroupService; protected CustomFieldDataService $customFieldService; protected UserGroupForm $form; public function __construct( - Application $application, + Application $application, WebControllerHelper $webControllerHelper, - UserGroupServiceInterface $userGroupService, + UserGroupService $userGroupService, CustomFieldDataService $customFieldService ) { parent::__construct($application, $webControllerHelper); diff --git a/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php b/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php index 94bbe09a..b35b5f22 100644 --- a/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php +++ b/app/modules/web/Controllers/UserGroup/UserGroupViewBase.php @@ -34,7 +34,7 @@ use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\CustomField\Ports\CustomFieldDataService; use SP\Domain\User\Models\UserGroup; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Web\Controllers\ControllerBase; @@ -49,14 +49,14 @@ abstract class UserGroupViewBase extends ControllerBase { use ItemTrait; - private UserGroupServiceInterface $userGroupService; + private UserGroupService $userGroupService; private CustomFieldDataService $customFieldService; private UserServiceInterface $userService; public function __construct( - Application $application, - WebControllerHelper $webControllerHelper, - UserGroupServiceInterface $userGroupService, + Application $application, + WebControllerHelper $webControllerHelper, + UserGroupService $userGroupService, UserServiceInterface $userService, CustomFieldDataService $customFieldService ) { diff --git a/lib/SP/Domain/Account/Services/AccountSearch.php b/lib/SP/Domain/Account/Services/AccountSearch.php index 5cd9fd28..fd4bbd19 100644 --- a/lib/SP/Domain/Account/Services/AccountSearch.php +++ b/lib/SP/Domain/Account/Services/AccountSearch.php @@ -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. * @@ -36,7 +36,7 @@ use SP\Domain\Common\Services\Service; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Database\QueryResult; use SP\Util\Filter; @@ -49,11 +49,11 @@ use function SP\processException; final class AccountSearch extends Service implements AccountSearchService { public function __construct( - Application $application, - private readonly UserServiceInterface $userService, - private readonly UserGroupServiceInterface $userGroupService, - private readonly AccountSearchRepository $accountSearchRepository, - private readonly AccountSearchDataBuilder $accountSearchDataBuilder + Application $application, + private readonly UserServiceInterface $userService, + private readonly UserGroupService $userGroupService, + private readonly AccountSearchRepository $accountSearchRepository, + private readonly AccountSearchDataBuilder $accountSearchDataBuilder ) { parent::__construct($application); } diff --git a/lib/SP/Domain/Import/Services/LdapImport.php b/lib/SP/Domain/Import/Services/LdapImport.php index 1bca861c..a6fe8e7a 100644 --- a/lib/SP/Domain/Import/Services/LdapImport.php +++ b/lib/SP/Domain/Import/Services/LdapImport.php @@ -37,7 +37,7 @@ use SP\Domain\Import\Dtos\LdapImportResultsDto; use SP\Domain\Import\Ports\LdapImportService; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Providers\Auth\Ldap\LdapBase; use SP\Providers\Auth\Ldap\LdapException; @@ -54,11 +54,11 @@ use function SP\processException; final class LdapImport extends Service implements LdapImportService { public function __construct( - Application $application, - private readonly UserServiceInterface $userService, - private readonly UserGroupServiceInterface $userGroupService, - private readonly LdapActionsService $ldapActionsService, - private readonly LdapConnectionInterface $ldapConnection + Application $application, + private readonly UserServiceInterface $userService, + private readonly UserGroupService $userGroupService, + private readonly LdapActionsService $ldapActionsService, + private readonly LdapConnectionInterface $ldapConnection ) { parent::__construct($application); } diff --git a/lib/SP/Domain/Install/Services/InstallerService.php b/lib/SP/Domain/Install/Services/InstallerService.php index bfadd33e..dfebb1c0 100644 --- a/lib/SP/Domain/Install/Services/InstallerService.php +++ b/lib/SP/Domain/Install/Services/InstallerService.php @@ -42,7 +42,7 @@ use SP\Domain\Install\Ports\InstallerServiceInterface; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; use SP\Domain\User\Models\UserProfile; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Database\DatabaseConnectionData; @@ -71,7 +71,7 @@ final class InstallerService implements InstallerServiceInterface RequestInterface $request, private readonly ConfigFileService $config, private readonly UserServiceInterface $userService, - private readonly UserGroupServiceInterface $userGroupService, + private readonly UserGroupService $userGroupService, private readonly UserProfileServiceInterface $userProfileService, private readonly ConfigService $configService, private readonly DatabaseConnectionData $databaseConnectionData, diff --git a/lib/SP/Domain/User/Ports/UserGroupServiceInterface.php b/lib/SP/Domain/User/Ports/UserGroupService.php similarity index 84% rename from lib/SP/Domain/User/Ports/UserGroupServiceInterface.php rename to lib/SP/Domain/User/Ports/UserGroupService.php index 116f4cb0..60b827fb 100644 --- a/lib/SP/Domain/User/Ports/UserGroupServiceInterface.php +++ b/lib/SP/Domain/User/Ports/UserGroupService.php @@ -28,16 +28,16 @@ use SP\DataModel\ItemSearchData; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; -use SP\Domain\User\Models\UserGroup; +use SP\Domain\User\Models\UserGroup as UserGroupModel; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; /** * Class UserGroupService * - * @package SP\Domain\Common\Services\UserGroup + * @template T of UserGroupModel */ -interface UserGroupServiceInterface +interface UserGroupService { /** * @throws ConstraintException @@ -50,17 +50,17 @@ interface UserGroupServiceInterface * @throws QueryException * @throws NoSuchItemException */ - public function getById(int $id): UserGroup; + public function getById(int $id): UserGroupModel; /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): UserGroupServiceInterface; + public function delete(int $id): void; /** - * @param int[] $ids + * @param int[] $ids * * @throws ServiceException * @throws ConstraintException @@ -71,17 +71,17 @@ interface UserGroupServiceInterface /** * @throws ServiceException */ - public function create(UserGroup $itemData): int; + public function create(UserGroupModel $userGroup): int; /** * @throws ServiceException */ - public function update(UserGroup $itemData): void; + public function update(UserGroupModel $userGroup): void; /** * Get all items from the service's repository * - * @return UserGroup[] + * @return UserGroupModel[] * @throws ConstraintException * @throws QueryException */ @@ -94,7 +94,7 @@ interface UserGroupServiceInterface * @throws ConstraintException * @throws QueryException */ - public function getByName(string $name): UserGroup; + public function getByName(string $name): UserGroupModel; /** * Returns the users that are using the given group id diff --git a/lib/SP/Domain/User/Services/UserGroupService.php b/lib/SP/Domain/User/Services/UserGroup.php similarity index 60% rename from lib/SP/Domain/User/Services/UserGroupService.php rename to lib/SP/Domain/User/Services/UserGroup.php index 868ac0bb..575edc64 100644 --- a/lib/SP/Domain/User/Services/UserGroupService.php +++ b/lib/SP/Domain/User/Services/UserGroup.php @@ -28,47 +28,35 @@ use SP\Core\Application; use SP\DataModel\ItemSearchData; 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; use SP\Domain\User\Models\UserGroup as UserGroupModel; use SP\Domain\User\Ports\UserGroupRepository; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserToUserGroupServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; -use SP\Infrastructure\Database\DatabaseInterface; use SP\Infrastructure\Database\QueryResult; +use function SP\__u; + /** * Class UserGroupService * - * @package SP\Domain\Common\Services\UserGroup + * @template T of UserGroupModel */ -final class UserGroupService extends Service implements UserGroupServiceInterface +final class UserGroup extends Service implements UserGroupService { - use ServiceItemTrait; - - protected UserGroupRepository $userGroupRepository; - protected UserToUserGroupServiceInterface $userToUserGroupService; - private DatabaseInterface $database; - public function __construct( - Application $application, - UserGroupRepository $userGroupRepository, - UserToUserGroupServiceInterface $userToUserGroupService, - DatabaseInterface $database + Application $application, + private readonly UserGroupRepository $userGroupRepository, + private readonly UserToUserGroupServiceInterface $userToUserGroupService, ) { parent::__construct($application); - - $this->userGroupRepository = $userGroupRepository; - $this->userToUserGroupService = $userToUserGroupService; - $this->database = $database; } /** - * @throws ConstraintException - * @throws QueryException + * @param ItemSearchData $itemSearchData + * @return QueryResult */ public function search(ItemSearchData $itemSearchData): QueryResult { @@ -85,13 +73,11 @@ final class UserGroupService extends Service implements UserGroupServiceInterfac $result = $this->userGroupRepository->getById($id); if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('Group not found'), SPException::INFO); + throw NoSuchItemException::info(__u('Group not found')); } - $data = $result->getData(); - $data->setUsers($this->userToUserGroupService->getUsersByGroupId($id)); - - return $data; + return $result->getData() + ->mutate(['users' => $this->userToUserGroupService->getUsersByGroupId($id)]); } /** @@ -99,15 +85,11 @@ final class UserGroupService extends Service implements UserGroupServiceInterfac * @throws QueryException * @throws NoSuchItemException */ - public function delete(int $id): UserGroupServiceInterface + public function delete(int $id): void { - $delete = $this->userGroupRepository->delete($id); - - if ($delete === 0) { - throw new NoSuchItemException(__u('Group not found'), SPException::INFO); + if ($this->userGroupRepository->delete($id)->getAffectedNumRows() === 0) { + throw NoSuchItemException::info(__u('Group not found')); } - - return $this; } /** @@ -119,13 +101,10 @@ final class UserGroupService extends Service implements UserGroupServiceInterfac */ public function deleteByIdBatch(array $ids): int { - $count = $this->userGroupRepository->deleteByIdBatch($ids); + $count = $this->userGroupRepository->deleteByIdBatch($ids)->getAffectedNumRows(); if ($count !== count($ids)) { - throw new ServiceException( - __u('Error while deleting the groups'), - SPException::WARNING - ); + throw ServiceException::warning(__u('Error while deleting the groups')); } return $count; @@ -134,13 +113,13 @@ final class UserGroupService extends Service implements UserGroupServiceInterfac /** * @throws ServiceException */ - public function create(UserGroupModel $itemData): int + public function create(UserGroupModel $userGroup): int { - return $this->transactionAware( - function () use ($itemData) { - $id = $this->userGroupRepository->create($itemData); + return $this->userGroupRepository->transactionAware( + function () use ($userGroup) { + $id = $this->userGroupRepository->create($userGroup)->getLastId(); - $users = $itemData->getUsers(); + $users = $userGroup->getUsers(); if ($users !== null) { $this->userToUserGroupService->add($id, $users); @@ -148,78 +127,78 @@ final class UserGroupService extends Service implements UserGroupServiceInterfac return $id; }, - $this->database + $this ); } /** * @throws ServiceException */ - public function update(UserGroupModel $itemData): void + public function update(UserGroupModel $userGroup): void { - $this->transactionAware( - function () use ($itemData) { - $this->userGroupRepository->update($itemData); + $this->userGroupRepository->transactionAware( + function () use ($userGroup) { + $this->userGroupRepository->update($userGroup); - $users = $itemData->getUsers(); + $users = $userGroup->getUsers(); if ($users !== null) { - $this->userToUserGroupService->update($itemData->getId(), $users); + $this->userToUserGroupService->update($userGroup->getId(), $users); } }, - $this->database + $this ); } /** * Get all items from the service's repository * - * @return UserGroupModel[] - * @throws ConstraintException - * @throws QueryException + * @return array */ public function getAll(): array { - return $this->userGroupRepository->getAll()->getDataAsArray(); + return $this->userGroupRepository->getAll()->getDataAsArray(UserGroupModel::class); } /** * Returns the item for given name * + * @param string $name + * @return UserGroupModel * @throws NoSuchItemException - * @throws ConstraintException - * @throws QueryException */ public function getByName(string $name): UserGroupModel { $result = $this->userGroupRepository->getByName($name); if ($result->getNumRows() === 0) { - throw new NoSuchItemException(__u('Group not found'), SPException::INFO); + throw NoSuchItemException::info(__u('Group not found')); } - return $result->getData(); + return $result->getData(UserGroupModel::class); } /** * Returns the users that are using the given group id * + * @return array * @throws ConstraintException * @throws QueryException */ public function getUsage(int $id): array { - return $this->userGroupRepository->getUsage($id)->getDataAsArray(); + return $this->userGroupRepository->getUsage($id)->getDataAsArray(UserGroupModel::class); } /** * Returns the items that are using the given group id * + * @return array * @throws ConstraintException * @throws QueryException */ public function getUsageByUsers(int $id): array { - return $this->userGroupRepository->getUsageByUsers($id)->getDataAsArray(); + return $this->userGroupRepository->getUsageByUsers($id)->getDataAsArray(UserGroupModel::class); } } diff --git a/lib/SP/Infrastructure/Database/QueryResult.php b/lib/SP/Infrastructure/Database/QueryResult.php index 7f132df0..6af04d25 100644 --- a/lib/SP/Infrastructure/Database/QueryResult.php +++ b/lib/SP/Infrastructure/Database/QueryResult.php @@ -40,7 +40,6 @@ class QueryResult private readonly SplFixedArray $data; private readonly int $numRows; private int $totalNumRows = 0; - private int $statusCode = 0; /** * QueryResult constructor. @@ -122,11 +121,6 @@ class QueryResult return $this->totalNumRows; } - public function getStatusCode(): int - { - return $this->statusCode; - } - public function getAffectedNumRows(): int { return $this->affectedNumRows; diff --git a/lib/SP/Infrastructure/User/Repositories/UserGroup.php b/lib/SP/Infrastructure/User/Repositories/UserGroup.php index 14fb9720..4cdb521c 100644 --- a/lib/SP/Infrastructure/User/Repositories/UserGroup.php +++ b/lib/SP/Infrastructure/User/Repositories/UserGroup.php @@ -75,6 +75,8 @@ final class UserGroup extends BaseRepository implements UserGroupRepository * * @param int $userGroupId * @return QueryResult + * @throws ConstraintException + * @throws QueryException */ public function getUsage(int $userGroupId): QueryResult { diff --git a/lib/SP/Providers/Acl/AclHandler.php b/lib/SP/Providers/Acl/AclHandler.php index 8113bc43..00665965 100644 --- a/lib/SP/Providers/Acl/AclHandler.php +++ b/lib/SP/Providers/Acl/AclHandler.php @@ -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\Domain\Account\Services\AccountAcl; use SP\Domain\Core\Events\EventReceiver; use SP\Domain\Core\Exceptions\FileNotFoundException; use SP\Domain\Core\Exceptions\SPException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Providers\EventsTrait; use SP\Providers\Provider; @@ -63,7 +63,7 @@ final class AclHandler extends Provider implements EventReceiver public function __construct( Application $application, private readonly UserProfileServiceInterface $userProfileService, - private readonly UserGroupServiceInterface $userGroupService + private readonly UserGroupService $userGroupService ) { parent::__construct($application); } diff --git a/tests/SPT/Domain/Account/Services/AccountSearchTest.php b/tests/SPT/Domain/Account/Services/AccountSearchTest.php index 40a3c36f..e53e0cc9 100644 --- a/tests/SPT/Domain/Account/Services/AccountSearchTest.php +++ b/tests/SPT/Domain/Account/Services/AccountSearchTest.php @@ -39,7 +39,7 @@ use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Infrastructure\Database\QueryResult; use SPT\Domain\Account\Services\Builders\AccountSearchTokenizerDataTrait; @@ -246,7 +246,7 @@ class AccountSearchTest extends UnitaryTestCase ]) ); - $userGroupService = $this->createMock(UserGroupServiceInterface::class); + $userGroupService = $this->createMock(UserGroupService::class); $userGroupService ->method('getByName') ->willReturn( diff --git a/tests/SPT/Domain/Import/Services/LdapImportTest.php b/tests/SPT/Domain/Import/Services/LdapImportTest.php index 3c02c7b3..4b159eac 100644 --- a/tests/SPT/Domain/Import/Services/LdapImportTest.php +++ b/tests/SPT/Domain/Import/Services/LdapImportTest.php @@ -35,7 +35,7 @@ use SP\Domain\Import\Dtos\LdapImportParamsDto; use SP\Domain\Import\Services\LdapImport; use SP\Domain\User\Models\User; use SP\Domain\User\Models\UserGroup; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserServiceInterface; use SP\Providers\Auth\Ldap\LdapException; use SP\Providers\Auth\Ldap\LdapParams; @@ -52,9 +52,9 @@ class LdapImportTest extends UnitaryTestCase { private LdapImport $ldapImport; - private UserServiceInterface|MockObject $userService; - private UserGroupServiceInterface|MockObject $userGroupService; - private LdapActionsService|MockObject $ldapActionsService; + private UserServiceInterface|MockObject $userService; + private UserGroupService|MockObject $userGroupService; + private LdapActionsService|MockObject $ldapActionsService; private LdapConnectionInterface|MockObject $ldapConnection; public static function emptyNameOrLoginProvider(): array @@ -575,7 +575,7 @@ class LdapImportTest extends UnitaryTestCase parent::setUp(); $this->userService = $this->createMock(UserServiceInterface::class); - $this->userGroupService = $this->createMock(UserGroupServiceInterface::class); + $this->userGroupService = $this->createMock(UserGroupService::class); $this->ldapActionsService = $this->createMock(LdapActionsService::class); $this->ldapConnection = $this->createMock(LdapConnectionInterface::class); diff --git a/tests/SPT/Domain/Install/Services/InstallerTest.php b/tests/SPT/Domain/Install/Services/InstallerTest.php index f9464658..3e70157c 100644 --- a/tests/SPT/Domain/Install/Services/InstallerTest.php +++ b/tests/SPT/Domain/Install/Services/InstallerTest.php @@ -36,7 +36,7 @@ use SP\Domain\Install\Adapters\InstallData; use SP\Domain\Install\Ports\InstallerServiceInterface; use SP\Domain\Install\Services\DatabaseSetupInterface; use SP\Domain\Install\Services\InstallerService; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Domain\User\Ports\UserServiceInterface; use SP\Domain\User\Services\UserService; @@ -68,7 +68,7 @@ class InstallerTest extends UnitaryTestCase */ private $configService; /** - * @var MockObject|UserGroupServiceInterface + * @var MockObject|UserGroupService */ private $userGroupService; /** @@ -442,7 +442,7 @@ class InstallerTest extends UnitaryTestCase $this->userService = $this->createMock(UserServiceInterface::class); $this->request = $this->createStub(RequestInterface::class); $this->configService = $this->createMock(ConfigService::class); - $this->userGroupService = $this->createMock(UserGroupServiceInterface::class); + $this->userGroupService = $this->createMock(UserGroupService::class); $this->userProfileService = $this->createMock(UserProfileServiceInterface::class); parent::setUp(); diff --git a/tests/SPT/Domain/User/Services/UserGroupTest.php b/tests/SPT/Domain/User/Services/UserGroupTest.php new file mode 100644 index 00000000..6e6442db --- /dev/null +++ b/tests/SPT/Domain/User/Services/UserGroupTest.php @@ -0,0 +1,381 @@ +. + */ + +namespace SPT\Domain\User\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\MockObject; +use SP\DataModel\ItemSearchData; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Core\Exceptions\ConstraintException; +use SP\Domain\Core\Exceptions\QueryException; +use SP\Domain\User\Models\UserGroup as UserGroupModel; +use SP\Domain\User\Ports\UserGroupRepository; +use SP\Domain\User\Ports\UserToUserGroupServiceInterface; +use SP\Domain\User\Services\UserGroup; +use SP\Infrastructure\Common\Repositories\NoSuchItemException; +use SP\Infrastructure\Database\QueryResult; +use SPT\Generators\UserGroupGenerator; +use SPT\Stubs\UserGroupRepositoryStub; +use SPT\UnitaryTestCase; + +/** + * Class UserGroupTest + */ +#[Group('unitary')] +class UserGroupTest extends UnitaryTestCase +{ + + private MockObject|UserGroupRepository $userGroupRepository; + private UserToUserGroupServiceInterface|MockObject $userToUserGroupService; + private UserGroup $userGroup; + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testGetById() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn(new QueryResult([new UserGroupModel()])); + + $this->userToUserGroupService + ->expects($this->once()) + ->method('getUsersByGroupId') + ->with(100) + ->willReturn([1, 2, 3]); + + $out = $this->userGroup->getById(100); + + $this->assertEquals([1, 2, 3], $out->getUsers()); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testGetByIdWithNoGroup() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('getById') + ->with(100) + ->willReturn(new QueryResult([])); + + $this->userToUserGroupService + ->expects($this->never()) + ->method('getUsersByGroupId'); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Group not found'); + + $this->userGroup->getById(100); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testGetUsage() + { + $queryResult = new QueryResult([new UserGroupModel()]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('getUsage') + ->with(100) + ->willReturn($queryResult); + + $out = $this->userGroup->getUsage(100); + + $this->assertEquals($queryResult->getDataAsArray(), $out); + } + + /** + * @throws ConstraintException + * @throws QueryException + */ + public function testGetUsageByUsers() + { + $queryResult = new QueryResult([new UserGroupModel()]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('getUsageByUsers') + ->with(100) + ->willReturn($queryResult); + + $out = $this->userGroup->getUsageByUsers(100); + + $this->assertEquals($queryResult->getDataAsArray(), $out); + } + + /** + * @throws ServiceException + */ + public function testUpdate() + { + $userGroup = UserGroupGenerator::factory()->buildUserGroupData(); + + $this->userGroupRepository + ->expects($this->once()) + ->method('update') + ->with($userGroup); + + $this->userToUserGroupService + ->expects($this->once()) + ->method('update') + ->with($userGroup->getId(), $userGroup->getUsers()); + + $this->userGroup->update($userGroup); + } + + /** + * @throws ServiceException + */ + public function testUpdateWithNoUsers() + { + $userGroup = UserGroupGenerator::factory()->buildUserGroupData()->mutate(['users' => null]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('update') + ->with($userGroup); + + $this->userToUserGroupService + ->expects($this->never()) + ->method('update'); + + $this->userGroup->update($userGroup); + } + + /** + * @throws NoSuchItemException + */ + public function testGetByName() + { + $userGroup = UserGroupGenerator::factory()->buildUserGroupData(); + + $this->userGroupRepository + ->expects($this->once()) + ->method('getByName') + ->with('a_group') + ->willReturn(new QueryResult([$userGroup])); + + $out = $this->userGroup->getByName('a_group'); + + $this->assertEquals($userGroup, $out); + } + + /** + * @throws NoSuchItemException + */ + public function testGetByNameWithNoGroup() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('getByName') + ->with('a_group') + ->willReturn(new QueryResult([])); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Group not found'); + + $this->userGroup->getByName('a_group'); + } + + /** + * @throws ServiceException + */ + public function testCreate() + { + $userGroup = UserGroupGenerator::factory()->buildUserGroupData(); + + $this->userGroupRepository + ->expects($this->once()) + ->method('create') + ->with($userGroup) + ->willReturn(new QueryResult([$userGroup], 1, 100)); + + $this->userToUserGroupService + ->expects($this->once()) + ->method('add') + ->with(100, $userGroup->getUsers()); + + $out = $this->userGroup->create($userGroup); + + $this->assertEquals(100, $out); + } + + /** + * @throws ServiceException + */ + public function testCreateWithNoUsers() + { + $userGroup = UserGroupGenerator::factory()->buildUserGroupData()->mutate(['users' => null]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('create') + ->with($userGroup) + ->willReturn(new QueryResult([$userGroup], 1, 100)); + + $this->userToUserGroupService + ->expects($this->never()) + ->method('add'); + + $out = $this->userGroup->create($userGroup); + + $this->assertEquals(100, $out); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatch() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn(new QueryResult(null, 3)); + + $out = $this->userGroup->deleteByIdBatch([100, 200, 300]); + + $this->assertEquals(3, $out); + } + + /** + * @throws ConstraintException + * @throws ServiceException + * @throws QueryException + */ + public function testDeleteByIdBatchWithException() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('deleteByIdBatch') + ->with([100, 200, 300]) + ->willReturn(new QueryResult(null, 1)); + + $this->expectException(ServiceException::class); + $this->expectExceptionMessage('Error while deleting the groups'); + + $this->userGroup->deleteByIdBatch([100, 200, 300]); + } + + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDelete() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn(new QueryResult(null, 1)); + + $this->userGroup->delete(100); + } + + /** + * @throws ConstraintException + * @throws NoSuchItemException + * @throws QueryException + */ + public function testDeleteWithException() + { + $this->userGroupRepository + ->expects($this->once()) + ->method('delete') + ->with(100) + ->willReturn(new QueryResult()); + + $this->expectException(NoSuchItemException::class); + $this->expectExceptionMessage('Group not found'); + + $this->userGroup->delete(100); + } + + public function testSearch() + { + $itemSearchData = new ItemSearchData('test', 1, 10); + + $queryResult = new QueryResult([1]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('search') + ->with($itemSearchData) + ->willReturn($queryResult); + + $out = $this->userGroup->search($itemSearchData); + + $this->assertEquals($queryResult, $out); + } + + public function testGetAll() + { + $queryResult = new QueryResult([UserGroupGenerator::factory()->buildUserGroupData()]); + + $this->userGroupRepository + ->expects($this->once()) + ->method('getAll') + ->willReturn($queryResult); + + $out = $this->userGroup->getAll(); + + $this->assertEquals($queryResult->getDataAsArray(), $out); + } + + protected function setUp(): void + { + parent::setUp(); + + $userGroupRepositoryMethods = array_filter( + get_class_methods(UserGroupRepositoryStub::class), + static fn(string $method) => $method != 'transactionAware' + ); + + $this->userGroupRepository = $this->createPartialMock( + UserGroupRepositoryStub::class, + $userGroupRepositoryMethods + ); + $this->userToUserGroupService = $this->createMock(UserToUserGroupServiceInterface::class); + + $this->userGroup = new UserGroup($this->application, $this->userGroupRepository, $this->userToUserGroupService); + } + + +} diff --git a/tests/SPT/Providers/Acl/AclHandlerTest.php b/tests/SPT/Providers/Acl/AclHandlerTest.php index 61e6e60b..a53cf233 100644 --- a/tests/SPT/Providers/Acl/AclHandlerTest.php +++ b/tests/SPT/Providers/Acl/AclHandlerTest.php @@ -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 PHPUnit\Framework\MockObject\MockObject; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Core\Exceptions\SPException; -use SP\Domain\User\Ports\UserGroupServiceInterface; +use SP\Domain\User\Ports\UserGroupService; use SP\Domain\User\Ports\UserProfileServiceInterface; use SP\Providers\Acl\AclHandler; use SPT\UnitaryTestCase; @@ -44,7 +44,7 @@ use SPT\UnitaryTestCase; class AclHandlerTest extends UnitaryTestCase { private MockObject|UserProfileServiceInterface $userProfileService; - private UserGroupServiceInterface|MockObject $userGroupService; + private UserGroupService|MockObject $userGroupService; private AclHandler $aclHandler; public static function userEventProvider(): array @@ -239,7 +239,7 @@ class AclHandlerTest extends UnitaryTestCase parent::setUp(); $this->userProfileService = $this->createMock(UserProfileServiceInterface::class); - $this->userGroupService = $this->createMock(UserGroupServiceInterface::class); + $this->userGroupService = $this->createMock(UserGroupService::class); $this->aclHandler = new AclHandler($this->application, $this->userProfileService, $this->userGroupService); } diff --git a/tests/SPT/Stubs/UserGroupRepositoryStub.php b/tests/SPT/Stubs/UserGroupRepositoryStub.php new file mode 100644 index 00000000..feba2bab --- /dev/null +++ b/tests/SPT/Stubs/UserGroupRepositoryStub.php @@ -0,0 +1,35 @@ +. + */ + +namespace SPT\Stubs; + +use SP\Domain\User\Ports\UserGroupRepository; + +/** + * Class UserGroupRepositoryStub + */ +abstract class UserGroupRepositoryStub implements UserGroupRepository +{ + use TransactionAwareTrait; +}