From b0b08818dddb16360417cc8527424892b7ee24f2 Mon Sep 17 00:00:00 2001 From: stfwi Date: Tue, 5 Jul 2011 19:50:41 +0100 Subject: ht files now updated when enabling/disabling module. Uninstall now deletes all htpasswdsync_* variables instead of explicit variable_del(). Removed _htpasswdsync_validate() as the functionality is now included in the name sanatizing process. --- HTPasswdSync.install | 35 ++++++++--- HTPasswdSync.module | 174 +++++++++++++++++++++++---------------------------- 2 files changed, 104 insertions(+), 105 deletions(-) diff --git a/HTPasswdSync.install b/HTPasswdSync.install index 8288989..e83c828 100644 --- a/HTPasswdSync.install +++ b/HTPasswdSync.install @@ -1,5 +1,6 @@ \ No newline at end of file diff --git a/HTPasswdSync.module b/HTPasswdSync.module index b20e542..36861bc 100644 --- a/HTPasswdSync.module +++ b/HTPasswdSync.module @@ -191,9 +191,8 @@ function _htpasswdsync_write_htfile(&$data, $file) { /** - * Generate the htgroup content - * for each role configured (htpasswdsync_roles array), list users - * and update the htgroup accordingly + * Generate the htgroup content for each role configured (htpasswdsync_roles array), + * list users and update the htgroup accordingly. * @return */ function _htpasswdsync_updategroup() { @@ -211,24 +210,22 @@ function _htpasswdsync_updategroup() { } foreach(_htpasswdsync_roles() as $rid) { - $roles = db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid)); - foreach($roles as $role) { - if(strlen(trim($role->name)) > 0) { - $role = _htpasswdsync_sanatize_name($role->name); - $groups[$role] = ''; - $users = db_query('SELECT u.name, u.mail FROM {users} AS u, {users_roles} AS ur WHERE ur.rid = :rid AND ur.uid = u.uid AND status = 1', array(':rid' => $rid)); - foreach($users as $user) { - $groups[$role] .= ' ' . _htpasswdsync_sanatize_name($user->name); - if($mail_domain != '') { - $user->mail = strtolower($user->mail); - if(strpos($user->mail, $mail_domain) !== false) { - $user->mail = reset(explode('@', $user->mail, 2)); - $groups[$role] .= ' ' . _htpasswdsync_sanatize_name($user->mail); - } + $role = _htpasswdsync_sanatize_name(reset(db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchCol())); + if(!empty($role)) { + $users = db_query('SELECT u.name, u.mail FROM {users} AS u, {users_roles} AS ur WHERE ur.rid = :rid AND ur.uid = u.uid AND status = 1', array(':rid' => $rid)); + $groups[$role] = array(); + $group = &$groups[$role]; + foreach($users as $user) { + $group[] = _htpasswdsync_sanatize_name($user->name); + if($mail_domain != '') { + $user->mail = strtolower($user->mail); + if(strpos($user->mail, $mail_domain) !== false) { + $user->mail = reset(explode('@', $user->mail, 2)); + $group[] = _htpasswdsync_sanatize_name($user->mail); } } } - break; // fetch max. one role + $group = implode(' ', $group); } } _htpasswdsync_write_htfile($groups, $file); @@ -240,7 +237,7 @@ function _htpasswdsync_updategroup() { * from the table htpasswdsync_passwd * @return void */ - function _htpasswdsync_updatepasswd() { + function _htpasswdsync_updatepasswd($removeDrupalUsers=false) { $file = _htpasswdsync_passfilename(); $passwords = array(); @@ -262,18 +259,20 @@ function _htpasswdsync_updategroup() { if(isset($passwords[str_replace(' ','', strtolower($r->username))])) unset($passwords[str_replace(' ','', strtolower($r->username))]); if(isset($passwords[strtolower($r->username)])) unset($passwords[strtolower($r->username)]); } - if($r->passwd == "****DELETED" || $r->passwd == '') { - if(isset($passwords[$r->username])) unset($passwords[$r->username]); - if(isset($passwords[$r->mail])) unset($passwords[$r->mail]); - } else { - $passwords[_htpasswdsync_sanatize_name($r->username)] = $r->passwd; - if($mail_domain != '') { - $r->mail = strtolower($r->mail); - if(strpos($r->mail, $mail_domain) !== false) { - $r->mail = reset(explode('@', $r->mail, 2)); - $passwords[_htpasswdsync_sanatize_name($r->mail)] = $r->passwd; - } + if(!$removeDrupalUsers) { + if($r->passwd == "****DELETED" || $r->passwd == '') { + if(isset($passwords[$r->username])) unset($passwords[$r->username]); + if(isset($passwords[$r->mail])) unset($passwords[$r->mail]); + } else { + $passwords[_htpasswdsync_sanatize_name($r->username)] = $r->passwd; + if($mail_domain != '') { + $r->mail = strtolower($r->mail); + if(strpos($r->mail, $mail_domain) !== false) { + $r->mail = reset(explode('@', $r->mail, 2)); + $passwords[_htpasswdsync_sanatize_name($r->mail)] = $r->passwd; + } + } } } } @@ -336,48 +335,6 @@ function _htpasswdsync_delete($account) { } -/** - * Validate user form input here we refuse username with characters that are - * not supported in htpasswd files - * @param $edit field submited - * @return none - */ -function _htpasswdsync_validate($edit, $account) { - if(isset($edit['name'])) { - if (ereg('[:[:space:]]', $edit['name'])) { - form_set_error('htpasswdsync', - t('The username contains an illegal character, like <space>, :')); - } - } -} - - -/** - * Returns help and module information - * @param string $path which path of the site we're displaying help - * @param array $arg Holds the current path as would be returned from arg() function - * @return string - */ -function htpasswdsync_help($path, $arg) { - if($path != 'admin/help#HTPpasswdSync') return ''; - return '
' . htmlspecialchars(@file_get_contents(dirname(__FILE__) . '/README.txt'), null, 'UTF-8') . '
'; -} - - -/** -* Returns the required permissions for the htpasswdsync module -* @return array -*/ -function htpasswdsync_permission() { - return array( - 'administer htpasswdsync' => array( - 'title' => t('Administer htpasswd synchronization'), - 'restrict access' => true - ) - ); -} - - /** * User cancel hook * @param array $edit @@ -413,11 +370,10 @@ function htpasswdsync_user_delete($account) { function htpasswdsync_field_attach_submit($entity_type, $entity, $form, &$form_state) { if($entity_type != 'user') return; if(!empty($entity->name) && !empty($entity->pass)) { - $r = db_query('SELECT pass FROM {users} WHERE uid=:uid', array(':uid' => $entity->uid)); - foreach($r as $v) { $r = $v->pass; break; } + $r = reset(db_query('SELECT pass FROM {users} WHERE uid=:uid', array(':uid' => $entity->uid))->fetchCol()); if($r != $entity->pass) { _htpasswdsync_update($entity); - } + } } } @@ -437,11 +393,54 @@ 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(); } +/** + * Returns help and module information + * @param string $path which path of the site we're displaying help + * @param array $arg Holds the current path as would be returned from arg() function + * @return string + */ +function htpasswdsync_help($path, $arg) { + if($path != 'admin/help#htpasswdsync') return ''; + return '
' . htmlspecialchars(@file_get_contents(dirname(__FILE__) . '/README.txt'), null, 'UTF-8') . '
'; +} + + +/** +* Returns the required permissions for the htpasswdsync module +* @return array +*/ +function htpasswdsync_permission() { + return array( + 'administer htpasswdsync' => array( + 'title' => t('Administer htpasswd synchronization'), + 'restrict access' => true + ) + ); +} + + +/** + * Administration menu entry + * @return array + */ +function htpasswdsync_menu() { + $items = array(); + $items['admin/config/people/htpasswdsync'] = array( + 'title' => 'Htpasswd file synchronization', + 'description' => 'Preferences for the HTPasswd Sync module', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('htpasswdsync_admin'), + 'access arguments' => array('administer htpasswdsync'), + ); + return $items; +} + + /** * Returns the configuration form structure * @return array @@ -507,9 +506,9 @@ 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:') . "
\t[...]\n"
             . "\tAuthType Basic\n"
@@ -580,23 +579,6 @@ function htpasswdsync_admin_validate($form, &$form_state) {
 }
 
 
-/**
- * Administration menu entry
- * @return array
- */
-function htpasswdsync_menu() {
-    $items = array();
-    $items['admin/config/people/htpasswdsync'] = array(
-        'title' => 'Htpasswd file synchronization',
-        'description' => 'Preferences for the HTPasswd Sync module',
-        'page callback' => 'drupal_get_form',
-        'page arguments' => array('htpasswdsync_admin'),
-        'access arguments' => array('administer htpasswdsync'),
-    );
-    return $items;
-}
-
-
 /**
  * Runtime phase htgroup / htpasswd file check
  * @param string $phase
@@ -662,4 +644,4 @@ function htpasswdsync_cron() {
     $res = db_query('DELETE FROM {htpasswdsync_htpasswd} WHERE username NOT IN (SELECT name from {users})');
     variable_set('htpasswdsync_cron_time', time());
 }
-?>
\ No newline at end of file
+?>
-- 
cgit v1.2.3