UserDataAuthenticationRequest::getFieldInfo()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 4
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
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 StatusValue;
25
use User;
26
27
/**
28
 * This represents additional user data requested on the account creation form
29
 *
30
 * @ingroup Auth
31
 * @since 1.27
32
 */
33
class UserDataAuthenticationRequest extends AuthenticationRequest {
34
	/** @var string|null Email address */
35
	public $email;
36
37
	/** @var string|null Real name */
38
	public $realname;
39
40
	public function getFieldInfo() {
41
		$config = \ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
42
		$ret = [
43
			'email' => [
44
				'type' => 'string',
45
				'label' => wfMessage( 'authmanager-email-label' ),
46
				'help' => wfMessage( 'authmanager-email-help' ),
47
				'optional' => true,
48
			],
49
			'realname' => [
50
				'type' => 'string',
51
				'label' => wfMessage( 'authmanager-realname-label' ),
52
				'help' => wfMessage( 'authmanager-realname-help' ),
53
				'optional' => true,
54
			],
55
		];
56
57
		if ( !$config->get( 'EnableEmail' ) ) {
58
			unset( $ret['email'] );
59
		}
60
61
		if ( in_array( 'realname', $config->get( 'HiddenPrefs' ), true ) ) {
62
			unset( $ret['realname'] );
63
		}
64
65
		return $ret;
66
	}
67
68
	/**
69
	 * Add data to the User object
70
	 * @param User $user User being created (not added to the database yet).
71
	 *   This may become a "UserValue" in the future, or User may be refactored
72
	 *   into such.
73
	 * @return StatusValue
74
	 */
75
	public function populateUser( $user ) {
76
		if ( $this->email !== null && $this->email !== '' ) {
77
			if ( !\Sanitizer::validateEmail( $this->email ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Sanitizer::validateEmail($this->email) of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
78
				return StatusValue::newFatal( 'invalidemailaddress' );
79
			}
80
			$user->setEmail( $this->email );
81
		}
82
		if ( $this->realname !== null && $this->realname !== '' ) {
83
			$user->setRealName( $this->realname );
84
		}
85
		return StatusValue::newGood();
86
	}
87
88
}
89