Skip to content

Commit 732925e

Browse files
committed
refactor: migrated user control panel page (#3834)
1 parent 4c9d884 commit 732925e

File tree

3 files changed

+98
-119
lines changed

3 files changed

+98
-119
lines changed

phpmyfaq/src/phpMyFAQ/Controller/Frontend/UserController.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use phpMyFAQ\Core\Exception;
2525
use phpMyFAQ\Session\Token;
2626
use phpMyFAQ\Translation;
27+
use phpMyFAQ\User\TwoFactor;
28+
use RobThree\Auth\TwoFactorAuthException;
2729
use Symfony\Component\HttpFoundation\RedirectResponse;
2830
use Symfony\Component\HttpFoundation\Request;
2931
use Symfony\Component\HttpFoundation\Response;
@@ -124,4 +126,95 @@ public function register(Request $request): Response
124126
),
125127
]);
126128
}
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+
}
127220
}

phpmyfaq/src/public-routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
'controller' => [UserController::class, 'bookmarks'],
100100
'methods' => 'GET',
101101
],
102+
'public.user.ucp' => [
103+
'path' => '/user/ucp',
104+
'controller' => [UserController::class, 'ucp'],
105+
'methods' => 'GET',
106+
],
102107
'public.sitemap' => [
103108
'path' => '/sitemap/{letter}/{language}.html',
104109
'controller' => [FrontendSitemapController::class, 'index'],

phpmyfaq/ucp.php

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)