setUserLogin($userLogin); } /** * Returns `true` if this setting can be displayed for the current user, `false` if otherwise. * * @return bool */ public function isReadableByCurrentUser() { return $this->isWritableByCurrentUser(); } /** * Returns `true` if this setting can be displayed for the current user, `false` if otherwise. * * @return bool */ public function isWritableByCurrentUser() { if (isset($this->hasReadAndWritePermission)) { return $this->hasReadAndWritePermission; } $this->hasReadAndWritePermission = Piwik::isUserHasSomeViewAccess(); return $this->hasReadAndWritePermission; } /** * Returns the display order. User settings are displayed after system settings. * * @return int */ public function getOrder() { return 60; } private function buildUserSettingName($name, $userLogin = null) { if (empty($userLogin)) { $userLogin = Piwik::getCurrentUserLogin(); } // the asterisk tag is indeed important here and better than an underscore. Imagine a plugin has the settings // "api_password" and "api". A user having the login "_password" could otherwise under circumstances change the // setting for "api" although he is not allowed to. It is not so important at the moment because only alNum is // currently allowed as a name this might change in the future. $appendix = '#' . $userLogin . '#'; if (Common::stringEndsWith($name, $appendix)) { return $name; } return $name . $appendix; } /** * Sets the name of the user this setting will be set for. * * @param $userLogin * @throws \Exception If the current user does not have permission to set the setting value * of `$userLogin`. */ public function setUserLogin($userLogin) { if (!empty($userLogin) && !Piwik::hasUserSuperUserAccessOrIsTheUser($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); } /** * Unsets all settings for a user. The settings will be removed from the database. Used when * a user is deleted. * * @param string $userLogin * @throws \Exception If the `$userLogin` is empty. */ public static function removeAllUserSettingsForUser($userLogin) { if (empty($userLogin)) { throw new \Exception('No userLogin specified'); } $pluginsSettings = Manager::getAllPluginSettings(); foreach ($pluginsSettings as $pluginSettings) { $settings = $pluginSettings->getSettings(); foreach ($settings as $setting) { if ($setting instanceof UserSetting) { $setting->setUserLogin($userLogin); $setting->removeValue(); } } $pluginSettings->save(); } } }