. */ namespace SP\Services\Crypt; use SP\Core\Crypt\Hash; use SP\Services\Account\AccountCryptService; use SP\Services\Config\ConfigService; use SP\Services\CustomField\CustomFieldCryptService; use SP\Services\Service; use SP\Services\ServiceException; /** * Class MasterPassService * * @package SP\Services\Crypt */ final class MasterPassService extends Service { const PARAM_MASTER_PASS_TIME = 'lastupdatempass'; const PARAM_MASTER_PASS_HASH = 'masterPwd'; /** * @var ConfigService */ protected $configService; /** * @var AccountCryptService */ protected $accountCryptService; /** * @var CustomFieldCryptService */ protected $customFieldCryptService; /** * @param int $userMPassTime * * @return bool * @throws ServiceException * @throws \SP\Repositories\NoSuchItemException */ public function checkUserUpdateMPass($userMPassTime) { return $userMPassTime >= $this->configService->getByParam(self::PARAM_MASTER_PASS_TIME, 0); } /** * @param string $masterPassword * * @return bool * @throws ServiceException * @throws \SP\Repositories\NoSuchItemException */ public function checkMasterPassword($masterPassword) { return Hash::checkHashKey($masterPassword, $this->configService->getByParam(self::PARAM_MASTER_PASS_HASH)); } /** * @param UpdateMasterPassRequest $request * * @throws \Exception */ public function changeMasterPassword(UpdateMasterPassRequest $request) { $this->transactionAware(function () use ($request) { $this->accountCryptService->updateMasterPassword($request); $this->accountCryptService->updateHistoryMasterPassword($request); $this->customFieldCryptService->updateMasterPassword($request); $this->updateConfig($request->getHash()); }); } /** * @param $hash * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function updateConfig($hash) { $this->configService->save(self::PARAM_MASTER_PASS_HASH, $hash); $this->configService->save(self::PARAM_MASTER_PASS_TIME, time()); } /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function initialize() { $this->configService = $this->dic->get(ConfigService::class); $this->accountCryptService = $this->dic->get(AccountCryptService::class); $this->customFieldCryptService = $this->dic->get(CustomFieldCryptService::class); } }