Elgg  Version 3.0
user_settings.php
Go to the documentation of this file.
1 <?php
10 use Elgg\Request;
12 
26 function _elgg_set_user_password(\Elgg\Hook $hook) {
27 
29  if (!$actor instanceof ElggUser) {
30  return;
31  }
32 
33  $user = $hook->getUserParam();
34  $request = $hook->getParam('request');
35 
36  if (!$user instanceof ElggUser || !$request instanceof Request) {
37  return;
38  }
39 
40  $password = $request->getParam('password', null, false);
41  $password2 = $request->getParam('password2', null, false);
42 
43  if (!$password) {
44  return null;
45  }
46 
47  if (!$actor->isAdmin() || $user->guid === $actor->guid) {
48  // let admin user change anyone's password without knowing it except his own.
49 
50  $current_password = $request->getParam('current_password', null, false);
51 
52  try {
53  elgg()->accounts->assertCurrentPassword($user, $current_password);
54  } catch (RegistrationException $e) {
55  $request->validation()->fail('password', '', elgg_echo('LoginException:ChangePasswordFailure'));
56 
57  return false;
58  }
59  }
60 
61  try {
62  elgg()->accounts->assertValidPassword([$password, $password2]);
63  } catch (RegistrationException $e) {
64  $request->validation()->fail('password', '', $e->getMessage());
65 
66  return false;
67  }
68 
69  $user->setPassword($password);
70  _elgg_services()->persistentLogin->handlePasswordChange($user, $actor);
71 
72  $request->validation()->pass('password', '', elgg_echo('user:password:success'));
73 }
74 
88 function _elgg_set_user_name(\Elgg\Hook $hook) {
89 
90  $user = $hook->getUserParam();
91  $request = $hook->getParam('request');
92  /* @var $request \Elgg\Request */
93 
94  $name = $request->getParam('name');
95  if (!isset($name)) {
96  return null;
97  }
98 
99  $name = strip_tags($name);
100  if (empty($name)) {
101  $request->validation()->fail('name', $request->getParam('name'), elgg_echo('user:name:fail'));
102 
103  return false;
104  }
105 
106  if ($name === $user->name) {
107  return null;
108  }
109 
110  $request->validation()->pass('name', $name, elgg_echo('user:name:success'));
111 
112  $user->name = $name;
113 
114 }
115 
130 function _elgg_set_user_username(\Elgg\Hook $hook) {
131 
132  $user = $hook->getUserParam();
133  $request = $hook->getParam('request');
134 
135  if (!$user instanceof ElggUser || !$request instanceof Request) {
136  return null;
137  }
138 
139  $username = $request->getParam('username');
140  if (!isset($username)) {
141  return null;
142  }
143 
144  if (!elgg_is_admin_logged_in()) {
145  return null;
146  }
147 
148  if ($user->username === $username) {
149  return null;
150  }
151 
152  // check if username is valid and does not exist
153  try {
154  elgg()->accounts->assertValidUsername($username, true);
155  } catch (RegistrationException $ex) {
156  $request->validation()->fail('username', $username, $ex->getMessage());
157 
158  return false;
159  }
160 
161  $user->username = $username;
162 
163  $request->validation()->pass('username', $username, elgg_echo('user:username:success'));
164 
165  // correctly forward after after a username change
166  elgg_register_plugin_hook_handler('response', 'action:usersettings/save', function (\Elgg\Hook $hook) use ($username) {
167  $response = $hook->getValue();
168  if (!$response instanceof ResponseBuilder) {
169  return;
170  }
171 
172  if ($response->getForwardURL() === REFERRER) {
173  $response->setForwardURL(elgg_generate_url('settings:account', [
174  'username' => $username,
175  ]));
176  }
177 
178  return $response;
179  });
180 }
181 
195 function _elgg_set_user_language(\Elgg\Hook $hook) {
196 
197  $user = $hook->getUserParam();
198  $request = $hook->getParam('request');
199 
200  if (!$user instanceof ElggUser || !$request instanceof Request) {
201  return null;
202  }
203 
204  $language = $request->getParam('language');
205  if (!isset($language)) {
206  return null;
207  }
208 
209  if ($language === $user->language) {
210  return null;
211  }
212 
213  $user->language = $language;
214 
215  $request->validation()->pass('language', $language, elgg_echo('user:language:success'));
216 }
217 
231 function _elgg_set_user_email(\Elgg\Hook $hook) {
232 
234  if (!$actor instanceof ElggUser) {
235  return null;
236  }
237 
238  $user = $hook->getUserParam();
239  $request = $hook->getParam('request');
240 
241  if (!$user instanceof ElggUser || !$request instanceof Request) {
242  return null;
243  }
244 
245  $email = $request->getParam('email');
246  if (!isset($email)) {
247  return null;
248  }
249 
250  if (strcmp($email, $user->email) === 0) {
251  // no change
252  return null;
253  }
254 
255  try {
256  elgg()->accounts->assertValidEmail($email, true);
257  } catch (RegistrationException $ex) {
258  $request->validation()->fail('email', $email, $ex->getMessage());
259 
260  return false;
261  }
262 
263  if (elgg()->config->security_email_require_password && $user->guid === $actor->guid) {
264  try {
265  // validate password
266  elgg()->accounts->assertCurrentPassword($user, $request->getParam('email_password'));
267  } catch (RegistrationException $e) {
268  $request->validation()->fail('email', $email, elgg_echo('email:save:fail:password'));
269  return false;
270  }
271  }
272 
273  $hook_params = $hook->getParams();
274  $hook_params['email'] = $email;
275 
276  if (elgg_trigger_plugin_hook('change:email', 'user', $hook_params, true)) {
277  $user->email = $email;
278  $request->validation()->pass('email', $email, elgg_echo('email:save:success'));
279  }
280 }
281 
296 function _elgg_set_user_default_access(\Elgg\Hook $hook) {
297 
298  if (!elgg()->config->allow_user_default_access) {
299  return null;
300  }
301 
302  $user = $hook->getUserParam();
303  $request = $hook->getParam('request');
304  /* @var $request \Elgg\Request */
305 
306  $default_access = $request->getParam('default_access');
307  if (!isset($default_access)) {
308  return null;
309  }
310 
311  if ($user->setPrivateSetting('elgg_default_access', $default_access)) {
312  $request->validation()->pass('default_access', $default_access, elgg_echo('user:default_access:success'));
313  } else {
314  $request->validation()->fail('default_access', $default_access, elgg_echo(elgg_echo('user:default_access:failure')));
315  }
316 }
317 
331 function _elgg_user_settings_menu_register($hook, $type, $return, $params) {
333  if (!$user) {
334  return;
335  }
336 
337  if (!elgg_in_context('settings')) {
338  return;
339  }
340 
341  $return[] = \ElggMenuItem::factory([
342  'name' => '1_account',
343  'text' => elgg_echo('usersettings:user:opt:linktext'),
344  'href' => "settings/user/{$user->username}",
345  'section' => 'configure',
346  ]);
347 
348  $return[] = \ElggMenuItem::factory([
349  'name' => '1_plugins',
350  'text' => elgg_echo('usersettings:plugins:opt:linktext'),
351  'href' => '#',
352  'section' => 'configure',
353  ]);
354 
355  $return[] = \ElggMenuItem::factory([
356  'name' => '1_statistics',
357  'text' => elgg_echo('usersettings:statistics:opt:linktext'),
358  'href' => "settings/statistics/{$user->username}",
359  'section' => 'configure',
360  ]);
361 
362  // register plugin user settings menu items
363  $active_plugins = elgg_get_plugins();
364 
365  foreach ($active_plugins as $plugin) {
366  $plugin_id = $plugin->getID();
367  if (!elgg_view_exists("usersettings/$plugin_id/edit") && !elgg_view_exists("plugins/$plugin_id/usersettings")) {
368  continue;
369  }
370 
371  if (elgg_language_key_exists($plugin_id . ':usersettings:title')) {
372  $title = elgg_echo($plugin_id . ':usersettings:title');
373  } else {
374  $title = $plugin->getDisplayName();
375  }
376 
377  $return[] = \ElggMenuItem::factory([
378  'name' => $plugin_id,
379  'text' => $title,
380  'href' => elgg_generate_url('settings:tools', [
381  'username' => $user->username,
382  'plugin_id' => $plugin_id,
383  ]),
384  'parent_name' => '1_plugins',
385  'section' => 'configure',
386  ]);
387  }
388 
389  return $return;
390 }
391 
404  if (empty($value)) {
405  return $value;
406  }
407 
408  if (!elgg_in_context("settings")) {
409  return $value;
410  }
411 
412  $configure = elgg_extract("configure", $value);
413  if (empty($configure)) {
414  return $value;
415  }
416 
417  foreach ($configure as $index => $menu_item) {
418  if (!($menu_item instanceof ElggMenuItem)) {
419  continue;
420  }
421 
422  if ($menu_item->getName() == "1_plugins") {
423  if (!$menu_item->getChildren()) {
424  // no need for this menu item if it has no children
425  unset($value["configure"][$index]);
426  }
427  }
428  }
429 
430  return $value;
431 }
432 
440 
441  elgg_register_plugin_hook_handler('register', 'menu:page', '_elgg_user_settings_menu_register');
442  elgg_register_plugin_hook_handler('prepare', 'menu:page', '_elgg_user_settings_menu_prepare');
443 
444  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_language');
445  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_password');
446  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_default_access');
447  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_name');
448  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_username');
449  elgg_register_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_email');
450 
451  // extend the account settings form
452  elgg_extend_view('forms/usersettings/save', 'core/settings/account/username', 100);
453  elgg_extend_view('forms/usersettings/save', 'core/settings/account/name', 100);
454  elgg_extend_view('forms/usersettings/save', 'core/settings/account/password', 100);
455  elgg_extend_view('forms/usersettings/save', 'core/settings/account/email', 100);
456  elgg_extend_view('forms/usersettings/save', 'core/settings/account/language', 100);
457  elgg_extend_view('forms/usersettings/save', 'core/settings/account/default_access', 100);
458 }
459 
463 return function (\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
464  $events->registerHandler('init', 'system', '_elgg_user_settings_init');
465 };
elgg_language_key_exists($key, $language= 'en')
Check if a given language key exists.
Definition: languages.php:96
_elgg_set_user_email(\Elgg\Hook $hook)
Set a user&#39;s email address Returns null if no change is required or input is not present in the form ...
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:205
$password2
Definition: register.php:17
HTTP response builder interface.
$plugin
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$username
Definition: delete.php:23
$params
Saves global plugin settings.
Definition: save.php:13
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:57
$request
Page handler for autocomplete endpoint.
Definition: livesearch.php:9
_elgg_set_user_password(\Elgg\Hook $hook)
Set a user&#39;s password Returns null if no change is required Returns true or false indicating success ...
Events service.
_elgg_user_settings_menu_register($hook, $type, $return, $params)
Register menu items for the user settings page menu.
$type
Definition: delete.php:21
$email
Definition: register.php:18
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
_elgg_set_user_name(\Elgg\Hook $hook)
Set a user&#39;s display name Returns null if no change is required or input is not present in the form R...
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
static factory($options)
Create an ElggMenuItem from an associative array.
Configuration exception.
const REFERRER
Definition: constants.php:42
if(!$user||!$user->canEdit()) $password
Base class for events and hooks.
_elgg_set_user_default_access(\Elgg\Hook $hook)
Set a user&#39;s default access level Returns null if no change is required or input is not present in th...
$title
Definition: user.php:20
$language
Definition: useradd.php:17
$plugin_id
Definition: save.php:15
_elgg_user_settings_menu_prepare($hook, $type, $value, $params)
Prepares the page menu to strip out empty plugins menu item for user settings.
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
Definition: pagehandler.php:58
_elgg_set_user_username(\Elgg\Hook $hook)
Set a user&#39;s username Returns null if no change is required or input is not present in the form Retur...
_elgg_set_user_language(\Elgg\Hook $hook)
Set a user&#39;s language Returns null if no change is required or input is not present in the form Retur...
$user
Definition: ban.php:7
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:238
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
elgg_get_page_owner_entity()
Gets the owner entity for the current page.
Definition: pageowner.php:65
_elgg_user_settings_init()
Initialize the user settings library.
elgg_extend_view($view, $view_extension, $priority=501)
Extends a view with another view.
Definition: views.php:293
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg_get_plugins($status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:76
$value
Definition: debugging.php:7
$configure
Definition: save.php:23
Request container.
Definition: Request.php:13
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:29
$index
Definition: gallery.php:47
var elgg
Definition: elgglib.js:4
if(!$user instanceof ElggUser) $default_access