Commit 70f048bb authored by Dries's avatar Dries

- Patch #1704530 by alexpott: convert contact module variables to configuration system.

parent 4156d29b
......@@ -84,10 +84,14 @@ function contact_install() {
}
/**
* Implements hook_uninstall().
* Moves contact setting from variable to config.
*
* @ingroup config_upgrade
*/
function contact_uninstall() {
variable_del('contact_default_status');
variable_del('contact_threshold_limit');
variable_del('contact_threshold_window');
function contact_update_8000() {
update_variables_to_config('contact.settings', array(
'contact_default_status' => 'user_default_enabled',
'contact_threshold_limit' => 'flood.limit',
'contact_threshold_window' => 'flood.interval',
));
}
......@@ -234,7 +234,7 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) {
* Implements hook_user_presave().
*/
function contact_user_presave($account) {
$account->data['contact'] = isset($account->contact) ? $account->contact : variable_get('contact_default_status', 1);
$account->data['contact'] = isset($account->contact) ? $account->contact : config('contact.settings')->get('user_default_enabled');
}
/**
......@@ -254,6 +254,19 @@ function contact_form_user_admin_settings_alter(&$form, &$form_state) {
'#type' => 'checkbox',
'#title' => t('Enable the personal contact form by default for new users.'),
'#description' => t('Changing this setting will not affect existing users.'),
'#default_value' => variable_get('contact_default_status', 1),
'#default_value' => config('contact.settings')->get('user_default_enabled'),
);
// Add submit handler to save contact configuration.
$form['#submit'][] = 'contact_form_user_admin_settings_submit';
}
/**
* Form submission handler for user_admin_settings().
*
* @see contact_form_user_admin_settings_alter()
*/
function contact_form_user_admin_settings_submit($form, &$form_state) {
config('contact.settings')
->set('user_default_enabled', $form_state['values']['contact_default_status'])
->save();
}
......@@ -20,10 +20,11 @@ function contact_site_form($form, &$form_state) {
global $user;
// Check if flood control has been activated for sending e-mails.
$limit = variable_get('contact_threshold_limit', 5);
$window = variable_get('contact_threshold_window', 3600);
if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
$config = config('contact.settings');
$limit = $config->get('flood.limit');
$interval = $config->get('flood.interval');
if (!flood_is_allowed('contact', $limit, $interval) && !user_access('administer contact forms')) {
drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($interval))), 'error');
throw new AccessDeniedHttpException();
}
......@@ -180,7 +181,7 @@ function contact_site_form_submit($form, &$form_state) {
drupal_mail('contact', 'page_autoreply', $from, $language_interface, $values, $to);
}
flood_register_event('contact', variable_get('contact_threshold_window', 3600));
flood_register_event('contact', config('contact.settings')->get('flood.interval'));
watchdog('mail', '%sender-name (@sender-from) sent an e-mail regarding %category.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%category' => $values['category']['category']));
// Jump to home page rather than back to contact page to avoid
......@@ -201,10 +202,11 @@ function contact_personal_form($form, &$form_state, $recipient) {
global $user;
// Check if flood control has been activated for sending e-mails.
$limit = variable_get('contact_threshold_limit', 5);
$window = variable_get('contact_threshold_window', 3600);
if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms') && !user_access('administer users')) {
drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
$config = config('contact.settings');
$limit = $config->get('flood.limit');
$interval = $config->get('flood.interval');
if (!flood_is_allowed('contact', $limit, $interval) && !user_access('administer contact forms') && !user_access('administer users')) {
drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($interval))), 'error');
throw new AccessDeniedHttpException();
}
......@@ -315,7 +317,7 @@ function contact_personal_form_submit($form, &$form_state) {
drupal_mail('contact', 'user_copy', $from, $language_interface, $values, $from);
}
flood_register_event('contact', variable_get('contact_threshold_window', 3600));
flood_register_event('contact', config('contact.settings')->get('flood.interval'));
watchdog('mail', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%recipient-name' => $values['recipient']->name));
// Jump to the contacted user's profile page.
......
......@@ -32,7 +32,7 @@ function setUp() {
$this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users'));
// Create some normal users with their contact forms enabled by default.
variable_set('contact_default_status', TRUE);
config('contact.settings')->set('user_default_enabled', 1)->save();
$this->web_user = $this->drupalCreateUser(array('access user contact forms'));
$this->contact_user = $this->drupalCreateUser();
}
......@@ -123,7 +123,7 @@ function testPersonalContactAccess() {
*/
function testPersonalContactFlood() {
$flood_limit = 3;
variable_set('contact_threshold_limit', $flood_limit);
config('contact.settings')->set('flood.limit', $flood_limit)->save();
// Clear flood table in preparation for flood test and allow other checks to complete.
db_delete('flood')->execute();
......@@ -140,7 +140,7 @@ function testPersonalContactFlood() {
// Submit contact form one over limit.
$this->drupalGet('user/' . $this->contact_user->uid. '/contact');
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(variable_get('contact_threshold_window', 3600)))), 'Normal user denied access to flooded contact form.');
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(config('contact.settings')->get('flood.interval')))), 'Normal user denied access to flooded contact form.');
// Test that the admin user can still access the contact form even though
// the flood limit was reached.
......
......@@ -34,8 +34,10 @@ function testSiteWideContact() {
$this->drupalLogin($admin_user);
$flood_limit = 3;
variable_set('contact_threshold_limit', $flood_limit);
variable_set('contact_threshold_window', 600);
config('contact.settings')
->set('flood.limit', $flood_limit)
->set('flood.interval', 600)
->save();
// Set settings.
$edit = array();
......@@ -158,7 +160,7 @@ function testSiteWideContact() {
// Submit contact form one over limit.
$this->drupalGet('contact');
$this->assertResponse(403, t('Access denied to anonymous user after reaching message treshold.'));
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(600))), t('Message threshold reached.'));
$this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600))), t('Message threshold reached.'));
// Delete created categories.
$this->drupalLogin($admin_user);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment