Elgg  Version master
EmailChangeController.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Users;
4 
7 use Elgg\Request;
8 use Symfony\Component\Mime\Address;
9 
16 
24  public function __invoke(Request $request) {
25  $translator = $request->elgg()->translator;
26 
27  $user = $request->getEntityParam();
28  if (!$user instanceof \ElggUser) {
29  return elgg_error_response($translator->translate('error:missing_data'));
30  }
31 
32  $new_email = $user->new_email;
33  if (empty($new_email)) {
34  return elgg_error_response($translator->translate('account:email:request:error:no_new_email'));
35  }
36 
37  unset($user->new_email);
38 
39  try {
40  $request->elgg()->accounts->assertValidEmail($new_email, true);
41  } catch (RegistrationException $e) {
42  return elgg_error_response($e->getMessage());
43  }
44 
45  $old_email = $user->email;
46  $user->email = $new_email;
47  $user->save();
48 
49  // notify old and new email of the change
51 
53  'from' => $site,
54  'to' => new Address($old_email, $user->getDisplayName()),
55  'subject' => $translator->translate('email:confirm:email:old:subject', [], $user->getLanguage()),
56  'body' => $translator->translate('email:confirm:email:old:body', [
57  $site->getDisplayName(),
58  $new_email,
59  $site->getURL(),
60  ], $user->getLanguage()),
61  'params' => [
62  'object' => $user,
63  'action' => 'email_change',
64  ],
65  ]);
66 
67  $user->notify('email_change', $user);
68 
69  return elgg_ok_response('', $translator->translate('email:save:success'), '');
70  }
71 }
$site
Definition: icons.php:5
$user
Definition: ban.php:7
Could not register a new user for whatever reason.
Request container.
Definition: Request.php:12
Controller to handle confirmation of a user e-mail address change.
__invoke(Request $request)
Execute an email change.
HTTP response builder interface.
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
$request
Definition: livesearch.php:12
elgg_send_email(array|\Elgg\Email $email)
Send an email to any email address.
elgg_ok_response($content='', string|array $message='', ?string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_error_response(string|array $message='', string $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.