MediaWiki  1.28.0
SpecialChangeEmail.php
Go to the documentation of this file.
1 <?php
25 
35  private $status;
36 
37  public function __construct() {
38  parent::__construct( 'ChangeEmail', 'editmyprivateinfo' );
39  }
40 
41  public function doesWrites() {
42  return true;
43  }
44 
48  public function isListed() {
49  return AuthManager::singleton()->allowsPropertyChange( 'emailaddress' );
50  }
51 
56  function execute( $par ) {
57  $this->checkLoginSecurityLevel();
58 
59  $out = $this->getOutput();
60  $out->disallowUserJs();
61 
63  }
64 
65  protected function checkExecutePermissions( User $user ) {
66 
67  if ( !AuthManager::singleton()->allowsPropertyChange( 'emailaddress' ) ) {
68  throw new ErrorPageError( 'changeemail', 'cannotchangeemail' );
69  }
70 
71  $this->requireLogin( 'changeemail-no-info' );
72 
73  // This could also let someone check the current email address, so
74  // require both permissions.
75  if ( !$this->getUser()->isAllowed( 'viewmyprivateinfo' ) ) {
76  throw new PermissionsError( 'viewmyprivateinfo' );
77  }
78 
79  parent::checkExecutePermissions( $user );
80  }
81 
82  protected function getFormFields() {
83  $user = $this->getUser();
84 
85  $fields = [
86  'Name' => [
87  'type' => 'info',
88  'label-message' => 'username',
89  'default' => $user->getName(),
90  ],
91  'OldEmail' => [
92  'type' => 'info',
93  'label-message' => 'changeemail-oldemail',
94  'default' => $user->getEmail() ?: $this->msg( 'changeemail-none' )->text(),
95  ],
96  'NewEmail' => [
97  'type' => 'email',
98  'label-message' => 'changeemail-newemail',
99  'autofocus' => true,
100  'help-message' => 'changeemail-newemail-help',
101  ],
102  ];
103 
104  return $fields;
105  }
106 
107  protected function getDisplayFormat() {
108  return 'ooui';
109  }
110 
111  protected function alterForm( HTMLForm $form ) {
112  $form->setId( 'mw-changeemail-form' );
113  $form->setTableId( 'mw-changeemail-table' );
114  $form->setSubmitTextMsg( 'changeemail-submit' );
115  $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
116 
117  $form->addHeaderText( $this->msg( 'changeemail-header' )->parseAsBlock() );
118  }
119 
120  public function onSubmit( array $data ) {
121  $status = $this->attemptChange( $this->getUser(), $data['NewEmail'] );
122 
123  $this->status = $status;
124 
125  return $status;
126  }
127 
128  public function onSuccess() {
129  $request = $this->getRequest();
130 
131  $returnto = $request->getVal( 'returnto' );
132  $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
133  if ( !$titleObj instanceof Title ) {
134  $titleObj = Title::newMainPage();
135  }
136  $query = $request->getVal( 'returntoquery' );
137 
138  if ( $this->status->value === true ) {
139  $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
140  } elseif ( $this->status->value === 'eauth' ) {
141  # Notify user that a confirmation email has been sent...
142  $this->getOutput()->wrapWikiMsg( "<div class='error' style='clear: both;'>\n$1\n</div>",
143  'eauthentsent', $this->getUser()->getName() );
144  // just show the link to go back
145  $this->getOutput()->addReturnTo( $titleObj, wfCgiToArray( $query ) );
146  }
147  }
148 
154  private function attemptChange( User $user, $newaddr ) {
155  $authManager = AuthManager::singleton();
156 
157  if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
158  return Status::newFatal( 'invalidemailaddress' );
159  }
160 
161  if ( $newaddr === $user->getEmail() ) {
162  return Status::newFatal( 'changeemail-nochange' );
163  }
164 
165  $oldaddr = $user->getEmail();
166  $status = $user->setEmailWithConfirmation( $newaddr );
167  if ( !$status->isGood() ) {
168  return $status;
169  }
170 
171  Hooks::run( 'PrefsEmailAudit', [ $user, $oldaddr, $newaddr ] );
172 
173  $user->saveSettings();
174  MediaWiki\Auth\AuthManager::callLegacyAuthPlugin( 'updateExternalDB', [ $user ] );
175 
176  return $status;
177  }
178 
179  public function requiresUnblock() {
180  return false;
181  }
182 
183  protected function getGroupName() {
184  return 'users';
185  }
186 }
getEmail()
Get the user's e-mail address.
Definition: User.php:2647
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:802
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:1555
$batch execute()
saveSettings()
Save this user's settings into the database.
Definition: User.php:3847
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:556
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
setId($id)
Definition: HTMLForm.php:1480
static newFatal($message)
Factory function for fatal errors.
Definition: StatusValue.php:63
addHeaderText($msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:757
isGood()
Returns whether the operation completed and didn't have any error or warnings.
checkExecutePermissions(User $user)
msg()
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Let users change their email address.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
Special page which uses an HTMLForm to handle processing.
setEmailWithConfirmation($str)
Set the user's e-mail address and a confirmation mail if needed.
Definition: User.php:2684
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:911
attemptChange(User $user, $newaddr)
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.
setTableId($id)
Set the id of the \ or outermost \ element.
Definition: HTMLForm.php:1469
alterForm(HTMLForm $form)
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1348
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:128
checkLoginSecurityLevel($level=null)
Verifies that the user meets the security level, possibly reauthenticating them in the process...
static callLegacyAuthPlugin($method, array $params, $return=null)
Call a legacy AuthPlugin method, if necessary.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
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:2573
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.
Show an error when a user tries to do something they do not have the necessary permissions for...
execute($par)
Main execution point.
static validateEmail($addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1942
getRequest()
Get the WebRequest being used for this instance.