Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsgiehl <stefan@matomo.org>2020-07-24 14:11:05 +0300
committersgiehl <stefan@matomo.org>2020-07-24 15:28:48 +0300
commit679e73f1236969db0c2d767655cb84456a727d24 (patch)
tree648722fa79cb524f8819857e79163e0c1cf16d59 /plugins/UsersManager
parent6b5f8138180716d5088d764f0b41d5787159b28a (diff)
parent3e1234a887f56a1cf853e29ba89370b234af5127 (diff)
Merge branch '3.x-dev' into 4.x-dev
Diffstat (limited to 'plugins/UsersManager')
-rw-r--r--plugins/UsersManager/API.php32
-rw-r--r--plugins/UsersManager/config/config.php4
-rw-r--r--plugins/UsersManager/tests/Integration/APITest.php26
-rw-r--r--plugins/UsersManager/tests/Integration/UserPreferencesTest.php32
4 files changed, 78 insertions, 16 deletions
diff --git a/plugins/UsersManager/API.php b/plugins/UsersManager/API.php
index 5286973b84..1b64000e23 100644
--- a/plugins/UsersManager/API.php
+++ b/plugins/UsersManager/API.php
@@ -179,7 +179,13 @@ class API extends \Piwik\Plugin\API
}
/**
- * Sets a user preference
+ * Sets a user preference. Plugins can add custom preference names by declaring them in their plugin config/config.php
+ * like this:
+ *
+ * ```php
+ * return array('usersmanager.user_preference_names' => DI\add(array('preference_name_1', 'preference_name_2')));
+ * ```
+ *
* @param string $userLogin
* @param string $preferenceName
* @param string $preferenceValue
@@ -188,7 +194,17 @@ class API extends \Piwik\Plugin\API
public function setUserPreference($userLogin, $preferenceName, $preferenceValue)
{
Piwik::checkUserHasSuperUserAccessOrIsTheUser($userLogin);
- Option::set($this->getPreferenceId($userLogin, $preferenceName), $preferenceValue);
+
+ if (!$this->model->userExists($userLogin)) {
+ throw new Exception('User does not exist: ' . $userLogin);
+ }
+
+ if ($userLogin === 'anonymous') {
+ Piwik::checkUserHasSuperUserAccess();
+ }
+
+ $nameIfSupported = $this->getPreferenceId($userLogin, $preferenceName);
+ Option::set($nameIfSupported, $preferenceValue);
}
/**
@@ -267,6 +283,18 @@ class API extends \Piwik\Plugin\API
if(false !== strpos($preference, self::OPTION_NAME_PREFERENCE_SEPARATOR)) {
throw new Exception("Preference name cannot contain underscores.");
}
+ $names = array(
+ self::PREFERENCE_DEFAULT_REPORT,
+ self::PREFERENCE_DEFAULT_REPORT_DATE,
+ 'isLDAPUser', // used in loginldap
+ 'hideSegmentDefinitionChangeMessage',// used in JS
+ );
+ $customPreferences = StaticContainer::get('usersmanager.user_preference_names');
+
+ if (!in_array($preference, $names, true)
+ && !in_array($preference, $customPreferences, true)) {
+ throw new Exception('Not supported preference name: ' . $preference);
+ }
return $login . self::OPTION_NAME_PREFERENCE_SEPARATOR . $preference;
}
diff --git a/plugins/UsersManager/config/config.php b/plugins/UsersManager/config/config.php
index d266508bcd..4463e40397 100644
--- a/plugins/UsersManager/config/config.php
+++ b/plugins/UsersManager/config/config.php
@@ -1,2 +1,4 @@
<?php
-return array();
+return array(
+ 'usersmanager.user_preference_names' => []
+);
diff --git a/plugins/UsersManager/tests/Integration/APITest.php b/plugins/UsersManager/tests/Integration/APITest.php
index e69da89f95..dcc235a563 100644
--- a/plugins/UsersManager/tests/Integration/APITest.php
+++ b/plugins/UsersManager/tests/Integration/APITest.php
@@ -12,7 +12,6 @@ use Piwik\Access\Role\View;
use Piwik\Access\Role\Write;
use Piwik\Auth\Password;
use Piwik\Config;
-use Piwik\Container\StaticContainer;
use Piwik\Mail;
use Piwik\Option;
use Piwik\Piwik;
@@ -203,7 +202,7 @@ class APITest extends IntegrationTestCase
public function test_getAllUsersPreferences_isEmpty_whenNoPreferenceAndMultipleRequested()
{
- $preferences = $this->api->getAllUsersPreferences(array('preferenceName', 'otherOne'));
+ $preferences = $this->api->getAllUsersPreferences(array('preferenceName', 'randomDoesNotExist'));
$this->assertEmpty($preferences);
}
@@ -254,24 +253,24 @@ class APITest extends IntegrationTestCase
$user2 = 'userLogin2';
$user3 = 'userLogin3';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
- $this->api->setUserPreference($user2, 'myPreferenceName', 'valueForUser2');
+ $this->api->setUserPreference($user2, API::PREFERENCE_DEFAULT_REPORT, 'valueForUser2');
$this->api->setUserPreference($user2, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
$this->api->addUser($user3, 'password', 'userlogin3@password.de');
- $this->api->setUserPreference($user3, 'myPreferenceName', 'valueForUser3');
- $this->api->setUserPreference($user3, 'otherPreferenceHere', 'otherPreferenceVALUE');
+ $this->api->setUserPreference($user3, API::PREFERENCE_DEFAULT_REPORT, 'valueForUser3');
+ $this->api->setUserPreference($user3, API::PREFERENCE_DEFAULT_REPORT_DATE, 'otherPreferenceVALUE');
$this->api->setUserPreference($user3, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
$expected = array(
$user2 => array(
- 'myPreferenceName' => 'valueForUser2'
+ API::PREFERENCE_DEFAULT_REPORT => 'valueForUser2'
),
$user3 => array(
- 'myPreferenceName' => 'valueForUser3',
- 'otherPreferenceHere' => 'otherPreferenceVALUE',
+ API::PREFERENCE_DEFAULT_REPORT => 'valueForUser3',
+ API::PREFERENCE_DEFAULT_REPORT_DATE => 'otherPreferenceVALUE',
),
);
- $result = $this->api->getAllUsersPreferences(array('myPreferenceName', 'otherPreferenceHere', 'randomDoesNotExist'));
+ $result = $this->api->getAllUsersPreferences(array(API::PREFERENCE_DEFAULT_REPORT, API::PREFERENCE_DEFAULT_REPORT_DATE, 'randomDoesNotExist'));
$this->assertSame($expected, $result);
}
@@ -280,15 +279,15 @@ class APITest extends IntegrationTestCase
{
$user2 = 'user_Login2';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
- $this->api->setUserPreference($user2, 'myPreferenceName', 'valueForUser2');
- $this->api->setUserPreference($user2, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
+ $this->api->setUserPreference($user2, API::PREFERENCE_DEFAULT_REPORT, 'valueForUser2');
+ $this->api->setUserPreference($user2, API::PREFERENCE_DEFAULT_REPORT_DATE, 'RandomNOTREQUESTED');
$expected = array(
$user2 => array(
- 'myPreferenceName' => 'valueForUser2'
+ API::PREFERENCE_DEFAULT_REPORT => 'valueForUser2'
),
);
- $result = $this->api->getAllUsersPreferences(array('myPreferenceName', 'otherPreferenceHere', 'randomDoesNotExist'));
+ $result = $this->api->getAllUsersPreferences(array(API::PREFERENCE_DEFAULT_REPORT, 'randomDoesNotExist'));
$this->assertSame($expected, $result);
}
@@ -1016,6 +1015,7 @@ class APITest extends IntegrationTestCase
{
return array(
'Piwik\Access' => new FakeAccess(),
+ 'usersmanager.user_preference_names' => \DI\add(['randomDoesNotExist', 'RandomNOTREQUESTED', 'preferenceName']),
'observers.global' => \DI\add([
['Access.Capability.addCapabilities', function (&$capabilities) {
$capabilities[] = new TestCap1();
diff --git a/plugins/UsersManager/tests/Integration/UserPreferencesTest.php b/plugins/UsersManager/tests/Integration/UserPreferencesTest.php
index d7a442065f..bf17cf688e 100644
--- a/plugins/UsersManager/tests/Integration/UserPreferencesTest.php
+++ b/plugins/UsersManager/tests/Integration/UserPreferencesTest.php
@@ -36,6 +36,38 @@ class UserPreferencesTest extends IntegrationTestCase
$this->userPreferences = new UserPreferences();
$this->setSuperUser();
+
+ $identity = FakeAccess::$identity;
+ FakeAccess::$identity = 'foo'; // avoids error user already exists when it doesn't
+ APIUsersManager::getInstance()->addUser($identity, '22111214k4,mdw<L', 'foo@example.com');
+ FakeAccess::$identity = $identity;
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage User does not exist
+ */
+ public function test_getDefaultReport_WhenLoginNotExists()
+ {
+ APIUsersManager::getInstance()->setUserPreference(
+ 'foo',
+ APIUsersManager::PREFERENCE_DEFAULT_REPORT,
+ '1'
+ );
+ }
+
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage Not supported preference name
+ */
+ public function test_getDefaultReport_WhenWrongPreference()
+ {
+ APIUsersManager::getInstance()->setUserPreference(
+ Piwik::getCurrentUserLogin(),
+ 'foo',
+ '1'
+ );
}
public function test_getDefaultReport_ShouldReturnFalseByDefault()