Elgg  Version 3.0
useradd.php
Go to the documentation of this file.
1 <?php
6 elgg_make_sticky_form('useradd');
7 
8 // Get variables
9 $username = get_input('username');
10 $password = get_input('password', null, false);
11 $password2 = get_input('password2', null, false);
12 $email = get_input('email');
13 $name = get_input('name');
14 
15 // This param is not included in the useradd form by default,
16 // but it allows sites to easily add the feature if necessary.
17 $language = get_input('language', elgg_get_config('language'));
18 
19 $admin = get_input('admin');
20 $admin = is_array($admin) ? $admin[0] : $admin;
21 
22 $autogen_password = get_input('autogen_password');
23 if ($autogen_password) {
26 }
27 
28 // no blank fields
29 if ($username == '' || $password == '' || $password2 == '' || $email == '' || $name == '') {
30  return elgg_error_response(elgg_echo('register:fields'));
31 }
32 
33 if (strcmp($password, $password2) != 0) {
34  return elgg_error_response(elgg_echo('RegistrationException:PasswordMismatch'));
35 }
36 
37 // For now, just try and register the user
38 try {
40  if ($guid === false) {
41  return elgg_error_response(elgg_echo('adduser:bad'));
42  }
43 
46  $new_user->makeAdmin();
47  }
48 
49  elgg_clear_sticky_form('useradd');
50 
51  $new_user->admin_created = true;
52  // @todo ugh, saving a guid as metadata!
54 
55  // The user language is set also by register_user(), but it defaults to
56  // language of the current user (admin), so we need to fix it here.
57  $new_user->language = $language;
58 
59  $subject = elgg_echo('useradd:subject', [], $new_user->language);
60  $body = elgg_echo('useradd:body', [
61  $name,
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  ]);
73 
74  return elgg_ok_response('', elgg_echo('adduser:ok', [elgg_get_site_entity()->getDisplayName()]));
75 } catch (RegistrationException $r) {
76  return elgg_error_response($r->getMessage());
77 }
$name
Definition: useradd.php:13
get_user($guid)
Get a user object from a GUID.
Definition: users.php:40
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:57
$admin
Definition: useradd.php:19
$username
Definition: useradd.php:9
$guid
Removes an admin notice.
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
notify_user($to, $from=0, $subject= '', $message= '', array $params=[], $methods_override=null)
Notify a user via their preferences.
$password2
Definition: useradd.php:11
$email
Definition: useradd.php:12
$autogen_password
Definition: useradd.php:22
$language
Definition: useradd.php:17
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_error_response($error= '', $forward_url=REFERRER, $status_code=ELGG_HTTP_OK)
Prepare an error response to be returned by a page or an action handler.
elgg_make_sticky_form($form_name)
Save form submission data (all GET and POST vars) into a session cache.
Definition: input.php:120
register_user($username, $password, $name, $email, $allow_multiple_emails=false, $subtype=null)
Registers a user, returning false if the username already exists.
Definition: users.php:205
$body
Definition: useradd.php:60
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
elgg_ok_response($content= '', $message= '', $forward_url=null, $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
generate_random_cleartext_password()
Generate a random 12 character clear text password.
Definition: users.php:146
if($guid===false) $new_user
Definition: useradd.php:44
elgg_clear_sticky_form($form_name)
Remove form submission data from the session.
Definition: input.php:136
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:39
$subject
Definition: useradd.php:59
$password
Definition: useradd.php:10
elgg_get_config($name, $default=null)
Get an Elgg configuration value.