1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Biurad\Security; |
20
|
|
|
|
21
|
|
|
use Biurad\Http\ServerRequest; |
22
|
|
|
use Biurad\Security\Interfaces\AccessMapInterface; |
23
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
24
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcherInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AccessMap allows configuration of different access control rules for |
28
|
|
|
* specific parts of the website. |
29
|
|
|
* |
30
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class AccessMap implements AccessMapInterface |
33
|
|
|
{ |
34
|
|
|
/** @var array<int,mixed> */ |
35
|
|
|
private array $map = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $attributes An array of attributes to pass to the access decision manager (like roles) |
39
|
|
|
* @param string|null $channel The channel to enforce (http, https, or null) |
40
|
|
|
*/ |
41
|
|
|
public function add(RequestMatcherInterface $requestMatcher, array $attributes = [], string $channel = null): void |
42
|
|
|
{ |
43
|
|
|
$this->map[] = [$requestMatcher, $attributes, $channel]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getPatterns(ServerRequestInterface $request): array |
50
|
|
|
{ |
51
|
|
|
if (!$request instanceof ServerRequest) { |
52
|
|
|
throw new \InvalidArgumentException(\sprintf('The request must be an instance of %s.', ServerRequest::class)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!empty($this->map)) { |
56
|
|
|
$request = $request->getRequest(); |
57
|
|
|
|
58
|
|
|
foreach ($this->map as [$requestMatcher, $attributes, $channel]) { |
59
|
|
|
if ($requestMatcher->matches($request)) { |
60
|
|
|
return [$attributes, $channel]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return [null, null]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|