Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

actions/useradd.php (4 issues)

Labels
Severity
1
<?php
2
/**
3
 * Elgg add action
4
 */
5
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) {
24
	$password = generate_random_cleartext_password();
25
	$password2 = $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 {
39
	$guid = register_user($username, $password, $name, $email, true);
40
41
	if (!$guid) {
42
		return elgg_error_response(elgg_echo('adduser:bad'));
43
	}
44
45
	$new_user = get_user($guid);
46
	if ($new_user && $admin && elgg_is_admin_logged_in()) {
47
		$new_user->makeAdmin();
48
	}
49
50
	elgg_clear_sticky_form('useradd');
51
52
	$new_user->admin_created = true;
0 ignored issues
show
The property admin_created does not exist on false.
Loading history...
53
	// @todo ugh, saving a guid as metadata!
54
	$new_user->created_by_guid = elgg_get_logged_in_user_guid();
0 ignored issues
show
The property created_by_guid does not exist on false.
Loading history...
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;
0 ignored issues
show
The property language does not exist on false.
Loading history...
59
60
	$subject = elgg_echo('useradd:subject', [], $new_user->language);
61
	$body = elgg_echo('useradd:body', [
62
		$name,
63
		elgg_get_site_entity()->name,
64
		elgg_get_site_entity()->url,
65
		$username,
66
		$password,
67
	], $new_user->language);
68
69
	notify_user($new_user->guid, elgg_get_site_entity()->guid, $subject, $body, [
0 ignored issues
show
The property guid does not exist on false.
Loading history...
70
		'action' => 'useradd',
71
		'object' => $new_user,
72
		'password' => $password,
73
	]);
74
75
	return elgg_ok_response('', elgg_echo('adduser:ok', [elgg_get_site_entity()->name]));
76
} catch (RegistrationException $r) {
77
	return elgg_error_response($r->getMessage());
78
}
79