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

UserSetting.php « Settings « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f56f7104df4e1b498f82d20d084cb8f26ad2d06 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Settings;
use Piwik\Common;
use Piwik\Piwik;

/**
 * Describes a per user setting. Each user will be able to change this setting for themselves,
 * but not for other users.
 *
 *
 * @api
 */
class UserSetting extends Setting
{
    private $userLogin = null;

    /**
     * Constructor.
     * 
     * @param string $name The setting's persisted name.
     * @param string $title The setting's display name.
     * @param null|string $userLogin The user this setting applies to. Will default to the current user login.
     */
    public function __construct($name, $title, $userLogin = null)
    {
        parent::__construct($name, $title);

        $this->setUserLogin($userLogin);

        $this->writableByCurrentUser = Piwik::isUserHasSomeViewAccess();
        $this->readableByCurrentUser = Piwik::isUserHasSomeViewAccess();
    }

    /**
     * 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);
                    $pluginSettings->removeSettingValue($setting);
                }

            }

            $pluginSettings->save();
        }
    }
}