r108101 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108100‎ | r108101 | r108102 >
Date:23:56, 4 January 2012
Author:laner
Status:ok (Comments)
Tags:ldap 
Comment:
More code refactoring
Modified paths:
  • /trunk/extensions/LdapAuthentication/LdapAuthentication.php (modified) (history)
  • /trunk/extensions/LdapAuthentication/LdapAutoAuthentication.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LdapAuthentication/LdapAutoAuthentication.php
@@ -45,7 +45,6 @@
4646 $mungedUsername = $wgAuth->getCanonicalName( $autoauthname );
4747
4848 $wgAuth->printDebug( "User exists in LDAP; finding the user by name ($mungedUsername) in MediaWiki.", NONSENSITIVE );
49 -
5049 $localId = User::idFromName( $mungedUsername );
5150 $wgAuth->printDebug( "Got id ($localId).", NONSENSITIVE );
5251
@@ -86,19 +85,15 @@
8786 }
8887
8988 $wgAuth->printDebug( "User does not exist in local database; creating.", NONSENSITIVE );
90 -
9189 // Checks passed, create the user
9290 $user->loadDefaults( $mungedUsername );
9391 $user->addToDatabase();
94 -
9592 $wgAuth->initUser( $user, true );
9693 $user->setCookies();
9794 wfSetupSession();
98 -
9995 # Update user count
10096 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
10197 $ssUpdate->doUpdate();
102 -
10398 # Notify hooks (e.g. Newuserlog)
10499 wfRunHooks( 'AuthPluginAutoCreate', array( $user ) );
105100
@@ -116,10 +111,9 @@
117112 * @var $wgAuth LdapAuthenticationPlugin
118113 */
119114 global $wgAuth;
120 - $wgAuth->printDebug( "Entering NoLogout.", NONSENSITIVE );
121115
 116+ $wgAuth->printDebug( "Entering NoLogout.", NONSENSITIVE );
122117 unset( $personal_urls['logout'] );
123 -
124118 return true;
125119 }
126120
Index: trunk/extensions/LdapAuthentication/LdapAuthentication.php
@@ -17,17 +17,8 @@
1818 # http://www.gnu.org/copyleft/gpl.html
1919
2020 /**
21 - * LdapAuthentication plugin.
 21+ * LdapAuthentication plugin. LDAP Authentication and authorization integration with MediaWiki.
2222 *
23 - * Password authentication, and Smartcard Authentication support are currently
24 - * available. All forms of authentication, current and future, should support
25 - * group, and attribute based restrictions; preference pulling; and group
26 - * syncronization. All forms of authentication should have basic support for
27 - * adding users, changing passwords, and updating preferences in LDAP.
28 - *
29 - * Password authentication has a number of configurations, including straight binds,
30 - * proxy based authentication, and anonymous-search based authentication.
31 - *
3223 * @file
3324 * @ingroup MediaWiki
3425 */
@@ -496,36 +487,29 @@
497488 return true;
498489 }
499490
500 - $this->connect();
501 - if ( $this->ldapconn ) {
502 - $this->printDebug( "PHP's LDAP connect method returned true (note, this does not imply it connected to the server).", NONSENSITIVE );
503 -
 491+ $ret = false;
 492+ if ( $this->connect() ) {
504493 $searchstring = $this->getSearchString( $username );
505494
506495 // If we are using auto authentication, and we got
507496 // anything back, then the user exists.
508497 if ( $this->useAutoAuth() && $searchstring != '' ) {
509 - // getSearchString is going to bind, but will not unbind
510 - LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
511 - return true;
512 - }
 498+ $ret = true;
 499+ } else {
 500+ // Search for the entry.
 501+ $entry = LdapAuthenticationPlugin::ldap_read( $this->ldapconn, $searchstring, "objectclass=*" );
513502
514 - // Search for the entry.
515 - $entry = LdapAuthenticationPlugin::ldap_read( $this->ldapconn, $searchstring, "objectclass=*" );
516 -
 503+ if ( $entry ) {
 504+ $this->printDebug( "Found a matching user in LDAP", NONSENSITIVE );
 505+ $ret = true;
 506+ } else {
 507+ $this->printDebug( "Did not find a matching user in LDAP", NONSENSITIVE );
 508+ }
 509+ }
517510 // getSearchString is going to bind, but will not unbind
518511 LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
519 - if ( !$entry ) {
520 - $this->printDebug( "Did not find a matching user in LDAP", NONSENSITIVE );
521 - return false;
522 - } else {
523 - $this->printDebug( "Found a matching user in LDAP", NONSENSITIVE );
524 - return true;
525 - }
526 - } else {
527 - $this->printDebug( "PHP's LDAP method returned false, this likely implies a misconfiguration of the plugin.", NONSENSITIVE );
528 - return false;
529512 }
 513+ return $ret;
530514 }
531515
532516 /**
@@ -574,6 +558,10 @@
575559
576560 // Connect and set options
577561 $this->ldapconn = LdapAuthenticationPlugin::ldap_connect( $servers );
 562+ if ( !$this->ldapconn ) {
 563+ $this->printDebug( "PHP's LDAP connect method returned null, this likely implies a misconfiguration of the plugin.", NONSENSITIVE );
 564+ return false;
 565+ }
578566 ldap_set_option( $this->ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3 );
579567 ldap_set_option( $this->ldapconn, LDAP_OPT_REFERRALS, 0 );
580568
@@ -591,6 +579,8 @@
592580 return false;
593581 }
594582 }
 583+ $this->printDebug( "PHP's LDAP connect method returned true (note, this does not imply it connected to the server).", NONSENSITIVE );
 584+
595585 return true;
596586 }
597587
@@ -625,15 +615,14 @@
626616 // We need to ensure that if we require a password, that it is
627617 // not blank. We don't allow blank passwords, so we are being
628618 // tricked if someone is supplying one when using password auth.
629 - // Smartcard authentication uses a pin, and does not require
630 - // a password to be given; a blank password here is wanted.
 619+ // auto-authentication is handled by the webserver; a blank password
 620+ // here is wanted.
631621 if ( '' == $password && !$this->useAutoAuth() ) {
632622 $this->printDebug( "User used a blank password", NONSENSITIVE );
633623 return false;
634624 }
635625
636 - $this->connect();
637 - if ( $this->ldapconn ) {
 626+ if ( $this->connect() ) {
638627 // Mediawiki munges the username before authenticate is called,
639628 // this can mess with authentication, group pulling/restriction,
640629 // preference pulling, etc. Let's allow the admin to use
@@ -673,27 +662,23 @@
674663 // DOMAIN\\USER-NAME.
675664 // Get the user's full DN so we can search for groups and such.
676665 $this->userdn = $this->getUserDN( $username );
677 - $this->printDebug( "Pulled the user's DN: $this->userdn", NONSENSITIVE );
 666+ $this->printDebug( "Fetched UserDN: $this->userdn", NONSENSITIVE );
 667+ } else {
 668+ // Now that we are bound, we can pull the user's info.
 669+ $this->getUserInfo();
678670 }
679671 }
 672+ }
680673
681 - $aa = $this->getConf( 'AuthAttribute' );
682 - if ( $aa ) {
683 -
684 - $this->printDebug( "Checking for auth attributes: $aa", NONSENSITIVE );
685 -
686 - $filter = "(" . $aa . ")";
687 - $attributes = array( "dn" );
688 -
689 - $entry = LdapAuthenticationPlugin::ldap_read( $this->ldapconn, $this->userdn, $filter, $attributes );
690 - $info = LdapAuthenticationPlugin::ldap_get_entries( $this->ldapconn, $entry );
691 -
692 - if ( $info["count"] < 1 ) {
693 - $this->printDebug( "Failed auth attribute check", NONSENSITIVE );
694 - LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
695 - $this->markAuthFailed();
696 - return false;
697 - }
 674+ // Ensure the user's entry has the required auth attribute
 675+ $aa = $this->getConf( 'AuthAttribute' );
 676+ if ( $aa ) {
 677+ $this->printDebug( "Checking for auth attributes: $aa", NONSENSITIVE );
 678+ if ( !isset( $this->userInfo ) || !isset( $this->userInfo[0][$aa] ) ) {
 679+ $this->printDebug( "Failed auth attribute check", NONSENSITIVE );
 680+ LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
 681+ $this->markAuthFailed();
 682+ return false;
698683 }
699684 }
700685
@@ -730,16 +715,10 @@
731716 */
732717 public function modifyUITemplate( &$template, &$type ) {
733718 $this->printDebug( "Entering modifyUITemplate", NONSENSITIVE );
734 -
735 - if ( !$this->getConf( 'AddLDAPUsers' ) ) {
736 - $template->set( 'create', false );
737 - }
738 -
 719+ $template->set( 'create', $this->getConf( 'AddLDAPUsers' ) );
739720 $template->set( 'usedomain', true );
740721 $template->set( 'useemail', $this->getConf( 'MailPassword' ) );
741722 $template->set( 'canreset', $this->getConf( 'MailPassword' ) );
742 -
743 -
744723 $template->set( 'domainnames', $this->domainList() );
745724 wfRunHooks( 'LDAPModifyUITemplate', array( &$template ) );
746725 }
@@ -758,7 +737,6 @@
759738
760739 if ( $this->getConf( 'AutoAuthDomain' ) ) {
761740 $this->printDebug( "Allowing auto-authentication login, removing the domain from the list.", NONSENSITIVE );
762 -
763741 // There is no reason for people to log in directly to the wiki if the are using an
764742 // auto-authentication domain. If they try to, they are probably up to something fishy.
765743 unset( $tempDomArr[array_search( $this->getConf( 'AutoAuthDomain' ), $tempDomArr )] );
@@ -801,7 +779,8 @@
802780 // We don't set local passwords, but we don't want the wiki
803781 // to send the user a failure.
804782 return true;
805 - } else if ( !$this->getConf( 'UpdateLDAP' ) ) {
 783+ }
 784+ if ( !$this->getConf( 'UpdateLDAP' ) ) {
806785 $this->printDebug( "Wiki is set to not allow updates", NONSENSITIVE );
807786
808787 // We aren't allowing the user to change his/her own password
@@ -816,19 +795,15 @@
817796 // allowed to do it.
818797 return false;
819798 }
820 -
821799 $pass = $this->getPasswordHash( $password );
822800
823 - $this->connect();
824 - if ( $this->ldapconn ) {
 801+ if ( $this->connect() ) {
825802 $this->userdn = $this->getSearchString( $user->getName() );
826 -
827803 $this->printDebug( "Binding as the writerDN", NONSENSITIVE );
828804 $bind = $this->bindAs( $writer, $this->getConf( 'WriterPassword' ) );
829805 if ( !$bind ) {
830806 return false;
831807 }
832 -
833808 $values["userpassword"] = $pass;
834809
835810 // Blank out the password in the database. We don't want to save
@@ -836,7 +811,6 @@
837812 $password = '';
838813
839814 $success = LdapAuthenticationPlugin::ldap_modify( $this->ldapconn, $this->userdn, $values );
840 -
841815 LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
842816 if ( $success ) {
843817 $this->printDebug( "Successfully modified the user's password", NONSENSITIVE );
@@ -858,10 +832,8 @@
859833 global $wgMemc;
860834
861835 $this->printDebug( "Entering updateExternalDB", NONSENSITIVE );
862 -
863836 if ( !$this->getConf( 'UpdateLDAP' ) || $this->getSessionDomain() == 'local' ) {
864837 $this->printDebug( "Either the user is using a local domain, or the wiki isn't allowing updates", NONSENSITIVE );
865 -
866838 // We don't handle local preferences, but we don't want the
867839 // wiki to return an error.
868840 return true;
@@ -870,7 +842,6 @@
871843 $writer = $this->getConf( 'WriterDN' );
872844 if ( !$writer ) {
873845 $this->printDebug( "The wiki doesn't have wgLDAPWriterDN set", NONSENSITIVE );
874 -
875846 // We can't modify LDAP preferences if we don't have a user
876847 // capable of editing LDAP attributes.
877848 return false;
@@ -880,11 +851,8 @@
881852 $this->realname = $user->getRealName();
882853 $this->nickname = $user->getOption( 'nickname' );
883854 $this->lang = $user->getOption( 'language' );
884 -
885 - $this->connect();
886 - if ( $this->ldapconn ) {
 855+ if ( $this->connect() ) {
887856 $this->userdn = $this->getSearchString( $user->getName() );
888 -
889857 $this->printDebug( "Binding as the writerDN", NONSENSITIVE );
890858 $bind = $this->bindAs( $writer, $this->getConf( 'WriterPassword' ) );
891859 if ( !$bind ) {
@@ -935,11 +903,9 @@
936904 if ( $this->getConf( 'UseLocal' ) && 'local' == $this->getSessionDomain() ) {
937905 return true;
938906 }
939 -
940907 if ( $this->getConf( 'UpdateLDAP' ) || $this->getConf( 'MailPassword' ) ) {
941908 return true;
942909 }
943 -
944910 return false;
945911 }
946912
@@ -962,7 +928,6 @@
963929 // Tell the wiki not to return an error.
964930 return true;
965931 }
966 -
967932 if ( $this->getConf( 'RequiredGroups' ) ) {
968933 $this->printDebug( "The wiki is requiring users to be in specific groups, and cannot add users as this would be a security hole.", NONSENSITIVE );
969934 // It is possible that later we can add users into
@@ -981,12 +946,12 @@
982947
983948 $this->email = $user->getEmail();
984949 $this->realname = $user->getRealName();
985 - $username = strtolower( $user->getName() );
986 -
 950+ $username = $user->getName();
 951+ if ( $this->getConf( 'LowercaseUsernameScheme' ) ) {
 952+ $username = strtolower( $username );
 953+ }
987954 $pass = $this->getPasswordHash( $password );
988 -
989 - $this->connect();
990 - if ( $this->ldapconn ) {
 955+ if ( $this->connect() ) {
991956 $writeloc = $this->getConf( 'WriteLocation' );
992957 $this->userdn = $this->getSearchString( $username );
993958 if ( '' == $this->userdn ) {
@@ -1010,12 +975,13 @@
1011976 return false;
1012977 }
1013978
1014 - // Set up LDAP attributes
 979+ // Set up LDAP objectclasses and attributes
 980+ // TODO: make objectclasses and attributes configurable
1015981 $values["uid"] = $username;
1016982 // sn is required for objectclass inetorgperson
1017983 $values["sn"] = $username;
1018 - if ( '' != $this->email ) { $values["mail"] = $this->email; }
1019 - if ( '' != $this->realname ) { $values["cn"] = $this->realname; }
 984+ if ( $this->email ) { $values["mail"] = $this->email; }
 985+ if ( $this->realname ) { $values["cn"] = $this->realname; }
1020986 else { $values["cn"] = $username; }
1021987 $values["userpassword"] = $pass;
1022988 $values["objectclass"] = array( "inetorgperson" );
@@ -1023,7 +989,7 @@
1024990 $result = true;
1025991 # Let other extensions modify the user object before creation
1026992 wfRunHooks( 'LDAPSetCreationValues', array( $this, $username, &$values, $writeloc, &$this->userdn, &$result ) );
1027 - if ( ! $result ) {
 993+ if ( !$result ) {
1028994 $this->printDebug( "Failed to add user because LDAPSetCreationValues returned false", NONSENSITIVE );
1029995 LdapAuthenticationPlugin::ldap_unbind( $this->ldapconn );
1030996 return false;
@@ -1067,14 +1033,12 @@
10681034 global $wgLDAPDomainNames;
10691035
10701036 $this->printDebug( "Entering validDomain", NONSENSITIVE );
1071 -
10721037 if ( in_array( $domain, $wgLDAPDomainNames ) || ( $this->getConf( 'UseLocal' ) && 'local' == $domain ) ) {
10731038 $this->printDebug( "User is using a valid domain ($domain).", NONSENSITIVE );
10741039 return true;
1075 - } else {
1076 - $this->printDebug( "User is not using a valid domain ($domain).", NONSENSITIVE );
1077 - return false;
10781040 }
 1041+ $this->printDebug( "User is not using a valid domain ($domain).", NONSENSITIVE );
 1042+ return false;
10791043 }
10801044
10811045 /**
@@ -1085,7 +1049,6 @@
10861050 */
10871051 public function updateUser( &$user ) {
10881052 $this->printDebug( "Entering updateUser", NONSENSITIVE );
1089 -
10901053 if ( $this->authFailed ) {
10911054 $this->printDebug( "User didn't successfully authenticate, exiting.", NONSENSITIVE );
10921055 return;
@@ -1093,36 +1056,31 @@
10941057
10951058 $saveSettings = false;
10961059
1097 - // If we aren't pulling preferences, we don't want to accidentally
1098 - // overwrite anything.
10991060 if ( $this->getConf( 'Preferences' ) ) {
11001061 $this->printDebug( "Setting user preferences.", NONSENSITIVE );
1101 -
1102 - if ( '' != $this->lang ) {
 1062+ if ( $this->lang ) {
11031063 $this->printDebug( "Setting language.", NONSENSITIVE );
11041064 $user->setOption( 'language', $this->lang );
11051065 }
1106 - if ( '' != $this->nickname ) {
 1066+ if ( $this->nickname ) {
11071067 $this->printDebug( "Setting nickname.", NONSENSITIVE );
11081068 $user->setOption( 'nickname', $this->nickname );
11091069 }
1110 - if ( '' != $this->realname ) {
 1070+ if ( $this->realname ) {
11111071 $this->printDebug( "Setting realname.", NONSENSITIVE );
11121072 $user->setRealName( $this->realname );
11131073 }
1114 - if ( '' != $this->email ) {
 1074+ if ( $this->email ) {
11151075 $this->printDebug( "Setting email.", NONSENSITIVE );
11161076 $user->setEmail( $this->email );
11171077 $user->confirmEmail();
11181078 }
1119 -
11201079 $saveSettings = true;
11211080 }
11221081
11231082 if ( $this->getConf( 'UseLDAPGroups' ) ) {
11241083 $this->printDebug( "Setting user groups.", NONSENSITIVE );
11251084 $this->setGroups( $user );
1126 -
11271085 $saveSettings = true;
11281086 }
11291087
@@ -1147,12 +1105,11 @@
11481106
11491107 if ( $this->authFailed ) {
11501108 $this->printDebug( "User didn't successfully authenticate, exiting.", NONSENSITIVE );
1151 - return;
 1109+ return null;
11521110 }
1153 -
11541111 if ( 'local' == $this->getSessionDomain() ) {
11551112 $this->printDebug( "User is using a local domain", NONSENSITIVE );
1156 - return;
 1113+ return null;
11571114 }
11581115
11591116 // We are creating an LDAP user, it is very important that we do
@@ -1163,7 +1120,7 @@
11641121 // The update user function does everything else we need done.
11651122 $this->updateUser( $user );
11661123
1167 - // updateUser() won't definately save the user's settings
 1124+ // updateUser() won't necessarily save the user's settings
11681125 $user->saveSettings();
11691126 }
11701127
@@ -1187,8 +1144,7 @@
11881145 }
11891146
11901147 /**
1191 - * Munge the username to always have a form of uppercase for the first letter,
1192 - * and lowercase for the rest of the letters.
 1148+ * Munge the username based on a scheme (lowercase, by default)
11931149 *
11941150 * @param string $username
11951151 * @return string
@@ -1197,17 +1153,14 @@
11981154 global $wgMemc;
11991155
12001156 $this->printDebug( "Entering getCanonicalName", NONSENSITIVE );
1201 -
12021157 $key = wfMemcKey( 'ldapauthentication', 'canonicalname', $username );
12031158 $canonicalname = $username;
12041159 if ( $username != '' ) {
12051160 $this->printDebug( "Username isn't empty.", NONSENSITIVE );
1206 -
12071161 if ( $this->getConf( 'LowercaseUsernameScheme' ) ) {
12081162 $canonicalname = strtolower( $canonicalname );
12091163 } else {
12101164 # Fetch username, so that we can possibly use it.
1211 - # Only do it if we haven't already fetched it.
12121165 $userInfo = $wgMemc->get( $key );
12131166 if ( is_array( $userInfo ) ) {
12141167 $this->printDebug( "Fetched userInfo from memcache.", NONSENSITIVE );
@@ -1216,10 +1169,18 @@
12171170 return $userInfo["canonicalname"];
12181171 }
12191172 } else {
1220 - $this->connect();
1221 - if ( $this->ldapconn ) {
1222 - $this->userdn = $this->getSearchString( $username );
1223 - wfRunHooks( 'SetUsernameAttributeFromLDAP', array( &$this->LDAPUsername, $this->userInfo ) );
 1173+ if ( $this->connect() ) {
 1174+ // Try to pull the username from LDAP. In the case of straight binds,
 1175+ // try to fetch the username by search before bind.
 1176+ $this->userdn = $this->getUserDN( $username, true );
 1177+ $hookSetUsername = $this->LDAPUsername;
 1178+ wfRunHooks( 'SetUsernameAttributeFromLDAP', array( &$hookSetUsername, $this->userInfo ) );
 1179+ if ( is_string( $hookSetUsername ) ) {
 1180+ $this->printDebug( "Username munged by hook: $hookSetUsername", NONSENSITIVE );
 1181+ $this->LDAPUsername = $hookSetUsername;
 1182+ } else {
 1183+ $this->printDebug( "Fetched username is not a string (check your hook code...). This message can be safely ignored if you do not have the SetUsernameAttributeFromLDAP hook defined.", NONSENSITIVE );
 1184+ }
12241185 }
12251186 }
12261187
@@ -1235,9 +1196,7 @@
12361197 // uppercase the first letter
12371198 $canonicalname[0] = strtoupper( $canonicalname[0] );
12381199 }
1239 -
12401200 $this->printDebug( "Munged username: $canonicalname", NONSENSITIVE );
1241 -
12421201 $wgMemc->set( $key, array( "username" => $username, "canonicalname" => $canonicalname ), 3600 * 24 );
12431202 return $canonicalname;
12441203 }
@@ -1260,13 +1219,29 @@
12611220 */
12621221 function getSearchString( $username ) {
12631222 $this->printDebug( "Entering getSearchString", NONSENSITIVE );
1264 -
12651223 $ss = $this->getConf( 'SearchString' );
12661224 if ( $ss ) {
12671225 // This is a straight bind
12681226 $this->printDebug( "Doing a straight bind", NONSENSITIVE );
12691227 $userdn = str_replace( "USER-NAME", $username, $ss );
12701228 } else {
 1229+ $userdn = $this->getUserDN( $username, true );
 1230+ }
 1231+ $this->printDebug( "userdn is: $userdn", SENSITIVE );
 1232+ return $userdn;
 1233+ }
 1234+
 1235+ /**
 1236+ * Gets the DN of a user based upon settings for the domain.
 1237+ * This function will set $this->LDAPUsername
 1238+ *
 1239+ * @param string $username
 1240+ * @return string
 1241+ * @access private
 1242+ */
 1243+ function getUserDN( $username, $bind=false ) {
 1244+ $this->printDebug( "Entering getUserDN", NONSENSITIVE );
 1245+ if ( $bind ) {
12711246 // This is a proxy bind, or an anonymous bind with a search
12721247 $proxyagent = $this->getConf( 'ProxyAgent');
12731248 if ( $proxyagent ) {
@@ -1278,56 +1253,26 @@
12791254 $this->printDebug( "Doing an anonymous bind", NONSENSITIVE );
12801255 $bind = $this->bindAs();
12811256 }
1282 -
12831257 if ( !$bind ) {
12841258 $this->printDebug( "Failed to bind", NONSENSITIVE );
12851259 return '';
12861260 }
1287 -
1288 - $userdn = $this->getUserDN( $username );
12891261 }
1290 - $this->printDebug( "userdn is: $userdn", SENSITIVE );
1291 - return $userdn;
1292 - }
12931262
1294 - /**
1295 - * Gets the DN of a user based upon settings for the domain.
1296 - * This function will set $this->LDAPUsername
1297 - * You must bind to the server before calling this.
1298 - *
1299 - * @param string $username
1300 - * @return string
1301 - * @access private
1302 - */
1303 - function getUserDN( $username ) {
1304 - $this->printDebug( "Entering getUserDN", NONSENSITIVE );
1305 -
13061263 // we need to do a subbase search for the entry
1307 - // Auto auth needs to check LDAP for required attributes.
1308 - $aa = $this->getConf( 'AuthAttribute' );
1309 - $searchattr = $this->getConf( 'SearchAttribute' );
1310 - if ( $aa && $this->useAutoAuth() ) {
1311 - $auth_filter = "(" . $aa . ")";
1312 - $srch_filter = "(" . $searchattr . "=" . $this->getLdapEscapedString( $username ) . ")";
1313 - $filter = "(&" . $srch_filter . $auth_filter . ")";
1314 - $this->printDebug( "Created an auth attribute filter: $filter", SENSITIVE );
1315 - } else {
1316 - $filter = "(" . $searchattr . "=" . $this->getLdapEscapedString( $username ) . ")";
1317 - $this->printDebug( "Created a regular filter: $filter", SENSITIVE );
1318 - }
 1264+ $filter = "(" . $searchattr . "=" . $this->getLdapEscapedString( $username ) . ")";
 1265+ $this->printDebug( "Created a regular filter: $filter", SENSITIVE );
13191266
 1267+ // We explicitly put memberof here because it's an operational attribute in some servers.
13201268 $attributes = array( "*", "memberof" );
13211269 $base = $this->getBaseDN( USERDN );
1322 -
13231270 $this->printDebug( "Using base: $base", SENSITIVE );
1324 -
13251271 $entry = LdapAuthenticationPlugin::ldap_search( $this->ldapconn, $base, $filter, $attributes );
13261272 if ( LdapAuthenticationPlugin::ldap_count_entries( $this->ldapconn, $entry ) == 0 ) {
13271273 $this->printDebug( "Couldn't find an entry", NONSENSITIVE );
13281274 $this->fetchedUserInfo = false;
13291275 return '';
13301276 }
1331 -
13321277 $this->userInfo = LdapAuthenticationPlugin::ldap_get_entries( $this->ldapconn, $entry );
13331278 $this->fetchedUserInfo = true;
13341279 if ( isset( $this->userInfo[0][$searchattr] ) ) {
@@ -1335,36 +1280,28 @@
13361281 $this->printDebug( "Setting the LDAPUsername based on fetched wgLDAPSearchAttributes: $username", NONSENSITIVE );
13371282 $this->LDAPUsername = $username;
13381283 }
1339 -
1340 - // This is a pretty useful thing to have for auto authentication,
1341 - // group checking, and pulling preferences.
1342 - wfRunHooks( 'SetUsernameAttributeFromLDAP', array( &$this->LDAPUsername, $this->userInfo ) );
1343 - if ( !is_string( $this->LDAPUsername ) ) {
1344 - $this->printDebug( "Fetched username is not a string (check your hook code...). This message can be safely ignored if you do not have the SetUsernameAttributeFromLDAP hook defined.", NONSENSITIVE );
1345 - $this->LDAPUsername = '';
1346 - }
1347 -
13481284 $userdn = $this->userInfo[0]["dn"];
13491285 return $userdn;
13501286 }
13511287
13521288 /**
1353 - * @return array|null
 1289+ * Load the current user's entry
 1290+ *
 1291+ * @return bool
13541292 */
13551293 function getUserInfo() {
13561294 // Don't fetch the same data more than once
13571295 if ( $this->fetchedUserInfo ) {
1358 - return $this->userInfo;
 1296+ return true;
13591297 }
1360 -
13611298 $userInfo = $this->getUserInfoStateless( $this->userdn );
13621299 if ( is_null( $userInfo ) ) {
13631300 $this->fetchedUserInfo = false;
1364 - return null;
13651301 } else {
13661302 $this->fetchedUserInfo = true;
1367 - return $userInfo;
 1303+ $this->userInfo = $userInfo;
13681304 }
 1305+ return $this->fetchedUserInfo;
13691306 }
13701307
13711308 /**
@@ -1373,8 +1310,8 @@
13741311 */
13751312 function getUserInfoStateless( $userdn ) {
13761313 global $wgMemc;
1377 - $key = wfMemcKey( 'ldapauthentication', 'userinfo', $userdn );
13781314
 1315+ $key = wfMemcKey( 'ldapauthentication', 'userinfo', $userdn );
13791316 $userInfo = $wgMemc->get( $key );
13801317 if ( !is_array( $userInfo ) ) {
13811318 $entry = LdapAuthenticationPlugin::ldap_read( $this->ldapconn, $userdn, "objectclass=*", array( '*', 'memberof' ) );
@@ -1393,16 +1330,15 @@
13941331 private function getPreferences() {
13951332 $this->printDebug( "Entering getPreferences", NONSENSITIVE );
13961333
1397 - $this->userInfo = $this->getUserInfo();
1398 - if ( is_null( $this->userInfo ) ) {
1399 - $this->printDebug( "Failed to get preferences", NONSENSITIVE );
1400 - }
1401 -
14021334 // Retrieve preferences
14031335 $prefs = $this->getConf( 'Preferences' );
14041336 if ( !$prefs ) {
1405 - return;
 1337+ return null;
14061338 }
 1339+ if ( !$this->getUserInfo() ) {
 1340+ $this->printDebug( "Failed to get preferences, the user's entry wasn't found.", NONSENSITIVE );
 1341+ return null;
 1342+ }
14071343 $this->printDebug( "Retrieving preferences", NONSENSITIVE );
14081344 foreach ( array_keys( $prefs ) as $key ) {
14091345 $attr = strtolower( $prefs[$key] );
@@ -1509,11 +1445,9 @@
15101446
15111447 if ( $this->getConf( 'GroupsUseMemberOf' ) ) {
15121448 $this->printDebug( "Using memberOf", NONSENSITIVE );
1513 - $this->userInfo = $this->getUserInfo();
1514 - if ( is_null( $this->userInfo ) ) {
1515 - $this->printDebug( "Failed to get memberOf attribute", NONSENSITIVE );
1516 - }
1517 - if ( isset( $this->userInfo[0]["memberof"] ) ) {
 1449+ if ( !$this->getUserInfo() ) {
 1450+ $this->printDebug( "Couldn't get the user's entry.", NONSENSITIVE );
 1451+ } else if ( isset( $this->userInfo[0]["memberof"] ) ) {
15181452 # The first entry is always a count
15191453 $memberOfMembers = $this->userInfo[0]["memberof"];
15201454 array_shift( $memberOfMembers );
@@ -1540,13 +1474,11 @@
15411475 } else {
15421476 $this->printDebug( "Searching for the groups", NONSENSITIVE );
15431477 $this->userLDAPGroups = $this->searchGroups( $usertopass );
1544 -
15451478 if ( $this->getConf( 'GroupSearchNestedGroups' ) ) {
15461479 $this->userLDAPGroups = $this->searchNestedGroups( $this->userLDAPGroups );
15471480 $this->printDebug( "Got the following nested groups:", SENSITIVE, $this->userLDAPGroups["dn"] );
15481481 }
15491482 }
1550 -
15511483 // Only find all groups if the user has any groups; otherwise, we are
15521484 // just wasting a search.
15531485 if ( $this->getConf( 'GroupsPrevail' ) && count( $this->userLDAPGroups ) != 0 ) {
@@ -1575,7 +1507,6 @@
15761508 }
15771509
15781510 $this->printDebug( "Searching groups:", SENSITIVE, $groups["dn"] );
1579 -
15801511 $groupstosearch = array( "short" => array(), "dn" => array() );
15811512 foreach ( $groups["dn"] as $group ) {
15821513 $returnedgroups = $this->searchGroups( $group );
@@ -1601,7 +1532,6 @@
16021533 }
16031534 }
16041535 }
1605 -
16061536 $searchedgroups = array_merge_recursive( $groups, $searchedgroups );
16071537
16081538 return $this->searchNestedGroups( $groupstosearch, $searchedgroups );
@@ -1617,7 +1547,6 @@
16181548 $this->printDebug( "Entering searchGroups", NONSENSITIVE );
16191549
16201550 $base = $this->getBaseDN( GROUPDN );
1621 -
16221551 $objectclass = $this->getConf( 'GroupObjectclass' );
16231552 $attribute = $this->getConf( 'GroupAttribute' );
16241553 $nameattribute = $this->getConf( 'GroupNameAttribute' );
@@ -1687,24 +1616,19 @@
16881617 }
16891618
16901619 $filter = "(&($attribute=$value)(objectclass=$objectclass))";
1691 -
16921620 $this->printDebug( "Search string: $filter", SENSITIVE );
1693 -
16941621 $info = LdapAuthenticationPlugin::ldap_search( $this->ldapconn, $base, $filter );
16951622 if ( !$info ) {
16961623 $this->printDebug( "No entries returned from search.", SENSITIVE );
1697 -
16981624 // Return an array so that other functions
16991625 // don't error out.
17001626 return array( "short" => array(), "dn" => array() );
17011627 }
17021628
17031629 $entries = LdapAuthenticationPlugin::ldap_get_entries( $this->ldapconn, $info );
1704 -
17051630 if ( $entries ){
17061631 // We need to shift because the first entry will be a count
17071632 array_shift( $entries );
1708 -
17091633 // Let's get a list of both full dn groups and shortname groups
17101634 foreach ( $entries as $entry ) {
17111635 $shortMember = strtolower( $entry[$nameattribute][0] );
@@ -1715,7 +1639,6 @@
17161640 }
17171641
17181642 $this->printDebug( "Returned groups:", SENSITIVE, $groups["dn"] );
1719 -
17201643 return $groups;
17211644 }
17221645
@@ -1729,7 +1652,6 @@
17301653 */
17311654 function hasLDAPGroup( $group ) {
17321655 $this->printDebug( "Entering hasLDAPGroup", NONSENSITIVE );
1733 -
17341656 return in_array( strtolower( $group ), $this->userLDAPGroups["short"] );
17351657 }
17361658
@@ -1742,7 +1664,6 @@
17431665 */
17441666 function isLDAPGroup( $group ) {
17451667 $this->printDebug( "Entering isLDAPGroup", NONSENSITIVE );
1746 -
17471668 return in_array( strtolower( $group ), $this->allLDAPGroups["short"] );
17481669 }
17491670
@@ -1757,7 +1678,6 @@
17581679 global $wgGroupPermissions;
17591680
17601681 // TODO: this is *really* ugly code. clean it up!
1761 -
17621682 $this->printDebug( "Entering setGroups.", NONSENSITIVE );
17631683
17641684 # Add ldap groups as local groups
@@ -1773,9 +1693,7 @@
17741694 # add groups permissions
17751695 $localAvailGrps = $user->getAllGroups();
17761696 $localUserGrps = $user->getEffectiveGroups();
1777 -
17781697 $defaultLocallyManagedGrps = array( 'bot', 'sysop', 'bureaucrat' );
1779 -
17801698 $locallyManagedGrps = $this->getConf( 'LocallyManagedGroups' );
17811699 if ( $locallyManagedGrps ) {
17821700 $locallyManagedGrps = array_unique( array_merge( $defaultLocallyManagedGrps, $locallyManagedGrps ) );
@@ -1787,7 +1705,6 @@
17881706
17891707 $this->printDebug( "Available groups are: ", NONSENSITIVE, $localAvailGrps );
17901708 $this->printDebug( "Effective groups are: ", NONSENSITIVE, $localUserGrps );
1791 -
17921709 # note: $localUserGrps does not need to be updated with $cGroup added,
17931710 # as $localAvailGrps contains $cGroup only once.
17941711 foreach ( $localAvailGrps as $cGroup ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r108775Follow up to r108101.laner23:02, 12 January 2012

Comments

#Comment by Johnduhart (talk | contribs)   15:50, 5 January 2012
-			if ( '' != $this->lang ) {
+			if ( $this->lang ) {

'0' == false, etc.

Status & tagging log