From 95c6ce268f486ceca7628b378ee17414a7708e0e Mon Sep 17 00:00:00 2001 From: stfwi Date: Tue, 5 Jul 2011 22:23:29 +0100 Subject: When changing user roles in the config form, the group file is now directly updated. The cron file update was removed as it is now obsolete. --- HTPasswdSync.module | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/HTPasswdSync.module b/HTPasswdSync.module index 36861bc..4c65b21 100644 --- a/HTPasswdSync.module +++ b/HTPasswdSync.module @@ -232,6 +232,24 @@ function _htpasswdsync_updategroup() { } +/** + * Removes a group from a file given either by RID or by role name. + * @param mixed $role + */ +function _htpasswdsync_remove_role($role) { + if(is_numeric($role)) { + $role = reset(db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $role))->fetchCol()); + } + $role = _htpasswdsync_sanatize_name($role); + $file = _htpasswdsync_grpfilename(); + $groups = _htpasswdsync_read_htfile($file); + if(isset($groups[$role])) { + unset($groups[$role]); + _htpasswdsync_write_htfile($groups, $file); + } +} + + /** * Generate the htpasswd content from the database update the htpasswd file * from the table htpasswdsync_passwd @@ -393,7 +411,7 @@ function htpasswdsync_user_role_update($role) { * @param object $role * @return void */ -function htpasswdsync_user_role_delete($role) { +function htpasswdsync_user_role_delete($role) { _htpasswdsync_updategroup(); } @@ -434,7 +452,7 @@ function htpasswdsync_menu() { 'title' => 'Htpasswd file synchronization', 'description' => 'Preferences for the HTPasswd Sync module', 'page callback' => 'drupal_get_form', - 'page arguments' => array('htpasswdsync_admin'), + 'page arguments' => array('htpasswdsync_admin_form'), 'access arguments' => array('administer htpasswdsync'), ); return $items; @@ -445,7 +463,15 @@ function htpasswdsync_menu() { * Returns the configuration form structure * @return array */ -function htpasswdsync_admin() { +function htpasswdsync_admin_form() { + // A workaround to update the files directly after saving the config form, + // Because the form is displayed directly after saving. + if(variable_get('htpasswdsync_flag_needs_rebuild', false)) { + variable_del('htpasswdsync_flag_needs_rebuild'); + _htpasswdsync_updatepasswd(); + _htpasswdsync_updategroup(); + } + $form['htpasswdsync_htpasswd'] = array( '#type' => 'textfield', '#title' => t('htpasswd file'), @@ -506,7 +532,6 @@ function htpasswdsync_admin() { ); // Small description ... - $notewhitespaces = _htpasswdsync_names_lowercase() ? " Enter your login name without whitespaces and special characters." : ""; $txt = t('This is how a simple .htaccess file could look like:') @@ -540,7 +565,7 @@ function htpasswdsync_admin() { * @param array& $form_state * @return void */ -function htpasswdsync_admin_validate($form, &$form_state) { +function htpasswdsync_admin_form_validate($form, &$form_state) { $form_state['values']['htpasswdsync_htpasswd'] = trim($form_state['values']['htpasswdsync_htpasswd']); $form_state['values']['htpasswdsync_htgroup'] = trim($form_state['values']['htpasswdsync_htgroup']); $error = false; @@ -572,9 +597,14 @@ function htpasswdsync_admin_validate($form, &$form_state) { } // Directly apply changes if no validation errors - if(!$error) { - _htpasswdsync_updatepasswd(); - _htpasswdsync_updategroup(); + if($error == false) { + // A workaround to update the files directly after saving the config form + variable_set('htpasswdsync_flag_needs_rebuild', true); + foreach(_htpasswdsync_roles() as $role) { + if($role > 0 && !in_array($role, $form_state['values']['htpasswdsync_roles'])) { + _htpasswdsync_remove_role($role); + } + } } } @@ -639,9 +669,6 @@ function htpasswdsync_requirements($phase) { * @return void */ function htpasswdsync_cron() { - $time = variable_get('htpasswdsync_cron_time', 0); - _htpasswdsync_commit_to_htpasswd(); - $res = db_query('DELETE FROM {htpasswdsync_htpasswd} WHERE username NOT IN (SELECT name from {users})'); - variable_set('htpasswdsync_cron_time', time()); + db_query('DELETE FROM {htpasswdsync_htpasswd} WHERE username NOT IN (SELECT name from {users})'); } ?> -- cgit v1.2.3