r107428 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107427‎ | r107428 | r107429 >
Date:21:23, 27 December 2011
Author:reedy
Status:ok
Tags:
Comment:
Simplify down, and don't fall through switch statements

Set connect to public (followup r107418), wrap long string
Modified paths:
  • /trunk/extensions/LdapAuthentication/LdapAuthentication.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LdapAuthentication/LdapAuthentication.php
@@ -278,25 +278,16 @@
279279 switch ( $preference ) {
280280 case 'ServerNames':
281281 global $wgLDAPServerNames;
282 - if ( isset( $wgLDAPServerNames[$domain] ) ) {
283 - return $wgLDAPServerNames[$domain];
284 - }
285 - return array();
 282+ return self::setOrDefault( $wgLDAPServerNames, $domain, array() );
286283 case 'UseLocal':
287284 global $wgLDAPUseLocal;
288285 return $wgLDAPUseLocal;
289286 case 'EncryptionType':
290287 global $wgLDAPEncryptionType;
291 - if ( isset( $wgLDAPEncryptionType[$domain] ) ) {
292 - return $wgLDAPEncryptionType[$domain];
293 - }
294 - return 'tls';
 288+ return self::setOrDefault( $wgLDAPEncryptionType, $domain, 'tls' );
295289 case 'Options':
296290 global $wgLDAPOptions;
297 - if ( isset( $wgLDAPOptions[$domain] ) ) {
298 - return $wgLDAPOptions[$domain];
299 - }
300 - return array();
 291+ return self::setOrDefault( $wgLDAPOptions, $domain, array() );
301292 case 'Port':
302293 global $wgLDAPPort;
303294 if ( isset( $wgLDAPPort[$domain] ) ) {
@@ -309,181 +300,104 @@
310301 }
311302 case 'SearchString':
312303 global $wgLDAPSearchStrings;
313 - if ( isset( $wgLDAPSearchStrings[$domain] ) ) {
314 - return $wgLDAPSearchStrings[$domain];
315 - }
 304+ return self::setOrDefault( $wgLDAPSearchStrings, $domain );
316305 case 'ProxyAgent':
317306 global $wgLDAPProxyAgent;
318 - if ( isset( $wgLDAPProxyAgent[$domain] ) ) {
319 - return $wgLDAPProxyAgent[$domain];
320 - }
 307+ return self::setOrDefault( $wgLDAPProxyAgent, $domain );
321308 case 'ProxyAgentPassword':
322309 global $wgLDAPProxyAgentPassword;
323 - if ( isset( $wgLDAPProxyAgentPassword[$domain] ) ) {
324 - return $wgLDAPProxyAgentPassword[$domain];
325 - }
 310+ return self::setOrDefault( $wgLDAPProxyAgentPassword, $domain );
326311 case 'SearchAttribute':
327312 global $wgLDAPSearchAttributes;
328 - if ( isset( $wgLDAPSearchAttributes[$domain] ) ) {
329 - return $wgLDAPSearchAttributes[$domain];
330 - }
 313+ return self::setOrDefault( $wgLDAPSearchAttributes, $domain );
331314 case 'BaseDN':
332315 global $wgLDAPBaseDNs;
333 - if ( isset( $wgLDAPBaseDNs[$domain] ) ) {
334 - return $wgLDAPBaseDNs[$domain];
335 - }
 316+ return self::setOrDefault( $wgLDAPBaseDNs, $domain );
336317 case 'GroupBaseDN':
337318 global $wgLDAPGroupBaseDNs;
338 - if ( isset( $wgLDAPGroupBaseDNs[$domain] ) ) {
339 - return $wgLDAPGroupBaseDNs[$domain];
340 - }
 319+ return self::setOrDefault( $wgLDAPGroupBaseDNs, $domain );
341320 case 'UserBaseDN':
342321 global $wgLDAPUserBaseDNs;
343 - if ( isset( $wgLDAPUserBaseDNs[$domain] ) ) {
344 - return $wgLDAPUserBaseDNs[$domain];
345 - }
 322+ return self::setOrDefault( $wgLDAPUserBaseDNs, $domain );
346323 case 'WriterDN':
347324 global $wgLDAPWriterDN;
348 - if ( isset( $wgLDAPWriterDN[$domain] ) ) {
349 - return $wgLDAPWriterDN[$domain];
350 - }
 325+ return self::setOrDefault( $wgLDAPWriterDN, $domain );
351326 case 'WriterPassword':
352327 global $wgLDAPWriterPassword;
353 - if ( isset( $wgLDAPWriterPassword[$domain] ) ) {
354 - return $wgLDAPWriterPassword[$domain];
355 - }
 328+ return self::setOrDefault( $wgLDAPWriterPassword, $domain );
356329 case 'WriteLocation':
357330 global $wgLDAPWriteLocation;
358 - if ( isset( $wgLDAPWriteLocation[$domain] ) ) {
359 - return $wgLDAPWriteLocation[$domain];
360 - }
 331+ return self::setOrDefault( $wgLDAPWriteLocation, $domain );
361332 case 'AddLDAPUsers':
362333 global $wgLDAPAddLDAPUsers;
363 - if ( isset( $wgLDAPAddLDAPUsers[$domain] ) ) {
364 - return $wgLDAPAddLDAPUsers[$domain];
365 - }
366 - return false;
 334+ return self::setOrDefault( $wgLDAPAddLDAPUsers, $domain, false );
367335 case 'UpdateLDAP':
368336 global $wgLDAPUpdateLDAP;
369 - if ( isset( $wgLDAPUpdateLDAP[$domain] ) ) {
370 - return $wgLDAPUpdateLDAP[$domain];
371 - }
372 - return false;
 337+ return self::setOrDefault( $wgLDAPUpdateLDAP, $domain, false );
373338 case 'PasswordHash':
374339 global $wgLDAPPasswordHash;
375 - if ( isset( $wgLDAPPasswordHash[$domain] ) ) {
376 - return $wgLDAPPasswordHash[$domain];
377 - } else {
378 - return 'clear';
379 - }
 340+ return self::setOrDefault( $wgLDAPPasswordHash, $domain, 'clear' );
380341 case 'MailPassword':
381342 global $wgLDAPMailPassword;
382 - if ( isset( $wgLDAPMailPassword[$domain] ) ) {
383 - return $wgLDAPMailPassword[$domain];
384 - }
385 - return false;
 343+ return self::setOrDefault( $wgLDAPMailPassword, $domain, false );
386344 case 'Preferences':
387345 global $wgLDAPPreferences;
388 - if ( isset( $wgLDAPPreferences[$domain] ) ) {
389 - return $wgLDAPPreferences[$domain];
390 - }
391 - return array();
 346+ return self::setOrDefault( $wgLDAPPreferences, $domain, array() );
392347 case 'DisableAutoCreate':
393348 global $wgLDAPDisableAutoCreate;
394 - if ( isset( $wgLDAPDisableAutoCreate[$domain] ) ) {
395 - return $wgLDAPDisableAutoCreate[$domain];
396 - }
397 - return false;
 349+ return self::setOrDefault( $wgLDAPDisableAutoCreate, $domain, false );
398350 case 'GroupUseFullDN':
399351 global $wgLDAPGroupUseFullDN;
400 - if ( isset( $wgLDAPGroupUseFullDN[$domain] ) ) {
401 - return $wgLDAPGroupUseFullDN[$domain];
402 - }
403 - return false;
 352+ return self::setOrDefault( $wgLDAPGroupUseFullDN, $domain, false );
404353 case 'LowerCaseUsername':
405354 global $wgLDAPLowerCaseUsername;
406355 if ( isset( $wgLDAPLowerCaseUsername[$domain] ) ) {
407356 $this->printDebug( "Configuration set to lowercase username.", NONSENSITIVE );
408357 return $wgLDAPLowerCaseUsername[$domain];
 358+ } else {
 359+ return false;
409360 }
410 - return false;
411361 case 'LowerCaseUsernameScheme':
412362 global $wgLDAPLowerCaseUsernameScheme;
413 - if ( isset( $wgLDAPLowerCaseUsernameScheme[$domain] ) ) {
414 - return $wgLDAPLowerCaseUsernameScheme[$domain];
415 - } else {
416 - // Default set to true for backwards compatibility with
417 - // versions < 2.0a
418 - return true;
419 - }
 363+ // Default set to true for backwards compatibility with
 364+ // versions < 2.0a
 365+ return self::setOrDefault( $wgLDAPLowerCaseUsernameScheme, $domain, true );
420366 case 'GroupUseRetievedUsername':
421367 global $wgLDAPGroupUseRetrievedUsername;
422 - if ( isset( $wgLDAPGroupUseRetrievedUsername[$domain] ) ) {
423 - return $wgLDAPGroupUseRetrievedUsername[$domain];
424 - }
425 - return false;
 368+ return self::setOrDefault( $wgLDAPGroupUseRetrievedUsername, $domain, false );
426369 case 'GroupObjectclass':
427370 global $wgLDAPGroupObjectclass;
428 - if ( isset( $wgLDAPGroupObjectclass[$domain] ) ) {
429 - return $wgLDAPGroupObjectclass[$domain];
430 - }
 371+ return self::setOrDefault( $wgLDAPGroupObjectclass, $domain );
431372 case 'GroupAttribute':
432373 global $wgLDAPGroupAttribute;
433 - if ( isset( $wgLDAPGroupAttribute[$domain] ) ) {
434 - return $wgLDAPGroupAttribute[$domain];
435 - }
 374+ return self::setOrDefault( $wgLDAPGroupAttribute, $domain );
436375 case 'GroupNameAttribute':
437376 global $wgLDAPGroupNameAttribute;
438 - if ( isset( $wgLDAPGroupNameAttribute[$domain] ) ) {
439 - return $wgLDAPGroupNameAttribute[$domain];
440 - }
 377+ return self::setOrDefault( $wgLDAPGroupNameAttribute, $domain );
441378 case 'GroupsUseMemberOf':
442379 global $wgLDAPGroupsUseMemberOf;
443 - if ( isset( $wgLDAPGroupsUseMemberOf[$domain] ) ) {
444 - return $wgLDAPGroupsUseMemberOf[$domain];
445 - }
446 - return false;
 380+ return self::setOrDefault( $wgLDAPGroupsUseMemberOf, $domain, false );
447381 case 'UseLDAPGroups':
448382 global $wgLDAPUseLDAPGroups;
449 - if ( isset( $wgLDAPUseLDAPGroups[$domain] ) ) {
450 - return $wgLDAPUseLDAPGroups[$domain];
451 - }
452 - return false;
 383+ return self::setOrDefault( $wgLDAPUseLDAPGroups, $domain, false );
453384 case 'LocallyManagedGroups':
454385 global $wgLDAPLocallyManagedGroups;
455 - if ( isset( $wgLDAPLocallyManagedGroups[$domain] ) ) {
456 - return $wgLDAPLocallyManagedGroups[$domain];
457 - }
458 - return array();
 386+ return self::setOrDefault( $wgLDAPLocallyManagedGroups, $domain, array() );
459387 case 'GroupsPrevail':
460388 global $wgLDAPGroupsPrevail;
461 - if ( isset( $wgLDAPGroupsPrevail[$domain] ) ) {
462 - return $wgLDAPGroupsPrevail[$domain];
463 - }
464 - return false;
 389+ return self::setOrDefault( $wgLDAPGroupsPrevail, $domain, false );
465390 case 'RequiredGroups':
466391 global $wgLDAPRequiredGroups;
467 - if ( isset( $wgLDAPRequiredGroups[$domain] ) ) {
468 - return $wgLDAPRequiredGroups[$domain];
469 - }
470 - return array();
 392+ return self::setOrDefault( $wgLDAPRequiredGroups, $domain, array() );
471393 case 'ExcludedGroups':
472394 global $wgLDAPExcludedGroups;
473 - if ( isset( $wgLDAPExcludedGroups[$domain] ) ) {
474 - return $wgLDAPExcludedGroups[$domain];
475 - }
476 - return array();
 395+ return self::setOrDefault( $wgLDAPExcludedGroups, $domain, array() );
477396 case 'GroupSearchNestedGroups':
478397 global $wgLDAPGroupSearchNestedGroups;
479 - if ( isset( $wgLDAPGroupSearchNestedGroups[$domain] ) ) {
480 - return $wgLDAPGroupSearchNestedGroups[$domain];
481 - }
482 - return false;
 398+ return self::setOrDefault( $wgLDAPGroupSearchNestedGroups, $domain, false );
483399 case 'AuthAttribute':
484400 global $wgLDAPAuthAttribute;
485 - if ( isset( $wgLDAPAuthAttribute[$domain] ) ) {
486 - return $wgLDAPAuthAttribute[$domain];
487 - }
 401+ return self::setOrDefault( $wgLDAPAuthAttribute, $domain );
488402 case 'AutoAuthUsername':
489403 global $wgLDAPAutoAuthUsername;
490404 return $wgLDAPAutoAuthUsername;
@@ -495,6 +409,19 @@
496410 }
497411
498412 /**
 413+ * Returns the item from $array at index $key if it is set,
 414+ * else, it returns $default
 415+ *
 416+ * @param $array array
 417+ * @param $key
 418+ * @param $default mixed
 419+ * @return mixed
 420+ */
 421+ private static function setOrDefault( $array, $key, $default = '' ) {
 422+ return isset( $array[$key] ) ? $array[$key] : $default;
 423+ }
 424+
 425+ /**
499426 * Check whether there exists a user account with the given name.
500427 * The name will be normalized to MediaWiki's requirements, so
501428 * you might need to munge it (for instance, for lowercase initial
@@ -549,7 +476,8 @@
550477 /**
551478 * Connect to LDAP
552479 */
553 - private function connect( $domain='' ) {
 480+ public function connect( $domain='' ) {
 481+ // FIXME: $domain isn't used
554482 if ( $domain == '' ) {
555483 $domain = $this->getSessionDomain();
556484 }
@@ -557,7 +485,9 @@
558486 $this->printDebug( "Entering Connect", NONSENSITIVE );
559487
560488 if ( !function_exists( 'ldap_connect' ) ) {
561 - $this->printDebug( "It looks like you are missing LDAP support; please ensure you have either compiled LDAP support in, or have enabled the module. If the authentication is working for you, the plugin isn't properly detecting the LDAP module, and you can safely ignore this message.", NONSENSITIVE );
 489+ $this->printDebug( "It looks like you are missing LDAP support; please ensure you have either compiled LDAP "
 490+ . "support in, or have enabled the module. If the authentication is working for you, the plugin isn't properly "
 491+ . "detecting the LDAP module, and you can safely ignore this message.", NONSENSITIVE );
562492 return false;
563493 }
564494

Follow-up revisions

RevisionCommit summaryAuthorDate
r107434Add non-session based configuration lookups, based on domain. Followup to r10...laner21:42, 27 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107418Added/update some documentation...reedy20:47, 27 December 2011

Status & tagging log