Banned::process()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 20
rs 9.9332
1
<?php
2
3
namespace kalanis\kw_auth\Methods;
4
5
6
use ArrayAccess;
7
use kalanis\kw_accounts\Interfaces\IAuth;
8
use kalanis\kw_auth\AuthException;
9
use kalanis\kw_auth\Interfaces\IKauTranslations;
10
use kalanis\kw_auth\Traits\TLang;
11
use kalanis\kw_bans\Bans;
12
use kalanis\kw_bans\BanException;
13
use kalanis\kw_bans\Interfaces\IKBTranslations;
14
use kalanis\kw_bans\Sources\File;
15
use kalanis\kw_paths\Path;
16
17
18
/**
19
 * Class Banned
20
 * @package kalanis\kw_auth\AuthMethods
21
 * Throws an exception if incoming user is banned
22
 * Just add it before any other method which try to log user in
23
 * @codeCoverageIgnore because access external content
24
 */
25
class Banned extends AMethods
26
{
27
    use TLang;
28
29
    protected const INPUT_NAME = 'name';
30
    protected const SERVER_REMOTE = 'REMOTE_ADDR';
31
32
    protected const BAN_IP4 = 'ban_ip4.txt';
33
    protected const BAN_IP6 = 'ban_ip6.txt';
34
    protected const BAN_NAME = 'ban_name.txt';
35
36
    protected const PREG_IP4 = '#^[0-9\./]$#i';
37
    protected const PREG_IP6 = '#^[0-9a-f:/]$#i';
38
    protected const PREG_NAME = '#^[\*\?\:;\\//]$#i';
39
40
    protected Path $libPath;
41
    protected Bans $libBan;
42
    /** @var ArrayAccess<string, string|int|float> */
43
    protected ArrayAccess $server;
44
45
    /**
46
     * @param IAuth|null $authenticator
47
     * @param AMethods|null $nextOne
48
     * @param Path $path
49
     * @param ArrayAccess<string, string|int|float> $server
50
     * @param IKauTranslations|null $kauLang
51
     * @param IKBTranslations|null $kbLang
52
     * @throws BanException
53
     */
54
    public function __construct(
55
        ?IAuth $authenticator,
56
        ?AMethods $nextOne,
57
        Path $path,
58
        ArrayAccess $server,
59
        ?IKauTranslations $kauLang = null,
60
        ?IKBTranslations $kbLang = null
61
    ) {
62
        parent::__construct($authenticator, $nextOne);
63
        $this->setAuLang($kauLang);
64
        $this->libPath = $path;
65
        $this->server = $server;
66
        $this->libBan = $this->getBans($kbLang);
67
    }
68
69
    /**
70
     * @param IKBTranslations|null $kbLang
71
     * @throws BanException
72
     * @return Bans
73
     */
74
    protected function getBans(?IKBTranslations $kbLang): Bans
75
    {
76
        $banPath = $this->getBanPath();
77
        return new Bans(
78
            $kbLang,
79
            new File($banPath . DIRECTORY_SEPARATOR . self::BAN_IP4),
80
            new File($banPath . DIRECTORY_SEPARATOR . self::BAN_IP6),
81
            new File($banPath . DIRECTORY_SEPARATOR . self::BAN_NAME)
82
        );
83
    }
84
85
    protected function getBanPath(): string
86
    {
87
        return $this->libPath->getDocumentRoot() . $this->libPath->getPathToSystemRoot() . DIRECTORY_SEPARATOR . 'conf';
88
    }
89
90
    public function process(\ArrayAccess $credentials): void
91
    {
92
        $name = $credentials->offsetExists(static::INPUT_NAME) ? strval($credentials->offsetGet(static::INPUT_NAME)) : '' ;
93
        $ip = strval($this->server->offsetGet(static::SERVER_REMOTE));
94
        try {
95
            if ($this->libBan->has(
96
                strval(preg_replace(static::PREG_IP4, '', $ip)),
97
                strval(preg_replace(static::PREG_IP6, '', $ip)),
98
                strval(preg_replace(static::PREG_NAME, '', $name))
99
            )) {
100
                throw new AuthException($this->getAuLang()->kauBanWantedUser(), 401);
101
            }
102
        } catch (BanException $ex) {
103
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
104
        }
105
    }
106
107
    public function remove(): void
108
    {
109
    }
110
}
111