Elgg  Version 4.x
useradd.php
Go to the documentation of this file.
1 <?php
7 
8 elgg_make_sticky_form('useradd', ['password', 'password2']);
9 
10 // Get variables
11 $username = get_input('username');
12 $password = get_input('password', null, false);
13 $password2 = get_input('password2', null, false);
14 $email = get_input('email');
15 $name = get_input('name');
16 
17 // This param is not included in the useradd form by default,
18 // but it allows sites to easily add the feature if necessary.
19 $language = get_input('language', elgg_get_config('language'));
20 
21 $admin = get_input('admin');
22 $admin = is_array($admin) ? $admin[0] : $admin;
23 
24 $autogen_password = get_input('autogen_password');
25 if ($autogen_password) {
28 }
29 
30 // no blank fields
31 if ($username == '' || $password == '' || $password2 == '' || $email == '' || $name == '') {
32  return elgg_error_response(elgg_echo('register:fields'));
33 }
34 
35 if (strcmp($password, $password2) != 0) {
36  return elgg_error_response(elgg_echo('RegistrationException:PasswordMismatch'));
37 }
38 
39 // For now, just try and register the user
40 try {
42  if ($guid === false) {
43  return elgg_error_response(elgg_echo('adduser:bad'));
44  }
45 
48  $new_user->makeAdmin();
49  }
50 
51  elgg_clear_sticky_form('useradd');
52 
53  $new_user->admin_created = true;
55 
56  // The user language is set also by register_user(), but it defaults to
57  // language of the current user (admin), so we need to fix it here.
58  $new_user->language = $language;
59 
60  $subject = elgg_echo('useradd:subject', [], $new_user->language);
61  $body = elgg_echo('useradd:body', [
62  elgg_get_site_entity()->getDisplayName(),
63  elgg_get_site_entity()->getURL(),
64  $username,
65  $password,
66  ], $new_user->language);
67 
69  'action' => 'useradd',
70  'object' => $new_user,
71  'password' => $password,
72  'apply_muting' => false,
73  ]);
74 
75  return elgg_ok_response('', elgg_echo('adduser:ok', [elgg_get_site_entity()->getDisplayName()]));
76 } catch (RegistrationException $r) {
77  return elgg_error_response($r->getMessage());
78 }
$name
Definition: useradd.php:15
get_user($guid)
Get a user object from a GUID.
Definition: users.php:20
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:54
Elgg registration action.
$admin
Definition: useradd.php:21
$username
Definition: useradd.php:11
Could not register a new user for whatever reason.
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
notify_user($to, $from=0, $subject= '', $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
register_user($username, $password, $name, $email, $allow_multiple_emails=false, $subtype=null, array $params=[])
Registers a user, returning false if the username already exists.
Definition: users.php:151
$password2
Definition: useradd.php:13
elgg_make_sticky_form($form_name, array $ignored_field_names=[])
Save form submission data (all GET and POST vars) into a session cache.
Definition: input.php:116
$email
Definition: useradd.php:14
$autogen_password
Definition: useradd.php:24
$language
Definition: useradd.php:19
get_input($variable, $default=null, $filter_result=true)
Parameter input functions.
Definition: input.php:20
$body
Definition: useradd.php:61
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
generate_random_cleartext_password()
Generate a random 12 character clear text password.
Definition: users.php:132
elgg_error_response($message= '', $forward_url=REFERRER, int $status_code=ELGG_HTTP_BAD_REQUEST)
Prepare an error response to be returned by a page or an action handler.
if($guid===false) $new_user
Definition: useradd.php:46
elgg_ok_response($content= '', $message= '', $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_clear_sticky_form($form_name)
Remove form submission data from the session.
Definition: input.php:132
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:36
$subject
Definition: useradd.php:60
$password
Definition: useradd.php:12
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
$guid
Reset an ElggUpgrade.
Definition: reset.php:6