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