tfa = $tfa; $this->session = $session; $this->jsonFactory = $jsonFactory; $this->google = $google; $this->tfaSession = $tfaSession; $this->dataObjectFactory = $dataObjectFactory; $this->alert = $alert; } /** * Get current user * @return \Magento\User\Model\User|null */ private function getUser() { return $this->session->getUser(); } /** * @inheritdoc * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function execute() { $response = $this->jsonFactory->create(); $user = $this->getUser(); if ($this->google->verify($user, $this->dataObjectFactory->create([ 'data' => $this->getRequest()->getParams(), ]))) { $this->tfa->getProvider(Google::CODE)->activate($user->getId()); $this->tfaSession->grantAccess(); $this->alert->event( 'MSP_TwoFactorAuth', 'New Google Authenticator code issued', AlertInterface::LEVEL_INFO, $user->getUserName() ); $response->setData([ 'success' => true, ]); } else { $response->setData([ 'success' => false, 'message' => 'Invalid code', ]); } return $response; } /** * Check if admin has permissions to visit related pages * * @return bool */ protected function _isAllowed() { $user = $this->getUser(); return $user && $this->tfa->getProviderIsAllowed($user->getId(), Google::CODE) && !$this->tfa->getProvider(Google::CODE)->isActive($user->getId()); } }