PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Settings/UserSetting.php

https://github.com/CodeYellowBV/piwik
PHP | 119 lines | 55 code | 20 blank | 44 comment | 6 complexity | b08f6211695458b347498495bf28d6e0 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Settings;
  10. use Piwik\Common;
  11. use Piwik\Piwik;
  12. /**
  13. * Describes a per user setting. Each user will be able to change this setting for themselves,
  14. * but not for other users.
  15. *
  16. *
  17. * @api
  18. */
  19. class UserSetting extends Setting
  20. {
  21. private $userLogin = null;
  22. /**
  23. * Constructor.
  24. *
  25. * @param string $name The setting's persisted name.
  26. * @param string $title The setting's display name.
  27. * @param null|string $userLogin The user this setting applies to. Will default to the current user login.
  28. */
  29. public function __construct($name, $title, $userLogin = null)
  30. {
  31. parent::__construct($name, $title);
  32. $this->setUserLogin($userLogin);
  33. $this->writableByCurrentUser = Piwik::isUserHasSomeViewAccess();
  34. $this->readableByCurrentUser = Piwik::isUserHasSomeViewAccess();
  35. }
  36. /**
  37. * Returns the display order. User settings are displayed after system settings.
  38. *
  39. * @return int
  40. */
  41. public function getOrder()
  42. {
  43. return 60;
  44. }
  45. private function buildUserSettingName($name, $userLogin = null)
  46. {
  47. if (empty($userLogin)) {
  48. $userLogin = Piwik::getCurrentUserLogin();
  49. }
  50. // the asterisk tag is indeed important here and better than an underscore. Imagine a plugin has the settings
  51. // "api_password" and "api". A user having the login "_password" could otherwise under circumstances change the
  52. // setting for "api" although he is not allowed to. It is not so important at the moment because only alNum is
  53. // currently allowed as a name this might change in the future.
  54. $appendix = '#' . $userLogin . '#';
  55. if (Common::stringEndsWith($name, $appendix)) {
  56. return $name;
  57. }
  58. return $name . $appendix;
  59. }
  60. /**
  61. * Sets the name of the user this setting will be set for.
  62. *
  63. * @param $userLogin
  64. * @throws \Exception If the current user does not have permission to set the setting value
  65. * of `$userLogin`.
  66. */
  67. public function setUserLogin($userLogin)
  68. {
  69. if (!empty($userLogin) && !Piwik::hasUserSuperUserAccessOrIsTheUser($userLogin)) {
  70. throw new \Exception('You do not have the permission to read the settings of a different user');
  71. }
  72. $this->userLogin = $userLogin;
  73. $this->key = $this->buildUserSettingName($this->name, $userLogin);
  74. }
  75. /**
  76. * Unsets all settings for a user. The settings will be removed from the database. Used when
  77. * a user is deleted.
  78. *
  79. * @param string $userLogin
  80. * @throws \Exception If the `$userLogin` is empty.
  81. */
  82. public static function removeAllUserSettingsForUser($userLogin)
  83. {
  84. if (empty($userLogin)) {
  85. throw new \Exception('No userLogin specified');
  86. }
  87. $pluginsSettings = Manager::getAllPluginSettings();
  88. foreach ($pluginsSettings as $pluginSettings) {
  89. $settings = $pluginSettings->getSettings();
  90. foreach ($settings as $setting) {
  91. if ($setting instanceof UserSetting) {
  92. $setting->setUserLogin($userLogin);
  93. $pluginSettings->removeSettingValue($setting);
  94. }
  95. }
  96. $pluginSettings->save();
  97. }
  98. }
  99. }