-
Notifications
You must be signed in to change notification settings - Fork 1
/
force2fausergroup.php
222 lines (195 loc) · 6.5 KB
/
force2fausergroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Force2faUsergroup Plugin
*
* @copyright Copyright (C) 2020 Tobias Zulauf All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
/**
* Plugin class for Fetch Metadata
*
* @since 1.0
*/
class PlgSystemForce2faUsergroup extends CMSPlugin
{
/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
* @since 1.0
*/
protected $autoloadLanguage = true;
/**
* Application object.
*
* @var CMSApplication
* @since 1.0
*/
protected $app;
/**
* Listener for the `onAfterInitialise` event
*
* @return void
*
* @since 1.0
*/
public function onAfterInitialise()
{
if ($this->isTwoFactorAuthenticationRequired())
{
$this->redirectIfTwoFactorAuthenticationRequired();
}
}
/**
* Method to catch user login and check whether this use has to setup 2fa
*
* @param array $options Array holding options (user, responseType)
*
* @return void
*
* @since 1.0.0
*/
public function onUserAfterLogin($options)
{
if ($this->isTwoFactorAuthenticationRequired())
{
$this->redirectIfTwoFactorAuthenticationRequired();
}
}
/**
* Checks if 2fa needs to be enforced
* if so returns true, else returns false
*
* Based on: https://github.com/joomla/joomla-cms/blob/4.0.0-beta3/libraries/src/Application/CMSApplication.php#L1163-L1173
*
* @return boolean
*
* @since 1.0.0
*
* @throws \Exception
*/
private function isTwoFactorAuthenticationRequired(): bool
{
$user = Factory::getUser();
if ($user->guest)
{
return false;
}
// Check session if user has set up 2fa
if ($this->app->getUserState('has2fa', false))
{
return false;
}
// If the user is not allowed to view the output then end here.
$forced2faGroups = (array) $this->params->get('force2fausergroups', []);
if (!empty($forced2faGroups))
{
$userGroups = (array) $user->get('groups', []);
if (!array_intersect($forced2faGroups, $userGroups))
{
return false;
}
}
if (!PluginHelper::isEnabled('twofactorauth'))
{
return false;
}
return !$this->hasUserConfiguredTwoFactorAuthentication();
}
/**
* Redirects user to his Two Factor Authentication setup page
*
* Based on: https://github.com/joomla/joomla-cms/blob/4.0.0-beta3/libraries/src/Application/CMSApplication.php#L1246-L1253
*
* @return void
*
* @since 1.0.0
*/
private function redirectIfTwoFactorAuthenticationRequired(): void
{
$option = (string) $this->app->input->get('option');
$task = (string) $this->app->input->get('task');
$view = (string) $this->app->input->get('view', null, 'string');
$layout = (string) $this->app->input->get('layout', null, 'string');
if ($this->app->isClient('site'))
{
// If user is already on edit profile screen or press update/apply button, do nothing to avoid infinite redirect
if (($option === 'com_users' && \in_array($task, ['profile.edit', 'profile.save', 'profile.apply', 'user.logout', 'user.menulogout'], true))
|| $option === 'com_users' && $view === 'profile' && $layout === 'edit')
{
return;
}
// Redirect to com_users profile edit
$this->app->enqueueMessage(Text::_('PLG_SYSTEM_FORCE2FAUSERGROUP_2FA_REDIRECT_MESSAGE'), 'notice');
$this->app->redirect('index.php?option=com_users&view=profile&layout=edit');
}
if ($option === 'com_admin' && \in_array($task, ['profile.edit', 'profile.save', 'profile.apply'], true)
|| ($option === 'com_admin' && $view === 'profile' && $layout === 'edit')
|| ($option === 'com_users' && \in_array($task, ['user.save', 'user.edit', 'user.apply', 'user.logout', 'user.menulogout'], true))
|| ($option === 'com_users' && $view === 'user' && $layout === 'edit')
|| ($option === 'com_login' && \in_array($task, ['save', 'edit', 'apply', 'logout', 'menulogout'], true)))
{
return;
}
$user = Factory::getUser();
// With 4.0.0 (https://github.com/joomla/joomla-cms/pull/32771) you are allowed to edit your user via com_users.
if (version_compare(JVERSION, '4.0.0', 'ge'))
{
// Redirect to com_users user edit
$this->app->enqueueMessage(Text::_('PLG_SYSTEM_FORCE2FAUSERGROUP_2FA_REDIRECT_MESSAGE'), 'notice');
$this->app->redirect('index.php?option=com_users&task=user.edit&id=' . $user->id);
}
// With 3.9.22 (https://github.com/joomla/joomla-cms/pull/30751) and 4.0.0 you can configure the 2FA options from your com_admin profile
if (version_compare(JVERSION, '3.9.22', 'ge'))
{
// Redirect to com_admin profile edit
$this->app->enqueueMessage(Text::_('PLG_SYSTEM_FORCE2FAUSERGROUP_2FA_REDIRECT_MESSAGE'), 'notice');
$this->app->redirect('index.php?option=com_admin&task=profile.edit&id=' . $user->id);
}
// Check whether the current user is allowed to edit his user via com_users as in 3.9 you can not edit 2fa in the backend.
if (($user->authorise('core.edit', 'com_users') && $user->authorise('core.manage', 'com_users'))
|| $user->authorise('core.admin')
)
{
// Redirect to com_users user edit
$this->app->enqueueMessage(Text::_('PLG_SYSTEM_FORCE2FAUSERGROUP_2FA_REDIRECT_MESSAGE'), 'notice');
$this->app->redirect('index.php?option=com_users&task=user.edit&id=' . $user->id);
}
// We are a user that is allowed to login to the backend but not to access com_users
// and we are not yet at a version that supports 2FA edit in com_admin or com_users. Redirect to the frontend.
$this->app->enqueueMessage(Text::_('PLG_SYSTEM_FORCE2FAUSERGROUP_2FA_REDIRECT_MESSAGE'), 'notice');
$this->app->redirect(JUri::root() . 'index.php?option=com_users&view=profile&layout=edit');
}
/**
* Checks if otpKey and otep for the user are not empty
* if any one is empty returns false, else returns true
*
* Based on: https://github.com/joomla/joomla-cms/blob/4.0.0-beta3/libraries/src/Application/CMSApplication.php#L1288-L1298
*
* @return boolean
*
* @since 1.0.0
*
* @throws \Exception
*/
private function hasUserConfiguredTwoFactorAuthentication(): bool
{
$user = Factory::getUser();
// Check whether there is a 2FA setup for that user
if (empty($user->otpKey) || empty($user->otep))
{
return false;
}
// Set session to user has configured 2fa
$this->app->setUserState('has2fa', true);
return true;
}
}