aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorstfwi <cerberos@atwillys.de>2011-07-05 19:50:41 +0100
committerstfwi <cerberos@atwillys.de>2011-07-05 19:50:41 +0100
commitb0b08818dddb16360417cc8527424892b7ee24f2 (patch)
tree0b38824c3e4242a626e2bd393c32251eaf842769
parent2c44ee697d05d864f3b8b4e81fb80883f6532c17 (diff)
downloaddrupal_htpasswdsync-b0b08818dddb16360417cc8527424892b7ee24f2.tar.gz
drupal_htpasswdsync-b0b08818dddb16360417cc8527424892b7ee24f2.tar.bz2
drupal_htpasswdsync-b0b08818dddb16360417cc8527424892b7ee24f2.zip
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.
-rw-r--r--HTPasswdSync.install35
-rw-r--r--HTPasswdSync.module174
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 @@
<?php
+
/**
* Install plugin
* @return void
@@ -8,22 +9,17 @@ function htpasswdsync_install() {
drupal_install_schema('htpasswdsync_db');
}
+
/**
* Uninstall plugin
* @return void
*/
function htpasswdsync_uninstall() {
- drupal_uninstall_schema('htpasswdsync_db');
- variable_del('htpasswdsync_htpasswd');
- variable_del('htpasswdsync_htgroup');
- variable_del('htpasswdsync_roles');
- variable_del('htpasswdsync_hash');
- variable_del('htpasswdsync_cron_time');
- variable_del('htpasswdsync_overwrite');
- variable_del('htpasswdsync_names_lowercase');
- variable_del('htpasswdsync_names_without_whitespace');
+ db_query("DELETE FROM {variable} WHERE LOCATE('htpasswdsync_', name) = 1");
+ drupal_uninstall_schema('htpasswdsync_db');
}
+
/**
* Returns module database schema specification
* @return array
@@ -50,4 +46,25 @@ function htpasswdsync_db_schema() {
);
return $schema;
}
+
+
+/**
+ * Enable plugin: Refresh database
+ * @return void
+ */
+function htpasswdsync_enable() {
+ if(variable_get('htpasswdsync_htpasswd', '---NOT--CONFIGURED---') != '---NOT--CONFIGURED---') {
+ _htpasswdsync_updatepasswd(false);
+ }
+}
+
+/**
+ * Disable plugin: remove users from htuser/htgroup file
+ * @return void
+ */
+function htpasswdsync_disable() {
+ if(variable_get('htpasswdsync_htpasswd', '---NOT--CONFIGURED---') != '---NOT--CONFIGURED---') {
+ _htpasswdsync_updatepasswd(true);
+ }
+}
?> \ 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;
+ }
+ }
}
}
}
@@ -337,48 +336,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 &lt;space&gt;, :'));
- }
- }
-}
-
-
-/**
- * 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 '<pre>' . htmlspecialchars(@file_get_contents(dirname(__FILE__) . '/README.txt'), null, 'UTF-8') . '</pre>';
-}
-
-
-/**
-* 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
* @param object $account
@@ -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,12 +393,55 @@ 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 '<pre>' . htmlspecialchars(@file_get_contents(dirname(__FILE__) . '/README.txt'), null, 'UTF-8') . '</pre>';
+}
+
+
+/**
+* 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:')
. "<pre>\t[...]\n"
. "\tAuthType Basic\n"
@@ -581,23 +580,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
* @return array
@@ -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
+?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy