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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\UsersManager;
use Exception;
use Piwik\Access\Role\Admin;
use Piwik\Access\Role\Write;
use Piwik\API\Request;
use Piwik\Auth\Password;
use Piwik\Common;
use Piwik\Config;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\CoreHome\SystemSummary;
use Piwik\Plugins\CorePluginsAdmin\CorePluginsAdmin;
use Piwik\SettingsPiwik;
/**
* Manage Piwik users
*
*/
class UsersManager extends \Piwik\Plugin
{
const PASSWORD_MIN_LENGTH = 6;
const PASSWORD_MAX_LENGTH = 200;
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return array(
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'SitesManager.deleteSite.end' => 'deleteSite',
'Tracker.Cache.getSiteAttributes' => 'recordAdminUsersInCache',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'Platform.initialized' => 'onPlatformInitialized',
'System.addSystemSummaryItems' => 'addSystemSummaryItems',
);
}
public static function isUsersAdminEnabled()
{
return (bool) Config::getInstance()->General['enable_users_admin'];
}
public static function dieIfUsersAdminIsDisabled()
{
Piwik::checkUserIsNotAnonymous();
if (!self::isUsersAdminEnabled()) {
throw new \Exception('Creating, updating, and deleting users has been disabled.');
}
}
public function addSystemSummaryItems(&$systemSummary)
{
if (!self::isUsersAdminEnabled()) {
return;
}
$userLogins = Request::processRequest('UsersManager.getUsersLogin', array('filter_limit' => '-1'));
$numUsers = count($userLogins);
if (in_array('anonymous', $userLogins)) {
$numUsers--;
}
$systemSummary[] = new SystemSummary\Item($key = 'users', Piwik::translate('General_NUsers', $numUsers), $value = null, array('module' => 'UsersManager', 'action' => 'index'), $icon = 'icon-user', $order = 5);
}
public function onPlatformInitialized()
{
$lastSeenTimeLogger = new LastSeenTimeLogger();
$lastSeenTimeLogger->logCurrentUserLastSeenTime();
}
/**
* Hooks when a website tracker cache is flushed (website/user updated, cache deleted, or empty cache)
* Will record in the tracker config file the list of Admin token_auth for this website. This
* will be used when the Tracking API is used with setIp(), setForceDateTime(), setVisitorId(), etc.
*
* @param $attributes
* @param $idSite
* @return void
*/
public function recordAdminUsersInCache(&$attributes, $idSite)
{
$model = new Model();
$logins = $model->getUsersLoginWithSiteAccess($idSite, Admin::ID);
$writeLogins = $model->getUsersLoginWithSiteAccess($idSite, Write::ID);
$logins = array_merge($logins, $writeLogins);
$token_auths = $model->getAllHashedTokensForLogins($logins);
$attributes['tracking_token_auth'] = array();
if (!empty($token_auths)) {
foreach ($token_auths as $token_auth) {
$attributes['tracking_token_auth'][] = self::hashTrackingToken($token_auth, $idSite);
}
}
}
public static function hashTrackingToken($tokenAuth, $idSite)
{
return sha1($idSite . $tokenAuth . SettingsPiwik::getSalt());
}
/**
* Delete user preferences associated with a particular site
*/
public function deleteSite($idSite)
{
Option::deleteLike('%\_' . API::PREFERENCE_DEFAULT_REPORT, $idSite);
}
/**
* Return list of plug-in specific JavaScript files to be imported by the asset manager
*
* @see \Piwik\AssetManager
*/
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = "plugins/UsersManager/angularjs/users-manager/users-manager.component.js";
$jsFiles[] = "plugins/UsersManager/angularjs/paged-users-list/paged-users-list.component.js";
$jsFiles[] = "plugins/UsersManager/angularjs/user-edit-form/user-edit-form.component.js";
$jsFiles[] = "plugins/UsersManager/angularjs/user-permissions-edit/user-permissions-edit.component.js";
$jsFiles[] = "plugins/UsersManager/angularjs/personal-settings/personal-settings.controller.js";
$jsFiles[] = "plugins/UsersManager/angularjs/personal-settings/anonymous-settings.controller.js";
$jsFiles[] = "plugins/UsersManager/angularjs/permissions-metadata/permissions-metadata.service.js";
$jsFiles[] = "plugins/UsersManager/angularjs/capabilities-edit/capabilities-edit.component.js";
}
/**
* Get CSS files
*/
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/UsersManager/stylesheets/usersManager.less";
$stylesheets[] = "plugins/UsersManager/angularjs/users-manager/users-manager.component.less";
$stylesheets[] = "plugins/UsersManager/angularjs/paged-users-list/paged-users-list.component.less";
$stylesheets[] = "plugins/UsersManager/angularjs/user-edit-form/user-edit-form.component.less";
$stylesheets[] = "plugins/UsersManager/angularjs/user-permissions-edit/user-permissions-edit.component.less";
$stylesheets[] = "plugins/UsersManager/angularjs/capabilities-edit/capabilities-edit.component.less";
}
/**
* Returns true if the password is complex enough (at least 6 characters and max 26 characters)
*
* @param $input string
* @return bool
*/
public static function isValidPasswordString($input)
{
if (!SettingsPiwik::isUserCredentialsSanityCheckEnabled()
&& !empty($input)
) {
return true;
}
$l = strlen($input);
return $l >= self::PASSWORD_MIN_LENGTH;
}
public static function checkPassword($password)
{
/**
* Triggered before core password validator check password.
*
* This event exists for enable option to create custom password validation rules.
* It can be used to validate password (length, used chars etc) and to notify about checking password.
*
* **Example**
*
* Piwik::addAction('UsersManager.checkPassword', function ($password) {
* if (strlen($password) < 10) {
* throw new Exception('Password is too short.');
* }
* });
*
* @param string $password Checking password in plain text.
*/
Piwik::postEvent('UsersManager.checkPassword', array($password));
if (!self::isValidPasswordString($password)) {
throw new Exception(Piwik::translate('UsersManager_ExceptionInvalidPassword', array(self::PASSWORD_MIN_LENGTH)));
}
if (mb_strlen($password) > self::PASSWORD_MAX_LENGTH) {
throw new Exception(Piwik::translate('UsersManager_ExceptionInvalidPasswordTooLong', array(self::PASSWORD_MAX_LENGTH)));
}
}
public static function getPasswordHash($password)
{
if (SettingsPiwik::isUserCredentialsSanityCheckEnabled()) {
self::checkBasicPasswordStrength($password);
}
// if change here, should also edit the installation process
// to change how the root pwd is saved in the config file
return md5($password);
}
public static function checkBasicPasswordStrength($password)
{
$ex = new \Exception('This password is too weak, please supply another value or reset it.');
$numDistinctCharacters = strlen(count_chars($password, 3));
if ($numDistinctCharacters < 2) {
throw $ex;
}
if (strlen($password) < 6) {
throw $ex;
}
}
/**
* Checks the password hash length. Used as a sanity check.
*
* @param string $passwordHash The password hash to check.
* @param string $exceptionMessage Message of the exception thrown.
* @throws Exception if the password hash length is incorrect.
*/
public static function checkPasswordHash($passwordHash, $exceptionMessage)
{
if (strlen($passwordHash) != 32 || !ctype_xdigit($passwordHash)) { // MD5 hash length
throw new Exception($exceptionMessage);
}
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = "General_OrCancel";
$translationKeys[] = "General_Save";
$translationKeys[] = "General_Done";
$translationKeys[] = "General_Pagination";
$translationKeys[] = "General_PleaseTryAgain";
$translationKeys[] = "General_Username";
$translationKeys[] = "General_Password";
$translationKeys[] = "UsersManager_DeleteConfirm";
$translationKeys[] = "UsersManager_ConfirmGrantSuperUserAccess";
$translationKeys[] = "UsersManager_ConfirmProhibitOtherUsersSuperUserAccess";
$translationKeys[] = "UsersManager_ConfirmProhibitMySuperUserAccess";
$translationKeys[] = "UsersManager_ExceptionUserHasViewAccessAlready";
$translationKeys[] = "UsersManager_ExceptionNoValueForUsernameOrEmail";
$translationKeys[] = "UsersManager_GiveUserAccess";
$translationKeys[] = "UsersManager_PrivAdmin";
$translationKeys[] = "UsersManager_PrivView";
$translationKeys[] = "UsersManager_RemoveUserAccess";
$translationKeys[] = "UsersManager_ConfirmWithPassword";
$translationKeys[] = "UsersManager_YourCurrentPassword";
$translationKeys[] = "UsersManager_UserHasPermission";
$translationKeys[] = "UsersManager_UserHasNoPermission";
$translationKeys[] = "UsersManager_PrivNone";
$translationKeys[] = "UsersManager_ManageUsers";
$translationKeys[] = "UsersManager_ManageUsersDesc";
$translationKeys[] = 'Mobile_NavigationBack';
$translationKeys[] = 'UsersManager_AddExistingUser';
$translationKeys[] = 'UsersManager_AddUser';
$translationKeys[] = 'UsersManager_EnterUsernameOrEmail';
$translationKeys[] = 'UsersManager_NoAccessWarning';
$translationKeys[] = 'UsersManager_BulkActions';
$translationKeys[] = 'UsersManager_SetPermission';
$translationKeys[] = 'UsersManager_RolesHelp';
$translationKeys[] = 'UsersManager_Role';
$translationKeys[] = 'UsersManager_2FA';
$translationKeys[] = 'UsersManager_UsesTwoFactorAuthentication';
$translationKeys[] = 'General_Actions';
$translationKeys[] = 'UsersManager_TheDisplayedWebsitesAreSelected';
$translationKeys[] = 'UsersManager_ClickToSelectAll';
$translationKeys[] = 'UsersManager_AllWebsitesAreSelected';
$translationKeys[] = 'UsersManager_ClickToSelectDisplayedWebsites';
$translationKeys[] = 'UsersManager_DeletePermConfirmSingle';
$translationKeys[] = 'UsersManager_DeletePermConfirmMultiple';
$translationKeys[] = 'UsersManager_ChangePermToSiteConfirmSingle';
$translationKeys[] = 'UsersManager_ChangePermToSiteConfirmMultiple';
$translationKeys[] = 'UsersManager_BasicInformation';
$translationKeys[] = 'UsersManager_Permissions';
$translationKeys[] = 'UsersManager_RemovePermissions';
$translationKeys[] = 'UsersManager_FirstSiteInlineHelp';
$translationKeys[] = 'UsersManager_SuperUsersPermissionsNotice';
$translationKeys[] = 'UsersManager_SuperUserIntro1';
$translationKeys[] = 'UsersManager_SuperUserIntro2';
$translationKeys[] = 'UsersManager_HasSuperUserAccess';
$translationKeys[] = 'UsersManager_AreYouSure';
$translationKeys[] = 'UsersManager_RemoveSuperuserAccessConfirm';
$translationKeys[] = 'UsersManager_AddSuperuserAccessConfirm';
$translationKeys[] = 'UsersManager_UserSearch';
$translationKeys[] = 'UsersManager_DeleteUsers';
$translationKeys[] = 'UsersManager_FilterByAccess';
$translationKeys[] = 'UsersManager_Username';
$translationKeys[] = 'UsersManager_RoleFor';
$translationKeys[] = 'UsersManager_TheDisplayedUsersAreSelected';
$translationKeys[] = 'UsersManager_AllUsersAreSelected';
$translationKeys[] = 'UsersManager_ClickToSelectDisplayedUsers';
$translationKeys[] = 'UsersManager_DeleteUserConfirmSingle';
$translationKeys[] = 'UsersManager_DeleteUserConfirmMultiple';
$translationKeys[] = 'UsersManager_DeleteUserPermConfirmSingle';
$translationKeys[] = 'UsersManager_DeleteUserPermConfirmMultiple';
$translationKeys[] = 'UsersManager_ResetTwoFactorAuthentication';
$translationKeys[] = 'UsersManager_ResetTwoFactorAuthenticationInfo';
$translationKeys[] = 'UsersManager_TwoFactorAuthentication';
$translationKeys[] = 'UsersManager_AddNewUser';
$translationKeys[] = 'UsersManager_EditUser';
$translationKeys[] = 'UsersManager_CreateUser';
$translationKeys[] = 'UsersManager_SaveBasicInfo';
$translationKeys[] = 'UsersManager_Email';
$translationKeys[] = 'UsersManager_LastSeen';
$translationKeys[] = 'UsersManager_SuperUserAccess';
$translationKeys[] = 'UsersManager_AreYouSureChangeDetails';
$translationKeys[] = 'UsersManager_AnonymousUserRoleChangeWarning';
$translationKeys[] = 'General_Warning';
$translationKeys[] = 'General_Add';
$translationKeys[] = 'General_Note';
$translationKeys[] = 'General_Yes';
$translationKeys[] = 'UsersManager_FilterByWebsite';
$translationKeys[] = 'UsersManager_GiveAccessToAll';
$translationKeys[] = 'UsersManager_OrManageIndividually';
$translationKeys[] = 'UsersManager_ChangePermToAllSitesConfirm';
$translationKeys[] = 'UsersManager_ChangePermToAllSitesConfirm2';
$translationKeys[] = 'UsersManager_CapabilitiesHelp';
$translationKeys[] = 'UsersManager_Capabilities';
$translationKeys[] = 'UsersManager_AreYouSureAddCapability';
$translationKeys[] = 'UsersManager_AreYouSureRemoveCapability';
$translationKeys[] = 'UsersManager_IncludedInUsersRole';
$translationKeys[] = 'UsersManager_NewsletterSignupFailureMessage';
$translationKeys[] = 'UsersManager_NewsletterSignupSuccessMessage';
$translationKeys[] = 'UsersManager_FirstWebsitePermission';
}
}
|