| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Permissions; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Block\Block; |
| 25 |
use MediaWiki\Page\PageIdentity; |
| 26 |
use MediaWiki\User\UserIdentity; |
| 27 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class SimpleAuthority implements Authority { |
| 39 |
|
| 40 |
|
| 41 |
private $actor; |
| 42 |
|
| 43 |
|
| 44 |
private $isTemp; |
| 45 |
|
| 46 |
|
| 47 |
private $permissions; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
UserIdentity $actor, |
| 57 |
array $permissions, |
| 58 |
bool $isTemp = false |
| 59 |
) { |
| 60 |
$this->actor = $actor; |
| 61 |
$this->isTemp = $isTemp; |
| 62 |
$this->permissions = array_fill_keys( $permissions, true ); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public function getUser(): UserIdentity { |
| 67 |
return $this->actor; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public function getBlock( int $freshness = IDBAccessObject::READ_NORMAL ): ?Block { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public function isAllowed( string $permission, ?PermissionStatus $status = null ): bool { |
| 77 |
return isset( $this->permissions[ $permission ] ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
public function isAllowedAny( ...$permissions ): bool { |
| 82 |
if ( !$permissions ) { |
| 83 |
throw new InvalidArgumentException( 'At least one permission must be specified' ); |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $permissions as $perm ) { |
| 87 |
if ( $this->isAllowed( $perm ) ) { |
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public function isAllowedAll( ...$permissions ): bool { |
| 97 |
if ( !$permissions ) { |
| 98 |
throw new InvalidArgumentException( 'At least one permission must be specified' ); |
| 99 |
} |
| 100 |
|
| 101 |
foreach ( $permissions as $perm ) { |
| 102 |
if ( !$this->isAllowed( $perm ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
private function checkPermission( string $permission, ?PermissionStatus $status ): bool { |
| 111 |
$ok = $this->isAllowed( $permission ); |
| 112 |
|
| 113 |
if ( !$ok && $status ) { |
| 114 |
|
| 115 |
$status->fatal( 'permissionserrors' ); |
| 116 |
$status->setPermission( $permission ); |
| 117 |
} |
| 118 |
|
| 119 |
return $ok; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public function probablyCan( |
| 124 |
string $action, |
| 125 |
PageIdentity $target, |
| 126 |
?PermissionStatus $status = null |
| 127 |
): bool { |
| 128 |
return $this->checkPermission( $action, $status ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public function definitelyCan( |
| 133 |
string $action, |
| 134 |
PageIdentity $target, |
| 135 |
?PermissionStatus $status = null |
| 136 |
): bool { |
| 137 |
return $this->checkPermission( $action, $status ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public function isDefinitelyAllowed( string $action, ?PermissionStatus $status = null ): bool { |
| 142 |
return $this->checkPermission( $action, $status ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public function authorizeAction( string $action, ?PermissionStatus $status = null ): bool { |
| 147 |
return $this->checkPermission( $action, $status ); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
public function authorizeRead( |
| 152 |
string $action, |
| 153 |
PageIdentity $target, |
| 154 |
?PermissionStatus $status = null |
| 155 |
): bool { |
| 156 |
return $this->checkPermission( $action, $status ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
public function authorizeWrite( |
| 161 |
string $action, |
| 162 |
PageIdentity $target, |
| 163 |
?PermissionStatus $status = null |
| 164 |
): bool { |
| 165 |
return $this->checkPermission( $action, $status ); |
| 166 |
} |
| 167 |
|
| 168 |
public function isRegistered(): bool { |
| 169 |
return $this->actor->isRegistered(); |
| 170 |
} |
| 171 |
|
| 172 |
public function isTemp(): bool { |
| 173 |
return $this->isTemp; |
| 174 |
} |
| 175 |
|
| 176 |
public function isNamed(): bool { |
| 177 |
return $this->isRegistered() && !$this->isTemp(); |
| 178 |
} |
| 179 |
} |