Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 9 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
AccountFilterMiddlewareBase | |
0.00% |
0 / 9 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
filterCore | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
filter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
0 | |||
handleBefore | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handleAfter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Middleware; |
6 | |
7 | use PeServer\App\Models\Data\SessionAccount; |
8 | use PeServer\App\Models\SessionKey; |
9 | use PeServer\Core\Http\HttpResponse; |
10 | use PeServer\Core\Http\HttpStatus; |
11 | use PeServer\Core\Mvc\Middleware\IMiddleware; |
12 | use PeServer\Core\Mvc\Middleware\MiddlewareArgument; |
13 | use PeServer\Core\Mvc\Middleware\MiddlewareResult; |
14 | |
15 | abstract class AccountFilterMiddlewareBase implements IMiddleware |
16 | { |
17 | /** |
18 | * アカウント用フィルタ処理。 |
19 | * |
20 | * @param MiddlewareArgument $argument |
21 | * @param string[] $levels |
22 | * @return MiddlewareResult |
23 | */ |
24 | protected function filterCore(MiddlewareArgument $argument, array $levels): MiddlewareResult |
25 | { |
26 | if (!$argument->stores->session->tryGet(SessionKey::ACCOUNT, $account)) { |
27 | return MiddlewareResult::error(HttpStatus::Forbidden); |
28 | } |
29 | |
30 | foreach ($levels as $level) { |
31 | /** @var SessionAccount $account */ |
32 | if ($account->level === $level) { |
33 | return MiddlewareResult::none(); |
34 | } |
35 | } |
36 | |
37 | return MiddlewareResult::error(HttpStatus::Forbidden); |
38 | } |
39 | |
40 | abstract protected function filter(MiddlewareArgument $argument): MiddlewareResult; |
41 | |
42 | //[IMiddleware] |
43 | |
44 | final public function handleBefore(MiddlewareArgument $argument): MiddlewareResult |
45 | { |
46 | return $this->filter($argument); |
47 | } |
48 | |
49 | final public function handleAfter(MiddlewareArgument $argument, HttpResponse $response): MiddlewareResult |
50 | { |
51 | return MiddlewareResult::none(); |
52 | } |
53 | } |