File: CompoundAuthorizer.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (51 lines) | stat: -rw-r--r-- 1,270 bytes parent folder | download
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
<?php

namespace MediaWiki\Rest\BasicAccess;

use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;

/**
 * Wraps an array of BasicAuthorizerInterface and checks them
 * all to authorize the request
 * @internal
 * @package MediaWiki\Rest\BasicAccess
 */
class CompoundAuthorizer implements BasicAuthorizerInterface {

	/** @var BasicAuthorizerInterface[] */
	private $authorizers;

	/**
	 * @param array $authorizers
	 */
	public function __construct( array $authorizers = [] ) {
		$this->authorizers = $authorizers;
	}

	/**
	 * Adds a BasicAuthorizerInterface to the chain of authorizers.
	 * @param BasicAuthorizerInterface $authorizer
	 * @return CompoundAuthorizer
	 */
	public function addAuthorizer( BasicAuthorizerInterface $authorizer ): CompoundAuthorizer {
		$this->authorizers[] = $authorizer;
		return $this;
	}

	/**
	 * Checks all registered authorizers and returns the first encountered error.
	 * @param RequestInterface $request
	 * @param Handler $handler
	 * @return string|null
	 */
	public function authorize( RequestInterface $request, Handler $handler ) {
		foreach ( $this->authorizers as $authorizer ) {
			$result = $authorizer->authorize( $request, $handler );
			if ( $result ) {
				return $result;
			}
		}
		return null;
	}
}