MediaWiki  1.29.2
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->getFullUrlForRedirect( $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 }
SpecialChangeEmail\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.
Definition: SpecialChangeEmail.php:179
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:265
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
SpecialChangeEmail\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialChangeEmail.php:41
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:675
SpecialChangeEmail\attemptChange
attemptChange(User $user, $newaddr)
Definition: SpecialChangeEmail.php:154
HTMLForm\addHeaderText
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition: HTMLForm.php:764
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:559
SpecialChangeEmail\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialChangeEmail.php:128
SpecialChangeEmail\__construct
__construct()
Definition: SpecialChangeEmail.php:37
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
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 account $user
Definition: hooks.txt:246
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
SpecialChangeEmail\checkExecutePermissions
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
Definition: SpecialChangeEmail.php:65
SpecialChangeEmail\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialChangeEmail.php:111
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:28
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:150
php
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
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
SpecialChangeEmail\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialChangeEmail.php:82
$query
null for the 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:1572
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:116
SpecialPage\checkLoginSecurityLevel
checkLoginSecurityLevel( $level=null)
Verifies that the user meets the security level, possibly reauthenticating them in the process.
Definition: SpecialPage.php:379
SpecialChangeEmail\$status
Status $status
Definition: SpecialChangeEmail.php:35
SpecialChangeEmail\execute
execute( $par)
Main execution point.
Definition: SpecialChangeEmail.php:56
SpecialChangeEmail\isListed
isListed()
Definition: SpecialChangeEmail.php:48
wfCgiToArray
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
Definition: GlobalFunctions.php:453
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:685
MediaWiki\Auth\AuthManager\callLegacyAuthPlugin
static callLegacyAuthPlugin( $method, array $params, $return=null)
Call a legacy AuthPlugin method, if necessary.
Definition: AuthManager.php:238
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:336
execute
$batch execute()
SpecialChangeEmail\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialChangeEmail.php:107
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:746
HTMLForm\setId
setId( $id)
Definition: HTMLForm.php:1491
FormSpecialPage\$par
string $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
SpecialChangeEmail
Let users change their email address.
Definition: SpecialChangeEmail.php:31
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:665
SpecialChangeEmail\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialChangeEmail.php:183
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:82
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1359
Title
Represents a title within MediaWiki.
Definition: Title.php:39
HTMLForm\addHiddenFields
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition: HTMLForm.php:918
SpecialChangeEmail\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialChangeEmail.php:120
HTMLForm\setTableId
setTableId( $id)
Set the id of the <table> or outermost <div> element.
Definition: HTMLForm.php:1480
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
array
the array() calling protocol came about after MediaWiki 1.4rc1.
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:128
$out
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:783