Fixing (un)blocking accounts in /admin/users.

When I look at the instructions in sql_conventions about setting bit
flags I don't understand how this could have worked before. Changed the
behavior for (un)blocking so that it should work now.
This commit is contained in:
Klaus Weidenbach 2015-03-08 19:02:48 +01:00
parent 23ea974615
commit c6d45858bb
2 changed files with 87 additions and 62 deletions

View File

@ -197,7 +197,7 @@ function admin_page_summary(&$a) {
); );
if ($r) { if ($r) {
$accounts['total'] = array('label' => t('# Accounts'), 'val' => $r[0]['total']); $accounts['total'] = array('label' => t('# Accounts'), 'val' => $r[0]['total']);
//@todo $accounts['blocked'] = array('label' => t('# blocked accounts'), 'val' => $r[0]['blocked']); $accounts['blocked'] = array('label' => t('# blocked accounts'), 'val' => $r[0]['blocked']);
$accounts['expired'] = array('label' => t('# expired accounts'), 'val' => $r[0]['expired']); $accounts['expired'] = array('label' => t('# expired accounts'), 'val' => $r[0]['expired']);
$accounts['expiring'] = array('label' => t('# expiring accounts'), 'val' => $r[0]['expiring']); $accounts['expiring'] = array('label' => t('# expiring accounts'), 'val' => $r[0]['expiring']);
} }
@ -639,48 +639,66 @@ function admin_page_queue($a) {
} }
/** /**
* Users admin page * @brief Handle POST actions on users admin page.
*
* This function is called when on the admin user/account page the form was
* submitted to handle multiple operations at once. If one of the icons next
* to an entry are pressed the function admin_page_users() will handle this.
* *
* @param App $a * @param App $a
*/ */
function admin_page_users_post(&$a){ function admin_page_users_post($a) {
$pending = ( x($_POST, 'pending') ? $_POST['pending'] : Array() ); $pending = ( x($_POST, 'pending') ? $_POST['pending'] : array() );
$users = ( x($_POST, 'user') ? $_POST['user'] : Array() ); $users = ( x($_POST, 'user') ? $_POST['user'] : array() );
$blocked = ( x($_POST, 'blocked') ? $_POST['blocked'] : array() );
check_form_security_token_redirectOnErr('/admin/users', 'admin_users'); check_form_security_token_redirectOnErr('/admin/users', 'admin_users');
if (x($_POST,'page_users_block')){ // change to switch structure?
foreach($users as $uid){ // account block/unblock button was submitted
q("UPDATE account SET account_flags = (account_flags & %d) where account_id = %d", if (x($_POST, 'page_users_block')) {
for ($i = 0; $i < count($users); $i++) {
// if account is blocked remove blocked bit-flag, otherwise add blocked bit-flag
$op = ($blocked[$i]) ? '& ~' : '| ';
q("UPDATE account SET account_flags = (account_flags $op%d) WHERE account_id = %d",
intval(ACCOUNT_BLOCKED), intval(ACCOUNT_BLOCKED),
intval( $uid ) intval($users[$i])
); );
} }
notice( sprintf( tt("%s user blocked/unblocked", "%s users blocked/unblocked", count($users)), count($users)) ); notice( sprintf( tt("%s user blocked/unblocked", "%s users blocked/unblocked", count($users)), count($users)) );
} }
if (x($_POST,'page_users_delete')){ // account delete button was submitted
require_once("include/Contact.php"); if (x($_POST, 'page_users_delete')) {
foreach($users as $uid){ require_once('include/Contact.php');
account_remove($uid,true,false); foreach ($users as $uid){
account_remove($uid, true, false);
} }
notice( sprintf( tt("%s user deleted", "%s users deleted", count($users)), count($users)) ); notice( sprintf( tt("%s user deleted", "%s users deleted", count($users)), count($users)) );
} }
// registration approved button was submitted
if (x($_POST,'page_users_approve')){ if (x($_POST, 'page_users_approve')) {
foreach($pending as $hash){ foreach ($pending as $hash) {
user_allow($hash); user_allow($hash);
} }
} }
if (x($_POST,'page_users_deny')){ // registration deny button was submitted
foreach($pending as $hash){ if (x($_POST, 'page_users_deny')) {
foreach ($pending as $hash) {
user_deny($hash); user_deny($hash);
} }
} }
goaway($a->get_baseurl(true) . '/admin/users' ); goaway($a->get_baseurl(true) . '/admin/users' );
} }
/** /**
* @param App $a * @brief Generate users admin page and handle single item operations.
*
* This function generates the users/account admin page and handles the actions
* if an icon next to an entry was clicked. If several items were selected and
* the form was submitted it is handled by the function admin_page_users_post().
*
* @param App &$a
* @return string * @return string
*/ */
function admin_page_users(&$a){ function admin_page_users(&$a){
@ -695,25 +713,34 @@ function admin_page_users(&$a){
goaway($a->get_baseurl(true) . '/admin/users' ); goaway($a->get_baseurl(true) . '/admin/users' );
} }
switch(argv(2)){
case "delete":{
check_form_security_token_redirectOnErr('/admin/users', 'admin_users', 't'); check_form_security_token_redirectOnErr('/admin/users', 'admin_users', 't');
switch (argv(2)){
case 'delete':
// delete user // delete user
require_once("include/Contact.php"); require_once('include/Contact.php');
account_remove($uid,true,false); account_remove($uid,true,false);
notice( sprintf(t("User '%s' deleted"), $account[0]['account_email']) . EOL); notice( sprintf(t("User '%s' deleted"), $account[0]['account_email']) . EOL);
}; break; break;
case "block":{ case 'block':
check_form_security_token_redirectOnErr('/admin/users', 'admin_users', 't'); q("UPDATE account SET account_flags = ( account_flags | %d ) WHERE account_id = %d",
q("UPDATE account SET account_flags = ( account_flags & ~%d ) where account_id = %d",
intval(ACCOUNT_BLOCKED), intval(ACCOUNT_BLOCKED),
intval( $uid ) intval($uid)
); );
notice( sprintf( (($account[0]['account_flags'] & ACCOUNT_BLOCKED) ? t("User '%s' unblocked"):t("User '%s' blocked")) , $account[0]['account_email']) . EOL); notice( sprintf( t("User '%s' blocked") , $account[0]['account_email']) . EOL);
}; break; break;
case 'unblock':
q("UPDATE account SET account_flags = ( account_flags & ~%d ) WHERE account_id = %d",
intval(ACCOUNT_BLOCKED),
intval($uid)
);
notice( sprintf( t("User '%s' unblocked"), $account[0]['account_email']) . EOL);
break;
} }
goaway($a->get_baseurl(true) . '/admin/users' ); goaway($a->get_baseurl(true) . '/admin/users' );
} }
@ -725,7 +752,7 @@ function admin_page_users(&$a){
/* get users */ /* get users */
$total = q("SELECT count(*) as total FROM account"); $total = q("SELECT count(*) as total FROM account");
if(count($total)) { if (count($total)) {
$a->set_pager_total($total[0]['total']); $a->set_pager_total($total[0]['total']);
$a->set_pager_itemspage(100); $a->set_pager_itemspage(100);
} }
@ -772,7 +799,7 @@ function admin_page_users(&$a){
// $users = array_map("_setup_users", $users); // $users = array_map("_setup_users", $users);
$t = get_markup_template("admin_users.tpl"); $t = get_markup_template('admin_users.tpl');
$o = replace_macros($t, array( $o = replace_macros($t, array(
// strings // // strings //
'$title' => t('Administration'), '$title' => t('Administration'),
@ -813,7 +840,7 @@ function admin_page_users(&$a){
* *
* @param App $a * @param App $a
*/ */
function admin_page_channels_post(&$a){ function admin_page_channels_post(&$a) {
$channels = ( x($_POST, 'channel') ? $_POST['channel'] : Array() ); $channels = ( x($_POST, 'channel') ? $_POST['channel'] : Array() );
check_form_security_token_redirectOnErr('/admin/channels', 'admin_channels'); check_form_security_token_redirectOnErr('/admin/channels', 'admin_channels');

View File

@ -10,15 +10,15 @@
return false; return false;
} }
</script> </script>
<div class = "generic-content-wrapper-styled" id='adminpage'> <div class="generic-content-wrapper-styled" id="adminpage">
<h1>{{$title}} - {{$page}}</h1> <h1>{{$title}} - {{$page}}</h1>
<form action="{{$baseurl}}/admin/users" method="post"> <form action="{{$baseurl}}/admin/users" method="post">
<input type='hidden' name='form_security_token' value='{{$form_security_token}}'> <input type="hidden" name="form_security_token" value="{{$form_security_token}}">
<h3>{{$h_pending}}</h3> <h3>{{$h_pending}}</h3>
{{if $pending}} {{if $pending}}
<table id='pending'> <table id="pending">
<thead> <thead>
<tr> <tr>
{{foreach $th_pending as $th}}<th>{{$th}}</th>{{/foreach}} {{foreach $th_pending as $th}}<th>{{$th}}</th>{{/foreach}}
@ -31,27 +31,25 @@
<tr> <tr>
<td class="created">{{$u.account_created}}</td> <td class="created">{{$u.account_created}}</td>
<td class="email">{{$u.account_email}}</td> <td class="email">{{$u.account_email}}</td>
<td class="checkbox"><input type="checkbox" class="pending_ckbx" id="id_pending_{{$u.hash}}" name="pending[]" value="{{$u.hash}}" /></td> <td class="checkbox"><input type="checkbox" class="pending_ckbx" id="id_pending_{{$u.hash}}" name="pending[]" value="{{$u.hash}}"></td>
<td class="tools"> <td class="tools">
<a href="{{$baseurl}}/regmod/allow/{{$u.hash}}" title='{{$approve}}'><i class='icon-thumbs-up-alt admin-icons'></i></a> <a href="{{$baseurl}}/regmod/allow/{{$u.hash}}" title="{{$approve}}"><i class="icon-thumbs-up-alt admin-icons"></i></a>
<a href="{{$baseurl}}/regmod/deny/{{$u.hash}}" title='{{$deny}}'><i class='icon-thumbs-down-alt admin-icons'></i></a> <a href="{{$baseurl}}/regmod/deny/{{$u.hash}}" title="{{$deny}}"><i class="icon-thumbs-down-alt admin-icons"></i></a>
</td> </td>
</tr> </tr>
{{/foreach}} {{/foreach}}
</tbody> </tbody>
</table> </table>
<div class='selectall'><a href='#' onclick="return selectall('pending_ckbx');">{{$select_all}}</a></div> <div class="selectall"><a href="#" onclick="return selectall('pending_ckbx');">{{$select_all}}</a></div>
<div class="submit"><input type="submit" name="page_users_deny" value="{{$deny}}"/> <input type="submit" name="page_users_approve" value="{{$approve}}" /></div> <div class="submit"><input type="submit" name="page_users_deny" value="{{$deny}}"> <input type="submit" name="page_users_approve" value="{{$approve}}"></div>
{{else}} {{else}}
<p>{{$no_pending}}</p> <p>{{$no_pending}}</p>
{{/if}} {{/if}}
<h3>{{$h_users}}</h3> <h3>{{$h_users}}</h3>
{{if $users}} {{if $users}}
<table id='users'> <table id="users">
<thead> <thead>
<tr> <tr>
{{foreach $th_users as $th}}<th>{{$th}}</th>{{/foreach}} {{foreach $th_users as $th}}<th>{{$th}}</th>{{/foreach}}
@ -62,28 +60,28 @@
<tbody> <tbody>
{{foreach $users as $u}} {{foreach $users as $u}}
<tr> <tr>
<td class='account_id'>{{$u.account_id}}</td> <td class="account_id">{{$u.account_id}}</td>
<td class='email'>{{if $u.blocked}} <td class="email">{{if $u.blocked}}
<i>{{$u.account_email}}</i> <i>{{$u.account_email}}</i>
{{else}} {{else}}
<strong>{{$u.account_email}}</strong> <strong>{{$u.account_email}}</strong>
{{/if}}</td> {{/if}}</td>
<td class='channels'>{{$u.channels}}</td> <td class="channels">{{$u.channels}}</td>
<td class='register_date'>{{$u.account_created}}</td> <td class="register_date">{{$u.account_created}}</td>
<td class='login_date'>{{$u.account_lastlog}}</td> <td class="login_date">{{$u.account_lastlog}}</td>
<td class='account_expires'>{{$u.account_expires}}</td> <td class="account_expires">{{$u.account_expires}}</td>
<td class='service_class'>{{$u.account_service_class}}</td> <td class="service_class">{{$u.account_service_class}}</td>
<td class="checkbox"><input type="checkbox" class="users_ckbx" id="id_user_{{$u.account_id}}" name="user[]" value="{{$u.account_id}}"/></td> <td class="checkbox"><input type="checkbox" class="users_ckbx" id="id_user_{{$u.account_id}}" name="user[]" value="{{$u.account_id}}"><input type="hidden" name="blocked[]" value="{{$u.blocked}}"></td>
<td class="tools"> <td class="tools">
<a href="{{$baseurl}}/admin/users/block/{{$u.account_id}}?t={{$form_security_token}}" title='{{if ($u.blocked)}}{{$unblock}}{{else}}{{$block}}{{/if}}'><i class='icon-ban-circle admin-icons {{if ($u.blocked)}}dim{{/if}}'></i></a> <a href="{{$baseurl}}/admin/users/{{if ($u.blocked)}}un{{/if}}block/{{$u.account_id}}?t={{$form_security_token}}" title='{{if ($u.blocked)}}{{$unblock}}{{else}}{{$block}}{{/if}}'><i class="icon-ban-circle admin-icons{{if ($u.blocked)}} dim{{/if}}"></i></a>
<a href="{{$baseurl}}/admin/users/delete/{{$u.account_id}}?t={{$form_security_token}}" title='{{$delete}}' onclick="return confirm_delete('{{$u.name}}')"><i class='icon-trash admin-icons'></i></a> <a href="{{$baseurl}}/admin/users/delete/{{$u.account_id}}?t={{$form_security_token}}" title='{{$delete}}' onclick="return confirm_delete('{{$u.name}}')"><i class="icon-trash admin-icons"></i></a>
</td> </td>
</tr> </tr>
{{/foreach}} {{/foreach}}
</tbody> </tbody>
</table> </table>
<div class='selectall'><a href='#' onclick="return selectall('users_ckbx');">{{$select_all}}</a></div> <div class="selectall"><a href="#" onclick="return selectall('users_ckbx');">{{$select_all}}</a></div>
<div class="submit"><input type="submit" name="page_users_block" value="{{$block}}/{{$unblock}}" /> <input type="submit" name="page_users_delete" value="{{$delete}}" onclick="return confirm_delete_multi()" /></div> <div class="submit"><input type="submit" name="page_users_block" value="{{$block}}/{{$unblock}}"> <input type="submit" name="page_users_delete" value="{{$delete}}" onclick="return confirm_delete_multi()"></div>
{{else}} {{else}}
NO USERS?!? NO USERS?!?
{{/if}} {{/if}}