Elgg  Version 4.3
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  'username' => $username,
43  'password' => $password,
44  'name' => $name,
45  'email' => $email,
46  'language' => $language,
47  ]);
48 
50  $new_user->makeAdmin();
51  }
52 
53  elgg_clear_sticky_form('useradd');
54 
55  $new_user->admin_created = true;
57 
58  $subject = elgg_echo('useradd:subject', [], $new_user->getLanguage());
59  $body = elgg_echo('useradd:body', [
60  elgg_get_site_entity()->getDisplayName(),
61  elgg_get_site_entity()->getURL(),
62  $username,
63  $password,
64  ], $new_user->getLanguage());
65 
67  'action' => 'useradd',
68  'object' => $new_user,
69  'password' => $password,
70  'apply_muting' => false,
71  ]);
72 
73  return elgg_ok_response('', elgg_echo('adduser:ok', [elgg_get_site_entity()->getDisplayName()]));
74 } catch (RegistrationException $r) {
75  return elgg_error_response($r->getMessage());
76 }
$name
Definition: useradd.php:15
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:52
Elgg registration action.
elgg_generate_password()
Generate a random 12 character clear text password.
Definition: users.php:91
$admin
Definition: useradd.php:21
elgg_register_user(array $params=[])
Registers a user.
Definition: users.php:111
$username
Definition: useradd.php:11
Could not register a new user for whatever reason.
if($failures) $new_user
Definition: register.php:35
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.
$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:107
$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:59
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:99
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.
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:123
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34
$subject
Definition: useradd.php:58
$password
Definition: useradd.php:12
elgg_get_config($name, $default=null)
Get an Elgg configuration value.