Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / auth / UserDataAuthenticationRequest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Auth
20 */
21
22 namespace MediaWiki\Auth;
23
24 use MediaWiki\MediaWikiServices;
25 use StatusValue;
26 use User;
27
28 /**
29 * This represents additional user data requested on the account creation form
30 *
31 * @ingroup Auth
32 * @since 1.27
33 */
34 class UserDataAuthenticationRequest extends AuthenticationRequest {
35 /** @var string|null Email address */
36 public $email;
37
38 /** @var string|null Real name */
39 public $realname;
40
41 public function getFieldInfo() {
42 $config = MediaWikiServices::getInstance()->getMainConfig();
43 $ret = [
44 'email' => [
45 'type' => 'string',
46 'label' => wfMessage( 'authmanager-email-label' ),
47 'help' => wfMessage( 'authmanager-email-help' ),
48 'optional' => true,
49 ],
50 'realname' => [
51 'type' => 'string',
52 'label' => wfMessage( 'authmanager-realname-label' ),
53 'help' => wfMessage( 'authmanager-realname-help' ),
54 'optional' => true,
55 ],
56 ];
57
58 if ( !$config->get( 'EnableEmail' ) ) {
59 unset( $ret['email'] );
60 }
61
62 if ( in_array( 'realname', $config->get( 'HiddenPrefs' ), true ) ) {
63 unset( $ret['realname'] );
64 }
65
66 return $ret;
67 }
68
69 /**
70 * Add data to the User object
71 * @param User $user User being created (not added to the database yet).
72 * This may become a "UserValue" in the future, or User may be refactored
73 * into such.
74 * @return StatusValue
75 */
76 public function populateUser( $user ) {
77 if ( $this->email !== null && $this->email !== '' ) {
78 if ( !\Sanitizer::validateEmail( $this->email ) ) {
79 return StatusValue::newFatal( 'invalidemailaddress' );
80 }
81 $user->setEmail( $this->email );
82 }
83 if ( $this->realname !== null && $this->realname !== '' ) {
84 $user->setRealName( $this->realname );
85 }
86 return StatusValue::newGood();
87 }
88
89 }