| Total Complexity | 68 |
| Total Lines | 679 |
| Duplicated Lines | 0 % |
| Coverage | 55.77% |
| Changes | 7 | ||
| Bugs | 1 | Features | 0 |
Complex classes like BoneUserController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BoneUserController, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 37 | class BoneUserController extends Controller implements SessionAwareInterface, SiteConfigAwareInterface |
||
| 38 | { |
||
| 39 | use HasSessionTrait; |
||
| 40 | use HasSiteConfigTrait; |
||
| 41 | |||
| 42 | /** @var UserService $userService */ |
||
| 43 | private $userService; |
||
| 44 | |||
| 45 | /** @var MailService $mailService */ |
||
| 46 | private $mailService; |
||
| 47 | |||
| 48 | /** @var string $loginRedirectRoute */ |
||
| 49 | private $loginRedirectRoute; |
||
| 50 | |||
| 51 | /** @var string $logo */ |
||
| 52 | private $logo; |
||
| 53 | |||
| 54 | /** @var string $adminLayout */ |
||
| 55 | private $adminLayout; |
||
| 56 | |||
| 57 | /** @var bool $registrationEnabled */ |
||
| 58 | private $registrationEnabled; |
||
| 59 | |||
| 60 | /** @var bool $profileRequired */ |
||
| 61 | private $profileRequired; |
||
| 62 | |||
| 63 | /** @var bool $rememberMeCookie */ |
||
| 64 | private $rememberMeCookie; |
||
| 65 | |||
| 66 | /** @var PasetoService $pasetoService */ |
||
| 67 | private $pasetoService = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * BoneUserController constructor. |
||
| 71 | * @param UserService $userService |
||
| 72 | * @param MailService $mailService |
||
| 73 | */ |
||
| 74 | 27 | public function __construct(UserService $userService, MailService $mailService, string $loginRedirectRoute = '/user/home', |
|
| 75 | string $adminLayout, PasetoService $pasetoService, bool $registrationEnabled = true, $profileRequired = false, |
||
| 76 | bool $rememberMeCookie = true) |
||
| 77 | { |
||
| 78 | 27 | $this->userService = $userService; |
|
| 79 | 27 | $this->mailService = $mailService; |
|
| 80 | 27 | $this->loginRedirectRoute = $loginRedirectRoute; |
|
| 81 | 27 | $this->adminLayout = $adminLayout; |
|
| 82 | 27 | $this->registrationEnabled = $registrationEnabled; |
|
| 83 | 27 | $this->profileRequired = $profileRequired; |
|
| 84 | 27 | $this->rememberMeCookie = $rememberMeCookie; |
|
| 85 | 27 | $this->profileRequired = $profileRequired; |
|
| 86 | 27 | $this->pasetoService = $pasetoService; |
|
| 87 | 27 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 19 | public function getLogo(): string |
|
| 93 | { |
||
| 94 | 19 | if (!$this->logo) { |
|
| 95 | 19 | $this->logo = $this->getSiteConfig()->getLogo(); |
|
| 96 | } |
||
| 97 | |||
| 98 | 19 | return $this->logo; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param ServerRequestInterface $request |
||
| 103 | * @return ResponseInterface $response |
||
| 104 | * @throws \Exception |
||
| 105 | */ |
||
| 106 | 1 | public function indexAction(ServerRequestInterface $request): ResponseInterface |
|
|
|
|||
| 107 | { |
||
| 108 | 1 | if ($this->getSession()->get('user')) { |
|
| 109 | 1 | return new RedirectResponse('/user/home'); |
|
| 110 | } |
||
| 111 | |||
| 112 | 1 | $body = $this->getView()->render('boneuser::index', ['logo' => $this->getLogo()]); |
|
| 113 | |||
| 114 | 1 | return new HtmlResponse($body); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param ServerRequestInterface $request |
||
| 119 | * @return ResponseInterface |
||
| 120 | * @throws UserException |
||
| 121 | */ |
||
| 122 | 4 | public function registerAction(ServerRequestInterface $request): ResponseInterface |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param ServerRequestInterface $request |
||
| 173 | * @return ResponseInterface |
||
| 174 | * @throws \Doctrine\ORM\ORMException |
||
| 175 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 176 | */ |
||
| 177 | 3 | public function activateAction(ServerRequestInterface $request): ResponseInterface |
|
| 178 | { |
||
| 179 | 3 | $email = $request->getAttribute('email'); |
|
| 180 | 3 | $token = $request->getAttribute('token'); |
|
| 181 | 3 | $translator = $this->getTranslator(); |
|
| 182 | 3 | $userService = $this->userService; |
|
| 183 | 3 | $loginRedirect = $this->loginRedirectRoute; |
|
| 184 | 3 | $message = null; |
|
| 185 | |||
| 186 | try { |
||
| 187 | |||
| 188 | 3 | $link = $userService->findEmailLink($email, $token); |
|
| 189 | 1 | $user = $link->getUser(); |
|
| 190 | 1 | $user->setState(new State(State::STATE_ACTIVATED)); |
|
| 191 | 1 | $user->setLastLogin(new DateTime()); |
|
| 192 | 1 | $userService->saveUser($user); |
|
| 193 | 1 | $userService->deleteEmailLink($link); |
|
| 194 | 1 | $this->getSession()->set('user', $user->getId()); |
|
| 195 | |||
| 196 | 2 | } catch (EmailLinkException $e) { |
|
| 197 | 2 | switch ($e->getMessage()) { |
|
| 198 | 2 | case EmailLinkException::LINK_EXPIRED: |
|
| 199 | 1 | $message = [$translator->translate('login.activation.expired', 'user') |
|
| 200 | 1 | . ' <a href="/user/resend-activation-mail/' . $email . '">' |
|
| 201 | 1 | . $translator->translate('login.activation.expired2', 'user') . '</a>', 'danger']; |
|
| 202 | 1 | break; |
|
| 203 | default: |
||
| 204 | 1 | $message = [$e->getMessage(), 'danger']; |
|
| 205 | 1 | break; |
|
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | 3 | $body = $this->getView()->render('boneuser::activate-user-account', [ |
|
| 210 | 3 | 'loginRedirect' => $loginRedirect, |
|
| 211 | 3 | 'message' => $message, |
|
| 212 | 3 | 'logo' => $this->getLogo(), |
|
| 213 | ]); |
||
| 214 | |||
| 215 | 3 | return new HtmlResponse($body); |
|
| 216 | } |
||
| 217 | |||
| 218 | 7 | private function initForm(LoginForm $form) |
|
| 219 | { |
||
| 220 | 7 | if ($this->rememberMeCookie === false) { |
|
| 221 | 7 | $form->getFields()->removeByName('remember'); |
|
| 222 | } |
||
| 223 | 7 | } |
|
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param ServerRequestInterface $request |
||
| 228 | * @return ResponseInterface |
||
| 229 | */ |
||
| 230 | 1 | public function loginAction(ServerRequestInterface $request): ResponseInterface |
|
| 231 | { |
||
| 232 | 1 | $form = new LoginForm('userlogin', $this->getTranslator()); |
|
| 233 | 1 | $this->initForm($form); |
|
| 234 | 1 | $body = $this->getView()->render('boneuser::login', ['form' => $form, 'logo' => $this->getLogo()]); |
|
| 235 | |||
| 236 | 1 | return new HtmlResponse($body); |
|
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param ServerRequestInterface $request |
||
| 242 | * @return ResponseInterface |
||
| 243 | */ |
||
| 244 | 6 | public function loginFormAction(ServerRequestInterface $request): ResponseInterface |
|
| 245 | { |
||
| 246 | 6 | $translator = $this->getTranslator(); |
|
| 247 | 6 | $form = new LoginForm('userlogin', $translator); |
|
| 248 | 6 | $this->initForm($form); |
|
| 249 | 6 | $post = $request->getParsedBody() ?: []; |
|
| 250 | 6 | $form->populate($post); |
|
|
1 ignored issue
–
show
|
|||
| 251 | 6 | $params = ['form' => $form]; |
|
| 252 | |||
| 253 | try { |
||
| 254 | |||
| 255 | 6 | if ($form->isValid()) { |
|
| 256 | 5 | $data = $form->getValues(); |
|
| 257 | 5 | $email = $data['email']; |
|
| 258 | 5 | $pass = $data['password']; |
|
| 259 | 5 | $userId = $this->userService->authenticate($email, $pass); |
|
| 260 | 5 | $locale = $translator->getLocale(); |
|
| 261 | 5 | $session = $this->getSession(); |
|
| 262 | 5 | $session->set('user', $userId); |
|
| 263 | 5 | $session->set('locale', $locale); |
|
| 264 | 5 | $this->rememberMeCookie && isset($data['remember']) ? $this->setCookie((int)$data['remember'], $userId) : null; |
|
| 265 | |||
| 266 | 5 | if ($route = $session->get('loginRedirectRoute')) { |
|
| 267 | 5 | $this->loginRedirectRoute = $route; |
|
| 268 | 5 | $session->unset('loginRedirectRoute'); |
|
| 269 | } |
||
| 270 | |||
| 271 | 5 | $user = $this->userService->findUserById($userId); |
|
| 272 | 1 | $user->setLastLogin(new DateTime()); |
|
| 273 | 1 | $this->userService->saveUser($user); |
|
| 274 | |||
| 275 | 1 | if ($this->profileRequired && !$this->userService->hasProfile($user)) { |
|
| 276 | 1 | $this->loginRedirectRoute = '/user/edit-profile'; |
|
| 277 | } |
||
| 278 | |||
| 279 | 2 | return new RedirectResponse('/' . $locale . $this->loginRedirectRoute); |
|
| 280 | } |
||
| 281 | 4 | } catch (UserException $e) { |
|
| 282 | 4 | switch ($e->getMessage()) { |
|
| 283 | 4 | case UserException::USER_NOT_FOUND: |
|
| 284 | 4 | case UserException::WRONG_PASSWORD: |
|
| 285 | 1 | $message = [Icon::WARNING . ' ' . $translator->translate('login.error.password', 'user') . '<a href="/user/lost-password/' . $email . '">' . $translator->translate('login.error.password2', 'user') . '</a>', 'danger']; |
|
| 286 | 1 | break; |
|
| 287 | 3 | case UserException::USER_UNACTIVATED: |
|
| 288 | 1 | $message = [Icon::WARNING . ' ' . $translator->translate('login.unactivated', 'user') . '<a href="/user/resend-activation-mail/' . $email . '">' . $translator->translate('login.unactivated2', 'user') . '</a>', 'danger']; |
|
| 289 | 1 | break; |
|
| 290 | 2 | case UserException::USER_DISABLED: |
|
| 291 | 2 | case UserException::USER_BANNED: |
|
| 292 | 1 | $message = [Icon::REMOVE . ' ' . $translator->translate('login.activation.banned', 'user'), 'danger']; |
|
| 293 | 1 | break; |
|
| 294 | default: |
||
| 295 | 1 | $message = $e->getMessage(); |
|
| 296 | 1 | break; |
|
| 297 | } |
||
| 298 | |||
| 299 | 4 | $params['message'] = $message; |
|
| 300 | } |
||
| 301 | |||
| 302 | 5 | $params['logo'] = $this->getLogo(); |
|
| 303 | 5 | $body = $this->getView()->render('boneuser::login', $params); |
|
| 304 | |||
| 305 | 5 | return new HtmlResponse($body); |
|
| 306 | |||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param int $length |
||
| 311 | * @param int $userId |
||
| 312 | * @throws \ParagonIE\Paseto\Exception\InvalidKeyException |
||
| 313 | * @throws \ParagonIE\Paseto\Exception\InvalidPurposeException |
||
| 314 | * @throws \ParagonIE\Paseto\Exception\PasetoException |
||
| 315 | */ |
||
| 316 | private function setCookie(int $length, int $userId): void |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param ServerRequestInterface $request |
||
| 341 | * @return ResponseInterface |
||
| 342 | */ |
||
| 343 | 2 | public function homePageAction(ServerRequestInterface $request): ResponseInterface |
|
| 344 | { |
||
| 345 | 2 | if ($this->loginRedirectRoute !== '/user/home') { |
|
| 346 | 1 | return new RedirectResponse($this->loginRedirectRoute); |
|
| 347 | } |
||
| 348 | |||
| 349 | 1 | $user = $request->getAttribute('user'); |
|
| 350 | 1 | $body = $this->getView()->render('boneuser::home', [ |
|
| 351 | 1 | 'message' => [$this->getTranslator()->translate('home.loggedin', 'user'), 'success'], |
|
| 352 | 1 | 'user' => $user, |
|
| 353 | 1 | 'logo' => $this->getSiteConfig()->getLogo(), |
|
| 354 | ]); |
||
| 355 | |||
| 356 | 1 | return new LayoutResponse($body, $this->adminLayout); |
|
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param ServerRequestInterface $request |
||
| 361 | * @return ResponseInterface |
||
| 362 | */ |
||
| 363 | 1 | public function logoutAction(ServerRequestInterface $request): ResponseInterface |
|
| 364 | { |
||
| 365 | 1 | SessionManager::destroySession(); |
|
| 366 | 1 | \setcookie('resu', '', 1, '/'); |
|
| 367 | |||
| 368 | 1 | return new RedirectResponse(new Uri('/')); |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param ServerRequestInterface $request |
||
| 373 | * @return ResponseInterface |
||
| 374 | */ |
||
| 375 | 4 | public function resendActivationEmailAction(ServerRequestInterface $request): ResponseInterface |
|
| 376 | { |
||
| 377 | 4 | $success = false; |
|
| 378 | 4 | $email = $request->getAttribute('email'); |
|
| 379 | 4 | $user = $this->userService->findUserByEmail($email); |
|
| 380 | 4 | $message = []; |
|
| 381 | 4 | $translator = $this->getTranslator(); |
|
| 382 | |||
| 383 | 4 | if (!$user) { |
|
| 384 | 1 | throw new Exception(UserException::USER_NOT_FOUND, 404); |
|
| 385 | } |
||
| 386 | |||
| 387 | 3 | if ($user->getState()->getValue() == State::STATE_ACTIVATED) { |
|
| 388 | 1 | $message = [UserException::USER_ACTIVATED, 'danger']; |
|
| 389 | } else { |
||
| 390 | try { |
||
| 391 | 2 | $link = $this->userService->generateEmailLink($user); |
|
| 392 | 1 | $mail = $this->mailService; |
|
| 393 | |||
| 394 | 1 | $env = $mail->getSiteConfig()->getEnvironment(); |
|
| 395 | 1 | $email = $user->getEmail(); |
|
| 396 | 1 | $token = $link->getToken(); |
|
| 397 | |||
| 398 | 1 | $mail = new EmailMessage(); |
|
| 399 | 1 | $mail->setTo($user->getEmail()); |
|
| 400 | 1 | $mail->setSubject($translator->translate('email.user.register.thankswith', 'user') . ' ' . $this->mailService->getSiteConfig()->getTitle()); |
|
| 401 | 1 | $mail->setTemplate('email.user::user_registration/user_registration'); |
|
| 402 | 1 | $mail->setViewData([ |
|
| 403 | 1 | 'siteUrl' => $env->getSiteURL(), |
|
| 404 | 1 | 'logo' => $this->getSiteConfig()->getEmailLogo(), |
|
| 405 | 1 | 'address' => $this->getSiteConfig()->getAddress(), |
|
| 406 | 1 | 'activationLink' => '/user/activate/' . $email . '/' . $token, |
|
| 407 | ]); |
||
| 408 | 1 | $this->mailService->sendEmail($mail); |
|
| 409 | |||
| 410 | 1 | } catch (Exception $e) { |
|
| 411 | 1 | $message = [$translator->translate('login.resendactivation.error', 'user') |
|
| 412 | 1 | . $this->getSiteConfig()->getContactEmail() . '', 'danger']; |
|
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | 3 | $body = $this->getView()->render('boneuser::resend-activation', [ |
|
| 417 | 3 | 'message' => null, |
|
| 418 | 3 | 'logo' => $this->getLogo(), |
|
| 419 | ]); |
||
| 420 | |||
| 421 | 3 | return new HtmlResponse($body); |
|
| 422 | } |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * @param ServerRequestInterface $request |
||
| 427 | * @return ResponseInterface |
||
| 428 | */ |
||
| 429 | 4 | public function forgotPasswordAction(ServerRequestInterface $request): ResponseInterface |
|
| 430 | { |
||
| 431 | 4 | $email = $request->getAttribute('email'); |
|
| 432 | 4 | $user = $this->userService->findUserByEmail($email); |
|
| 433 | |||
| 434 | 4 | if (!$user) { |
|
| 435 | 1 | throw new Exception(UserException::USER_NOT_FOUND, 404); |
|
| 436 | } |
||
| 437 | |||
| 438 | 3 | if ($user->getState()->getValue() == State::STATE_UNACTIVATED) { |
|
| 439 | 1 | return new RedirectResponse('/user/resend-activation-mail/' . $email); |
|
| 440 | } |
||
| 441 | |||
| 442 | try { |
||
| 443 | |||
| 444 | 2 | $link = $this->userService->generateEmailLink($user); |
|
| 445 | 1 | $email = $user->getEmail(); |
|
| 446 | 1 | $token = $link->getToken(); |
|
| 447 | 1 | $env = $this->getSiteConfig()->getEnvironment(); |
|
| 448 | 1 | $mail = new EmailMessage(); |
|
| 449 | 1 | $mail->setTo($email); |
|
| 450 | 1 | $mail->setSubject($this->getTranslator()->translate('email.forgotpass.subject', 'user') . $this->mailService->getSiteConfig()->getTitle() . '.'); |
|
| 451 | 1 | $mail->setTemplate('email.user::user_registration/reset_password'); |
|
| 452 | 1 | $mail->setViewData([ |
|
| 453 | 1 | 'siteUrl' => $env->getSiteURL(), |
|
| 454 | 1 | 'logo' => $this->getSiteConfig()->getEmailLogo(), |
|
| 455 | 1 | 'address' => $this->getSiteConfig()->getAddress(), |
|
| 456 | 1 | 'resetLink' => '/user/reset-password/' . $email . '/' . $token, |
|
| 457 | ]); |
||
| 458 | 1 | $this->mailService->sendEmail($mail); |
|
| 459 | |||
| 460 | |||
| 461 | 1 | } catch (Exception $e) { |
|
| 462 | 1 | $this->view->message = [$e->getMessage(), 'danger']; |
|
| 463 | } |
||
| 464 | |||
| 465 | 2 | $body = $this->getView()->render('boneuser::forgot-password', ['logo' => $this->getLogo()]); |
|
| 466 | |||
| 467 | 2 | return new HtmlResponse($body); |
|
| 468 | } |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * @param ServerRequestInterface $request |
||
| 473 | * @return ResponseInterface |
||
| 474 | */ |
||
| 475 | public function resetPasswordAction(ServerRequestInterface $request): ResponseInterface |
||
| 476 | { |
||
| 477 | $email = $request->getAttribute('email'); |
||
| 478 | $token = $request->getAttribute('token'); |
||
| 479 | $form = new ResetPasswordForm('resetpass'); |
||
| 480 | $translator = $this->getTranslator(); |
||
| 481 | $params = []; |
||
| 482 | $success = false; |
||
| 483 | $user = $this->userService->findUserByEmail($email); |
||
| 484 | |||
| 485 | if (!$user) { |
||
| 486 | throw new Exception(UserException::USER_NOT_FOUND, 404); |
||
| 487 | } |
||
| 488 | |||
| 489 | try { |
||
| 490 | $link = $this->userService->findEmailLink($email, $token); |
||
| 491 | |||
| 492 | if ($request->getMethod() === 'POST') { |
||
| 493 | |||
| 494 | $data = $request->getParsedBody(); |
||
| 495 | $form->populate($data); |
||
|
1 ignored issue
–
show
|
|||
| 496 | |||
| 497 | if ($form->isValid()) { |
||
| 498 | $data = $form->getValues(); |
||
| 499 | |||
| 500 | if ($data['password'] === $data['confirm']) { |
||
| 501 | $this->userService->changePassword($user, $data['password']); |
||
| 502 | $this->userService->deleteEmailLink($link); |
||
| 503 | $message = [$translator->translate('email.resetpass.success', 'user'), 'success']; |
||
| 504 | $success = true; |
||
| 505 | SessionManager::set('user', $user->getId()); |
||
| 506 | } else { |
||
| 507 | $message = [$translator->translate('email.resetpass.nomatch', 'user'), 'danger']; |
||
| 508 | $form = new ResetPasswordForm('resetpass'); |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } catch (EmailLinkException $e) { |
||
| 513 | $message = [$e->getMessage(), 'danger']; |
||
| 514 | } catch (Exception $e) { |
||
| 515 | throw $e; |
||
| 516 | } |
||
| 517 | |||
| 518 | if (isset($message)) { |
||
| 519 | $params['message'] = $message; |
||
| 520 | } |
||
| 521 | |||
| 522 | $params['success'] = $success; |
||
| 523 | $params['form'] = $form; |
||
| 524 | $params['logo'] = $this->getLogo(); |
||
| 525 | $body = $this->getView()->render('boneuser::reset-pass', $params); |
||
| 526 | |||
| 527 | return new HtmlResponse($body); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param ServerRequestInterface $request |
||
| 532 | * @return ResponseInterface |
||
| 533 | */ |
||
| 534 | public function changePasswordAction(ServerRequestInterface $request): ResponseInterface |
||
| 535 | { |
||
| 536 | $user = $request->getAttribute('user'); |
||
| 537 | $form = new ResetPasswordForm('resetpass'); |
||
| 538 | $translator = $this->getTranslator(); |
||
| 539 | $message = null; |
||
| 540 | $success = false; |
||
| 541 | |||
| 542 | if ($request->getMethod() === 'POST') { |
||
| 543 | |||
| 544 | $data = $request->getParsedBody(); |
||
| 545 | $form->populate($data); |
||
| 546 | |||
| 547 | if ($form->isValid()) { |
||
| 548 | $data = $form->getValues(); |
||
| 549 | if ($data['password'] === $data['confirm']) { |
||
| 550 | $this->userService->changePassword($user, $data['password']); |
||
| 551 | $message = [Icon::CHECK_CIRCLE . ' ' . $translator->translate('email.resetpass.success', 'user'), 'success']; |
||
| 552 | $success = true; |
||
| 553 | } else { |
||
| 554 | $message = [Icon::WARNING . ' ' . $translator->translate('email.resetpass.nomatch', 'user'), 'danger']; |
||
| 555 | $form = new ResetPasswordForm('resetpass'); |
||
| 556 | } |
||
| 557 | } else { |
||
| 558 | $message = [Icon::WARNING . ' There was a problem with your form.', 'danger']; |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | $params['success'] = $success; |
||
| 563 | $params['form'] = $form; |
||
| 564 | $params['logo'] = $this->getLogo(); |
||
| 565 | |||
| 566 | $body = $this->getView()->render('boneuser::change-pass', [ |
||
| 567 | 'success' => $success, |
||
| 568 | 'form' => $form, |
||
| 569 | 'logo' => $this->getLogo(), |
||
| 570 | 'message' => $message |
||
| 571 | ]); |
||
| 572 | |||
| 573 | return new HtmlResponse($body); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param ServerRequestInterface $request |
||
| 578 | * @return ResponseInterface |
||
| 579 | */ |
||
| 580 | public function changeEmailAction(ServerRequestInterface $request): ResponseInterface |
||
| 581 | { |
||
| 582 | $user = $request->getAttribute('user'); |
||
| 583 | $form = new LoginForm('changeemail', $this->getTranslator()); |
||
| 584 | $form->getField('email')->setLabel('New email'); |
||
| 585 | $form->getField('submit')->setValue('Submit'); |
||
| 586 | $translator = $this->getTranslator(); |
||
| 587 | $params = [ |
||
| 588 | 'form' => $form |
||
| 589 | ]; |
||
| 590 | |||
| 591 | if ($request->getMethod() === 'POST') { |
||
| 592 | |||
| 593 | $data = $request->getParsedBody(); |
||
| 594 | $form->populate($data); |
||
| 595 | |||
| 596 | if ($form->isValid($data)) { |
||
| 597 | |||
| 598 | $newEmail = $form->getField('email')->getValue(); |
||
| 599 | $password = $form->getField('password')->getValue(); |
||
| 600 | |||
| 601 | $existing = $this->userService->findUserByEmail($newEmail); |
||
| 602 | |||
| 603 | if ($existing) { |
||
| 604 | $message = [$translator->translate('email.changeemail.registered', 'user') . $this->getSiteConfig()->getTitle() . '.', 'danger']; |
||
| 605 | } else { |
||
| 606 | if ($this->userService->checkPassword($user, $password)) { |
||
| 607 | |||
| 608 | $link = $this->userService->generateEmailLink($user); |
||
| 609 | |||
| 610 | try { |
||
| 611 | |||
| 612 | $link = $this->userService->generateEmailLink($user); |
||
| 613 | $email = $user->getEmail(); |
||
| 614 | $token = $link->getToken(); |
||
| 615 | $env = $this->getSiteConfig()->getEnvironment(); |
||
| 616 | $mail = new EmailMessage(); |
||
| 617 | $mail->setTo($email); |
||
| 618 | $mail->setSubject($translator->translate('email.changeemail.subject', 'user') . $this->mailService->getSiteConfig()->getTitle() . '.'); |
||
| 619 | $mail->setTemplate('email.user::user_registration/change_email'); |
||
| 620 | $mail->setViewData([ |
||
| 621 | 'siteUrl' => $env->getSiteURL(), |
||
| 622 | 'logo' => $this->getSiteConfig()->getEmailLogo(), |
||
| 623 | 'address' => $this->getSiteConfig()->getAddress(), |
||
| 624 | 'resetLink' => '/user/reset-email/' . $email . '/' . $newEmail . '/' . $token, |
||
| 625 | ]); |
||
| 626 | $this->mailService->sendEmail($mail); |
||
| 627 | $message = [$translator->translate('email.changeemail.sent', 'user'), 'info']; |
||
| 628 | unset ($params['form']); |
||
| 629 | |||
| 630 | } catch (Exception $e) { |
||
| 631 | $message = [$translator->translate('email.changeemail.notsent', 'user') . $this->config->email->support . '.', 'danger']; |
||
| 632 | } |
||
| 633 | |||
| 634 | } else { |
||
| 635 | $message = [$translator->translate('email.changeemail.wrongpass', 'user'), 'danger']; |
||
| 636 | } |
||
| 637 | } |
||
| 638 | } |
||
| 639 | $params['message'] = $message; |
||
| 640 | } |
||
| 641 | $params['logo'] = $this->getLogo(); |
||
| 642 | |||
| 643 | $body = $this->getView()->render('boneuser::change-email', $params); |
||
| 644 | |||
| 645 | return new LayoutResponse($body, 'layouts::admin'); |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @param ServerRequestInterface $request |
||
| 650 | * @return ResponseInterface |
||
| 651 | */ |
||
| 652 | public function editProfileAction(ServerRequestInterface $request): ResponseInterface |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param ServerRequestInterface $requestApiController |
||
| 687 | * @return ResponseInterface |
||
| 688 | */ |
||
| 689 | public function resetEmailAction(ServerRequestInterface $request): ResponseInterface |
||
| 716 | } |
||
| 717 | } |
||
| 718 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.