Elgg  Version master
register.php
Go to the documentation of this file.
1 <?php
8 
9 /* @var $request \Elgg\Request */
10 
11 // Get variables
12 $username = (string) $request->getParam('username');
13 $password = (string) $request->getParam('password', null, false);
14 $password2 = (string) $request->getParam('password2', null, false);
15 $email = (string) $request->getParam('email');
16 $name = (string) $request->getParam('name');
17 
19 $name = trim(strip_tags($name));
20 $email = trim($email);
21 
22 try {
24  $failures = $validation->getFailures();
25  if ($failures) {
26  $messages = array_map(function (\Elgg\Validation\ValidationResult $e) {
27  return $e->getError();
28  }, $failures);
29 
30  throw new RegistrationException(implode(PHP_EOL, $messages));
31  }
32 
34  'username' => $username,
35  'password' => $password,
36  'name' => $name,
37  'email' => $email,
38  'validated' => false,
39  ]);
40 
41  $fail = function () use ($new_user) {
42  elgg_call(ELGG_IGNORE_ACCESS, function () use ($new_user) {
43  $new_user->delete();
44  });
45  };
46 
47  try {
48  // allow plugins to respond to self registration
49  // note: To catch all new users, even those created by an admin,
50  // register for the create:after, user event instead.
51  // only passing vars that aren't in ElggUser.
52  $params = $request->getParams();
53  $params['user'] = $new_user;
54 
55  if (!elgg_trigger_event_results('register', 'user', $params, true)) {
56  throw new RegistrationException(elgg_echo('registerbad'));
57  }
58 
59  if ($new_user->isValidated() === null) {
60  // no event decided to set validation status, so it will become validated
61  $new_user->setValidationStatus(true, 'register_action');
62  }
63  } catch (\Exception $e) {
64  // Catch all exception to make sure there are no incomplete user entities left behind
65  $fail();
66  throw $e;
67  }
68 
70  'user' => $new_user,
71  ];
72 
73  if (!$new_user->isEnabled()) {
74  // Plugins can alter forwarding URL by registering for 'response', 'action:register' event
76  }
77 
78  try {
80  // set forward url
82  $response_message = elgg_echo('registerok', [elgg_get_site_entity()->getDisplayName()]);
83 
85  } catch (LoginException $e) {
86  // if exception thrown, this probably means there is a validation
87  // plugin that has disabled the user
88  return elgg_error_response($e->getMessage(), REFERRER, $e->getCode() ?: ELGG_HTTP_UNAUTHORIZED);
89  }
90 } catch (RegistrationException $r) {
91  return elgg_error_response($r->getMessage(), REFERRER, $r->getCode() ?: ELGG_HTTP_BAD_REQUEST);
92 }
$messages
Definition: admin.php:12
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags...
Definition: elgglib.php:299
$password2
Definition: register.php:14
$forward_url
Definition: register.php:81
Elgg registration action.
Generic parent class for login exceptions.
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
elgg_register_user(array $params=[])
Registers a user.
Definition: users.php:162
$name
Definition: register.php:16
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
const ELGG_HTTP_UNAUTHORIZED
Definition: constants.php:65
elgg_trigger_event_results(string $event, string $type, array $params=[], $returnvalue=null)
Triggers an event where it is expected that the mixed return value could be manipulated by event call...
Definition: events.php:117
$email
Definition: register.php:15
Could not register a new user for whatever reason.
if($failures) $new_user
Definition: register.php:33
$fail
Definition: register.php:41
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
$password
Definition: register.php:13
if(!elgg_trigger_event_results('register', 'user', $params, true)) if($new_user->isValidated()===null) catch(\Exception $e) $response_data
Definition: register.php:69
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.
const REFERRER
Used in calls to forward() to specify the browser should be redirected to the referring page...
Definition: constants.php:37
elgg_validate_registration_data(string $username, string|array $password, string $name, string $email, bool $allow_multiple_emails=false)
Assert that given registration details are valid and can be used to register the user.
Definition: users.php:179
elgg_get_login_forward_url(\ElggUser $user)
Determine which URL the user should be forwarded to upon successful login.
Definition: sessions.php:141
$failures
Definition: register.php:24
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
$params['user']
Definition: register.php:53
$username
Definition: register.php:12
const ELGG_HTTP_BAD_REQUEST
Definition: constants.php:64
elgg_login(\ElggUser $user, bool $persistent=false)
Log in a user.
Definition: sessions.php:81
Login as the specified user.
$response_message
Definition: register.php:82