MediaWiki  master
SpecialChangeEmail.php
Go to the documentation of this file.
1 <?php
33  private $status;
34 
35  public function __construct() {
36  parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
37  }
38 
39  public function doesWrites() {
40  return true;
41  }
42 
46  public function isListed() {
48 
49  return $wgAuth->allowPropChange( 'emailaddress' );
50  }
51 
56  function execute( $par ) {
57  $out = $this->getOutput();
58  $out->disallowUserJs();
59 
61  }
62 
63  protected function checkExecutePermissions( User $user ) {
65 
66  if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
67  throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
68  }
69 
70  $this->requireLogin( 'changeemail-no-info' );
71 
72  // This could also let someone check the current email address, so
73  // require both permissions.
74  if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
75  throw new PermissionsError( 'viewmyprivateinfo' );
76  }
77 
78  parent::checkExecutePermissions( $user );
79  }
80 
81  protected function getFormFields() {
82  $user = $this->getUser();
83 
84  $fields = [
85  'Name' => [
86  'type' => 'info',
87  'label-message' => 'username',
88  'default' => $user->getName(),
89  ],
90  'OldEmail' => [
91  'type' => 'info',
92  'label-message' => 'changeemail-oldemail',
93  'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
94  ],
95  'NewEmail' => [
96  'type' => 'email',
97  'label-message' => 'changeemail-newemail',
98  'autofocus' => true,
99  'help-message' => 'changeemail-newemail-help',
100  ],
101  ];
102 
103  if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
104  $fields['Password'] = [
105  'type' => 'password',
106  'label-message' => 'changeemail-password'
107  ];
108  }
109 
110  return $fields;
111  }
112 
113  protected function getDisplayFormat() {
114  return 'ooui';
115  }
116 
117  protected function alterForm( HTMLForm $form ) {
118  $form->setId( 'mw-changeemail-form' );
119  $form->setTableId( 'mw-changeemail-table' );
120  $form->setSubmitTextMsg( 'changeemail-submit' );
121  $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
122 
123  $form->addHeaderText( $this->msg( 'changeemail-header' )->parseAsBlock() );
124  if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' ) ) {
125  $form->addHeaderText( $this->msg( 'changeemail-passwordrequired' )->parseAsBlock() );
126  }
127  }
128 
129  public function onSubmit( array $data ) {
130  $password = isset( $data['Password'] ) ? $data['Password'] : null;
131  $status = $this->attemptChange( $this->getUser(), $password, $data['NewEmail'] );
132 
133  $this->status = $status;
134 
135  return $status;
136  }
137 
138  public function onSuccess() {
139  $request = $this->getRequest();
140 
141  $returnto = $request->getVal( 'returnto' );
142  $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
143  if ( !$titleObj instanceof Title ) {
144  $titleObj = Title::newMainPage();
145  }
146  $query = $request->getVal( 'returntoquery' );
147 
148  if ( $this->status->value === true ) {
149  $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
150  } elseif ( $this->status->value === 'eauth' ) {
151  # Notify user that a confirmation email has been sent...
152  $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
153  'eauthentsent', $this->getUser()->getName() );
154  // just show the link to go back
155  $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
156  }
157  }
158 
165  private function attemptChange( User $user, $pass, $newaddr ) {
166  global $wgAuth;
167 
168  if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
169  return Status::newFatal( 'invalidemailaddress' );
170  }
171 
172  if ( $newaddr === $user->getEmail() ) {
173  return Status::newFatal( 'changeemail-nochange' );
174  }
175 
176  $throttleInfo = LoginForm::incrementLoginThrottle( $user->getName() );
177  if ( $throttleInfo ) {
178  $lang = $this->getLanguage();
179  return Status::newFatal(
180  'changeemail-throttled',
181  $lang->formatDuration( $throttleInfo['wait'] )
182  );
183  }
184 
185  if ( $this->getConfig()->get( 'RequirePasswordforEmailChange' )
186  && !$user->checkTemporaryPassword( $pass )
187  && !$user->checkPassword( $pass )
188  ) {
189  return Status::newFatal( 'wrongpassword' );
190  }
191 
192  LoginForm::clearLoginThrottle( $user->getName() );
193 
194  $oldaddr = $user->getEmail();
195  $status = $user->setEmailWithConfirmation( $newaddr );
196  if ( !$status->isGood() ) {
197  return $status;
198  }
199 
200  Hooks::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
201 
202  $user->saveSettings();
203 
204  $wgAuth->updateExternalDB( $user );
205 
206  return $status;
207  }
208 
209  public function requiresUnblock() {
210  return false;
211  }
212 
213  protected function getGroupName() {
214  return 'users';
215  }
216 }
getEmail()
Get the user's e-mail address.
Definition: User.php:2796
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:776
the array() calling protocol came about after MediaWiki 1.4rc1.
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1435
checkPassword($password)
Check to see if the given clear-text password is one of the accepted passwords.
Definition: User.php:4284
$batch execute()
saveSettings()
Save this user's settings into the database.
Definition: User.php:3942
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:548
Let users change their email address.
setId($id)
Definition: HTMLForm.php:1435
if(!isset($args[0])) $lang
addHeaderText($msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:736
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
$wgAuth $wgAuth
Authentication plugin.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:256
Represents a title within MediaWiki.
Definition: Title.php:36
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
Special page which uses an HTMLForm to handle processing.
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2139
setEmailWithConfirmation($str)
Set the user's e-mail address and a confirmation mail if needed.
Definition: User.php:2833
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:890
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:47
wfCgiToArray($query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
An error page which can definitely be safely rendered using the OutputPage.
execute($par)
Main execution point.
setTableId($id)
Set the id of the \<table\> or outermost \<div\> element.
Definition: HTMLForm.php:1424
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1303
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
attemptChange(User $user, $pass, $newaddr)
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2458
getName()
Get the name of this Special Page.
requireLogin($reasonMsg= 'exception-nologin-text', $titleMsg= 'exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
getUser()
Shortcut to get the User executing this instance.
string $par
The sub-page of the special page.
getConfig()
Shortcut to get main config object.
Show an error when a user tries to do something they do not have the necessary permissions for...
getLanguage()
Shortcut to get user's language.
static validateEmail($addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1941
getRequest()
Get the WebRequest being used for this instance.
checkTemporaryPassword($plaintext)
Check if the given clear-text password matches the temporary password sent by e-mail for password res...
Definition: User.php:4377
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: Status.php:133