Elgg  Version master
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  'apply_muting' => false,
56  ];
57 
59  'from' => $site,
60  'to' => new Address($old_email, $user->getDisplayName()),
61  'subject' => $translator->translate('email:confirm:email:old:subject', [], $user->getLanguage()),
62  'body' => $translator->translate('email:confirm:email:old:body', [
63  $site->getDisplayName(),
64  $new_email,
65  $site->getURL(),
66  ], $user->getLanguage()),
67  'params' => $notification_params,
68  ]);
70 
71  $subject = $translator->translate('email:confirm:email:new:subject', [], $user->getLanguage());
72  $body = $translator->translate('email:confirm:email:new:body', [
73  $site->getDisplayName(),
74  $site->getURL(),
75  ], $user->getLanguage());
76 
77  notify_user($user->guid, $site->guid, $subject, $body, $notification_params, ['email']);
78 
79  return elgg_ok_response('', $translator->translate('email:save:success'), '');
80  }
81 }
__invoke(Request $request)
Execute a email change.
Elgg registration action.
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.
$request
Definition: livesearch.php:12
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.
$site
Definition: icons.php:5
Controller to handle confirmation of a user e-mail address change.
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.
getEntityParam(string $key= 'guid')
Gets the "entity" key from the params if it holds an Elgg entity.
Definition: Request.php:91
$notification
Definition: body.php:13
$user
Definition: ban.php:7
elgg()
Get the DI container.
Definition: Request.php:125
elgg_send_email(\Elgg\Email $email)
Send an email to any email address.
$body
Definition: useradd.php:55
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:101
Request container.
Definition: Request.php:12
notify_user(int|array $to, int $from=0, string $subject= '', string $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
Email address.
Definition: Address.php:15
$subject
Definition: useradd.php:54