ApiUserrights::getWebUITokenSalt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 *
6
 * Created on Mar 24, 2009
7
 *
8
 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License along
21
 * with this program; if not, write to the Free Software Foundation, Inc.,
22
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
 * http://www.gnu.org/copyleft/gpl.html
24
 *
25
 * @file
26
 */
27
28
/**
29
 * @ingroup API
30
 */
31
class ApiUserrights extends ApiBase {
32
33
	private $mUser = null;
34
35
	/**
36
	 * Get a UserrightsPage object, or subclass.
37
	 * @return UserrightsPage
38
	 */
39
	protected function getUserRightsPage() {
40
		return new UserrightsPage;
41
	}
42
43
	/**
44
	 * Get all available groups.
45
	 * @return array
46
	 */
47
	protected function getAllGroups() {
48
		return User::getAllGroups();
49
	}
50
51
	public function execute() {
52
		$pUser = $this->getUser();
53
54
		// Deny if the user is blocked and doesn't have the full 'userrights' permission.
55
		// This matches what Special:UserRights does for the web UI.
56
		if ( $pUser->isBlocked() && !$pUser->isAllowed( 'userrights' ) ) {
57
			$this->dieBlocked( $pUser->getBlock() );
0 ignored issues
show
Bug introduced by
It seems like $pUser->getBlock() can be null; however, dieBlocked() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
		}
59
60
		$params = $this->extractRequestParams();
61
62
		$user = $this->getUrUser( $params );
63
64
		$form = $this->getUserRightsPage();
65
		$form->setContext( $this->getContext() );
66
		$r['user'] = $user->getName();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$r was never initialized. Although not strictly required by PHP, it is generally a good practice to add $r = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
		$r['userid'] = $user->getId();
68
		list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
69
			$user, (array)$params['add'],
70
			(array)$params['remove'], $params['reason']
71
		);
72
73
		$result = $this->getResult();
74
		ApiResult::setIndexedTagName( $r['added'], 'group' );
75
		ApiResult::setIndexedTagName( $r['removed'], 'group' );
76
		$result->addValue( null, $this->getModuleName(), $r );
77
	}
78
79
	/**
80
	 * @param array $params
81
	 * @return User
82
	 */
83
	private function getUrUser( array $params ) {
84
		if ( $this->mUser !== null ) {
85
			return $this->mUser;
86
		}
87
88
		$this->requireOnlyOneParameter( $params, 'user', 'userid' );
89
90
		$user = isset( $params['user'] ) ? $params['user'] : '#' . $params['userid'];
91
92
		$form = $this->getUserRightsPage();
93
		$form->setContext( $this->getContext() );
94
		$status = $form->fetchUser( $user );
95
		if ( !$status->isOK() ) {
96
			$this->dieStatus( $status );
97
		}
98
99
		$this->mUser = $status->value;
100
101
		return $status->value;
102
	}
103
104
	public function mustBePosted() {
105
		return true;
106
	}
107
108
	public function isWriteMode() {
109
		return true;
110
	}
111
112
	public function getAllowedParams() {
113
		return [
114
			'user' => [
115
				ApiBase::PARAM_TYPE => 'user',
116
			],
117
			'userid' => [
118
				ApiBase::PARAM_TYPE => 'integer',
119
			],
120
			'add' => [
121
				ApiBase::PARAM_TYPE => $this->getAllGroups(),
122
				ApiBase::PARAM_ISMULTI => true
123
			],
124
			'remove' => [
125
				ApiBase::PARAM_TYPE => $this->getAllGroups(),
126
				ApiBase::PARAM_ISMULTI => true
127
			],
128
			'reason' => [
129
				ApiBase::PARAM_DFLT => ''
130
			],
131
			'token' => [
132
				// Standard definition automatically inserted
133
				ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
134
			],
135
		];
136
	}
137
138
	public function needsToken() {
139
		return 'userrights';
140
	}
141
142
	protected function getWebUITokenSalt( array $params ) {
143
		return $this->getUrUser( $params )->getName();
144
	}
145
146
	protected function getExamplesMessages() {
147
		return [
148
			'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
149
				=> 'apihelp-userrights-example-user',
150
			'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
151
				=> 'apihelp-userrights-example-userid',
152
		];
153
	}
154
155
	public function getHelpUrls() {
156
		return 'https://www.mediawiki.org/wiki/API:User_group_membership';
157
	}
158
}
159