/
AddHostAction.php
135 lines (118 loc) · 4.8 KB
/
AddHostAction.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2020 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Module\Application\Admin\Access;
use Ampache\Config\ConfigContainerInterface;
use Ampache\Module\Application\ApplicationActionInterface;
use Ampache\Module\Application\Exception\AccessDeniedException;
use Ampache\Module\Authorization\AccessLevelEnum;
use Ampache\Module\Authorization\AccessListManagerInterface;
use Ampache\Module\Authorization\Exception\InvalidEndIpException;
use Ampache\Module\Authorization\Exception\InvalidIpRangeException;
use Ampache\Module\Authorization\Exception\InvalidStartIpException;
use Ampache\Module\Authorization\GuiGatekeeperInterface;
use Ampache\Module\System\AmpError;
use Ampache\Module\System\Core;
use Ampache\Module\System\LegacyLogger;
use Ampache\Module\Util\UiInterface;
use PHPUnit\Framework\MockObject\DuplicateMethodException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
final class AddHostAction implements ApplicationActionInterface
{
public const REQUEST_KEY = 'add_host';
private UiInterface $ui;
private ConfigContainerInterface $configContainer;
private AccessListManagerInterface $accessListManager;
private LoggerInterface $logger;
public function __construct(
UiInterface $ui,
ConfigContainerInterface $configContainer,
AccessListManagerInterface $accessListManager,
LoggerInterface $logger
) {
$this->ui = $ui;
$this->configContainer = $configContainer;
$this->accessListManager = $accessListManager;
$this->logger = $logger;
}
public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
{
// Make sure we've got a valid form submission
if (
$gatekeeper->mayAccess(AccessLevelEnum::TYPE_INTERFACE, AccessLevelEnum::LEVEL_ADMIN) === false ||
!Core::form_verify('add_acl')
) {
throw new AccessDeniedException();
}
$this->ui->showHeader();
$data = $request->getParsedBody();
$startIp = $data['start'] ?? '';
$endIp = $data['end'] ?? '';
try {
$this->accessListManager->create(
$startIp,
$endIp,
$data['name'] ?? '',
(int) ($data['user'] ?? -1),
(int) $data['level'] ?? 0,
$data['type'] ?? '',
$data['addtype'] ?? ''
);
} catch (InvalidIpRangeException $e) {
AmpError::add('start', T_('IP Address version mismatch'));
AmpError::add('end', T_('IP Address version mismatch'));
} catch (InvalidStartIpException $e) {
AmpError::add('start', T_('An Invalid IPv4 / IPv6 Address was entered'));
} catch (InvalidEndIpException $e) {
AmpError::add('end', T_('An Invalid IPv4 / IPv6 Address was entered'));
} catch (DuplicateMethodException $e) {
$this->logger->critical(
'Error: An ACL entry equal to the created one already exists. Not adding duplicate: ' . $startIp . ' - ' . $endIp,
[LegacyLogger::CONTEXT_TYPE => __CLASS__]
);
AmpError::add('general', T_('Duplicate ACL entry defined'));
}
if (!AmpError::occurred()) {
$this->ui->showConfirmation(
T_('No Problem'),
T_('Your new Access Control List(s) have been created'),
sprintf(
'%s/admin/access.php',
$this->configContainer->getWebPath()
)
);
} else {
$this->ui->show(
'show_add_access.inc.php',
[
'action' => 'show_add_' . Core::get_post('type'),
'add_type' => 'add_host'
]
);
}
$this->ui->showQueryStats();
$this->ui->showFooter();
return null;
}
}