Skip to content

Commit

Permalink
refs #4126 make sure a user cannot read/change settings of another user
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Oct 24, 2013
1 parent ac87e01 commit ab66ccb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/Settings/UserSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ private function buildUserSettingName($name, $userLogin = null)
* Sets (overwrites) the userLogin.
*
* @param $userLogin
*
* @throws \Exception In case you set a userLogin that is not your userLogin and you are not the superUser.
*/
public function setUserLogin($userLogin)
{
if (!empty($userLogin) && !Piwik::isUserIsSuperUserOrTheUser($userLogin)) {
throw new \Exception('You do not have the permission to read the settings of a different user');
}

$this->userLogin = $userLogin;
$this->key = $this->buildUserSettingName($this->name, $userLogin);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPUnit/Core/Plugin/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,31 @@ public function test_userSetting_shouldSaveValuesPerUser()
$this->assertSettingHasValue($user, null);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage You do not have the permission to read the settings of a different user
*/
public function test_userSetting_shouldThrowException_IfSomeoneTriesToReadSettingsFromAnotherUserAndIsNotSuperuser()
{
$this->setUser();

$this->buildUserSetting('myname', 'mytitle', 'myRandomName');
}

public function test_userSetting_shouldBeAbleToSetLoginAndChangeValues_IfUserIsSuperUser()
{
$this->setSuperUser();

$setting = $this->buildUserSetting('myname', 'mytitle', 'myRandomName');
$this->settings->addSetting($setting);

$this->settings->setSettingValue($setting, 5);
$this->assertSettingHasValue($setting, 5);

$this->settings->removeSettingValue($setting);
$this->assertSettingHasValue($setting, null);
}

private function buildUserSetting($name, $title, $userLogin = null)
{
return new \Piwik\Settings\UserSetting($name, $title, $userLogin);
Expand Down

0 comments on commit ab66ccb

Please sign in to comment.