. */ namespace SP\Modules\Web\Controllers\Helpers\Account; use Defuse\Crypto\Exception\BadFormatException; use Defuse\Crypto\Exception\CryptoException; use Defuse\Crypto\Exception\EnvironmentIsBrokenException; use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException; use SP\Core\Acl\Acl; use SP\Core\Application; use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Session as CryptSession; use SP\Domain\Account\Adapters\AccountPassItemWithIdAndName; use SP\Domain\Common\Providers\Image; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Acl\AclInterface; use SP\Domain\Core\Exceptions\FileNotFoundException; use SP\Domain\Crypt\Ports\MasterPassService; use SP\Domain\Http\Ports\RequestService; use SP\Domain\Image\Ports\ImageService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Modules\Web\Controllers\Helpers\HelperBase; use SP\Modules\Web\Controllers\Helpers\HelperException; use SP\Mvc\View\TemplateInterface; /** * Class AccountPasswordHelper * * @package SP\Modules\Web\Controllers\Helpers */ final class AccountPasswordHelper extends HelperBase { private Acl $acl; private Image $imageUtil; private MasterPassService $masterPassService; public function __construct( Application $application, TemplateInterface $template, RequestService $request, AclInterface $acl, ImageService $imageUtil, MasterPassService $masterPassService ) { parent::__construct($application, $template, $request); $this->acl = $acl; $this->imageUtil = $imageUtil; $this->masterPassService = $masterPassService; } /** * @param AccountPassItemWithIdAndName $accountData * * @param bool $useImage * * @return array * @throws BadFormatException * @throws CryptoException * @throws EnvironmentIsBrokenException * @throws WrongKeyOrModifiedCiphertextException * @throws FileNotFoundException * @throws HelperException * @throws NoSuchItemException * @throws ServiceException */ public function getPasswordView( AccountPassItemWithIdAndName $accountData, bool $useImage ): array { $this->checkActionAccess(); $this->view->addTemplate('viewpass'); $this->view->assign('header', __('Account Password')); $this->view->assign('isImage', (int)$useImage); $pass = $this->getPasswordClear($accountData); if ($useImage) { $this->view->assign( 'login', $this->imageUtil->convertText($accountData->getLogin()) ); $this->view->assign( 'pass', $this->imageUtil->convertText($pass) ); } else { $this->view->assign('login', $accountData->getLogin()); $this->view->assign( 'pass', htmlspecialchars($pass, ENT_COMPAT) ); } return [ 'useimage' => $useImage, 'html' => $this->view->render(), ]; } /** * @throws HelperException */ private function checkActionAccess(): void { if (!$this->acl->checkUserAccess(AclActionsInterface::ACCOUNT_VIEW_PASS)) { throw new HelperException(__u('You don\'t have permission to access this account')); } } /** * Returns account's password * * @param AccountPassItemWithIdAndName $accountData * * @return string * @throws BadFormatException * @throws CryptoException * @throws EnvironmentIsBrokenException * @throws WrongKeyOrModifiedCiphertextException * @throws HelperException * @throws NoSuchItemException * @throws ServiceException */ public function getPasswordClear(AccountPassItemWithIdAndName $accountData): string { $this->checkActionAccess(); if (!$this->masterPassService->checkUserUpdateMPass($this->context->getUserData()->getLastUpdateMPass())) { throw new HelperException( __('Master password updated') .'
' .__('Please, restart the session for update it') ); } return trim( Crypt::decrypt( $accountData->getPass(), $accountData->getKey(), CryptSession::getSessionKey($this->context) ) ); } }