Added support for setting the duration of user bans and sending email notifications of bans

(DBOINCP-87)
This commit is contained in:
Tristan Olive 2015-05-12 10:33:48 -04:00
parent d670551ab3
commit a6e103b019
4 changed files with 129 additions and 6 deletions

View File

@ -77,6 +77,14 @@ function boincuser_menu() {
'type' => MENU_CALLBACK,
'weight' => 5
);
$items['moderate/user/%/ban'] = array(
'title' => bts('Ban user'),
'description' => 'Ban a user from using community features',
'page callback' => 'drupal_get_form',
'page arguments' => array('boincuser_moderate_user_ban_form', 2),
'access arguments' => array('assign community member role'),
'type' => MENU_CALLBACK,
);
$items['join'] = array(
'title' => '',
'description' => '',
@ -1322,6 +1330,66 @@ function boincuser_moderate_profile_reject($uid, $reason = '') {
drupal_goto();
}
/**
* Ban a user and send a notification of the reason.
*/
function boincuser_moderate_user_ban($uid, $reason = '', $duration = '') {
if (user_access('assign community member role')
OR user_access('assign all roles')) {
$account = user_load($uid);
if ($account->uid) {
module_load_include('inc', 'rules', 'modules/system.rules');
if ($duration === '') {
$duration = variable_get('boinc_penalty_period', 7*24*60*60);
}
$penalty_expiration = ($duration > 0) ? time() + $duration : 4294967295;
$boincuser_record = array(
'uid' => $uid,
'penalty_expiration' => $penalty_expiration,
);
drupal_write_record('boincuser', $boincuser_record, 'uid');
$community_role = array_search('community member', user_roles(true));
if (isset($account->roles[$community_role])) {
unset($account->roles[$community_role]);
user_save($account, array('roles' => $account->roles));
}
global $user;
global $base_url;
global $base_path;
$site_name = variable_get('site_name', bts('project'));
$site_url = $base_url . $base_path;
$moderator = user_load($user->uid);
$settings = array(
'from' => '',
'subject' => "User moderation at {$site_name}",
'message' => ''
. "{$account->boincuser_name},\n"
. "\n"
. "You have been banned from using community features at"
. " {$site_name} for the following reason: \n"
. "\n"
. "{$reason}\n"
. "\n"
. "The duration of this ban can be found on your account page: \n"
. "\n"
. "{$site_url}account \n"
. "\n"
. "If you feel that this decision was made in error, please send a"
. " private message to {$moderator->boincuser_name}. \n"
. "\n"
. "Thanks, \n"
. "\n"
. "{$site_name} support team",
);
rules_action_mail_to_user($account, $settings);
drupal_set_message('This user has been banned.');
}
}
drupal_goto();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* User data access support functions

View File

@ -513,6 +513,59 @@ function boincuser_moderate_profile_reject_form_submit($form, &$form_state) {
}
/**
* The definition of the ban user form.
*/
function boincuser_moderate_user_ban_form(&$form_state, $uid) {
$form_state['storage']['ban_user_uid'] = $uid;
$form['reason'] = array(
'#title' => bts('Reason for banning this user'),
'#type' => 'textarea',
'#description' => bts('This reason will be included in an email to the user. Please write a brief explanation of why the user is being banned.'),
'#default_value' => '',
);
$form['duration'] = array(
'#title' => bts('Duration of the ban'),
'#type' => 'textfield',
'#description' => bts('The number of days until the ban expires. Set to 0 to ban permanently.'),
'#default_value' => '',
);
// Form control
$form['form control tabs prefix'] = array(
'#value' => '<ul class="form-control tab-list">',
'#weight' => 1001,
);
$form['submit'] = array(
'#prefix' => '<li class="first tab">',
'#type' => 'submit',
'#value' => bts('Submit'),
'#suffix' => '</li>',
'#weight' => 1002,
);
$form['form control tabs'] = array(
'#value' => '<li class="tab">' . l(bts('Cancel'), "account/{$uid}") . '</li>',
'#weight' => 1003,
);
$form['form control tabs suffix'] = array(
'#value' => '</ul>',
'#weight' => 1004,
);
return $form;
}
/**
* The ban user submit handler
*/
function boincuser_moderate_user_ban_form_submit($form, &$form_state) {
$uid = $form_state['storage']['ban_user_uid'];
$reason = $form_state['values']['reason'];
$duration = $form_state['values']['duration'];
if ($duration) $duration = $duration * 24*60*60;
boincuser_moderate_user_ban($uid, $reason, $duration);
}
/**
* Hack to fix submission of the flag friend unfriend form
*/

View File

@ -58,6 +58,10 @@ function boincuser_check_credit_requirements() {
// Set user roles based on current penalty status and total credit
if ($account->boincuser_penalty_expiration > time()) {
drupal_set_message(bts(
'You are banned from community participation until @date',
array('@date' => format_date($account->boincuser_penalty_expiration))
), 'warning', FALSE);
if (isset($account->roles[$community_role])) {
// Remove from the community role, if not already
unset($account->roles[$community_role]);
@ -77,11 +81,9 @@ function boincuser_check_credit_requirements() {
}
}
else {
drupal_set_message(t('You must earn @more_credit to be able to post
comments on this site.', array('@more_credit' => format_plural(
$min_credit_to_post - $account->boincuser_total_credit,
'1 more credit', '@count more credits'
))
drupal_set_message(bts(
'You must earn @count more credits to be able to post comments on this site.',
array('@count' => $min_credit_to_post - $account->boincuser_total_credit)
), 'warning', FALSE);
if (isset($account->roles[$unrestricted_role])) {
// Either the threshold has been raised or credits have been revoked;

View File

@ -104,7 +104,7 @@ if ($user->uid AND ($user->uid != $account->uid)) {
if (array_search('community member', $account->roles)) {
$user_links[] = array(
'title' => bts('Ban user'),
'href' => "user_control/{$account->uid}/ban"
'href' => "moderate/user/{$account->uid}/ban"
);
}
else {