r49328 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r49327‎ | r49328 | r49329 >
Date:03:56, 9 April 2009
Author:werdna
Status:deferred
Tags:
Comment:
* Validation and saving for email address and real name fields.
Modified paths:
  • /branches/preferences-work/phase3/includes/Preferences.php (modified) (history)
  • /branches/preferences-work/phase3/includes/specials/SpecialPreferences.php (modified) (history)

Diff [purge]

Index: branches/preferences-work/phase3/includes/specials/SpecialPreferences.php
@@ -6,9 +6,21 @@
77 }
88
99 function execute( $par ) {
10 - global $wgOut, $wgUser;
 10+ global $wgOut, $wgUser, $wgRequest;
1111
1212 $wgOut->setPageTitle( wfMsg( 'preferences' ) );
 13+ $wgOut->setArticleRelated( false );
 14+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 15+ $wgOut->addScriptFile( 'prefs.js' );
 16+
 17+ $wgOut->disallowUserJs(); # Prevent hijacked user scripts from sniffing passwords etc.
 18+
 19+ if ( $wgRequest->getCheck( 'success' ) ) {
 20+ $wgOut->wrapWikiMsg(
 21+ '<div class="successbox"><strong>$1</strong></div>',
 22+ 'savedprefs'
 23+ );
 24+ }
1325
1426 $formDescriptor = Preferences::getPreferences( $wgUser );
1527
@@ -22,7 +34,7 @@
2335 }
2436
2537 static function trySubmit( $formData ) {
26 - global $wgUser;
 38+ global $wgUser, $wgEmailAuthentication, $wgEnableEmail;
2739
2840 // Stuff that shouldn't be saved as a preference.
2941 $saveBlacklist = array(
@@ -30,6 +42,41 @@
3143 'emailaddress',
3244 );
3345
 46+ if( $wgEnableEmail ) {
 47+ $newadr = $formData['emailaddress'];
 48+ $oldadr = $wgUser->getEmail();
 49+ if( ($newadr != '') && ($newadr != $oldadr) ) {
 50+ # the user has supplied a new email address on the login page
 51+ # new behaviour: set this new emailaddr from login-page into user database record
 52+ $wgUser->setEmail( $newadr );
 53+ # but flag as "dirty" = unauthenticated
 54+ $wgUser->invalidateEmail();
 55+ if ($wgEmailAuthentication) {
 56+ # Mail a temporary password to the dirty address.
 57+ # User can come back through the confirmation URL to re-enable email.
 58+ $result = $wgUser->sendConfirmationMail();
 59+ if( WikiError::isError( $result ) ) {
 60+ return wfMsg( 'mailerror', htmlspecialchars( $result->getMessage() ) );
 61+ } else {
 62+ // TODO return this somehow
 63+# wfMsg( 'eauthentsent', $wgUser->getName() );
 64+ }
 65+ }
 66+ } else {
 67+ $wgUser->setEmail( $newadr );
 68+ }
 69+ if( $oldadr != $newadr ) {
 70+ wfRunHooks( 'PrefsEmailAudit', array( $wgUser, $oldadr, $newadr ) );
 71+ }
 72+ }
 73+
 74+ // Fortunately, the realname field is MUCH simpler
 75+ global $wgAllowRealName;
 76+ if ($wgAllowRealName) {
 77+ $realName = $formData['realname'];
 78+ $wgUser->setRealName( $realName );
 79+ }
 80+
3481 foreach( $saveBlacklist as $b )
3582 unset( $formData[$b] );
3683
@@ -39,6 +86,10 @@
4087
4188 $wgUser->saveSettings();
4289
 90+ // Done
 91+ global $wgOut;
 92+ $wgOut->redirect( SpecialPage::getTitleFor( 'Preferences' )->getFullURL( 'success' ) );
 93+
4394 return true;
4495 }
4596 }
Index: branches/preferences-work/phase3/includes/Preferences.php
@@ -70,14 +70,17 @@
7171 }
7272
7373 // Actually changeable stuff
74 - $defaultPreferences['realname'] =
75 - array(
76 - 'type' => 'text',
77 - 'default' => $user->getRealName(),
78 - 'section' => 'user',
79 - 'label-message' => 'yourrealname',
80 - 'help-message' => 'prefs-help-realname',
81 - );
 74+ global $wgAllowRealName;
 75+ if ($wgAllowRealName) {
 76+ $defaultPreferences['realname'] =
 77+ array(
 78+ 'type' => 'text',
 79+ 'default' => $user->getRealName(),
 80+ 'section' => 'user',
 81+ 'label-message' => 'yourrealname',
 82+ 'help-message' => 'prefs-help-realname',
 83+ );
 84+ }
8285
8386 global $wgEmailConfirmToEdit;
8487
@@ -90,8 +93,26 @@
9194 'help-message' => $wgEmailConfirmToEdit
9295 ? 'prefs-help-email-required'
9396 : 'prefs-help-email',
 97+ 'validation-callback' => array( 'Preferences', 'validateEmail' ),
9498 );
9599
 100+ global $wgAuth;
 101+ if ($wgAuth->allowPasswordChange()) {
 102+ global $wgUser; // For skin.
 103+ $link = $wgUser->getSkin()->link( SpecialPage::getTitleFor( 'ResetPass' ),
 104+ wfMsgHtml( 'prefs-resetpass' ), array() ,
 105+ array('returnto' => SpecialPage::getTitleFor( 'Preferences') ) );
 106+
 107+ $defaultPreferences['password'] =
 108+ array(
 109+ 'type' => 'info',
 110+ 'raw' => true,
 111+ 'default' => $link,
 112+ 'label-message' => 'yourpassword',
 113+ 'section' => 'user',
 114+ );
 115+ }
 116+
96117 $defaultPreferences['gender'] =
97118 array(
98119 'type' => 'select',
@@ -795,4 +816,17 @@
796817
797818 return $signature;
798819 }
 820+
 821+ static function validateEmail( $email, $alldata ) {
 822+ global $wgUser; // To check
 823+ if ( !$wgUser->isValidEmailAddr( $email ) ) {
 824+ return wfMsgExt( 'invalidemailaddress', 'parseinline' );
 825+ }
 826+
 827+ global $wgEmailConfirmToEdit;
 828+ if( $wgEmailConfirmToEdit && !$email ) {
 829+ return wfMsgExt( 'noemailtitle', 'parseinline' );
 830+ }
 831+ return true;
 832+ }
799833 }

Status & tagging log