|
24 | 24 | use phpMyFAQ\Core\Exception; |
25 | 25 | use phpMyFAQ\Session\Token; |
26 | 26 | use phpMyFAQ\Translation; |
| 27 | +use phpMyFAQ\User\TwoFactor; |
| 28 | +use RobThree\Auth\TwoFactorAuthException; |
27 | 29 | use Symfony\Component\HttpFoundation\RedirectResponse; |
28 | 30 | use Symfony\Component\HttpFoundation\Request; |
29 | 31 | use Symfony\Component\HttpFoundation\Response; |
@@ -124,4 +126,95 @@ public function register(Request $request): Response |
124 | 126 | ), |
125 | 127 | ]); |
126 | 128 | } |
| 129 | + |
| 130 | + /** |
| 131 | + * Displays the User Control Panel. |
| 132 | + * |
| 133 | + * @throws Exception |
| 134 | + * @throws \Exception |
| 135 | + */ |
| 136 | + #[Route(path: '/user/ucp', name: 'public.user.ucp')] |
| 137 | + public function ucp(Request $request): Response |
| 138 | + { |
| 139 | + if (!$this->currentUser->isLoggedIn()) { |
| 140 | + return new RedirectResponse($this->configuration->getDefaultUrl()); |
| 141 | + } |
| 142 | + |
| 143 | + $faqSession = $this->container->get('phpmyfaq.user.session'); |
| 144 | + $faqSession->setCurrentUser($this->currentUser); |
| 145 | + $faqSession->userTracking('user_control_panel', $this->currentUser->getUserId()); |
| 146 | + |
| 147 | + if ($this->configuration->get('main.enableGravatarSupport')) { |
| 148 | + $gravatar = $this->container->get('phpmyfaq.services.gravatar'); |
| 149 | + $gravatarImg = sprintf('<a target="_blank" href="https://www.gravatar.com">%s</a>', $gravatar->getImage( |
| 150 | + $this->currentUser->getUserData('email'), |
| 151 | + ['class' => 'img-responsive rounded-circle', 'size' => 125], |
| 152 | + )); |
| 153 | + } else { |
| 154 | + $gravatarImg = ''; |
| 155 | + } |
| 156 | + |
| 157 | + $qrCode = ''; |
| 158 | + $secret = ''; |
| 159 | + try { |
| 160 | + $twoFactor = new TwoFactor($this->configuration, $this->currentUser); |
| 161 | + $secret = $twoFactor->getSecret($this->currentUser); |
| 162 | + if ('' === $secret || is_null($secret)) { |
| 163 | + try { |
| 164 | + $secret = $twoFactor->generateSecret(); |
| 165 | + } catch (TwoFactorAuthException $exception) { |
| 166 | + $this->configuration->getLogger()->error('Cannot generate 2FA secret: ' . $exception->getMessage()); |
| 167 | + } |
| 168 | + |
| 169 | + $twoFactor->saveSecret($secret); |
| 170 | + } |
| 171 | + |
| 172 | + $qrCode = $twoFactor->getQrCode($secret); |
| 173 | + } catch (TwoFactorAuthException|\Exception $exception) { |
| 174 | + $this->configuration->getLogger()->error('2FA error: ' . $exception->getMessage()); |
| 175 | + } |
| 176 | + |
| 177 | + $session = $this->container->get('session'); |
| 178 | + |
| 179 | + return $this->render('ucp.twig', [ |
| 180 | + ...$this->getHeader($request), |
| 181 | + 'headerUserControlPanel' => Translation::get(key: 'headerUserControlPanel'), |
| 182 | + 'ucpGravatarImage' => $gravatarImg, |
| 183 | + 'msgHeaderUserData' => Translation::get(key: 'headerUserControlPanel'), |
| 184 | + 'userid' => $this->currentUser->getUserId(), |
| 185 | + 'csrf' => Token::getInstance($session)->getTokenInput('ucp'), |
| 186 | + 'lang' => $this->configuration->getLanguage()->getLanguage(), |
| 187 | + 'readonly' => $this->currentUser->isLocalUser() ? '' : 'readonly disabled', |
| 188 | + 'msgRealName' => Translation::get(key: 'ad_user_name'), |
| 189 | + 'realname' => $this->currentUser->getUserData('display_name'), |
| 190 | + 'msgEmail' => Translation::get(key: 'msgNewContentMail'), |
| 191 | + 'email' => $this->currentUser->getUserData('email'), |
| 192 | + 'msgIsVisible' => Translation::get(key: 'msgUserDataVisible'), |
| 193 | + 'checked' => (int) $this->currentUser->getUserData('is_visible') === 1 ? 'checked' : '', |
| 194 | + 'msgPassword' => Translation::get(key: 'ad_auth_passwd'), |
| 195 | + 'msgConfirm' => Translation::get(key: 'ad_user_confirm'), |
| 196 | + 'msgSave' => Translation::get(key: 'msgSave'), |
| 197 | + 'msgCancel' => Translation::get(key: 'msgCancel'), |
| 198 | + 'twofactor_enabled' => (bool) $this->currentUser->getUserData('twofactor_enabled'), |
| 199 | + 'msgTwofactorEnabled' => Translation::get(key: 'msgTwofactorEnabled'), |
| 200 | + 'msgTwofactorConfig' => Translation::get(key: 'msgTwofactorConfig'), |
| 201 | + 'msgTwofactorConfigModelTitle' => Translation::get(key: 'msgTwofactorConfigModelTitle'), |
| 202 | + 'twofactor_secret' => $secret, |
| 203 | + 'qr_code_secret' => $qrCode, |
| 204 | + 'qr_code_secret_alt' => Translation::get(key: 'qr_code_secret_alt'), |
| 205 | + 'msgTwofactorNewSecret' => Translation::get(key: 'msgTwofactorNewSecret'), |
| 206 | + 'msgWarning' => Translation::get(key: 'msgWarning'), |
| 207 | + 'ad_gen_yes' => Translation::get(key: 'ad_gen_yes'), |
| 208 | + 'ad_gen_no' => Translation::get(key: 'ad_gen_no'), |
| 209 | + 'msgConfirmTwofactorConfig' => Translation::get(key: 'msgConfirmTwofactorConfig'), |
| 210 | + 'csrfTokenRemoveTwofactor' => Token::getInstance($session)->getTokenString('remove-twofactor'), |
| 211 | + 'msgGravatarNotConnected' => Translation::get(key: 'msgGravatarNotConnected'), |
| 212 | + 'webauthnSupportEnabled' => $this->configuration->get('security.enableWebAuthnSupport'), |
| 213 | + 'csrfExportUserData' => Token::getInstance($session)->getTokenInput('export-userdata'), |
| 214 | + 'exportUserDataUrl' => 'api/user/data/export', |
| 215 | + 'msgDownloadYourData' => Translation::get(key: 'msgDownloadYourData'), |
| 216 | + 'msgDataExportDescription' => Translation::get(key: 'msgDataExportDescription'), |
| 217 | + 'msgDownload' => Translation::get(key: 'msgDownload'), |
| 218 | + ]); |
| 219 | + } |
127 | 220 | } |
0 commit comments