. */ namespace SP\Modules\Api\Controllers; use SP\Core\Acl\ActionsInterface; use SP\Core\Crypt\Crypt; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Modules\Api\Controllers\Help\AccountHelp; use SP\Mvc\Model\QueryCondition; use SP\Services\Account\AccountRequest; use SP\Services\Account\AccountSearchFilter; use SP\Services\Account\AccountService; use SP\Services\Api\ApiResponse; /** * Class AccountController * * @package SP\Modules\Api\Controllers */ final class AccountController extends ControllerBase { /** * @var AccountService */ private $accountService; /** * viewAction */ public function viewAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_VIEW); $accountId = $this->apiService->getParamInt('id', true); $accountDetails = $this->accountService->getById($accountId)->getAccountVData(); $this->accountService->incrementViewCounter($accountId); $this->eventDispatcher->notifyEvent('show.account', new Event($this, EventMessage::factory() ->addDescription(__u('Cuenta visualizada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId)); } catch (\Exception $e) { $this->returnResponseException($e); processException($e); } } /** * viewPassAction */ public function viewPassAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_VIEW_PASS); $accountId = $this->apiService->getParamInt('id', true); $accountPassData = $this->accountService->getPasswordForId($accountId); $password = Crypt::decrypt($accountPassData->getPass(), $accountPassData->getKey(), $this->apiService->getMasterPass()); $this->accountService->incrementDecryptCounter($accountId); $accountDetails = $this->accountService->getById($accountId)->getAccountVData(); $this->eventDispatcher->notifyEvent('show.account.pass', new Event($this, EventMessage::factory() ->addDescription(__u('Clave visualizada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess(["password" => $password], $accountId)); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * viewPassAction */ public function editPassAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_EDIT_PASS); $accountRequest = new AccountRequest(); $accountRequest->id = $this->apiService->getParamInt('id', true); $accountRequest->pass = $this->apiService->getParamString('pass', true); $accountRequest->passDateChange = $this->apiService->getParamInt('expireDate'); $accountRequest->userEditId = $this->context->getUserData()->getId(); $this->accountService->editPassword($accountRequest); $accountDetails = $this->accountService->getById($accountRequest->id)->getAccountVData(); $this->eventDispatcher->notifyEvent('edit.account.pass', new Event($this, EventMessage::factory() ->addDescription(__u('Clave actualizada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountRequest->id, __('Clave actualizada'))); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * createAction */ public function createAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_CREATE); $accountRequest = new AccountRequest(); $accountRequest->name = $this->apiService->getParamString('name', true); $accountRequest->clientId = $this->apiService->getParamInt('clientId', true); $accountRequest->categoryId = $this->apiService->getParamInt('categoryId', true); $accountRequest->login = $this->apiService->getParamString('login'); $accountRequest->url = $this->apiService->getParamString('url'); $accountRequest->notes = $this->apiService->getParamString('notes'); $accountRequest->isPrivate = $this->apiService->getParamInt('private'); $accountRequest->isPrivateGroup = $this->apiService->getParamInt('privateGroup'); $accountRequest->passDateChange = $this->apiService->getParamInt('expireDate'); $accountRequest->parentId = $this->apiService->getParamInt('parentId'); $accountRequest->userId = $this->context->getUserData()->getId(); $accountRequest->userGroupId = $this->context->getUserData()->getUserGroupId(); $accountRequest->tags = array_map('intval', $this->apiService->getParamArray('tagsId', false, [])); $pass = $this->accountService->getPasswordEncrypted($this->apiService->getParamRaw('pass', true), $this->apiService->getMasterPass()); $accountRequest->pass = $pass['pass']; $accountRequest->key = $pass['key']; $accountId = $this->accountService->create($accountRequest); $accountDetails = $this->accountService->getById($accountId)->getAccountVData(); $this->eventDispatcher->notifyEvent('create.account', new Event($this, EventMessage::factory() ->addDescription(__u('Cuenta creada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId, __('Cuenta creada'))); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * editAction */ public function editAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_EDIT); $accountRequest = new AccountRequest(); $accountRequest->id = $this->apiService->getParamInt('id', true); $accountRequest->name = $this->apiService->getParamString('name', true); $accountRequest->clientId = $this->apiService->getParamInt('clientId', true); $accountRequest->categoryId = $this->apiService->getParamInt('categoryId', true); $accountRequest->login = $this->apiService->getParamString('login'); $accountRequest->url = $this->apiService->getParamString('url'); $accountRequest->notes = $this->apiService->getParamString('notes'); $accountRequest->isPrivate = $this->apiService->getParamInt('private'); $accountRequest->isPrivateGroup = $this->apiService->getParamInt('privateGroup'); $accountRequest->passDateChange = $this->apiService->getParamInt('expireDate'); $accountRequest->parentId = $this->apiService->getParamInt('parentId'); $accountRequest->userEditId = $this->context->getUserData()->getId(); $tagsId = array_map('intval', $this->apiService->getParamArray('tagsId', false, [])); if (!empty($tagsId)) { $accountRequest->updateTags = true; $accountRequest->tags = $tagsId; } $this->accountService->update($accountRequest); $accountDetails = $this->accountService->getById($accountRequest->id)->getAccountVData(); $this->eventDispatcher->notifyEvent('edit.account', new Event($this, EventMessage::factory() ->addDescription(__u('Cuenta actualizada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountRequest->id, __('Cuenta actualizada'))); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * searchAction */ public function searchAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_SEARCH); $accountSearchFilter = new AccountSearchFilter(); $accountSearchFilter->setCleanTxtSearch($this->apiService->getParamString('text')); $accountSearchFilter->setCategoryId($this->apiService->getParamInt('categoryId')); $accountSearchFilter->setClientId($this->apiService->getParamInt('clientId')); $tagsId = array_map('intval', $this->apiService->getParamArray('tagsId', false, [])); if (!empty($tagsId)) { $accountSearchFilter->setTagsId($tagsId); } $op = $this->apiService->getParamString('op'); if ($op !== null) { switch ($op) { case 'and': $accountSearchFilter->setFilterOperator(QueryCondition::CONDITION_AND); break; case 'or': $accountSearchFilter->setFilterOperator(QueryCondition::CONDITION_OR); break; } } $accountSearchFilter->setLimitCount($this->apiService->getParamInt('count', false, 50)); $accountSearchFilter->setSortOrder($this->apiService->getParamInt('order', false, AccountSearchFilter::SORT_DEFAULT)); $this->returnResponse(ApiResponse::makeSuccess($this->accountService->getByFilter($accountSearchFilter))); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * deleteAction */ public function deleteAction() { try { $this->setupApi(ActionsInterface::ACCOUNT_DELETE); $accountId = $this->apiService->getParamInt('id', true); $accountDetails = $this->accountService->getById($accountId)->getAccountVData(); $this->accountService->delete($accountId); $this->eventDispatcher->notifyEvent('delete.account', new Event($this, EventMessage::factory() ->addDescription(__u('Cuenta eliminada')) ->addDetail(__u('Cuenta'), $accountDetails->getName()) ->addDetail(__u('Cliente'), $accountDetails->getClientName()) ->addDetail(__u('ID'), $accountDetails->getId())) ); $this->returnResponse(ApiResponse::makeSuccess($accountDetails, $accountId, __('Cuenta eliminada'))); } catch (\Exception $e) { processException($e); $this->returnResponseException($e); } } /** * @throws \SP\Core\Exceptions\InvalidClassException */ protected function initialize() { $this->accountService = $this->dic->get(AccountService::class); $this->apiService->setHelpClass(AccountHelp::class); } }