Completed
Pull Request — master (#17)
by Paul
01:22
created

main_controller::submit()   D

Complexity

Conditions 15
Paths 59

Size

Total Lines 102

Duplication

Lines 14
Ratio 13.73 %

Importance

Changes 0
Metric Value
dl 14
loc 102
rs 4.7333
c 0
b 0
f 0
cc 15
nc 59
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
*
4
* 2FA extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2015 Paul Sohier
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace paul999\tfa\controller;
12
13
use paul999\tfa\helper\session_helper_interface;
14
use phpbb\config\config;
15
use phpbb\controller\helper;
16
use phpbb\db\driver\driver_interface;
17
use phpbb\exception\http_exception;
18
use phpbb\request\request_interface;
19
use phpbb\template\template;
20
use phpbb\user;
21
use phpbb\log\log;
22
23
/**
24
 * Controller
25
 */
26
class main_controller
27
{
28
	/**
29
	 * @var helper
30
	 */
31
	private $controller_helper;
32
33
	/**
34
	 * @var template
35
	 */
36
	private $template;
37
38
	/**
39
	 * @var driver_interface
40
	 */
41
	private $db;
42
43
	/**
44
	 * @var user
45
	 */
46
	private $user;
47
48
	/**
49
	 * @var request_interface
50
	 */
51
	private $request;
52
53
	/**
54
	 * @var config
55
	 */
56
	private $config;
57
58
	/**
59
	 * @var session_helper_interface
60
	 */
61
	private $session_helper;
62
63
	/**
64
	 * @var string
65
	 */
66
	private $root_path;
67
68
	/**
69
	 * @var string
70
	 */
71
	private $php_ext;
72
    /**
73
     * @var phpbb\log\log
74
     */
75
    private $log;
76
77
    /**
78
     * Constructor
79
     *
80
     * @access public
81
     * @param helper $controller_helper
82
     * @param driver_interface $db
83
     * @param template $template
84
     * @param user $user
85
     * @param request_interface $request
86
     * @param config $config
87
     * @param log $log
88
     * @param session_helper_interface $session_helper
89
     * @param string $root_path
90
     * @param string $php_ext
91
     */
92
	public function __construct(helper $controller_helper, driver_interface $db, template $template, user $user, request_interface $request, config $config, log $log, session_helper_interface $session_helper, $root_path, $php_ext)
93
	{
94
		$this->controller_helper 	= $controller_helper;
95
		$this->template 			= $template;
96
		$this->db					= $db;
97
		$this->user					= $user;
98
		$this->request				= $request;
99
		$this->config				= $config;
100
		$this->session_helper		= $session_helper;
101
		$this->root_path			= $root_path;
102
		$this->php_ext				= $php_ext;
103
        $this->log                  = $log;
0 ignored issues
show
Documentation Bug introduced by
It seems like $log of type object<phpbb\log\log> is incompatible with the declared type object<paul999\tfa\controller\phpbb\log\log> of property $log.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104
    }
105
106
	/**
107
	 * @param int  $user_id
108
	 * @param bool $admin
109
	 * @param bool $auto_login
110
	 * @param bool $viewonline
111
	 * @param string $class
112
	 * @return \Symfony\Component\HttpFoundation\Response
113
	 * @throws http_exception
114
	 */
115
	public function submit($user_id, $admin, $auto_login, $viewonline, $class)
116
	{
117
		$this->user->add_lang_ext('paul999/tfa', 'common');
118
119
		if (!check_form_key('tfa_login_page'))
120
		{
121
			throw new http_exception(403, 'FORM_INVALID');
122
		}
123
124
		if (empty($this->user->data['tfa_random']) || $user_id != $this->user->data['tfa_uid'])
125
		{
126
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
127
		}
128
		$random = $this->request->variable('random', '');
129
130
		if ($this->user->data['tfa_random'] !== $random || strlen($random) !== 40)
131
		{
132
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
133
		}
134
		$sql_ary = array(
135
			'tfa_random' => '',
136
			'tfa_uid'    => 0,
137
		);
138
		$sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . "
139
			WHERE
140
				session_id = '" . $this->db->sql_escape($this->user->data['session_id']) . "' AND
141
				session_user_id = " . (int) $this->user->data['user_id'];
142
		$this->db->sql_query($sql);
143
144
		if (empty($class))
145
		{
146
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
147
		}
148
149
		$module = $this->session_helper->findModule($class);
150
151
		if ($module == null)
152
		{
153
			throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
154
		}
155
156
		$redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}");
157
		try
158
		{
159 View Code Duplication
			if (!$module->login($user_id))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
			{
161
				$this->template->assign_var('S_ERROR', $this->user->lang('TFA_INCORRECT_KEY'));
162
				$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect);
163
			}
164
		}
165
		catch (http_exception $ex) // @TODO: Replace exception with own exception
0 ignored issues
show
Bug introduced by
The class phpbb\exception\http_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
166
		{
167
168
            $this->log->add('error', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', $ex->getMessage());
169
170
            if ($admin) {
171
                // Also log it to admin  log just to be sure.
172
                $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', $ex->getMessage());
173
            }
174 View Code Duplication
			if ($ex->getStatusCode() == 400)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
			{
176
				$this->template->assign_var('S_ERROR', $this->user->lang($ex->getMessage()));
177
				$this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect);
178
			}
179
			else
180
			{
181
				throw $ex;
182
			}
183
		}
184
185
		$old_session_id = $this->user->session_id;
186
187
		if ($admin)
188
		{
189
			$cookie_expire = time() - 31536000;
190
			$this->user->set_cookie('u', '', $cookie_expire);
191
			$this->user->set_cookie('sid', '', $cookie_expire);
192
		}
193
194
		$result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline);
195
196
		// Successful session creation
197
		if ($result === true)
198
		{
199
			// If admin re-authentication we remove the old session entry because a new one has been created...
200
			if ($admin)
201
			{
202
				// the login array is used because the user ids do not differ for re-authentication
203
				$sql = 'DELETE FROM ' . SESSIONS_TABLE . "
204
					WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "'
205
					AND session_user_id = " . (int) $user_id;
206
				$this->db->sql_query($sql);
207
208
                $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ADMIN_AUTH_SUCCESS');
209
210
				redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id']));
211
			}
212
213
			redirect(append_sid($redirect, false, true, $this->user->data['session_id']));
214
		}
215
		throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG');
216
	}
217
}
218