1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25namespace Ampache\Module\Application\Admin\User;
26
27use Ampache\Config\ConfigContainerInterface;
28use Ampache\Config\ConfigurationKeyEnum;
29use Ampache\Repository\Model\ModelFactoryInterface;
30use Ampache\Module\Application\Exception\AccessDeniedException;
31use Ampache\Module\System\Core;
32use Ampache\Module\User\Authorization\UserAccessKeyGeneratorInterface;
33use Ampache\Module\Util\UiInterface;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36
37final class GenerateRsstokenAction extends AbstractUserAction
38{
39    public const REQUEST_KEY = 'generate_rsstoken';
40
41    private UiInterface $ui;
42
43    private ModelFactoryInterface $modelFactory;
44
45    private ConfigContainerInterface $configContainer;
46
47    private UserAccessKeyGeneratorInterface $userAccessKeyGenerator;
48
49    public function __construct(
50        UiInterface $ui,
51        ModelFactoryInterface $modelFactory,
52        ConfigContainerInterface $configContainer,
53        UserAccessKeyGeneratorInterface $userAccessKeyGenerator
54    ) {
55        $this->ui                     = $ui;
56        $this->modelFactory           = $modelFactory;
57        $this->configContainer        = $configContainer;
58        $this->userAccessKeyGenerator = $userAccessKeyGenerator;
59    }
60
61    protected function handle(ServerRequestInterface $request): ?ResponseInterface
62    {
63        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::DEMO_MODE) === true) {
64            return null;
65        }
66
67        if (!Core::form_verify('generate_rsstoken')) {
68            throw new AccessDeniedException();
69        }
70        $this->ui->showHeader();
71
72        $client = $this->modelFactory->createUser((int) Core::get_request('user_id'));
73
74        if ($client->id) {
75            $this->userAccessKeyGenerator->generateRssToken($client);
76        }
77
78        $this->ui->showConfirmation(
79            T_('No Problem'),
80            T_('A new user RSS token has been generated'),
81            sprintf('%s/admin/users.php', $this->configContainer->getWebPath())
82        );
83
84        $this->ui->showQueryStats();
85        $this->ui->showFooter();
86
87        return null;
88    }
89}
90