Patch not to override user preferences

Jump to: navigation, search

Hello

I'm not sure this is a place for a patch so tell me if I'm wrong.

I want to load user attributes from LDAP while allowing users to change their nicknames(or signatures) or real names. So I added an option to control whether override the current MediaWiki user preferences or not.

I'm submitting the patch. I would like to have this feature in the extension.

patch:

--- C:/Users/iwao/Downloads/LdapAuthentication.trunk.php        Thu Jan 12 09:38:09 2012
+++ C:/Users/iwao/Downloads/LdapAuthentication.php      Thu Jan 12 13:30:21 2012
@@ -53,6 +53,7 @@
 $wgLDAPPasswordHash = array();
 $wgLDAPMailPassword = array();
 $wgLDAPPreferences = array();
+$wgLDAPPreferencesNoOverride = array();
 $wgLDAPDisableAutoCreate = array();
 $wgLDAPDebug = 0;
 $wgLDAPGroupUseFullDN = array();
@@ -389,6 +390,9 @@
                case 'Preferences':
                        global $wgLDAPPreferences;
                        return self::setOrDefault( $wgLDAPPreferences, $domain, array() );
+               case 'PreferencesNoOverride':
+                       global $wgLDAPPreferencesNoOverride;
+                       return self::setOrDefault( $wgLDAPPreferencesNoOverride, $domain, array() );
                case 'DisableAutoCreate':
                        global $wgLDAPDisableAutoCreate;
                        return self::setOrDefault( $wgLDAPDisableAutoCreate, $domain, false );
@@ -1042,6 +1046,27 @@
        }
 
        /**
+        * Check if the current user preference is invalid and to update 
+        *
+        * @param $pref Preference to set
+        * @param $oldvalue Old value stored in MediaWiki
+        * @param $newvalue New value to set 
+        */
+       private function isUserPreferenceInvalid( $pref, $oldvalue, $newvalue ) {
+               $this->printDebug( "Entering isUserPreferenceInvalid", NONSENSITIVE );
+               $nooverride = $this->getConf( 'PreferencesNoOverride' );
+               $this->printDebug( $nooverride [ $pref ] ? 'true' : 'false', NONSENSITIVE);
+               if ( $newvalue ) {
+                       if ( is_null( $oldvalue ) ||
+                            ( !isset( $nooverride[ $pref ] ) || $nooverride[ $pref ] ) ) {
+
+                               return true;
+                       }                             
+               }
+               return false;
+       }
+
+       /**
         * When a user logs in, update user with information from LDAP.
         *
         * @param $user User
@@ -1058,24 +1083,27 @@
 
                if ( $this->getConf( 'Preferences' ) ) {
                        $this->printDebug( "Setting user preferences.", NONSENSITIVE );
-                       if ( $this->lang ) {
+                       if ( $this->isUserPreferenceInvalid( 'language', $user->getOption('language'), $this->lang ) ) {
                                $this->printDebug( "Setting language.", NONSENSITIVE );
                                $user->setOption( 'language', $this->lang );
+                               $saveSettings = true;
                        }
-                       if ( $this->nickname ) {
+                       if ( $this->isUserPreferenceInvalid( 'nickname', $user->getOption( 'nickname' ), $this->nickname ) ) {
                                $this->printDebug( "Setting nickname.", NONSENSITIVE );
                                $user->setOption( 'nickname', $this->nickname );
+                               $saveSettings = true;
                        }
-                       if ( $this->realname ) {
+                       if ( $this->isUserPreferenceInvalid( 'realname', $user->getRealName(), $this->realname ) ) {
                                $this->printDebug( "Setting realname.", NONSENSITIVE );
                                $user->setRealName( $this->realname );
+                               $saveSettings = true;
                        }
-                       if ( $this->email ) {
+                       if ( $this->isUserPreferenceInvalid( 'email', $user->getEmail(), $this->email ) ) {
                                $this->printDebug( "Setting email.", NONSENSITIVE );
                                $user->setEmail( $this->email );
                                $user->confirmEmail();
+                               $saveSettings = true;
                        }
-                       $saveSettings = true;
                }
 
                if ( $this->getConf( 'UseLDAPGroups' ) ) {

Options shall be specified like this:

$wgLDAPPreferencesNoOverride = array(
  "mydomain" => array( "realname" => true, "nickname" => false )
  );

If not specified, user preferences will be overridden.

Yuryu04:31, 12 January 2012
Personal tools

Variants
Actions
Navigation
Support
Download
Development
Communication
Toolbox