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=1);
24
25namespace Ampache\Module\User;
26
27use Ampache\Config\ConfigContainerInterface;
28use Ampache\Config\ConfigurationKeyEnum;
29use Ampache\Repository\Model\User;
30use Ampache\Module\Util\UtilityFactoryInterface;
31use Ampache\Repository\UserRepositoryInterface;
32
33/**
34 * Disables/Enables users
35 */
36final class UserStateToggler implements UserStateTogglerInterface
37{
38    private ConfigContainerInterface $configContainer;
39
40    private UtilityFactoryInterface $utilityFactory;
41
42    private UserRepositoryInterface $userRepository;
43
44    public function __construct(
45        ConfigContainerInterface $configContainer,
46        UtilityFactoryInterface $utilityFactory,
47        UserRepositoryInterface $userRepository
48    ) {
49        $this->configContainer = $configContainer;
50        $this->utilityFactory  = $utilityFactory;
51        $this->userRepository  = $userRepository;
52    }
53
54    public function enable(User $user): bool
55    {
56        $this->userRepository->enable($user->getId());
57
58        if ($this->configContainer->isFeatureEnabled(ConfigurationKeyEnum::USER_NO_EMAIL_CONFIRM) === false) {
59            $mailer = $this->utilityFactory->createMailer();
60            $mailer->set_default_sender();
61
62            /* HINT: Ampache site_title */
63            $mailer->subject = sprintf(
64                T_('Account enabled at %s'),
65                $this->configContainer->get(ConfigurationKeyEnum::SITE_TITLE)
66            );
67
68            /* HINT: Username */
69            $mailer->message = sprintf(T_('A new user has been enabled. %s'), $user->username) .
70                /* HINT: Ampache Login Page */"\n\n" .
71                sprintf(
72                    T_('You can log in at the following address %s'),
73                    $this->configContainer->getWebPath()
74                );
75            $mailer->recipient      = $user->email;
76            $mailer->recipient_name = $user->fullname;
77
78            $mailer->send();
79        }
80
81        return true;
82    }
83
84    public function disable(User $user): bool
85    {
86        return $user->disable();
87    }
88}
89