Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
56 / 70
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
activate
80.00% covered (warning)
80.00%
56 / 70
50.00% covered (danger)
50.00%
2 / 4
19.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 execute
92.59% covered (success)
92.59%
25 / 27
0.00% covered (danger)
0.00%
0 / 1
12.06
 send_notification
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\console\command\user;
15
16use phpbb\config\config;
17use phpbb\console\command\command;
18use phpbb\language\language;
19use phpbb\log\log_interface;
20use phpbb\notification\manager;
21use phpbb\user;
22use phpbb\user_loader;
23use Symfony\Component\Console\Command\Command as symfony_command;
24use Symfony\Component\Console\Input\InputArgument;
25use Symfony\Component\Console\Input\InputInterface;
26use Symfony\Component\Console\Input\InputOption;
27use Symfony\Component\Console\Output\OutputInterface;
28use Symfony\Component\Console\Style\SymfonyStyle;
29
30class activate extends command
31{
32    /** @var config */
33    protected $config;
34
35    /** @var language */
36    protected $language;
37
38    /** @var log_interface */
39    protected $log;
40
41    /** @var manager */
42    protected $notifications;
43
44    /** @var user_loader */
45    protected $user_loader;
46
47    /**
48     * phpBB root path
49     *
50     * @var string
51     */
52    protected $phpbb_root_path;
53
54    /**
55     * PHP extension.
56     *
57     * @var string
58     */
59    protected $php_ext;
60
61    /**
62     * Construct method
63     *
64     * @param user             $user
65     * @param config           $config
66     * @param language         $language
67     * @param log_interface    $log
68     * @param manager          $notifications
69     * @param user_loader      $user_loader
70     * @param string           $phpbb_root_path
71     * @param string           $php_ext
72     */
73    public function __construct(user $user, config $config, language $language, log_interface $log, manager $notifications, user_loader $user_loader, $phpbb_root_path, $php_ext)
74    {
75        $this->config = $config;
76        $this->language = $language;
77        $this->log = $log;
78        $this->notifications = $notifications;
79        $this->user_loader = $user_loader;
80        $this->phpbb_root_path = $phpbb_root_path;
81        $this->php_ext = $php_ext;
82
83        $this->language->add_lang('acp/users');
84        parent::__construct($user);
85    }
86
87    /**
88     * Sets the command name and description
89     *
90     * @return void
91     */
92    protected function configure()
93    {
94        $this
95            ->setName('user:activate')
96            ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE'))
97            ->setHelp($this->language->lang('CLI_HELP_USER_ACTIVATE'))
98            ->addArgument(
99                'username',
100                InputArgument::REQUIRED,
101                $this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_USERNAME')
102            )
103            ->addOption(
104                'deactivate',
105                'd',
106                InputOption::VALUE_NONE,
107                $this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_DEACTIVATE')
108            )
109            ->addOption(
110                'send-email',
111                null,
112                InputOption::VALUE_NONE,
113                $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY')
114            )
115        ;
116    }
117
118    /**
119     * Executes the command user:activate
120     *
121     * Activate (or deactivate) a user account
122     *
123     * @param InputInterface  $input  The input stream used to get the options
124     * @param OutputInterface $output The output stream, used to print messages
125     *
126     * @return int 0 if all is well, 1 if any errors occurred
127     */
128    protected function execute(InputInterface $input, OutputInterface $output)
129    {
130        $io = new SymfonyStyle($input, $output);
131
132        $name = $input->getArgument('username');
133        $mode = ($input->getOption('deactivate')) ? 'deactivate' : 'activate';
134
135        $user_id  = $this->user_loader->load_user_by_username($name);
136        $user_row = $this->user_loader->get_user($user_id);
137
138        if ($user_row['user_id'] == ANONYMOUS)
139        {
140            $io->error($this->language->lang('NO_USER'));
141            return symfony_command::FAILURE;
142        }
143
144        // Check if the user is already active (or inactive)
145        if ($mode == 'activate' && $user_row['user_type'] != USER_INACTIVE)
146        {
147            $io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_ACTIVE'));
148            return symfony_command::FAILURE;
149        }
150        else if ($mode == 'deactivate' && $user_row['user_type'] == USER_INACTIVE)
151        {
152            $io->error($this->language->lang('CLI_DESCRIPTION_USER_ACTIVATE_INACTIVE'));
153            return symfony_command::FAILURE;
154        }
155
156        // Activate the user account
157        if (!function_exists('user_active_flip'))
158        {
159            require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
160        }
161
162        user_active_flip($mode, $user_row['user_id']);
163
164        // Notify the user upon activation
165        if ($mode == 'activate' && $this->config['require_activation'] == USER_ACTIVATION_ADMIN)
166        {
167            $this->send_notification($user_row, $input);
168        }
169
170        // Log and display the result
171        $msg = ($mode == 'activate') ? 'USER_ADMIN_ACTIVATED' : 'USER_ADMIN_DEACTIVED';
172        $log = ($mode == 'activate') ? 'LOG_USER_ACTIVE' : 'LOG_USER_INACTIVE';
173
174        $this->log->add('admin', ANONYMOUS, '', $log, false, array($user_row['username']));
175        $this->log->add('user', ANONYMOUS, '', $log . '_USER', false, array(
176            'reportee_id' => $user_row['user_id']
177        ));
178
179        $io->success($this->language->lang($msg));
180
181        return symfony_command::SUCCESS;
182    }
183
184    /**
185     * Send account activation notification to user
186     *
187     * @param array           $user_row The user data array
188     * @param InputInterface  $input    The input stream used to get the options
189     * @return void
190     */
191    protected function send_notification($user_row, InputInterface $input)
192    {
193        $this->notifications->delete_notifications('notification.type.admin_activate_user', $user_row['user_id']);
194
195        if ($input->getOption('send-email'))
196        {
197            if (!class_exists('messenger'))
198            {
199                require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
200            }
201
202            $messenger = new \messenger(false);
203            $messenger->template('admin_welcome_activated', $user_row['user_lang']);
204            $messenger->set_addresses($user_row);
205            $messenger->anti_abuse_headers($this->config, $this->user);
206            $messenger->assign_vars(array(
207                    'USERNAME'    => html_entity_decode($user_row['username'], ENT_COMPAT))
208            );
209
210            $messenger->send(NOTIFY_EMAIL);
211        }
212    }
213}