MediaWiki REL1_28
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 ) {
58
59 $out = $this->getOutput();
60 $out->disallowUserJs();
61
62 parent::execute( $par );
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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.
Special page which uses an HTMLForm to handle processing.
string $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition HTMLForm.php:128
setTableId( $id)
Set the id of the <table> or outermost <div> element.
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setId( $id)
addHeaderText( $msg, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:757
addHiddenFields(array $fields)
Add an array of hidden fields to the output.
Definition HTMLForm.php:911
This serves as the entry point to the authentication system.
Show an error when a user tries to do something they do not have the necessary permissions for.
Let users change their email address.
doesWrites()
Indicates whether this special page may perform database writes.
requiresUnblock()
Whether this action cannot be executed by a blocked user.
getDisplayFormat()
Get display format for the form.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
getFormFields()
Get an HTMLForm descriptor array.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
attemptChange(User $user, $newaddr)
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
execute( $par)
Main execution point.
onSubmit(array $data)
Process the form on POST submission.
getName()
Get the name of this Special Page.
getOutput()
Get the OutputPage being used for this instance.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
checkLoginSecurityLevel( $level=null)
Verifies that the user meets the security level, possibly reauthenticating them in the process.
getUser()
Shortcut to get the User executing this instance.
getRequest()
Get the WebRequest being used for this instance.
msg()
Wrapper around wfMessage that sets the current context.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40
Represents a title within MediaWiki.
Definition Title.php:36
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
the array() calling protocol came about after MediaWiki 1.4rc1.
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:249
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2685
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:886
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:1595
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:37