aboutsummaryrefslogtreecommitdiff
path: root/mod/bulk_user_admin
diff options
context:
space:
mode:
Diffstat (limited to 'mod/bulk_user_admin')
-rw-r--r--mod/bulk_user_admin/actions/bulk_user_admin/delete.php30
-rw-r--r--mod/bulk_user_admin/actions/bulk_user_admin/delete_by_domain.php36
-rw-r--r--mod/bulk_user_admin/languages/en.php9
-rw-r--r--mod/bulk_user_admin/manifest.xml18
-rw-r--r--mod/bulk_user_admin/readme.md11
-rw-r--r--mod/bulk_user_admin/start.php69
-rw-r--r--mod/bulk_user_admin/views/default/admin/user.php98
-rw-r--r--mod/bulk_user_admin/views/default/admin/users/email_domain_stats.php12
-rw-r--r--mod/bulk_user_admin/views/default/bulk_user_admin/css.php26
-rw-r--r--mod/bulk_user_admin/views/default/bulk_user_admin/email_domain_stats.php49
-rw-r--r--mod/bulk_user_admin/views/default/bulk_user_admin/search_by_domain.php16
-rw-r--r--mod/bulk_user_admin/views/default/bulk_user_admin/user.php49
12 files changed, 423 insertions, 0 deletions
diff --git a/mod/bulk_user_admin/actions/bulk_user_admin/delete.php b/mod/bulk_user_admin/actions/bulk_user_admin/delete.php
new file mode 100644
index 000000000..0d7a7e7df
--- /dev/null
+++ b/mod/bulk_user_admin/actions/bulk_user_admin/delete.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Bulk delete users
+ */
+
+$guids = get_input('bulk_user_admin_guids');
+$errors = array();
+
+foreach ($guids as $guid) {
+ $user = get_entity($guid);
+
+ if (!$user instanceof ElggUser) {
+ $errors[] = "$guid is not a user.";
+ continue;
+ }
+
+ if (!$user->delete()) {
+ $errors[] = "Could not delete $user->name ($user->username).";
+ }
+}
+
+if ($errors) {
+ foreach ($errors as $error) {
+ register_error($error);
+ }
+} else {
+ system_message("Users deleted.");
+}
+
+forward(REFERER);
diff --git a/mod/bulk_user_admin/actions/bulk_user_admin/delete_by_domain.php b/mod/bulk_user_admin/actions/bulk_user_admin/delete_by_domain.php
new file mode 100644
index 000000000..c51076936
--- /dev/null
+++ b/mod/bulk_user_admin/actions/bulk_user_admin/delete_by_domain.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Bulk delete users by email
+ */
+
+$domain = get_input('domain');
+
+$errors = array();
+
+$options = array(
+ 'limit' => 50,
+ 'offset' => 0,
+);
+
+$users = bulk_user_admin_get_users_by_email_domain($domain, $options);
+
+while ($users) {
+ foreach ($users as $user) {
+ if (!$user->delete()) {
+ $errors[] = "Could not delete $user->name ($user->username).";
+ }
+ }
+
+ $options['offset'] = $options['offset'] + $options['limit'];
+ $users = bulk_user_admin_get_users_by_email_domain($domain, $options);
+}
+
+if ($errors) {
+ foreach ($errors as $error) {
+ register_error($error);
+ }
+} else {
+ system_message("Users deleted.");
+}
+
+forward(REFERER);
diff --git a/mod/bulk_user_admin/languages/en.php b/mod/bulk_user_admin/languages/en.php
new file mode 100644
index 000000000..d29b8398d
--- /dev/null
+++ b/mod/bulk_user_admin/languages/en.php
@@ -0,0 +1,9 @@
+<?php
+
+$english = array(
+ 'admin:users:email_domain_stats' => "User Email Domain Stats",
+);
+
+ add_translation("en",$english);
+
+?> \ No newline at end of file
diff --git a/mod/bulk_user_admin/manifest.xml b/mod/bulk_user_admin/manifest.xml
new file mode 100644
index 000000000..1d0679acb
--- /dev/null
+++ b/mod/bulk_user_admin/manifest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<plugin_manifest xmlns="http://www.elgg.org/plugin_manifest/1.8">
+ <name>Bulk User Admin</name>
+ <author>Brett Profitt</author>
+ <version>1.8</version>
+ <description>Delete users in bulk</description>
+ <website>http://www.elgg.org/</website>
+ <copyright>(C) Brett Profitt 2011</copyright>
+ <license>GNU Public License version 2</license>
+
+ <!-- All plugins must require either elgg_version or elgg_release. -->
+ <requires>
+ <type>elgg_release</type>
+ <version>1.8</version>
+ </requires>
+
+ <category>admin</category>
+</plugin_manifest> \ No newline at end of file
diff --git a/mod/bulk_user_admin/readme.md b/mod/bulk_user_admin/readme.md
new file mode 100644
index 000000000..39f41cd8d
--- /dev/null
+++ b/mod/bulk_user_admin/readme.md
@@ -0,0 +1,11 @@
+Bulk user Admin
+===============
+
+(C) 2011 Brett Profitt
+GPL 2
+
+About
+=====
+Adds ability to bulk delete users from the admin section.
+
+Adds menu to display users by domain and the most common domains registered.
diff --git a/mod/bulk_user_admin/start.php b/mod/bulk_user_admin/start.php
new file mode 100644
index 000000000..2af97b23e
--- /dev/null
+++ b/mod/bulk_user_admin/start.php
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Allow bulk delete operations
+ */
+
+/**
+ * Init
+ */
+function bulk_user_admin_init() {
+ elgg_register_event_handler('pagesetup', 'system', 'bulk_user_admin_admin_page_setup');
+
+ elgg_extend_view('admin/user_opt/search', 'bulk_user_admin/search_by_domain');
+ elgg_extend_view('css/admin', 'bulk_user_admin/css');
+
+ elgg_register_action('bulk_user_admin/delete', dirname(__FILE__) . '/actions/bulk_user_admin/delete.php', 'admin');
+ elgg_register_action('bulk_user_admin/delete_by_domain', dirname(__FILE__) . '/actions/bulk_user_admin/delete_by_domain.php', 'admin');
+
+}
+
+function bulk_user_admin_get_users_by_email_domain($domain, $options = array()) {
+ $domain = sanitise_string($domain);
+ $db_prefix = elgg_get_config('dbprefix');
+
+ $where = "ue.email LIKE '%@$domain'";
+ if (!isset($options['wheres'])) {
+ $options['wheres'] = array($where);
+ } else {
+ if (!is_array($options['wheres'])) {
+ $options['wheres'] = array($options['wheres']);
+ }
+ $options['wheres'][] = $where;
+ }
+
+ $join = "JOIN {$db_prefix}users_entity ue on e.guid = ue.guid";
+ if (!isset($options['joins'])) {
+ $options['joins'] = array($join);
+ } else {
+ if (!is_array($options['joins'])) {
+ $options['joins'] = array($options['joins']);
+ }
+ $options['joins'][] = $join;
+ }
+
+ $options['type'] = 'user';
+
+ return elgg_get_entities($options);
+}
+
+/**
+ * Sets up admin menu. Triggered on pagesetup.
+ */
+function bulk_user_admin_admin_page_setup() {
+ if (elgg_get_context() == 'admin' && elgg_is_admin_logged_in()) {
+ elgg_register_admin_menu_item('administer', 'email_domain_stats', 'users');
+ }
+}
+
+function bulk_user_admin_get_email_domain_stats() {
+ $db_prefix = elgg_get_config('dbprefix');
+ $q = "SELECT email, substring_index(email, '@', -1) as domain, count(*) as count
+ FROM {$db_prefix}users_entity ue
+ JOIN {$db_prefix}entities e ON ue.guid = e.guid
+ WHERE e.enabled = 'yes'
+ group by domain order by count desc, domain asc;";
+
+ return get_data($q);
+}
+
+elgg_register_event_handler('init', 'system', 'bulk_user_admin_init'); \ No newline at end of file
diff --git a/mod/bulk_user_admin/views/default/admin/user.php b/mod/bulk_user_admin/views/default/admin/user.php
new file mode 100644
index 000000000..c63f091ec
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/admin/user.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * Display a list of users to delete in bulk.
+ *
+ * Also used to show the search by domain results
+ */
+
+// Are we performing a search
+$limit = get_input('limit', 10);
+$offset = get_input('offset', 0);
+$domain = get_input('domain');
+
+$context = elgg_get_context();
+
+if (!$domain) {
+ $title = elgg_echo('admin:user');
+} else {
+ $title = "Users in the domain $domain";
+}
+
+elgg_set_context('search');
+
+$options = array(
+ 'type' => 'user',
+ 'limit' => $limit,
+ 'offset' => $offset,
+ 'full_view' => false
+);
+
+if ($domain) {
+ $users = bulk_user_admin_get_users_by_email_domain($domain, $options);
+ $options['count'] = true;
+ $users_count = bulk_user_admin_get_users_by_email_domain($domain, $options);
+} else {
+ $users = elgg_get_entities($options);
+ $options['count'] = true;
+ $users_count = elgg_get_entities($options);
+}
+
+$pagination = elgg_view('navigation/pagination', array(
+ 'baseurl' => current_page_url(),
+ 'offset' => $offset,
+ 'count' => $users_count
+));
+
+$form_body = '';
+foreach ($users as $user) {
+ $form_body .= elgg_view('bulk_user_admin/user', array('entity' => $user));
+}
+
+$delete_button = elgg_view('input/submit', array(
+ 'value' => 'Delete checked',
+));
+
+$form_body .= $delete_button;
+
+$site = elgg_get_config('site');
+
+$checked_form = elgg_view('input/form', array(
+ 'action' => $site->url . 'action/bulk_user_admin/delete',
+ 'body' => $form_body
+));
+
+
+$domain_form = '';
+
+if ($domain) {
+ $delete_button = "<br /><br />" . elgg_view('input/submit', array(
+ 'value' => 'Delete all in domain',
+ ));
+
+ $hidden = elgg_view('input/hidden', array(
+ 'name' => 'domain',
+ 'value' => $domain
+ ));
+
+ $form_body = $delete_button . $hidden;
+
+ $domain_form = elgg_view('input/form', array(
+ 'action' => $site->url . 'action/bulk_user_admin/delete_by_domain',
+ 'body' => $form_body
+ ));
+
+}
+
+$summary = "<div>$users_count user(s) found</div>";
+
+if ($domain) {
+ $summary .= '<br />';
+ $summary .= elgg_view('output/url', array(
+ 'href' => elgg_http_remove_url_query_element(current_page_url(), 'domain'),
+ 'text' => 'All users'
+ ));
+}
+
+elgg_set_context('admin');
+
+echo $title . $summary . $pagination . $checked_form . $domain_form . $pagination; \ No newline at end of file
diff --git a/mod/bulk_user_admin/views/default/admin/users/email_domain_stats.php b/mod/bulk_user_admin/views/default/admin/users/email_domain_stats.php
new file mode 100644
index 000000000..0caa50331
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/admin/users/email_domain_stats.php
@@ -0,0 +1,12 @@
+<?php
+/**
+ * Shows a list of email domains on the site and how many users have are part of the domain.
+ */
+
+$title = 'Email domain stats';
+
+$domain_list = elgg_view('bulk_user_admin/email_domain_stats', array(
+ 'domains' => bulk_user_admin_get_email_domain_stats()
+));
+
+echo $title . $domain_list; \ No newline at end of file
diff --git a/mod/bulk_user_admin/views/default/bulk_user_admin/css.php b/mod/bulk_user_admin/views/default/bulk_user_admin/css.php
new file mode 100644
index 000000000..3254c1700
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/bulk_user_admin/css.php
@@ -0,0 +1,26 @@
+
+table.bulk_user_admin_email_domains {
+ width: 300px;
+}
+
+table.bulk_user_admin_email_domains th {
+ text-align: center;
+ font-weight: bold;
+ font-size: 125%;
+}
+
+table.bulk_user_admin_email_domains td {
+ padding: 3px;
+}
+
+table.bulk_user_admin_email_domains td.center {
+ text-align: center;
+}
+
+table.bulk_user_admin_email_domains tr.odd {
+ background-color: #fff;
+}
+
+table.bulk_user_admin_email_domains tr.even {
+ background-color: #dedede;
+} \ No newline at end of file
diff --git a/mod/bulk_user_admin/views/default/bulk_user_admin/email_domain_stats.php b/mod/bulk_user_admin/views/default/bulk_user_admin/email_domain_stats.php
new file mode 100644
index 000000000..3936eb0ea
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/bulk_user_admin/email_domain_stats.php
@@ -0,0 +1,49 @@
+<?php
+
+$domains = $vars['domains'];
+
+?>
+<table class="bulk_user_admin_email_domains">
+ <tr>
+ <th>Domain</th>
+ <th>Registered users</th>
+ </tr>
+<?php
+
+
+$i = 0;
+foreach ($domains as $domain_info) {
+ if (!$domain_info->domain) {
+ continue;
+ }
+
+ $domain = elgg_view('output/url', array(
+ 'text' => $domain_info->domain,
+ 'href' => $domain_info->domain
+ ));
+
+ $url = elgg_http_add_url_query_elements($vars['url'] . 'admin/user', array('domain' => $domain_info->domain));
+
+ // can't use $_GET variables in admin
+ // otherwise admin_page_handler() tries to call the view: view/name?variable=value
+ // which clearly doesn't work
+ // so we'll pass the domain via post
+ $users = '<form id="domain:' . $domain_info->domain . '" action="' . elgg_get_site_url() . 'admin/user" method="post">';
+ $users .= elgg_view('input/hidden', array('name' => 'domain', 'value' => $domain_info->domain));
+ $users .= '</form>';
+ $users .= '<a href="javascript:document.forms[\'domain:' . $domain_info->domain . '\'].submit();">' . $domain_info->count . '</a>';
+
+ $class = ($i % 2) ? 'odd' : 'even';
+
+ echo <<<___HTML
+ <tr class="$class">
+ <td>$domain</td>
+ <td class="center">$users</td>
+ </tr>
+___HTML;
+
+ $i++;
+}
+
+?>
+</table> \ No newline at end of file
diff --git a/mod/bulk_user_admin/views/default/bulk_user_admin/search_by_domain.php b/mod/bulk_user_admin/views/default/bulk_user_admin/search_by_domain.php
new file mode 100644
index 000000000..5c65a3903
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/bulk_user_admin/search_by_domain.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * Search by email domain
+ */
+?>
+<div id="search-box">
+ <form action="<?php echo current_page_url() ?>" method="get">
+ <b>Search by email domain</b>
+ <?php
+
+ echo elgg_view('input/text', array('name' => 'domain'));
+
+ ?>
+ <input type="submit" value="Search by domain" />
+ </form>
+</div>
diff --git a/mod/bulk_user_admin/views/default/bulk_user_admin/user.php b/mod/bulk_user_admin/views/default/bulk_user_admin/user.php
new file mode 100644
index 000000000..d05d92fd3
--- /dev/null
+++ b/mod/bulk_user_admin/views/default/bulk_user_admin/user.php
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Show a user for bulk actions. Includes a checkbox on the left.
+ */
+if($vars['entity'] instanceof ElggUser){
+$icon = elgg_view_entity_icon($vars['entity'], 'small');
+
+$banned = $vars['entity']->isBanned();
+$user = $vars['entity'];
+
+$checkbox = "<input type=\"checkbox\" name=\"bulk_user_admin_guids[]\" value=\"$user->guid\">";
+$first_login = elgg_view_friendly_time($user->time_created);
+$last_login = elgg_view_friendly_time($user->last_login);
+$last_action = elgg_view_friendly_time($user->last_action);
+$objects = elgg_get_entities(array(
+ 'owner_guid' => $user->guid,
+ 'count' => true
+));
+
+$db_prefix = elgg_get_config('dbprefix');
+
+$q = "SELECT COUNT(id) as count FROM {$db_prefix}annotations WHERE owner_guid = $user->guid";
+$data = get_data($q);
+$annotations = (int) $data[0]->count;
+
+$q = "SELECT COUNT(id) as count FROM {$db_prefix}metadata WHERE owner_guid = $user->guid";
+$data = get_data($q);
+$metadata = (int) $data[0]->count;
+
+// the CSS for classless <label> is really, really annoying.
+$info = <<<___HTML
+<label style="font-size: inherit; font-weight: inherit; color: inherit;">
+<p>$checkbox $user->name | $user->username | $user->email | $user->guid </p>
+<p>Last login: $last_login | First login: $first_login | Last action: $last_action</p>
+<p>Objects: $objects | Annotations: $annotations | Metadata: $metadata</p>
+___HTML;
+
+if ($banned) {
+ $info .= '<div id="profile_banned">';
+ $info .= elgg_echo('profile:banned');
+ $info .= '<br />';
+ $info .= $user->ban_reason;
+ $info .= '</div>';
+}
+
+$info .= '</label>';
+
+echo elgg_view('page/components/image_block', array('image' => $icon, 'body' => $info));
+} \ No newline at end of file