Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| BasicRequestAuthorizer | |
85.71% |
6 / 7 |
|
50.00% |
1 / 2 |
6.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| authorize | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
5.20 | |||
| isReadAllowed | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| isWriteAllowed | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\BasicAccess; |
| 4 | |
| 5 | use MediaWiki\Rest\Handler; |
| 6 | use MediaWiki\Rest\RequestInterface; |
| 7 | |
| 8 | /** |
| 9 | * A request authorizer which checks needsReadAccess() and needsWriteAccess() in the |
| 10 | * handler and calls isReadAllowed() and/or isWriteAllowed() in the subclass |
| 11 | * accordingly. |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | abstract class BasicRequestAuthorizer { |
| 16 | |
| 17 | protected RequestInterface $request; |
| 18 | protected Handler $handler; |
| 19 | |
| 20 | public function __construct( RequestInterface $request, Handler $handler ) { |
| 21 | $this->request = $request; |
| 22 | $this->handler = $handler; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @see BasicAuthorizerInterface::authorize() |
| 27 | * @return string|null If the request is denied, the string error code. If |
| 28 | * the request is allowed, null. |
| 29 | */ |
| 30 | public function authorize() { |
| 31 | if ( $this->handler->needsReadAccess() && !$this->isReadAllowed() ) { |
| 32 | return 'rest-read-denied'; |
| 33 | } |
| 34 | if ( $this->handler->needsWriteAccess() && !$this->isWriteAllowed() ) { |
| 35 | return 'rest-write-denied'; |
| 36 | } |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Check if the current user is allowed to read from the wiki |
| 42 | * |
| 43 | * @return bool |
| 44 | */ |
| 45 | abstract protected function isReadAllowed(); |
| 46 | |
| 47 | /** |
| 48 | * Check if the current user is allowed to write to the wiki |
| 49 | * |
| 50 | * @return bool |
| 51 | */ |
| 52 | abstract protected function isWriteAllowed(); |
| 53 | } |