Elgg  Version 6.3
EmailChangeController.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Users;
4 
5 use Elgg\Email;
9 use Elgg\Request;
10 
17 
25  public function __invoke(Request $request) {
26  $translator = $request->elgg()->translator;
27 
28  $user = $request->getEntityParam();
29  if (!$user instanceof \ElggUser) {
30  return elgg_error_response($translator->translate('error:missing_data'));
31  }
32 
33  $new_email = $user->new_email;
34  if (empty($new_email)) {
35  return elgg_error_response($translator->translate('account:email:request:error:no_new_email'));
36  }
37 
38  unset($user->new_email);
39 
40  try {
41  $request->elgg()->accounts->assertValidEmail($new_email, true);
42  } catch (RegistrationException $e) {
43  return elgg_error_response($e->getMessage());
44  }
45 
46  $old_email = $user->email;
47  $user->email = $new_email;
48  $user->save();
49 
50  // notify old and new email of the change
52  $notification_params = [
53  'object' => $user,
54  'action' => 'email_change',
55  ];
56 
58  'from' => $site,
59  'to' => new Address($old_email, $user->getDisplayName()),
60  'subject' => $translator->translate('email:confirm:email:old:subject', [], $user->getLanguage()),
61  'body' => $translator->translate('email:confirm:email:old:body', [
62  $site->getDisplayName(),
63  $new_email,
64  $site->getURL(),
65  ], $user->getLanguage()),
66  'params' => $notification_params,
67  ]);
69 
70  $user->notify('email_change', $user);
71 
72  return elgg_ok_response('', $translator->translate('email:save:success'), '');
73  }
74 }
$site
Definition: icons.php:5
$user
Definition: ban.php:7
Email address.
Definition: Address.php:15
static factory(array $options=[])
Create an email instance form an array of options.
Definition: Email.php:80
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.
$notification
Definition: body.php:13
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
HTTP response builder interface.
$request
Definition: livesearch.php:12
elgg_send_email(\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.