From 0ed17a8f46443794f18040f50149885a1df51d55 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Mon, 22 Jan 2018 01:15:52 +0100 Subject: [PATCH] * [ADD] Added services for every repository * [ADD] New Select component to deal with structured data from a query which will be shown in an HTML select component * [MOD] Improved accounts search filtering * [MOD] Code refactoring --- .../Controllers/AccessManagerController.php | 2 + .../web/Controllers/AccountController.php | 5 +- .../web/Controllers/ApiTokenController.php | 18 +- .../web/Controllers/CategoryController.php | 11 +- .../web/Controllers/ClientController.php | 10 +- .../web/Controllers/CustomFieldController.php | 9 +- .../web/Controllers/Helpers/AccountHelper.php | 28 ++- .../Helpers/AccountSearchHelper.php | 14 +- .../web/Controllers/Helpers/HelperBase.php | 1 + .../web/Controllers/ItemsController.php | 1 - .../web/Controllers/MainController.php | 7 +- .../web/Controllers/PassresetController.php | 2 + .../web/Controllers/PublicLinkController.php | 17 +- app/modules/web/Controllers/TagController.php | 10 +- .../web/Controllers/UserController.php | 16 +- .../web/Controllers/UserGroupController.php | 33 ++-- .../web/Controllers/UserProfileController.php | 17 +- .../views/account/account-permissions.inc | 19 +- .../material-blue/views/account/account.inc | 12 +- .../views/account/search-searchbox.inc | 15 +- .../material-blue/views/itemshow/apitoken.inc | 12 +- .../material-blue/views/itemshow/client.inc | 8 +- .../views/itemshow/publiclink.inc | 5 +- .../material-blue/views/itemshow/user.inc | 10 +- .../views/itemshow/usergroup.inc | 5 +- lib/SP/Account/AccountSearchFilter.php | 14 +- lib/SP/Account/AccountUtil.php | 82 ++++----- lib/SP/DataModel/ItemData.php | 58 +++++++ lib/SP/DataModel/UserGroupData.php | 2 +- lib/SP/Mvc/Model/QueryFilter.php | 95 ++++++++++ .../View/Components/ItemAdapterInterface.php | 61 +++++++ lib/SP/Mvc/View/Components/SelectItem.php | 105 +++++++++++ .../Mvc/View/Components/SelectItemAdapter.php | 164 ++++++++++++++++++ .../Account/AccountRepository.php | 160 +++++++---------- .../ApiToken/ApiTokenRepository.php | 9 +- .../Category/CategoryRepository.php | 11 +- .../Repositories/Client/ClientRepository.php | 28 +-- .../PublicLink/PublicLinkRepository.php | 15 +- lib/SP/Repositories/RepositoryItemTrait.php | 46 ----- lib/SP/Repositories/Tag/TagRepository.php | 11 +- .../UserGroup/UserGroupRepository.php | 8 +- .../UserProfile/UserProfileRepository.php | 8 +- .../Services/Account/AccountSearchService.php | 15 +- lib/SP/Services/Account/AccountService.php | 40 +++-- lib/SP/Services/ApiToken/ApiTokenService.php | 135 ++++++++++++++ lib/SP/Services/Category/CategoryService.php | 70 +++++++- lib/SP/Services/Client/ClientService.php | 94 +++++++++- .../CustomField/CustomFieldDefService.php | 126 ++++++++++++++ .../Services/PublicLink/PublicLinkService.php | 137 +++++++++++++++ lib/SP/Services/ServiceItemInterface.php | 38 ++++ lib/SP/Services/ServiceItemTrait.php | 31 ++-- lib/SP/Services/Tag/TagService.php | 77 +++++++- lib/SP/Services/User/UserService.php | 18 +- .../Services/UserGroup/UserGroupService.php | 76 +++++++- .../UserGroup/UserToUserGroupService.php | 110 ++++++++++++ .../UserProfile/UserProfileService.php | 76 +++++++- lib/SP/Storage/QueryData.php | 10 ++ 57 files changed, 1779 insertions(+), 438 deletions(-) create mode 100644 lib/SP/DataModel/ItemData.php create mode 100644 lib/SP/Mvc/Model/QueryFilter.php create mode 100644 lib/SP/Mvc/View/Components/ItemAdapterInterface.php create mode 100644 lib/SP/Mvc/View/Components/SelectItem.php create mode 100644 lib/SP/Mvc/View/Components/SelectItemAdapter.php create mode 100644 lib/SP/Services/ApiToken/ApiTokenService.php create mode 100644 lib/SP/Services/CustomField/CustomFieldDefService.php create mode 100644 lib/SP/Services/PublicLink/PublicLinkService.php create mode 100644 lib/SP/Services/ServiceItemInterface.php create mode 100644 lib/SP/Services/UserGroup/UserToUserGroupService.php diff --git a/app/modules/web/Controllers/AccessManagerController.php b/app/modules/web/Controllers/AccessManagerController.php index f0fec5ba..684599c0 100644 --- a/app/modules/web/Controllers/AccessManagerController.php +++ b/app/modules/web/Controllers/AccessManagerController.php @@ -59,6 +59,7 @@ class AccessManagerController extends ControllerBase /** * @throws \SP\Core\Exceptions\InvalidArgumentException + * @throws \SP\Core\Dic\ContainerException */ public function indexAction() { @@ -69,6 +70,7 @@ class AccessManagerController extends ControllerBase * Returns a tabbed grid with items * * @throws \SP\Core\Exceptions\InvalidArgumentException + * @throws \SP\Core\Dic\ContainerException */ protected function getGridTabs() { diff --git a/app/modules/web/Controllers/AccountController.php b/app/modules/web/Controllers/AccountController.php index 4d45e8f5..f637af4c 100644 --- a/app/modules/web/Controllers/AccountController.php +++ b/app/modules/web/Controllers/AccountController.php @@ -27,17 +27,14 @@ namespace SP\Modules\Web\Controllers; use SP\Controller\ControllerBase; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; -use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Vault; use SP\Core\Exceptions\SPException; use SP\Core\Exceptions\ValidationException; use SP\Core\SessionUtil; use SP\DataModel\AccountExtData; -use SP\DataModel\Dto\AccountDetailsResponse; use SP\Forms\AccountForm; use SP\Http\JsonResponse; use SP\Http\Request; -use SP\Mgmt\Files\FileUtil; use SP\Modules\Web\Controllers\Helpers\AccountHelper; use SP\Modules\Web\Controllers\Helpers\AccountPasswordHelper; use SP\Modules\Web\Controllers\Helpers\AccountSearchHelper; @@ -45,7 +42,6 @@ use SP\Modules\Web\Controllers\Helpers\LayoutHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\Account\AccountRepository; use SP\Repositories\PublicLink\PublicLinkRepository; use SP\Services\Account\AccountFileService; use SP\Services\Account\AccountService; @@ -171,6 +167,7 @@ class AccountController extends ControllerBase implements CrudControllerInterfac * View public link action * * @param string $hash Link's hash + * @throws \SP\Core\Dic\ContainerException */ public function viewLinkAction($hash) { diff --git a/app/modules/web/Controllers/ApiTokenController.php b/app/modules/web/Controllers/ApiTokenController.php index c65b5c0d..fdc35839 100644 --- a/app/modules/web/Controllers/ApiTokenController.php +++ b/app/modules/web/Controllers/ApiTokenController.php @@ -41,8 +41,9 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\ApiToken\ApiTokenRepository; -use SP\Repositories\User\UserRepository; +use SP\Mvc\View\Components\SelectItemAdapter; +use SP\Services\ApiToken\ApiTokenService; +use SP\Services\User\UserService; /** * Class ApiTokenController @@ -55,12 +56,14 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa use ItemTrait; /** - * @var ApiTokenRepository + * @var ApiTokenService */ protected $apiTokenService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -110,7 +113,6 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa * * @param $apiTokenId * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Defuse\Crypto\Exception\CryptoException */ protected function setViewData($apiTokenId = null) { @@ -120,8 +122,8 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa $this->view->assign('apiToken', $apiToken); - $this->view->assign('users', UserRepository::getServiceItems()); - $this->view->assign('actions', ApiTokensUtil::getTokenActions()); + $this->view->assign('users', (new SelectItemAdapter(UserService::getItemsBasic()))->getItemsFromModelSelected([$apiToken->getUserId()])); + $this->view->assign('actions', (new SelectItemAdapter(ApiTokensUtil::getTokenActions()))->getItemsFromArraySelected([$apiToken->getActionId()])); $this->view->assign('sk', SessionUtil::getSessionKey(true)); $this->view->assign('nextAction', Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)); @@ -297,11 +299,13 @@ class ApiTokenController extends ControllerBase implements CrudControllerInterfa /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->apiTokenService = new ApiTokenRepository(); + $this->apiTokenService = new ApiTokenService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/CategoryController.php b/app/modules/web/Controllers/CategoryController.php index d8179b3d..843f2d27 100644 --- a/app/modules/web/Controllers/CategoryController.php +++ b/app/modules/web/Controllers/CategoryController.php @@ -24,7 +24,6 @@ namespace SP\Modules\Web\Controllers; - use SP\Controller\ControllerBase; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; @@ -39,7 +38,7 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\Category\CategoryRepository; +use SP\Services\Category\CategoryService; /** * Class CategoryController @@ -52,12 +51,14 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa use ItemTrait; /** - * @var CategoryRepository + * @var CategoryService */ protected $categoryService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -271,12 +272,14 @@ class CategoryController extends ControllerBase implements CrudControllerInterfa /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->categoryService = new CategoryRepository(); + $this->categoryService = new CategoryService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/ClientController.php b/app/modules/web/Controllers/ClientController.php index cf67527e..c4927ef1 100644 --- a/app/modules/web/Controllers/ClientController.php +++ b/app/modules/web/Controllers/ClientController.php @@ -39,7 +39,7 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\Client\ClientRepository; +use SP\Services\Client\ClientService; /** * Class ClientController @@ -52,12 +52,14 @@ class ClientController extends ControllerBase implements CrudControllerInterface use ItemTrait; /** - * @var ClientRepository + * @var ClientService */ protected $clientService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -267,12 +269,14 @@ class ClientController extends ControllerBase implements CrudControllerInterface /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->clientService = new ClientRepository(); + $this->clientService = new ClientService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/CustomFieldController.php b/app/modules/web/Controllers/CustomFieldController.php index 02de900c..0490aa47 100644 --- a/app/modules/web/Controllers/CustomFieldController.php +++ b/app/modules/web/Controllers/CustomFieldController.php @@ -41,6 +41,7 @@ use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; use SP\Repositories\CustomField\CustomFieldDefRepository; use SP\Repositories\CustomField\CustomFieldTypeRepository; +use SP\Services\CustomField\CustomFieldDefService; /** * Class CustomFieldController @@ -53,12 +54,14 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte use ItemTrait; /** - * @var CustomFieldDefRepository + * @var CustomFieldDefService */ protected $customFieldService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -270,12 +273,14 @@ class CustomFieldController extends ControllerBase implements CrudControllerInte /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->customFieldService = new CustomFieldDefRepository(); + $this->customFieldService = new CustomFieldDefService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/Helpers/AccountHelper.php b/app/modules/web/Controllers/Helpers/AccountHelper.php index 9b6cf79c..31c8db66 100644 --- a/app/modules/web/Controllers/Helpers/AccountHelper.php +++ b/app/modules/web/Controllers/Helpers/AccountHelper.php @@ -32,6 +32,7 @@ use SP\DataModel\Dto\AccountAclDto; use SP\DataModel\Dto\AccountDetailsResponse; use SP\Mgmt\Users\UserPass; use SP\Modules\Web\Controllers\Traits\ItemTrait; +use SP\Mvc\View\Components\SelectItemAdapter; use SP\Repositories\Account\AccountHistoryRepository; use SP\Repositories\PublicLink\PublicLinkRepository; use SP\Services\Account\AccountService; @@ -143,6 +144,7 @@ class AccountHelper extends HelperBase * Establecer variables comunes del formulario para todos los interfaces * * @throws \SP\Core\Exceptions\SPException + * @throws \SP\Core\Dic\ContainerException */ public function setCommonData() { @@ -190,17 +192,23 @@ class AccountHelper extends HelperBase $this->view->assign('customFields', $this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $this->accountId)); $this->view->assign('actionId', Acl::getActionRoute($this->actionId)); - $this->view->assign('categories', (new CategoryService())->getAllItemsForSelect()); + $this->view->assign('categories', (new SelectItemAdapter(CategoryService::getItemsBasic()))->getItemsFromModel()); - $this->view->assign('customers', (new ClientService())->getAllItemsForSelect()); + $this->view->assign('clients', (new SelectItemAdapter(ClientService::getItemsBasic()))->getItemsFromModel()); - $this->view->assign('otherUsers', (new UserService())->getAllItemsForSelect()); - $this->view->assign('otherUsersJson', Json::getJson($this->view->otherUsers)); + $userItemAdapter = new SelectItemAdapter(UserService::getItemsBasic()); - $this->view->assign('otherGroups', (new UserGroupService())->getAllItemsForSelect()); - $this->view->assign('otherGroupsJson', Json::getJson($this->view->otherGroups)); + $this->view->assign('otherUsers', $userItemAdapter->getItemsFromModel()); + $this->view->assign('otherUsersJson', $userItemAdapter->getJsonItemsFromModel()); - $this->view->assign('tagsJson', Json::getJson((new TagService())->getAllItemsForSelect())); + $userGroupItemAdapter = new SelectItemAdapter(UserGroupService::getItemsBasic()); + + $this->view->assign('otherGroups', $userGroupItemAdapter->getItemsFromModel()); + $this->view->assign('otherGroupsJson', $userGroupItemAdapter->getJsonItemsFromModel()); + + $tagItemAdapter = new SelectItemAdapter(TagService::getItemsBasic()); + + $this->view->assign('tagsJson', $tagItemAdapter->getJsonItemsFromModel()); $this->view->assign('allowPrivate', $userProfileData->isAccPrivate()); $this->view->assign('allowPrivateGroup', $userProfileData->isAccPrivateGroup()); $this->view->assign('mailRequestEnabled', $this->configData->isMailRequestsEnabled()); @@ -209,7 +217,8 @@ class AccountHelper extends HelperBase $this->view->assign('otherAccounts', $this->accountService->getForUser($this->accountId)); $this->view->assign('linkedAccounts', $this->accountService->getLinked($this->accountId)); - $this->view->assign('addCustomerEnabled', $this->acl->checkUserAccess(ActionsInterface::CLIENT)); + // FIXME: fix inline client/category creation + $this->view->assign('addClientEnabled', $this->acl->checkUserAccess(ActionsInterface::CLIENT)); $this->view->assign('addCategoryEnabled', $this->acl->checkUserAccess(ActionsInterface::CATEGORY)); $this->view->assign('disabled', $this->view->isView ? 'disabled' : ''); @@ -222,6 +231,8 @@ class AccountHelper extends HelperBase /** * Set icons for view + * + * @throws \SP\Core\Dic\ContainerException */ protected function getActions() { @@ -318,6 +329,7 @@ class AccountHelper extends HelperBase * Comprobar si el usuario dispone de acceso al módulo * * @return bool + * @throws \SP\Core\Dic\ContainerException */ public function checkAccess() { diff --git a/app/modules/web/Controllers/Helpers/AccountSearchHelper.php b/app/modules/web/Controllers/Helpers/AccountSearchHelper.php index 29bc6442..108239fe 100644 --- a/app/modules/web/Controllers/Helpers/AccountSearchHelper.php +++ b/app/modules/web/Controllers/Helpers/AccountSearchHelper.php @@ -36,10 +36,14 @@ use SP\Html\DataGrid\DataGridHeaderSort; use SP\Html\DataGrid\DataGridPager; use SP\Html\DataGrid\DataGridSort; use SP\Http\Request; +use SP\Mvc\View\Components\SelectItemAdapter; use SP\Repositories\Category\CategoryRepository; use SP\Repositories\Client\ClientRepository; use SP\Repositories\Tag\TagRepository; use SP\Services\Account\AccountSearchService; +use SP\Services\Category\CategoryService; +use SP\Services\Client\ClientService; +use SP\Services\Tag\TagService; /** * Class AccountSearch @@ -69,16 +73,16 @@ class AccountSearchHelper extends HelperBase /** * Obtener los datos para la caja de búsqueda + * + * @throws \SP\Core\Dic\ContainerException */ public function getSearchBox() { $this->view->addTemplate('search-searchbox'); - $clientService = new ClientRepository(); - - $this->view->assign('customers', $clientService->getItemsForSelectByUser()); - $this->view->assign('categories', CategoryRepository::getServiceItems()); - $this->view->assign('tags', TagRepository::getServiceItems()); + $this->view->assign('clients', (new SelectItemAdapter((new ClientService())->getAllForUser()))->getItemsFromModelSelected([$this->accountSearchFilter->getClientId()])); + $this->view->assign('categories', (new SelectItemAdapter(CategoryService::getItemsBasic()))->getItemsFromModelSelected([$this->accountSearchFilter->getCategoryId()])); + $this->view->assign('tags', (new SelectItemAdapter(TagService::getItemsBasic()))->getItemsFromModelSelected($this->accountSearchFilter->getTagsId())); } /** diff --git a/app/modules/web/Controllers/Helpers/HelperBase.php b/app/modules/web/Controllers/Helpers/HelperBase.php index c930aa2c..5968058e 100644 --- a/app/modules/web/Controllers/Helpers/HelperBase.php +++ b/app/modules/web/Controllers/Helpers/HelperBase.php @@ -68,6 +68,7 @@ abstract class HelperBase * @param Config $config * @param Session $session * @param EventDispatcher $eventDispatcher + * @throws \SP\Core\Dic\ContainerException */ final public function __construct(Template $template, Config $config, Session $session, EventDispatcher $eventDispatcher) { diff --git a/app/modules/web/Controllers/ItemsController.php b/app/modules/web/Controllers/ItemsController.php index 1c445488..c93fcc38 100644 --- a/app/modules/web/Controllers/ItemsController.php +++ b/app/modules/web/Controllers/ItemsController.php @@ -24,7 +24,6 @@ namespace SP\Modules\Web\Controllers; -use SP\Account\AccountUtil; use SP\Controller\RequestControllerTrait; use SP\Core\SessionUtil; use SP\DataModel\DataModelInterface; diff --git a/app/modules/web/Controllers/MainController.php b/app/modules/web/Controllers/MainController.php index 23c3c100..0c740a31 100644 --- a/app/modules/web/Controllers/MainController.php +++ b/app/modules/web/Controllers/MainController.php @@ -27,28 +27,23 @@ namespace SP\Modules\Web\Controllers; defined('APP_ROOT') || die(); use SP\Account\AccountUtil; -use SP\Controller\AccountController; use SP\Controller\ControllerBase; use SP\Core\Acl\ActionsInterface; use SP\Core\DiFactory; use SP\Core\Exceptions\SPException; use SP\Core\Init; use SP\Core\Language; -use SP\Core\Messages\NoticeMessage; use SP\Core\Plugin\PluginUtil; use SP\Core\SessionFactory; use SP\Core\SessionUtil; use SP\Core\Task; -use SP\Mvc\View\Template; use SP\Core\Upgrade\Check; -use SP\DataModel\NoticeData; use SP\Html\DataGrid\DataGridAction; use SP\Html\Html; use SP\Http\Request; use SP\Mgmt\Notices\Notice; -use SP\Mgmt\PublicLinks\PublicLink; +use SP\Mvc\View\Template; use SP\Util\Checks; -use SP\Util\HttpUtil; use SP\Util\Util; /** diff --git a/app/modules/web/Controllers/PassresetController.php b/app/modules/web/Controllers/PassresetController.php index 0915e230..57671c0b 100644 --- a/app/modules/web/Controllers/PassresetController.php +++ b/app/modules/web/Controllers/PassresetController.php @@ -38,6 +38,8 @@ class PassresetController extends ControllerBase { /** * Password reset action + * + * @throws \SP\Core\Dic\ContainerException */ public function indexAction() { diff --git a/app/modules/web/Controllers/PublicLinkController.php b/app/modules/web/Controllers/PublicLinkController.php index 62a2a098..a47d32d8 100644 --- a/app/modules/web/Controllers/PublicLinkController.php +++ b/app/modules/web/Controllers/PublicLinkController.php @@ -25,7 +25,6 @@ namespace SP\Modules\Web\Controllers; use Defuse\Crypto\Exception\CryptoException; -use SP\Account\AccountUtil; use SP\Controller\ControllerBase; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; @@ -36,12 +35,14 @@ use SP\DataModel\PublicLinkListData; use SP\Forms\PublicLinkForm; use SP\Http\JsonResponse; use SP\Http\Request; -use SP\Mgmt\PublicLinks\PublicLink; use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; +use SP\Mvc\View\Components\SelectItemAdapter; use SP\Repositories\PublicLink\PublicLinkRepository; +use SP\Services\Account\AccountService; +use SP\Services\PublicLink\PublicLinkService; /** * Class PublicLinkController @@ -54,12 +55,14 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter use ItemTrait; /** - * @var PublicLinkRepository + * @var PublicLinkService */ protected $publicLinkService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -108,8 +111,8 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter * Sets view data for displaying user's data * * @param $publicLinkId - * @throws SPException * @throws \Psr\Container\ContainerExceptionInterface + * @throws SPException */ protected function setViewData($publicLinkId = null) { @@ -118,7 +121,7 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter $publicLink = $publicLinkId ? $this->publicLinkService->getById($publicLinkId) : new PublicLinkListData(); $this->view->assign('publicLink', $publicLink); - $this->view->assign('accounts', AccountUtil::getAccountsForUser($this->session)); + $this->view->assign('accounts', (new SelectItemAdapter((new AccountService())->getForUser()))->getItemsFromModelSelected([$publicLink->getItemId()])); $this->view->assign('sk', SessionUtil::getSessionKey(true)); $this->view->assign('nextAction', Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)); @@ -285,11 +288,13 @@ class PublicLinkController extends ControllerBase implements CrudControllerInter /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->publicLinkService = new PublicLinkRepository(); + $this->publicLinkService = new PublicLinkService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/TagController.php b/app/modules/web/Controllers/TagController.php index 20f8ec2e..6af7dfae 100644 --- a/app/modules/web/Controllers/TagController.php +++ b/app/modules/web/Controllers/TagController.php @@ -39,7 +39,7 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\Tag\TagRepository; +use SP\Services\Tag\TagService; /** * Class TagController @@ -52,12 +52,14 @@ class TagController extends ControllerBase implements CrudControllerInterface use ItemTrait; /** - * @var TagRepository + * @var TagService */ protected $tagService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -265,12 +267,14 @@ class TagController extends ControllerBase implements CrudControllerInterface /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->tagService = new TagRepository(); + $this->tagService = new TagService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/UserController.php b/app/modules/web/Controllers/UserController.php index 15cd5d9c..dddf4136 100644 --- a/app/modules/web/Controllers/UserController.php +++ b/app/modules/web/Controllers/UserController.php @@ -24,7 +24,6 @@ namespace SP\Modules\Web\Controllers; -use SP\Providers\Auth\AuthUtil; use SP\Controller\ControllerBase; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; @@ -39,10 +38,11 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\User\UserRepository; -use SP\Repositories\UserGroup\UserGroupRepository; -use SP\Repositories\UserProfile\UserProfileRepository; +use SP\Mvc\View\Components\SelectItemAdapter; +use SP\Providers\Auth\AuthUtil; use SP\Services\User\UserService; +use SP\Services\UserGroup\UserGroupService; +use SP\Services\UserProfile\UserProfileService; /** * Class UserController @@ -61,6 +61,8 @@ class UserController extends ControllerBase implements CrudControllerInterface /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -119,8 +121,8 @@ class UserController extends ControllerBase implements CrudControllerInterface $user = $userId ? $this->userService->getById($userId) : new UserData(); $this->view->assign('user', $user); - $this->view->assign('groups', UserGroupRepository::getServiceItems()); - $this->view->assign('profiles', UserProfileRepository::getServiceItems()); + $this->view->assign('groups', (new SelectItemAdapter(UserGroupService::getItemsBasic()))->getItemsFromModel()); + $this->view->assign('profiles', (new SelectItemAdapter(UserProfileService::getItemsBasic()))->getItemsFromModel()); $this->view->assign('isUseSSO', $this->configData->isAuthBasicAutoLoginEnabled()); $this->view->assign('sk', SessionUtil::getSessionKey(true)); $this->view->assign('nextAction', Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)); @@ -374,6 +376,6 @@ class UserController extends ControllerBase implements CrudControllerInterface { $this->checkLoggedIn(); - $this->userService = new UserRepository(); + $this->userService = new UserService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/UserGroupController.php b/app/modules/web/Controllers/UserGroupController.php index 477bd858..5e170db8 100644 --- a/app/modules/web/Controllers/UserGroupController.php +++ b/app/modules/web/Controllers/UserGroupController.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -38,10 +38,10 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\User\UserRepository; -use SP\Repositories\UserGroup\UserGroupRepository; -use SP\Repositories\UserGroup\UserToGroupService; -use SP\Repositories\UserGroup\UserToUserGroupRepository; +use SP\Mvc\View\Components\SelectItemAdapter; +use SP\Services\User\UserService; +use SP\Services\UserGroup\UserGroupService; +use SP\Services\UserGroup\UserToUserGroupService; /** * Class GroupController @@ -54,16 +54,18 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf use ItemTrait; /** - * @var UserGroupRepository + * @var UserGroupService */ protected $userGroupService; /** - * @var UserToUserGroupRepository + * @var UserToUserGroupService */ protected $userToUserGroupService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -120,8 +122,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf $group = $userGroupId ? $this->userGroupService->getById($userGroupId) : new UserGroupData(); $this->view->assign('group', $group); - $this->view->assign('users', UserRepository::getServiceItems()); - $this->view->assign('groupUsers', $this->userToUserGroupService->getById($userGroupId)); + $this->view->assign('users', (new SelectItemAdapter(UserService::getItemsBasic()))->getItemsFromModelSelected($this->userToUserGroupService->getUsersByGroupId($userGroupId))); $this->view->assign('sk', SessionUtil::getSessionKey(true)); $this->view->assign('nextAction', Acl::getActionRoute(ActionsInterface::ACCESS_MANAGE)); @@ -176,7 +177,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf } try { - $this->userGroupService->logAction($id, ActionsInterface::GROUP_DELETE); +// $this->userGroupService->logAction($id, ActionsInterface::GROUP_DELETE); $this->userGroupService->delete($id); $this->deleteCustomFieldsForItem(ActionsInterface::GROUP, $id); @@ -207,7 +208,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf $groupData = $form->getItemData(); $id = $this->userGroupService->create($groupData); - $this->userGroupService->logAction($id, ActionsInterface::GROUP_CREATE); +// $this->userGroupService->logAction($id, ActionsInterface::GROUP_CREATE); $this->userToUserGroupService->add($id, $groupData->getUsers()); $this->addCustomFieldsForItem(ActionsInterface::GROUP, $id); @@ -242,7 +243,7 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf $groupData = $form->getItemData(); $this->userGroupService->update($groupData); - $this->userGroupService->logAction($id, ActionsInterface::GROUP_EDIT); +// $this->userGroupService->logAction($id, ActionsInterface::GROUP_EDIT); $this->userToUserGroupService->update($groupData->getId(), $groupData->getUsers()); $this->updateCustomFieldsForItem(ActionsInterface::GROUP, $id); @@ -287,12 +288,14 @@ class UserGroupController extends ControllerBase implements CrudControllerInterf /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->userGroupService = new UserGroupRepository(); - $this->userToUserGroupService = new UserToUserGroupRepository(); + $this->userGroupService = new UserGroupService(); + $this->userToUserGroupService = new UserToUserGroupService(); } } \ No newline at end of file diff --git a/app/modules/web/Controllers/UserProfileController.php b/app/modules/web/Controllers/UserProfileController.php index 8a6aad16..46b86410 100644 --- a/app/modules/web/Controllers/UserProfileController.php +++ b/app/modules/web/Controllers/UserProfileController.php @@ -38,7 +38,7 @@ use SP\Modules\Web\Controllers\Helpers\ItemsGridHelper; use SP\Modules\Web\Controllers\Traits\ItemTrait; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Mvc\Controller\CrudControllerInterface; -use SP\Repositories\UserProfile\UserProfileRepository; +use SP\Services\UserProfile\UserProfileService; /** * Class UserProfileController @@ -51,12 +51,14 @@ class UserProfileController extends ControllerBase implements CrudControllerInte use ItemTrait; /** - * @var UserProfileRepository + * @var UserProfileService */ protected $userProfileService; /** * Search action + * + * @throws \SP\Core\Dic\ContainerException */ public function searchAction() { @@ -106,7 +108,6 @@ class UserProfileController extends ControllerBase implements CrudControllerInte * * @param $profileId * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Defuse\Crypto\Exception\CryptoException */ protected function setViewData($profileId = null) { @@ -171,7 +172,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte } try { - $this->userProfileService->logAction($id, ActionsInterface::PROFILE_DELETE); +// $this->userProfileService->logAction($id, ActionsInterface::PROFILE_DELETE); $this->userProfileService->delete($id); $this->deleteCustomFieldsForItem(ActionsInterface::PROFILE, $id); @@ -202,7 +203,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte $profileData = $form->getItemData(); $id = $this->userProfileService->create($profileData); - $this->userProfileService->logAction($id, ActionsInterface::PROFILE_CREATE); +// $this->userProfileService->logAction($id, ActionsInterface::PROFILE_CREATE); $this->addCustomFieldsForItem(ActionsInterface::PROFILE, $id); @@ -236,7 +237,7 @@ class UserProfileController extends ControllerBase implements CrudControllerInte $profileData = $form->getItemData(); $this->userProfileService->update($profileData); - $this->userProfileService->logAction($id, ActionsInterface::PROFILE_EDIT); +// $this->userProfileService->logAction($id, ActionsInterface::PROFILE_EDIT); $this->updateCustomFieldsForItem(ActionsInterface::PROFILE, $id); @@ -280,11 +281,13 @@ class UserProfileController extends ControllerBase implements CrudControllerInte /** * Initialize class + * + * @throws \SP\Core\Dic\ContainerException */ protected function initialize() { $this->checkLoggedIn(); - $this->userProfileService = new UserProfileRepository(); + $this->userProfileService = new UserProfileService(); } } \ No newline at end of file diff --git a/app/modules/web/themes/material-blue/views/account/account-permissions.inc b/app/modules/web/themes/material-blue/views/account/account-permissions.inc index 143ed343..cf2ce573 100644 --- a/app/modules/web/themes/material-blue/views/account/account-permissions.inc +++ b/app/modules/web/themes/material-blue/views/account/account-permissions.inc @@ -25,11 +25,11 @@ @@ -47,8 +47,8 @@ - + + value="getId(); ?>" getId() === $accountData->getUserGroupId()) ? 'selected' : ''; ?>>getName(); ?> @@ -123,8 +124,8 @@ $('#otherUsers').selectize({ persist: false, valueField: 'id', - labelField: 'login', - searchField: ['login'], + labelField: 'name', + searchField: ['name'], plugins: ['remove_button'], options: , onInitialize: function () { diff --git a/app/modules/web/themes/material-blue/views/account/account.inc b/app/modules/web/themes/material-blue/views/account/account.inc index 96acac03..25dfe185 100644 --- a/app/modules/web/themes/material-blue/views/account/account.inc +++ b/app/modules/web/themes/material-blue/views/account/account.inc @@ -48,12 +48,13 @@ - + > - + + value="getId(); ?>" getId() === $accountData->getCategoryId()) ? 'selected' : ''; ?>>getName(); ?> diff --git a/app/modules/web/themes/material-blue/views/account/search-searchbox.inc b/app/modules/web/themes/material-blue/views/account/search-searchbox.inc index c7588d2b..9be6d4c8 100644 --- a/app/modules/web/themes/material-blue/views/account/search-searchbox.inc +++ b/app/modules/web/themes/material-blue/views/account/search-searchbox.inc @@ -47,17 +47,19 @@ @@ -96,9 +98,10 @@
diff --git a/app/modules/web/themes/material-blue/views/itemshow/apitoken.inc b/app/modules/web/themes/material-blue/views/itemshow/apitoken.inc index 9b74efe5..4554d080 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/apitoken.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/apitoken.inc @@ -20,9 +20,9 @@ @@ -35,9 +35,9 @@ diff --git a/app/modules/web/themes/material-blue/views/itemshow/client.inc b/app/modules/web/themes/material-blue/views/itemshow/client.inc index c192f0d1..6e2aad0e 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/client.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/client.inc @@ -1,7 +1,7 @@ @@ -22,7 +22,7 @@
+ maxlength="50" value="getName(); ?>">
@@ -35,7 +35,7 @@
+ maxlength="50" value="getDescription(); ?>">
@@ -59,7 +59,7 @@ diff --git a/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc b/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc index 4e593373..c88c8f53 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/publiclink.inc @@ -20,9 +20,10 @@ diff --git a/app/modules/web/themes/material-blue/views/itemshow/user.inc b/app/modules/web/themes/material-blue/views/itemshow/user.inc index 0c68e982..419e1dd4 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/user.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/user.inc @@ -71,9 +71,10 @@ @@ -87,9 +88,10 @@ diff --git a/app/modules/web/themes/material-blue/views/itemshow/usergroup.inc b/app/modules/web/themes/material-blue/views/itemshow/usergroup.inc index e9bead88..1a5d5894 100644 --- a/app/modules/web/themes/material-blue/views/itemshow/usergroup.inc +++ b/app/modules/web/themes/material-blue/views/itemshow/usergroup.inc @@ -52,11 +52,10 @@ use SP\Util\ArrayUtil; diff --git a/lib/SP/Account/AccountSearchFilter.php b/lib/SP/Account/AccountSearchFilter.php index e5a15727..8c3650f5 100644 --- a/lib/SP/Account/AccountSearchFilter.php +++ b/lib/SP/Account/AccountSearchFilter.php @@ -296,27 +296,27 @@ class AccountSearchFilter { switch ($this->sortKey) { case self::SORT_NAME: - $orderKey[] = 'name'; + $orderKey[] = 'A.name'; break; case self::SORT_CATEGORY: - $orderKey[] = 'categoryName'; + $orderKey[] = 'A.categoryName'; break; case self::SORT_LOGIN: - $orderKey[] = 'login'; + $orderKey[] = 'A.login'; break; case self::SORT_URL: - $orderKey[] = 'url'; + $orderKey[] = 'A.url'; break; case self::SORT_CLIENT: - $orderKey[] = 'clientName'; + $orderKey[] = 'A.clientName'; break; default : - $orderKey[] = 'name'; + $orderKey[] = 'A.name'; break; } if ($this->isSortViews() && !$this->getSortKey()) { - array_unshift($orderKey, 'countView DESC'); + array_unshift($orderKey, 'A.countView DESC'); $this->setSortOrder(self::SORT_DIR_DESC); } diff --git a/lib/SP/Account/AccountUtil.php b/lib/SP/Account/AccountUtil.php index 262e1e43..b3707934 100644 --- a/lib/SP/Account/AccountUtil.php +++ b/lib/SP/Account/AccountUtil.php @@ -32,6 +32,7 @@ use SP\Core\Exceptions\SPException; use SP\Core\Session\Session; use SP\Core\SessionFactory; use SP\DataModel\ItemSearchData; +use SP\Mvc\Model\QueryFilter; use SP\Storage\DbWrapper; use SP\Storage\QueryData; @@ -223,34 +224,33 @@ class AccountUtil return []; } - $Data = new QueryData(); - - $queryWhere = self::getAccountFilterUser($Data, $session); - - $queryWhere[] = 'A.parentId = ?'; - $Data->addParam($accountId); + $queryFilter = self::getAccountFilterUser($session) + ->addFilter('A.parentId = ?', [$accountId]); $query = /** @lang SQL */ 'SELECT A.id, A.name, C.name AS clientName FROM Account A INNER JOIN Client C ON Account.clientId = C.id - WHERE ' . implode(' AND ', $queryWhere) . ' ORDER BY name'; + WHERE ' . $queryFilter->getFilters() . ' ORDER BY name'; - $Data->setQuery($query); + $queryData = new QueryData(); + $queryData->setParams($queryFilter->getParams()); + $queryData->setQuery($query); - return DbWrapper::getResultsArray($Data); + return DbWrapper::getResultsArray($queryData); } /** * Devuelve el filtro para la consulta SQL de cuentas que un usuario puede acceder * - * @param QueryData $Data - * @param Session $session - * @param bool $useGlobalSearch - * @return array + * @param Session $session + * @param bool $useGlobalSearch + * @return QueryFilter */ - public static function getAccountFilterUser(QueryData $Data, Session $session, $useGlobalSearch = false) + public static function getAccountFilterUser(Session $session, $useGlobalSearch = false) { + $queryFilter = new QueryFilter(); + $configData = $session->getConfig(); $userData = $session->getUserData(); @@ -259,39 +259,29 @@ class AccountUtil && !($useGlobalSearch && $session->getUserProfile()->isAccGlobalSearch() && $configData->isGlobalSearch()) ) { // Filtro usuario y grupo - $filterUser[] = 'userId = ?'; - $Data->addParam($userData->getId()); + $filter = + /** @lang SQL */ + 'A.userId = ? + OR A.userGroupId = ? + OR A.id IN (SELECT accountId AS accountId FROM AccountToUser WHERE accountId = A.id AND userId = ? UNION ALL SELECT accountId FROM AccountToUserGroup WHERE accountId = A.id AND userGroupId = ?) + OR A.userGroupId IN (SELECT userGroupId FROM UserToUserGroup WHERE userGroupId = Account.userGroupId AND userId = ?)'; - $filterUser[] = 'userGroupId = ?'; - $Data->addParam($userData->getUserGroupId()); - - // Filtro de cuenta en usuarios y grupos secundarios - $filterUser[] = /** @lang SQL */ - 'A.id IN (SELECT accountId AS accountId FROM AccountToUser WHERE accountId = A.id AND userId = ? UNION ALL SELECT accountId FROM AccountToUserGroup WHERE accountId = A.id AND userGroupId = ?)'; - $Data->addParam($userData->getId()); - $Data->addParam($userData->getUserGroupId()); - - // Filtro de grupo principal de cuenta en grupos que incluyen al usuario - $filterUser[] = /** @lang SQL */ - 'A.userGroupId IN (SELECT userGroupId FROM UserToUserGroup WHERE userGroupId = Account.userGroupId AND userId = ?)'; - $Data->addParam($userData->getId()); + $params = [$userData->getId(), $userData->getUserGroupId(), $userData->getId(), $userData->getUserGroupId(), $userData->getId()]; if ($configData->isAccountFullGroupAccess()) { // Filtro de grupos secundarios en grupos que incluyen al usuario - $filterUser[] = /** @lang SQL */ - 'A.id = (SELECT accountId FROM AccountToUserGroup aug INNER JOIN UserToUserGroup uug ON uug.userGroupId = aug.userGroupId WHERE aug.accountId = A.id AND uug.userId = ? LIMIT 1)'; - $Data->addParam($userData->getId()); + $filter .= /** @lang SQL */ + PHP_EOL . 'OR A.id = (SELECT accountId FROM AccountToUserGroup aug INNER JOIN UserToUserGroup uug ON uug.userGroupId = aug.userGroupId WHERE aug.accountId = A.id AND uug.userId = ? LIMIT 1)'; + $params[] = $userData->getId(); } - $queryWhere[] = '(' . implode(' OR ', $filterUser) . ')'; + $queryFilter->addFilter($filter, $params); } - $queryWhere[] = '(isPrivate = 0 OR (isPrivate = 1 AND userId = ?))'; - $Data->addParam($userData->getId()); - $queryWhere[] = '(isPrivateGroup = 0 OR (isPrivateGroup = 1 AND userGroupId = ?))'; - $Data->addParam($userData->getUserGroupId()); + $queryFilter->addFilter(/** @lang SQL */ + '(A.isPrivate = 0 OR (A.isPrivate = 1 AND A.userId = ?)) AND (A.isPrivateGroup = 0 OR (A.isPrivateGroup = 1 AND A.userGroupId = ?))', [$userData->getId(), $userData->getUserGroupId()]); - return $queryWhere; + return $queryFilter; } /** @@ -356,24 +346,24 @@ class AccountUtil */ public static function getAccountsForUser(Session $session, $accountId = null) { - $Data = new QueryData(); - - $queryWhere = self::getAccountFilterUser($Data, $session); + $queryFilter = self::getAccountFilterUser($session); if (null !== $accountId) { - $queryWhere[] = 'A.id <> ? AND (A.parentId = 0 OR A.parentId IS NULL)'; - $Data->addParam($accountId); + $queryFilter->addFilter('A.id <> ? AND (A.parentId = 0 OR A.parentId IS NULL)', [$accountId]); } $query = /** @lang SQL */ 'SELECT A.id, A.name, C.name AS clientName FROM Account A LEFT JOIN Client C ON A.clientId = C.id - WHERE ' . implode(' AND ', $queryWhere) . ' ORDER BY name'; + WHERE ' . $queryFilter->getFilters() . ' ORDER BY name'; - $Data->setQuery($query); - return DbWrapper::getResultsArray($Data); + $queryData = new QueryData(); + $queryData->setQuery($query); + $queryData->setParams($queryFilter->getParams()); + + return DbWrapper::getResultsArray($queryData); } /** diff --git a/lib/SP/DataModel/ItemData.php b/lib/SP/DataModel/ItemData.php new file mode 100644 index 00000000..9587880b --- /dev/null +++ b/lib/SP/DataModel/ItemData.php @@ -0,0 +1,58 @@ +. + */ + +namespace SP\DataModel; + +/** + * Class ItemData + * + * @package SP\DataModel + */ +class ItemData implements DataModelInterface +{ + /** + * @var int + */ + public $id; + /** + * @var string + */ + public $name; + + /** + * @return int + */ + public function getId() + { + return (int)$this->id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } +} \ No newline at end of file diff --git a/lib/SP/DataModel/UserGroupData.php b/lib/SP/DataModel/UserGroupData.php index 66759eda..94835e4c 100644 --- a/lib/SP/DataModel/UserGroupData.php +++ b/lib/SP/DataModel/UserGroupData.php @@ -87,7 +87,7 @@ class UserGroupData extends DataModelBase implements DataModelInterface */ public function getUsers() { - return (is_array($this->users)) ? $this->users : []; + return is_array($this->users) ? $this->users : []; } /** diff --git a/lib/SP/Mvc/Model/QueryFilter.php b/lib/SP/Mvc/Model/QueryFilter.php new file mode 100644 index 00000000..1f9254d3 --- /dev/null +++ b/lib/SP/Mvc/Model/QueryFilter.php @@ -0,0 +1,95 @@ +. + */ + +namespace SP\Mvc\Model; + +/** + * Class QueryFilter + * + * @package SP\Mvc\Model + */ +class QueryFilter +{ + const FILTER_AND = ' AND '; + const FILTER_OR = ' OR '; + + /** + * @var array + */ + protected $query = []; + /** + * @var array + */ + protected $param = []; + + /** + * @param $query + * @param $params + * @return QueryFilter + */ + public function addFilter($query, array $params) + { + $this->query[] = $query; + $this->param = array_merge($this->param, $params); + + return $this; + } + + /** + * @param string $type + * @return string|null + */ + public function getFilters($type = self::FILTER_AND) + { + if ($type !== self::FILTER_AND && $type !== self::FILTER_OR) { + throw new \RuntimeException(__u('Tipo de filtro inválido')); + } + + return $this->hasFilters() ? implode($type, $this->query) : null; + } + + /** + * @return bool + */ + public function hasFilters() + { + return !empty($this->query); + } + + /** + * @return array + */ + public function getParams() + { + return $this->param; + } + + /** + * @return int + */ + public function getFiltersCount() + { + return count($this->query); + } +} \ No newline at end of file diff --git a/lib/SP/Mvc/View/Components/ItemAdapterInterface.php b/lib/SP/Mvc/View/Components/ItemAdapterInterface.php new file mode 100644 index 00000000..2701012f --- /dev/null +++ b/lib/SP/Mvc/View/Components/ItemAdapterInterface.php @@ -0,0 +1,61 @@ +. + */ + +namespace SP\Mvc\View\Components; + +/** + * Interface ItemAdapterInterface + * + * @package SP\Mvc\View\Components + */ +interface ItemAdapterInterface +{ + /** + * Returns a collection of items for a select component + * + * @return array + */ + public function getItemsFromModel(); + + /** + * Returns a JSON like collection of items for a select component + * + * @return string + */ + public function getJsonItemsFromModel(); + + /** + * Returns a collection of items for a select component + * + * @return array + */ + public function getItemsFromArray(); + + /** + * Returns a collection of items for a select component + * + * @return string + */ + public function getJsonItemsFromArray(); +} \ No newline at end of file diff --git a/lib/SP/Mvc/View/Components/SelectItem.php b/lib/SP/Mvc/View/Components/SelectItem.php new file mode 100644 index 00000000..1042edbc --- /dev/null +++ b/lib/SP/Mvc/View/Components/SelectItem.php @@ -0,0 +1,105 @@ +. + */ + +namespace SP\Mvc\View\Components; + +/** + * Class SelectItem + * + * @package SP\Mvc\View\Components + */ +class SelectItem +{ + /** + * @var int + */ + protected $id; + /** + * @var string + */ + protected $name; + /** + * @var mixed + */ + protected $item; + /** + * @var bool + */ + protected $selected = false; + + /** + * SelectItem constructor. + * + * @param int $id + * @param string $name + * @param null $item + */ + public function __construct($id, $name, $item = null) + { + $this->id = (int)$id; + $this->name = (string)$name; + $this->item = $item; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return bool + */ + public function isSelected() + { + return $this->selected; + } + + /** + * @param bool $selected + */ + public function setSelected($selected) + { + $this->selected = (bool)$selected; + } + + /** + * @param $property + * @return mixed + */ + public function getItemProperty($property) + { + return null !== $this->item && isset($this->item->{$property}) ? $this->item->{$property} : null; + } +} \ No newline at end of file diff --git a/lib/SP/Mvc/View/Components/SelectItemAdapter.php b/lib/SP/Mvc/View/Components/SelectItemAdapter.php new file mode 100644 index 00000000..ad660a42 --- /dev/null +++ b/lib/SP/Mvc/View/Components/SelectItemAdapter.php @@ -0,0 +1,164 @@ +. + */ + +namespace SP\Mvc\View\Components; + +use RuntimeException; +use SP\DataModel\DataModelInterface; +use SP\Util\Json; + +/** + * Class SelectItemAdapter + * + * @package SP\Mvc\View\Components + */ +class SelectItemAdapter implements ItemAdapterInterface +{ + /** + * @var array + */ + protected $items; + + /** + * SelectItemAdapter constructor. + * + * @param array $items + */ + public function __construct(array $items) + { + $this->items = $items; + } + + /** + * Returns a JSON like collection of items for a select component + * + * @return string + * @throws \SP\Core\Exceptions\SPException + */ + public function getJsonItemsFromModel() + { + $out = []; + + foreach ($this->items as $item) { + if (!$item instanceof DataModelInterface) { + throw new RuntimeException(__u('Tipo de objeto incorrecto')); + } + + $out[] = ['id' => $item->getId(), 'name' => $item->getName()]; + } + + return Json::getJson($out); + } + + /** + * Returns a collection of items for a select component + * + * @return array + */ + public function getItemsFromArray() + { + $out = []; + + foreach ($this->items as $key => $value) { + $out[] = new SelectItem($key, $value); + } + + return $out; + } + + /** + * Returns a collection of items for a select component + * + * @return string + * @throws \SP\Core\Exceptions\SPException + */ + public function getJsonItemsFromArray() + { + $out = []; + + foreach ($this->items as $key => $value) { + $out[] = ['id' => $key, 'name' => $value]; + } + + return Json::getJson($out); + } + + /** + * Returns a collection of items for a select component and set selected ones from an array + * + * @param array $selected + * @return SelectItem[] + */ + public function getItemsFromModelSelected(array $selected) + { + $items = $this->getItemsFromModel(); + + foreach ($items as $item) { + if ($selected !== null && in_array($item->getId(), $selected, false)) { + $item->setSelected(true); + } + } + + return $items; + } + + /** + * Returns a collection of items for a select component + * + * @return SelectItem[] + */ + public function getItemsFromModel() + { + $out = []; + + foreach ($this->items as $item) { + if (!$item instanceof DataModelInterface) { + throw new RuntimeException(__u('Tipo de objeto incorrecto')); + } + + $out[] = new SelectItem($item->getId(), $item->getName(), $item); + } + + return $out; + } + + /** + * Returns a collection of items for a select component and set selected ones from an array + * + * @param array $selected + * @return SelectItem[] + */ + public function getItemsFromArraySelected(array $selected) + { + $items = $this->getItemsFromArray(); + + foreach ($items as $item) { + if ($selected !== null && in_array($item->getId(), $selected, false)) { + $item->setSelected(true); + } + } + + return $items; + } +} \ No newline at end of file diff --git a/lib/SP/Repositories/Account/AccountRepository.php b/lib/SP/Repositories/Account/AccountRepository.php index 149a33fa..cec0656d 100644 --- a/lib/SP/Repositories/Account/AccountRepository.php +++ b/lib/SP/Repositories/Account/AccountRepository.php @@ -34,7 +34,9 @@ use SP\DataModel\AccountPassData; use SP\DataModel\AccountSearchVData; use SP\DataModel\AccountVData; use SP\DataModel\Dto\AccountSearchResponse; +use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; +use SP\Mvc\Model\QueryFilter; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; @@ -56,20 +58,18 @@ class AccountRepository extends Repository implements RepositoryItemInterface */ public function getPasswordForId($id) { - $Data = new QueryData(); - $Data->setMapClassName(AccountPassData::class); - $Data->setLimit(1); + $queryFilter = AccountUtil::getAccountFilterUser($this->session) + ->addFilter('A.id = ?', [$id]); - $Data->setSelect('A.id, A.name, A.login, A.pass, A.key, A.parentId'); - $Data->setFrom('Account A'); + $queryData = new QueryData(); + $queryData->setMapClassName(AccountPassData::class); + $queryData->setLimit(1); + $queryData->setSelect('A.id, A.name, A.login, A.pass, A.key, A.parentId'); + $queryData->setFrom('Account A'); + $queryData->setWhere($queryFilter->getFilters()); + $queryData->setParams($queryFilter->getParams()); - $queryWhere = AccountUtil::getAccountFilterUser($Data, $this->session); - $queryWhere[] = 'A.id = ?'; - $Data->addParam($id); - - $Data->setWhere($queryWhere); - - return DbWrapper::getResults($Data, $this->db); + return DbWrapper::getResults($queryData, $this->db); } /** @@ -526,160 +526,126 @@ class AccountRepository extends Repository implements RepositoryItemInterface */ public function getByFilter(AccountSearchFilter $accountSearchFilter) { - $arrFilterCommon = []; - $arrFilterSelect = []; - $arrayQueryJoin = []; - $arrQueryWhere = []; + $queryJoin = []; $queryLimit = ''; - $data = new QueryData(); + $queryFilterCommon = new QueryFilter(); + $queryFilterSelect = new QueryFilter(); - $txtSearch = $accountSearchFilter->getTxtSearch(); + $searchText = $accountSearchFilter->getTxtSearch(); - if ($txtSearch !== null && $txtSearch !== '') { + if ($searchText !== null && $searchText !== '') { // Analizar la cadena de búsqueda por etiquetas especiales $stringFilter = $accountSearchFilter->getStringFilters(); if (!empty($stringFilter)) { - $arrFilterCommon[] = $stringFilter['query']; foreach ($stringFilter['values'] as $value) { - $data->addParam($value); + $queryFilterCommon->addFilter($stringFilter['query'], [$value]); } } else { - $txtSearch = '%' . $txtSearch . '%'; + $searchText = '%' . $searchText . '%'; - $arrFilterCommon[] = 'name LIKE ?'; - $data->addParam($txtSearch); - - $arrFilterCommon[] = 'login LIKE ?'; - $data->addParam($txtSearch); - - $arrFilterCommon[] = 'url LIKE ?'; - $data->addParam($txtSearch); - - $arrFilterCommon[] = 'notes LIKE ?'; - $data->addParam($txtSearch); + $queryFilterCommon->addFilter('A.name LIKE ? OR A.login LIKE ? OR A.url LIKE ? OR A.notes LIKE ?', [$searchText, $searchText, $searchText, $searchText]); } } if ($accountSearchFilter->getCategoryId() !== 0) { - $arrFilterSelect[] = 'categoryId = ?'; - $data->addParam($accountSearchFilter->getCategoryId()); + $queryFilterSelect->addFilter('A.categoryId = ?', [$accountSearchFilter->getCategoryId()]); } if ($accountSearchFilter->getClientId() !== 0) { - $arrFilterSelect[] = 'clientId = ?'; - $data->addParam($accountSearchFilter->getClientId()); + $queryFilterSelect->addFilter('A.clientId = ?', [$accountSearchFilter->getClientId()]); } $tagsId = $accountSearchFilter->getTagsId(); $numTags = count($tagsId); if ($numTags > 0) { - $tags = str_repeat('?,', $numTags - 1) . '?'; - - $arrFilterSelect[] = 'id IN (SELECT accountId FROM AccountToTag WHERE tagId IN (' . $tags . '))'; - - foreach ($tagsId as $tag) { - $data->addParam($tag); - } + $queryFilterSelect->addFilter('A.id IN (SELECT accountId FROM AccountToTag WHERE tagId IN (' . str_repeat('?,', $numTags - 1) . '?' . '))', $tagsId); } - if ($accountSearchFilter->isSearchFavorites() === true) { - $arrayQueryJoin[] = 'INNER JOIN AccountToFavorite AF ON (AF.accountId = id AND AF.userId = ?)'; - $data->addParam($this->session->getUserData()->getId()); + $where = []; + + if ($queryFilterCommon->hasFilters()) { + $where[] = $queryFilterCommon->getFilters(QueryFilter::FILTER_OR); } - if (count($arrFilterCommon) > 0) { - $arrQueryWhere[] = '(' . implode(' OR ', $arrFilterCommon) . ')'; + if ($queryFilterSelect->hasFilters()) { + $where[] = $queryFilterSelect->getFilters(); } - if (count($arrFilterSelect) > 0) { - $arrQueryWhere[] = '(' . implode(' AND ', $arrFilterSelect) . ')'; + $queryFilterUser = AccountUtil::getAccountFilterUser($this->session, $accountSearchFilter->getGlobalSearch()); + + if ($queryFilterUser->hasFilters()) { + $where[] = $queryFilterUser->getFilters(); } - $arrQueryWhere = array_merge($arrQueryWhere, AccountUtil::getAccountFilterUser($data, $this->session, $accountSearchFilter->getGlobalSearch())); + $queryData = new QueryData(); + $queryData->setWhere($where); + $queryData->setParams(array_merge($queryFilterCommon->getParams(), $queryFilterSelect->getParams(), $queryFilterUser->getParams())); if ($accountSearchFilter->getLimitCount() > 0) { $queryLimit = '?, ?'; - $data->addParam($accountSearchFilter->getLimitStart()); - $data->addParam($accountSearchFilter->getLimitCount()); + $queryData->addParam($accountSearchFilter->getLimitStart()); + $queryData->addParam($accountSearchFilter->getLimitCount()); } - $queryWhere = ''; - - if (count($arrQueryWhere) === 1) { - $queryWhere = implode($arrQueryWhere); - } elseif (count($arrQueryWhere) > 1) { - $queryWhere = implode(' AND ', $arrQueryWhere); + if ($accountSearchFilter->isSearchFavorites() === true) { + $queryJoin[] = 'INNER JOIN AccountToFavorite AF ON (AF.accountId = id AND AF.userId = ?)'; + $queryData->addParam($this->session->getUserData()->getId()); } - $queryJoin = implode('', $arrayQueryJoin); + $queryJoin = implode('', $queryJoin); - $data->setSelect('*'); - $data->setFrom('account_search_v ' . $queryJoin); - $data->setWhere($queryWhere); - $data->setOrder($accountSearchFilter->getOrderString()); - $data->setLimit($queryLimit); + $queryData->setSelect('*'); + $queryData->setFrom('account_search_v A' . $queryJoin); + $queryData->setOrder($accountSearchFilter->getOrderString()); + $queryData->setLimit($queryLimit); -// Log::writeNewLog(__FUNCTION__, $Data->getQuery(), Log::DEBUG); -// Log::writeNewLog(__FUNCTION__, print_r($Data->getParams(), true), Log::DEBUG); + $queryData->setMapClassName(AccountSearchVData::class); - $data->setMapClassName(AccountSearchVData::class); - - return new AccountSearchResponse($this->db->getFullRowCount($data), DbWrapper::getResultsArray($data, $this->db)); + return new AccountSearchResponse($this->db->getFullRowCount($queryData), DbWrapper::getResultsArray($queryData, $this->db)); } /** - * @param $accountId + * @param QueryFilter $queryFilter * @return array */ - public function getForUser($accountId) + public function getForUser(QueryFilter $queryFilter) { - $Data = new QueryData(); - - $queryWhere = AccountUtil::getAccountFilterUser($Data, $this->session); - - if (null !== $accountId) { - $queryWhere[] = 'A.id <> ? AND (A.parentId = 0 OR A.parentId IS NULL)'; - $Data->addParam($accountId); - } - $query = /** @lang SQL */ 'SELECT A.id, A.name, C.name AS clientName FROM Account A LEFT JOIN Client C ON A.clientId = C.id - WHERE ' . implode(' AND ', $queryWhere) . ' ORDER BY name'; + WHERE ' . $queryFilter->getFilters() . ' ORDER BY name'; - $Data->setQuery($query); + $queryData = new QueryData(); + $queryData->setMapClassName(ItemData::class); + $queryData->setQuery($query); + $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($Data); + return DbWrapper::getResultsArray($queryData, $this->db); } /** - * @param $accountId + * @param QueryFilter $queryFilter * @return array */ - public function getLinked($accountId) + public function getLinked(QueryFilter $queryFilter) { - $Data = new QueryData(); - - $queryWhere = AccountUtil::getAccountFilterUser($Data, $this->session); - - $queryWhere[] = 'A.parentId = ?'; - $Data->addParam($accountId); - $query = /** @lang SQL */ 'SELECT A.id, A.name, C.name AS clientName FROM Account A INNER JOIN Client C ON A.clientId = C.id - WHERE ' . implode(' AND ', $queryWhere) . ' ORDER BY name'; + WHERE ' . $queryFilter->getFilters() . ' ORDER BY name'; - $Data->setQuery($query); + $queryData = new QueryData(); + $queryData->setQuery($query); + $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($Data); + return DbWrapper::getResultsArray($queryData, $this->db); } } \ No newline at end of file diff --git a/lib/SP/Repositories/ApiToken/ApiTokenRepository.php b/lib/SP/Repositories/ApiToken/ApiTokenRepository.php index 6fefc4cd..0919f2c2 100644 --- a/lib/SP/Repositories/ApiToken/ApiTokenRepository.php +++ b/lib/SP/Repositories/ApiToken/ApiTokenRepository.php @@ -52,8 +52,7 @@ class ApiTokenRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return $this - * @throws SPException + * @return int * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -69,11 +68,7 @@ class ApiTokenRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Token no encontrado')); - } - - return $this; + return $Data->getQueryNumRows(); } /** diff --git a/lib/SP/Repositories/Category/CategoryRepository.php b/lib/SP/Repositories/Category/CategoryRepository.php index 3e546431..a4cbb642 100644 --- a/lib/SP/Repositories/Category/CategoryRepository.php +++ b/lib/SP/Repositories/Category/CategoryRepository.php @@ -228,8 +228,9 @@ class CategoryRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return CategoryRepository - * @throws SPException + * @return int + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) { @@ -243,11 +244,7 @@ class CategoryRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Categoría no encontrada')); - } - - return $this; + return $Data->getQueryNumRows(); } /** diff --git a/lib/SP/Repositories/Client/ClientRepository.php b/lib/SP/Repositories/Client/ClientRepository.php index b5e4bbc1..6e448120 100644 --- a/lib/SP/Repositories/Client/ClientRepository.php +++ b/lib/SP/Repositories/Client/ClientRepository.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -27,7 +27,9 @@ namespace SP\Repositories\Client; use SP\Account\AccountUtil; use SP\Core\Exceptions\SPException; use SP\DataModel\ClientData; +use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; +use SP\Mvc\Model\QueryFilter; use SP\Repositories\Repository; use SP\Repositories\RepositoryItemInterface; use SP\Repositories\RepositoryItemTrait; @@ -231,7 +233,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return ClientRepository + * @return int * @throws SPException */ public function delete($id) @@ -250,11 +252,7 @@ class ClientRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Cliente no encontrado')); - } - - return $this; + return $Data->getQueryNumRows(); } /** @@ -306,24 +304,26 @@ class ClientRepository extends Repository implements RepositoryItemInterface /** * Devolver los clientes visibles por el usuario * + * @param QueryFilter $queryFilter * @return array */ - public function getItemsForSelectByUser() + public function getAllForFilter(QueryFilter $queryFilter) { - $Data = new QueryData(); - $query = /** @lang SQL */ 'SELECT C.id, C.name FROM Account A RIGHT JOIN Client C ON clientId = C.id WHERE A.clientId IS NULL OR isGlobal = 1 - OR (' . implode(' AND ', AccountUtil::getAccountFilterUser($Data, $this->session)) . ') + OR (' . $queryFilter->getFilters() . ') GROUP BY id ORDER BY name'; - $Data->setQuery($query); + $queryData = new QueryData(); + $queryData->setMapClassName(ItemData::class); + $queryData->setQuery($query); + $queryData->setParams($queryFilter->getParams()); - return DbWrapper::getResultsArray($Data, $this->db); + return DbWrapper::getResultsArray($queryData, $this->db); } } \ No newline at end of file diff --git a/lib/SP/Repositories/PublicLink/PublicLinkRepository.php b/lib/SP/Repositories/PublicLink/PublicLinkRepository.php index 92bc26c8..17e360c4 100644 --- a/lib/SP/Repositories/PublicLink/PublicLinkRepository.php +++ b/lib/SP/Repositories/PublicLink/PublicLinkRepository.php @@ -78,7 +78,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return mixed + * @return int * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -95,11 +95,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Enlace no encontrado')); - } - - return $this; + return $Data->getQueryNumRows(); } /** @@ -487,9 +483,10 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface * Refreshes a public link * * @param $id - * @return $this + * @return bool * @throws SPException * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ @@ -516,9 +513,7 @@ class PublicLinkRepository extends Repository implements RepositoryItemInterface $Data->addParam($id); $Data->setOnErrorMessage(__u('Error al renovar enlace')); - DbWrapper::getQuery($Data, $this->db); - - return $this; + return DbWrapper::getQuery($Data, $this->db); } /** diff --git a/lib/SP/Repositories/RepositoryItemTrait.php b/lib/SP/Repositories/RepositoryItemTrait.php index 0b6e5da7..46da5066 100644 --- a/lib/SP/Repositories/RepositoryItemTrait.php +++ b/lib/SP/Repositories/RepositoryItemTrait.php @@ -35,52 +35,6 @@ use SP\Storage\DBUtil; */ trait RepositoryItemTrait { - /** - * Cache de elementos para select - * - * @var array - */ - private static $itemsSelectCache; - - /** - * Returns service items for a select - * - * @return mixed - */ - public static function getServiceItems() - { - $service = new static(); - return $service->getItemsForSelect(); - } - - /** - * Devolver los elementos para un campo select - * - * @param bool $useCache Usar la cache de elementos si está creada - * @return array - */ - public function getItemsForSelect($useCache = true) - { - // Usar cache si está creada - if ($useCache === true && is_array(self::$itemsSelectCache)) { - return self::$itemsSelectCache; - } - - self::$itemsSelectCache = []; - - /** @var DataModelInterface $item */ - /** @var RepositoryItemInterface $this */ - foreach ($this->getAll() as $item) { - $obj = new \stdClass(); - $obj->id = (int)$item->getId(); - $obj->name = $item->getName(); - - self::$itemsSelectCache[] = $obj; - } - - return self::$itemsSelectCache; - } - /** * Eliminar elementos en lotes * diff --git a/lib/SP/Repositories/Tag/TagRepository.php b/lib/SP/Repositories/Tag/TagRepository.php index e2e792ad..65f34640 100644 --- a/lib/SP/Repositories/Tag/TagRepository.php +++ b/lib/SP/Repositories/Tag/TagRepository.php @@ -216,8 +216,9 @@ class TagRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return TagRepository - * @throws SPException + * @return int + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException */ public function delete($id) { @@ -231,11 +232,7 @@ class TagRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Etiqueta no encontrada')); - } - - return $this; + return $Data->getQueryNumRows(); } /** diff --git a/lib/SP/Repositories/UserGroup/UserGroupRepository.php b/lib/SP/Repositories/UserGroup/UserGroupRepository.php index 4ab89162..d26d4471 100644 --- a/lib/SP/Repositories/UserGroup/UserGroupRepository.php +++ b/lib/SP/Repositories/UserGroup/UserGroupRepository.php @@ -48,7 +48,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface * Deletes an item * * @param $id - * @return mixed + * @return int * @throws SPException */ public function delete($id) @@ -67,11 +67,7 @@ class UserGroupRepository extends Repository implements RepositoryItemInterface DbWrapper::getQuery($Data); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Grupo no encontrado')); - } - - return $this; + return $Data->getQueryNumRows(); } /** diff --git a/lib/SP/Repositories/UserProfile/UserProfileRepository.php b/lib/SP/Repositories/UserProfile/UserProfileRepository.php index 05af2025..25edfdf0 100644 --- a/lib/SP/Repositories/UserProfile/UserProfileRepository.php +++ b/lib/SP/Repositories/UserProfile/UserProfileRepository.php @@ -68,7 +68,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac * Deletes an item * * @param $id - * @return mixed + * @return int * @throws SPException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException @@ -89,11 +89,7 @@ class UserProfileRepository extends Repository implements RepositoryItemInterfac DbWrapper::getQuery($Data, $this->db); - if ($Data->getQueryNumRows() === 0) { - throw new SPException(SPException::SP_INFO, __u('Perfil no encontrado')); - } - - return $this; + return $Data->getQueryNumRows(); } /** diff --git a/lib/SP/Services/Account/AccountSearchService.php b/lib/SP/Services/Account/AccountSearchService.php index c178c6b8..d6f56ec0 100644 --- a/lib/SP/Services/Account/AccountSearchService.php +++ b/lib/SP/Services/Account/AccountSearchService.php @@ -132,6 +132,7 @@ class AccountSearchService * @param AccountSearchFilter $accountSearchFilter * @return array * @throws \SP\Core\Exceptions\SPException + * @throws \SP\Core\Dic\ContainerException */ public function processSearchResults(AccountSearchFilter $accountSearchFilter) { @@ -210,7 +211,7 @@ class AccountSearchService return [ 'type' => 'user', - 'query' => 'userId = ? OR id IN (SELECT AU.accountId FROM AccountToUser AU WHERE AU.accountId = id AND AU.userId = ? UNION ALL SELECT AUG.accountId FROM AccountToUserGroup AUG WHERE AUG.accountId = id AND AUG.userGroupId = ?)', + 'query' => 'A.userId = ? OR A.id IN (SELECT AU.accountId FROM AccountToUser AU WHERE AU.accountId = A.id AND AU.userId = ? UNION ALL SELECT AUG.accountId FROM AccountToUserGroup AUG WHERE AUG.accountId = A.id AND AUG.userGroupId = ?)', 'values' => [$userData->getId(), $userData->getId(), $userData->getUserGroupId()] ]; break; @@ -223,7 +224,7 @@ class AccountSearchService return [ 'type' => 'user', - 'query' => 'userId = ?', + 'query' => 'A.userId = ?', 'values' => [$userData->getId()] ]; break; @@ -236,7 +237,7 @@ class AccountSearchService return [ 'type' => 'group', - 'query' => 'userGroupId = ? OR id IN (SELECT AUG.accountId FROM AccountToUserGroup AUG WHERE AUG.accountId = id AND AUG.userGroupId = ?)', + 'query' => 'A.userGroupId = ? OR A.id IN (SELECT AUG.accountId FROM AccountToUserGroup AUG WHERE AUG.accountId = id AND AUG.userGroupId = ?)', 'values' => [$userGroupData->getId(), $userGroupData->getId()] ]; break; @@ -249,28 +250,28 @@ class AccountSearchService return [ 'type' => 'group', - 'query' => 'userGroupId = ?', + 'query' => 'A.userGroupId = ?', 'values' => [$userGroupData->getId()] ]; break; case 'file': return [ 'type' => 'file', - 'query' => 'id IN (SELECT AF.accountId FROM AccountFile AF WHERE AF.name LIKE ?)', + 'query' => 'A.id IN (SELECT AF.accountId FROM AccountFile AF WHERE AF.name LIKE ?)', 'values' => ['%' . $filters[2] . '%'] ]; break; case 'expired': return [ 'type' => 'expired', - 'query' => 'passDateChange > 0 AND UNIX_TIMESTAMP() > passDateChange', + 'query' => 'A.passDateChange > 0 AND UNIX_TIMESTAMP() > A.passDateChange', 'values' => [] ]; break; case 'private': return [ 'type' => 'private', - 'query' => '(isPrivate = 1 AND userId = ?) OR (isPrivateGroup = 1 AND userGroupId = ?)', + 'query' => '(A.isPrivate = 1 AND A.userId = ?) OR (A.isPrivateGroup = 1 AND A.userGroupId = ?)', 'values' => [$this->session->getUserData()->getId(), $this->session->getUserData()->getUserGroupId()] ]; break; diff --git a/lib/SP/Services/Account/AccountService.php b/lib/SP/Services/Account/AccountService.php index c238477a..d0ddf935 100644 --- a/lib/SP/Services/Account/AccountService.php +++ b/lib/SP/Services/Account/AccountService.php @@ -27,11 +27,13 @@ namespace SP\Services\Account; use Defuse\Crypto\Exception\CryptoException; use SP\Account\AccountAcl; use SP\Account\AccountRequest; +use SP\Account\AccountUtil; use SP\Core\Acl\Acl; use SP\Core\Acl\ActionsInterface; use SP\Core\Crypt\Crypt; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; +use SP\Core\Session\Session; use SP\Core\Traits\InjectableTrait; use SP\DataModel\Dto\AccountDetailsResponse; use SP\Log\Log; @@ -69,6 +71,10 @@ class AccountService implements AccountServiceInterface * @var AccountToTagRepository */ protected $accountToTagRepository; + /** + * @var Session + */ + protected $session; /** * AccountService constructor. @@ -85,16 +91,19 @@ class AccountService implements AccountServiceInterface * @param AccountToUserGroupRepository $accountToUserGroupRepository * @param AccountToUserRepository $accountToUserRepository * @param AccountToTagRepository $accountToTagRepository + * @param Session $session */ public function inject(AccountRepository $accountRepository, AccountToUserGroupRepository $accountToUserGroupRepository, AccountToUserRepository $accountToUserRepository, - AccountToTagRepository $accountToTagRepository) + AccountToTagRepository $accountToTagRepository, + Session $session) { $this->accountRepository = $accountRepository; $this->accountToUserGroupRepository = $accountToUserGroupRepository; $this->accountToUserRepository = $accountToUserRepository; $this->accountToTagRepository = $accountToTagRepository; + $this->session = $session; } /** @@ -366,9 +375,15 @@ class AccountService implements AccountServiceInterface * @param $accountId * @return array */ - public function getForUser($accountId) + public function getForUser($accountId = null) { - return $this->accountRepository->getForUser($accountId); + $queryFilter = AccountUtil::getAccountFilterUser($this->session); + + if (null !== $accountId) { + $queryFilter->addFilter('A.id <> ? AND (A.parentId = 0 OR A.parentId IS NULL)', [$accountId]); + } + + return $this->accountRepository->getForUser($queryFilter); } /** @@ -377,15 +392,10 @@ class AccountService implements AccountServiceInterface */ public function getLinked($accountId) { - return $this->accountRepository->getLinked($accountId); - } + $queryFilter = AccountUtil::getAccountFilterUser($this->session) + ->addFilter('A.parentId = ?', [$accountId]); - /** - * Returns all the items mapping fields for a select type element (id and name fields) - */ - public function getAllItemsForSelect() - { - return $this->getItemsForSelect($this->accountRepository); + return $this->accountRepository->getLinked($queryFilter); } /** @@ -396,4 +406,12 @@ class AccountService implements AccountServiceInterface { return $this->accountRepository->getPasswordHistoryForId($id); } + + /** + * @return array + */ + public function getAllBasic() + { + return $this->accountRepository->getAll(); + } } \ No newline at end of file diff --git a/lib/SP/Services/ApiToken/ApiTokenService.php b/lib/SP/Services/ApiToken/ApiTokenService.php new file mode 100644 index 00000000..dd2c868d --- /dev/null +++ b/lib/SP/Services/ApiToken/ApiTokenService.php @@ -0,0 +1,135 @@ +. + */ + +namespace SP\Services\ApiToken; + +use SP\Core\Exceptions\SPException; +use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; +use SP\Repositories\ApiToken\ApiTokenRepository; +use SP\Services\ServiceItemTrait; + +/** + * Class ApiTokenService + * + * @package SP\Services\ApiToken + */ +class ApiTokenService +{ + use InjectableTrait; + use ServiceItemTrait; + + /** + * @var ApiTokenRepository + */ + protected $apiTokenRepository; + + /** + * CategoryService constructor. + * + * @throws \SP\Core\Dic\ContainerException + */ + public function __construct() + { + $this->injectDependencies(); + } + + /** + * @param ApiTokenRepository $apiTokenRepository + */ + public function inject(ApiTokenRepository $apiTokenRepository) + { + $this->apiTokenRepository = $apiTokenRepository; + } + + /** + * @param ItemSearchData $itemSearchData + * @return mixed + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->apiTokenRepository->search($itemSearchData); + } + + /** + * @param $id + * @return mixed + */ + public function getById($id) + { + return $this->apiTokenRepository->getById($id); + } + + /** + * @param $id + * @return ApiTokenService + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function delete($id) + { + if ($this->apiTokenRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Token no encontrado')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function create($itemData) + { + return $this->apiTokenRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->apiTokenRepository->update($itemData); + } + + + /** + * @return array + */ + public function getAllBasic() + { + return $this->apiTokenRepository->getAll(); + } +} \ No newline at end of file diff --git a/lib/SP/Services/Category/CategoryService.php b/lib/SP/Services/Category/CategoryService.php index 72d1a507..98295ee7 100644 --- a/lib/SP/Services/Category/CategoryService.php +++ b/lib/SP/Services/Category/CategoryService.php @@ -24,7 +24,9 @@ namespace SP\Services\Category; +use SP\Core\Exceptions\SPException; use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; use SP\Repositories\Category\CategoryRepository; use SP\Services\ServiceItemTrait; @@ -45,6 +47,8 @@ class CategoryService /** * CategoryService constructor. + * + * @throws \SP\Core\Dic\ContainerException */ public function __construct() { @@ -54,10 +58,70 @@ class CategoryService } /** - * Returns all the items mapping fields for a select type element (id and name fields) + * @param ItemSearchData $itemSearchData + * @return mixed */ - public function getAllItemsForSelect() + public function search(ItemSearchData $itemSearchData) { - return $this->getItemsForSelect($this->categoryRepository); + return $this->categoryRepository->search($itemSearchData); + } + + /** + * @param $id + * @return mixed + */ + public function getById($id) + { + return $this->categoryRepository->getById($id); + } + + /** + * @param $id + * @return $this + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function delete($id) + { + if ($this->categoryRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Categoría no encontrada')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function create($itemData) + { + return $this->categoryRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->categoryRepository->update($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->categoryRepository->getAll(); } } \ No newline at end of file diff --git a/lib/SP/Services/Client/ClientService.php b/lib/SP/Services/Client/ClientService.php index 9e2a9886..b3125d3b 100644 --- a/lib/SP/Services/Client/ClientService.php +++ b/lib/SP/Services/Client/ClientService.php @@ -24,7 +24,11 @@ namespace SP\Services\Client; +use SP\Account\AccountUtil; +use SP\Core\Exceptions\SPException; +use SP\Core\Session\Session; use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; use SP\Repositories\Client\ClientRepository; use SP\Services\ServiceItemTrait; @@ -42,22 +46,102 @@ class ClientService * @var ClientRepository */ protected $clientRepository; + /** + * @var Session + */ + protected $session; /** * ClientService constructor. + * + * @throws \SP\Core\Dic\ContainerException */ public function __construct() { $this->injectDependencies(); - - $this->clientRepository = new ClientRepository(); } /** - * Returns all the items mapping fields for a select type element (id and name fields) + * @param ClientRepository $clientRepository + * @param Session $session */ - public function getAllItemsForSelect() + public function inject(ClientRepository $clientRepository, Session $session) { - return $this->getItemsForSelect($this->clientRepository); + $this->clientRepository = $clientRepository; + $this->session = $session; + } + + /** + * @param ItemSearchData $itemSearchData + * @return \SP\DataModel\ClientData[] + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->clientRepository->search($itemSearchData); + } + + /** + * @param $id + * @return mixed + */ + public function getById($id) + { + return $this->clientRepository->getById($id); + } + + /** + * @param $id + * @return $this + * @throws SPException + */ + public function delete($id) + { + if ($this->clientRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Cliente no encontrado')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + */ + public function create($itemData) + { + return $this->clientRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->clientRepository->update($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->clientRepository->getAll(); + } + + /** + * Returns all clients visible for a given user + * + * @return array + */ + public function getAllForUser() + { + return $this->clientRepository->getAllForFilter(AccountUtil::getAccountFilterUser($this->session)); } } \ No newline at end of file diff --git a/lib/SP/Services/CustomField/CustomFieldDefService.php b/lib/SP/Services/CustomField/CustomFieldDefService.php new file mode 100644 index 00000000..a881bb84 --- /dev/null +++ b/lib/SP/Services/CustomField/CustomFieldDefService.php @@ -0,0 +1,126 @@ +. + */ + +namespace SP\Services\CustomField; + +use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; +use SP\Repositories\CustomField\CustomFieldDefRepository; +use SP\Services\ServiceItemTrait; + +/** + * Class CustomFieldDefService + * + * @package SP\Services\CustomField + */ +class CustomFieldDefService +{ + use InjectableTrait; + use ServiceItemTrait; + + /** + * @var CustomFieldDefRepository + */ + protected $customFieldDefRepository; + + /** + * ClientService constructor. + * + * @throws \SP\Core\Dic\ContainerException + */ + public function __construct() + { + $this->injectDependencies(); + } + + /** + * @param CustomFieldDefRepository $customFieldDefRepository + */ + public function inject(CustomFieldDefRepository $customFieldDefRepository) + { + $this->customFieldDefRepository = $customFieldDefRepository; + } + + /** + * @param ItemSearchData $itemSearchData + * @return \SP\DataModel\CustomFieldDefinitionData[] + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->customFieldDefRepository->search($itemSearchData); + } + + /** + * @param $id + * @return \SP\DataModel\CustomFieldDefinitionData + */ + public function getById($id) + { + return $this->customFieldDefRepository->getById($id); + } + + /** + * @param $id + * @return bool + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function delete($id) + { + return $this->customFieldDefRepository->delete($id); + } + + /** + * @param $itemData + * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function create($itemData) + { + return $this->customFieldDefRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->customFieldDefRepository->update($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->customFieldDefRepository->getAll(); + } +} \ No newline at end of file diff --git a/lib/SP/Services/PublicLink/PublicLinkService.php b/lib/SP/Services/PublicLink/PublicLinkService.php new file mode 100644 index 00000000..e28cbf0b --- /dev/null +++ b/lib/SP/Services/PublicLink/PublicLinkService.php @@ -0,0 +1,137 @@ +. + */ + +namespace SP\Services\PublicLink; + +use SP\Core\Exceptions\SPException; +use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; +use SP\Repositories\PublicLink\PublicLinkRepository; +use SP\Services\ServiceItemTrait; + +/** + * Class PublicLinkService + * + * @package SP\Services\PublicLink + */ +class PublicLinkService +{ + use InjectableTrait; + use ServiceItemTrait; + + /** + * @var PublicLinkRepository + */ + protected $publicLinkRepository; + + /** + * CategoryService constructor. + * + * @throws \SP\Core\Dic\ContainerException + */ + public function __construct() + { + $this->injectDependencies(); + } + + /** + * @param PublicLinkRepository $publicLinkRepository + */ + public function inject(PublicLinkRepository $publicLinkRepository) + { + $this->publicLinkRepository = $publicLinkRepository; + } + + /** + * @param ItemSearchData $itemSearchData + * @return mixed + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->publicLinkRepository->search($itemSearchData); + } + + /** + * @param $id + * @return \SP\DataModel\PublicLinkData + * @throws \SP\Core\Exceptions\SPException + */ + public function getById($id) + { + return $this->publicLinkRepository->getById($id); + } + + /** + * @param $id + * @return bool + * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function refresh($id) + { + return $this->publicLinkRepository->refresh($id); + } + + /** + * @param $id + * @return $this + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + * @throws \SP\Core\Exceptions\SPException + */ + public function delete($id) + { + if ($this->publicLinkRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Enlace no encontrado')); + } + + return $this; + } + + /** + * @param $itemData + * @return int + * @throws SPException + * @throws \Defuse\Crypto\Exception\CryptoException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function create($itemData) + { + return $this->publicLinkRepository->create($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->publicLinkRepository->getAll(); + } +} \ No newline at end of file diff --git a/lib/SP/Services/ServiceItemInterface.php b/lib/SP/Services/ServiceItemInterface.php new file mode 100644 index 00000000..5c7a380a --- /dev/null +++ b/lib/SP/Services/ServiceItemInterface.php @@ -0,0 +1,38 @@ +. + */ + +namespace SP\Services; + +/** + * Interface ServiceItemInterface + * + * @package SP\Services + */ +interface ServiceItemInterface +{ + /** + * @return array + */ + public function getAll(); +} \ No newline at end of file diff --git a/lib/SP/Services/ServiceItemTrait.php b/lib/SP/Services/ServiceItemTrait.php index a3ef1c7d..675535e6 100644 --- a/lib/SP/Services/ServiceItemTrait.php +++ b/lib/SP/Services/ServiceItemTrait.php @@ -2,8 +2,8 @@ /** * sysPass * - * @author nuxsmin - * @link http://syspass.org + * @author nuxsmin + * @link http://syspass.org * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. @@ -25,7 +25,6 @@ namespace SP\Services; use SP\DataModel\DataModelInterface; -use SP\Repositories\RepositoryItemInterface; /** * Trait ServiceItemTrait @@ -35,29 +34,19 @@ use SP\Repositories\RepositoryItemInterface; trait ServiceItemTrait { /** - * Devolver los elementos para un campo select + * Returns service items for a select * - * @param RepositoryItemInterface $repositoryItem - * @return array + * @return DataModelInterface[] */ - public function getItemsForSelect(RepositoryItemInterface $repositoryItem) + public static function getItemsBasic() { - $items = []; - - /** @var DataModelInterface $item */ - foreach ($repositoryItem->getAll() as $item) { - $obj = new \stdClass(); - $obj->id = (int)$item->getId(); - $obj->name = $item->getName(); - - $items[] = $obj; - } - - return $items; + return (new static())->getAllBasic(); } /** - * Returns all the items mapping fields for a select type element (id and name fields) + * Get all items from the service's repository + * + * @return mixed */ - abstract public function getAllItemsForSelect(); + abstract public function getAllBasic(); } \ No newline at end of file diff --git a/lib/SP/Services/Tag/TagService.php b/lib/SP/Services/Tag/TagService.php index 5477025c..1743ce1a 100644 --- a/lib/SP/Services/Tag/TagService.php +++ b/lib/SP/Services/Tag/TagService.php @@ -24,7 +24,9 @@ namespace SP\Services\Tag; +use SP\Core\Exceptions\SPException; use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; use SP\Repositories\Tag\TagRepository; use SP\Services\ServiceItemTrait; @@ -46,18 +48,83 @@ class TagService /** * TagService constructor. + * + * @throws \SP\Core\Dic\ContainerException */ public function __construct() { - $this->tagRepository = new TagRepository(); + $this->injectDependencies(); } - /** - * Returns all the items mapping fields for a select type element (id and name fields) + * @param TagRepository $tagRepository */ - public function getAllItemsForSelect() + public function inject(TagRepository $tagRepository) { - return $this->getItemsForSelect($this->tagRepository); + $this->tagRepository = $tagRepository; + } + + /** + * @param ItemSearchData $itemSearchData + * @return \SP\DataModel\ClientData[] + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->tagRepository->search($itemSearchData); + } + + /** + * @param $id + * @return mixed + */ + public function getById($id) + { + return $this->tagRepository->getById($id); + } + + /** + * @param $id + * @return $this + * @throws SPException + */ + public function delete($id) + { + if ($this->tagRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Etiqueta no encontrada')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + */ + public function create($itemData) + { + return $this->tagRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->tagRepository->update($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->tagRepository->getAll(); } } \ No newline at end of file diff --git a/lib/SP/Services/User/UserService.php b/lib/SP/Services/User/UserService.php index c38ee49e..3bba345e 100644 --- a/lib/SP/Services/User/UserService.php +++ b/lib/SP/Services/User/UserService.php @@ -65,14 +65,6 @@ class UserService $this->userRepository = $userRepository; } - /** - * Returns all the items mapping fields for a select type element (id and name fields) - */ - public function getAllItemsForSelect() - { - return $this->getItemsForSelect($this->userRepository); - } - /** * Actualiza el último inicio de sesión del usuario en la BBDD. * @@ -264,4 +256,14 @@ class UserService return $this->userRepository->updateOnLogin($userData); } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->userRepository->getBasicInfo(); + } } \ No newline at end of file diff --git a/lib/SP/Services/UserGroup/UserGroupService.php b/lib/SP/Services/UserGroup/UserGroupService.php index 16029162..6b58c0f2 100644 --- a/lib/SP/Services/UserGroup/UserGroupService.php +++ b/lib/SP/Services/UserGroup/UserGroupService.php @@ -25,7 +25,9 @@ namespace SP\Services\UserGroup; +use SP\Core\Exceptions\SPException; use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; use SP\Repositories\UserGroup\UserGroupRepository; use SP\Services\ServiceItemTrait; @@ -47,17 +49,83 @@ class UserGroupService /** * UserGroup constructor. + * + * @throws \SP\Core\Dic\ContainerException */ public function __construct() { - $this->userGroupRepository = new UserGroupRepository(); + $this->injectDependencies(); } /** - * Returns all the items mapping fields for a select type element (id and name fields) + * @param UserGroupRepository $userGroupRepository */ - public function getAllItemsForSelect() + public function inject(UserGroupRepository $userGroupRepository) { - return $this->getItemsForSelect($this->userGroupRepository); + $this->userGroupRepository = $userGroupRepository; + } + + /** + * @param ItemSearchData $itemSearchData + * @return \SP\DataModel\ClientData[] + */ + public function search(ItemSearchData $itemSearchData) + { + return $this->userGroupRepository->search($itemSearchData); + } + + /** + * @param $id + * @return mixed + */ + public function getById($id) + { + return $this->userGroupRepository->getById($id); + } + + /** + * @param $id + * @return $this + * @throws SPException + */ + public function delete($id) + { + if ($this->userGroupRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Grupo no encontrado')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + */ + public function create($itemData) + { + return $this->userGroupRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->userGroupRepository->update($itemData); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->userGroupRepository->getAll(); } } \ No newline at end of file diff --git a/lib/SP/Services/UserGroup/UserToUserGroupService.php b/lib/SP/Services/UserGroup/UserToUserGroupService.php new file mode 100644 index 00000000..191078c2 --- /dev/null +++ b/lib/SP/Services/UserGroup/UserToUserGroupService.php @@ -0,0 +1,110 @@ +. + */ + +namespace SP\Services\UserGroup; + +use SP\Core\Traits\InjectableTrait; +use SP\Repositories\UserGroup\UserToUserGroupRepository; +use SP\Services\ServiceItemTrait; + +/** + * Class UserToUserGroupService + * + * @package SP\Services\UserGroup + */ +class UserToUserGroupService +{ + use InjectableTrait; + + /** + * @var UserToUserGroupRepository + */ + protected $userToUserGroupRepository; + + /** + * UserGroup constructor. + * + * @throws \SP\Core\Dic\ContainerException + */ + public function __construct() + { + $this->injectDependencies(); + } + + /** + * @param UserToUserGroupRepository $userToUserGroupRepository + */ + public function inject(UserToUserGroupRepository $userToUserGroupRepository) + { + $this->userToUserGroupRepository = $userToUserGroupRepository; + } + + /** + * @param $id + * @return \SP\DataModel\UserToUserGroupData[] + */ + public function getById($id) + { + return $this->userToUserGroupRepository->getById($id); + } + + /** + * @param $id + * @param array $users + * @return UserToUserGroupRepository + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function add($id, array $users) + { + return $this->userToUserGroupRepository->add($id, $users); + } + + /** + * @param $id + * @param array $users + * @return UserToUserGroupRepository + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($id, array $users) + { + return $this->userToUserGroupRepository->update($id, $users); + } + + /** + * @param $id + * @return array + */ + public function getUsersByGroupId($id) + { + $usersId = []; + + foreach ($this->userToUserGroupRepository->getById($id) as $userToUserGroupData) { + $usersId[] = $userToUserGroupData->getUserId(); + } + + return $usersId; + } +} \ No newline at end of file diff --git a/lib/SP/Services/UserProfile/UserProfileService.php b/lib/SP/Services/UserProfile/UserProfileService.php index df86eae7..a51d2c6a 100644 --- a/lib/SP/Services/UserProfile/UserProfileService.php +++ b/lib/SP/Services/UserProfile/UserProfileService.php @@ -24,7 +24,9 @@ namespace SP\Services\UserProfile; +use SP\Core\Exceptions\SPException; use SP\Core\Traits\InjectableTrait; +use SP\DataModel\ItemSearchData; use SP\Repositories\UserProfile\UserProfileRepository; use SP\Services\ServiceItemTrait; @@ -45,12 +47,24 @@ class UserProfileService /** * UserProfileService constructor. + * + * @throws \SP\Core\Dic\ContainerException */ public function __construct() { + $this->injectDependencies(); + $this->userProfileRepository = new UserProfileRepository(); } + /** + * @param UserProfileRepository $userProfileRepository + */ + public function inject(UserProfileRepository $userProfileRepository) + { + $this->userProfileRepository = $userProfileRepository; + } + /** * @param $id * @return mixed @@ -61,10 +75,66 @@ class UserProfileService } /** - * Returns all the items mapping fields for a select type element (id and name fields) + * @param ItemSearchData $itemSearchData + * @return \SP\DataModel\ClientData[] */ - public function getAllItemsForSelect() + public function search(ItemSearchData $itemSearchData) { - return $this->getItemsForSelect($this->userProfileRepository); + return $this->userProfileRepository->search($itemSearchData); + } + + /** + * @param $id + * @return $this + * @throws SPException + */ + public function delete($id) + { + if ($this->userProfileRepository->delete($id) === 0) { + throw new SPException(SPException::SP_INFO, __u('Perfil no encontrado')); + } + + return $this; + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + */ + public function create($itemData) + { + return $this->userProfileRepository->create($itemData); + } + + /** + * @param $itemData + * @return mixed + * @throws SPException + * @throws \SP\Core\Exceptions\ConstraintException + * @throws \SP\Core\Exceptions\QueryException + */ + public function update($itemData) + { + return $this->userProfileRepository->update($itemData); + } + + /** + * @param $id + * @return array + */ + public function getUsersForProfile($id) + { + return $this->userProfileRepository->getUsersForProfile($id); + } + + /** + * Get all items from the service's repository + * + * @return array + */ + public function getAllBasic() + { + return $this->userProfileRepository->getAll(); } } \ No newline at end of file diff --git a/lib/SP/Storage/QueryData.php b/lib/SP/Storage/QueryData.php index 8aa8ae42..9d285771 100644 --- a/lib/SP/Storage/QueryData.php +++ b/lib/SP/Storage/QueryData.php @@ -105,6 +105,16 @@ class QueryData } } + /** + * Añadir un parámetro a la consulta + * + * @param array $params + */ + public function addParams(array $params) + { + $this->data = array_merge($this->data, $params); + } + /** * @return array */