/** * Update an array of POSTed users. */ public function update_users($handler_vars) { if (isset($handler_vars['delete'])) { $currentuser = User::identify(); $wsse = Utils::WSSE($handler_vars['nonce'], $handler_vars['timestamp']); if (isset($handler_vars['digest']) && $handler_vars['digest'] != $wsse['digest']) { Session::error(_t('WSSE authentication failed.')); return Session::messages_get(true, 'array'); } foreach ($_POST as $id => $delete) { // skip POST elements which are not user ids if (preg_match('/^p\\d+$/', $id) && $delete) { $id = (int) substr($id, 1); $ids[] = array('id' => $id); } } if (isset($handler_vars['checkbox_ids'])) { $checkbox_ids = $handler_vars['checkbox_ids']; foreach ($checkbox_ids as $id => $delete) { if ($delete) { $ids[] = array('id' => $id); } } } $count = 0; if (!isset($ids)) { Session::notice(_t('No users deleted.')); return Session::messages_get(true, 'array'); } foreach ($ids as $id) { $id = $id['id']; $user = User::get_by_id($id); if ($currentuser != $user) { $assign = intval($handler_vars['reassign']); if ($user->id == $assign) { return; } $posts = Posts::get(array('user_id' => $user->id, 'nolimit' => 1)); if (isset($posts[0])) { if (0 == $assign) { foreach ($posts as $post) { $post->delete(); } } else { Posts::reassign($assign, $posts); } } $user->delete(); } else { $msg_status = _t('You cannot delete yourself.'); } $count++; } if (!isset($msg_status)) { $msg_status = sprintf(_t('Deleted %d users.'), $count); } Session::notice($msg_status); } }
/** * Success method for the delete_users form * @param FormUI $form The delete_users form */ public function do_delete_users(FormUI $form) { $success = true; // Get the user to assign deleted users' posts to $assign = intval($form->reassign->value); if (in_array($assign, $form->deletion_queue->value)) { Session::error(_t('You may not assign posts from deleted users to a user that is being deleted')); return false; } $count = 0; if (count($form->deletion_queue->value) == 0) { Session::notice(_t('No users deleted.')); return false; } foreach ($form->deletion_queue->value as $id) { $user = User::get_by_id($id); $posts = Posts::get(array('user_id' => $user->id, 'nolimit' => 1)); $one_success = $user->delete(); if ($one_success && count($posts)) { if (0 == $assign) { /** @var Post $post */ foreach ($posts as $post) { $post->delete(); } } else { Posts::reassign($assign, $posts); } } $success = $success && $one_success; $count++; } if ($success) { $msg_status = sprintf(_n('Deleted one user.', 'Deleted %s users.', $count), $count); Session::notice($msg_status); return true; } else { $msg_status = _t('There was a problem deleting users.'); Session::error($msg_status); return false; } }