1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Permissions;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
/**
* Users can authorize applications to use their account via OAuth. Grants are used to
* limit permissions for these application. This service allows application logic to
* access grants.
*
* @since 1.38
*/
class GrantsInfo {
/**
* Risk level classification for grants which aren't particularly risky. These grants might
* be abused, e.g. for vandalism, but the effect is easy to undo and the efficiency of abusing
* them isn't particularly different from registering new user accounts and using those for
* abuse.
* Note that risk levels depend on the use case; the default classification is meant for
* "normal" (public, open registration) wikis. Classification for e.g. a private wiki holding
* confidential information could be quite different.
*/
public const RISK_LOW = 'low';
/**
* Risk level classification for grants which can be used for disruptive vandalism or other
* kinds of abuse that couldn't be achieved just by registering new accounts, such as main
* page vandalism, vandalism of popular templates, page merge vandalism, or blocks.
*/
public const RISK_VANDALISM = 'vandalism';
/**
* Risk level classification for grants which can be used to cause damage that is hard or
* impossible to undo, such as exfiltrating sensitive private data or creating security
* vulnerabilities.
*/
public const RISK_SECURITY = 'security';
/**
* Risk level classification for grants which are used for internal purposes and should not
* be handed out.
*/
public const RISK_INTERNAL = 'internal';
/**
* @internal For use by ServiceWiring
*/
public const CONSTRUCTOR_OPTIONS = [
MainConfigNames::GrantPermissions,
MainConfigNames::GrantPermissionGroups,
MainConfigNames::GrantRiskGroups,
];
private ServiceOptions $options;
public function __construct(
ServiceOptions $options
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
}
/**
* List all known grants.
* @return string[]
*/
public function getValidGrants(): array {
return array_keys( $this->options->get( MainConfigNames::GrantPermissions ) );
}
/**
* Map all grants to corresponding user rights.
* @return string[][] grant => array of rights in the grant
*/
public function getRightsByGrant(): array {
$res = [];
foreach ( $this->options->get( MainConfigNames::GrantPermissions ) as $grant => $rights ) {
$res[$grant] = array_keys( array_filter( $rights ) );
}
return $res;
}
/**
* Fetch the rights allowed by a set of grants.
* @param string[]|string $grants
* @return string[]
*/
public function getGrantRights( $grants ): array {
$rights = [];
foreach ( (array)$grants as $grant ) {
if ( isset( $this->options->get( MainConfigNames::GrantPermissions )[$grant] ) ) {
$rights = array_merge(
$rights,
array_keys( array_filter( $this->options->get( MainConfigNames::GrantPermissions )[$grant] ) )
);
}
}
return array_unique( $rights );
}
/**
* Test that all grants in the list are known.
* @param string[] $grants
* @return bool
*/
public function grantsAreValid( array $grants ): bool {
return array_diff( $grants, $this->getValidGrants() ) === [];
}
/**
* Divide the grants into groups.
* @param string[]|null $grantsFilter
* @return string[][] Map of (group => (grant list))
*/
public function getGrantGroups( ?array $grantsFilter = null ): array {
if ( is_array( $grantsFilter ) ) {
$grantsFilter = array_fill_keys( $grantsFilter, true );
}
$groups = [];
foreach ( $this->options->get( MainConfigNames::GrantPermissions ) as $grant => $rights ) {
if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
continue;
}
if ( isset( $this->options->get( MainConfigNames::GrantPermissionGroups )[$grant] ) ) {
$groups[$this->options->get( MainConfigNames::GrantPermissionGroups )[$grant]][] = $grant;
} else {
$groups['other'][] = $grant;
}
}
return $groups;
}
/**
* Get the list of grants that are hidden and should always be granted.
* @return string[]
*/
public function getHiddenGrants(): array {
$grants = [];
foreach ( $this->options->get( MainConfigNames::GrantPermissionGroups ) as $grant => $group ) {
if ( $group === 'hidden' ) {
$grants[] = $grant;
}
}
return $grants;
}
/**
* Returns a map of grant name => risk group. The risk groups are the GrantsInfo::RISK_*
* constants, plus $default for grants where the risk level is not defined.
* @param string $default Default risk group to assign to grants for which no risk group
* is configured. $default does not have to be one of the RISK_* constants.
* @return string[]
* @since 1.42
*/
public function getRiskGroupsByGrant( string $default = 'unknown' ): array {
$res = [];
$grantRiskGroups = $this->options->get( MainConfigNames::GrantRiskGroups );
foreach ( $this->options->get( MainConfigNames::GrantPermissions ) as $grant => $_ ) {
$res[$grant] = $grantRiskGroups[$grant] ?? $default;
}
return $res;
}
}
|