xref: /plugin/twofactor/Settings.php (revision 6d69ca71a73d69c25b5dbf99abe9d3639a50db92)
1<?php
2
3namespace dokuwiki\plugin\twofactor;
4
5/**
6 * Encapsulate the attribute plugin for per user/provider storage
7 *
8 * @todo maybe do our own settings saving with backwards compatibility to attribute?
9 */
10class Settings
11{
12
13    /** @var \helper_plugin_attribute */
14    protected $attribute;
15
16    /** @var string Identifier of the provider these settings are for */
17    protected $providerID;
18
19    /** @var string Login of the user these settings are for */
20    protected $user;
21
22    /**
23     * @param string $module Name of the provider
24     * @param string $user User login
25     */
26    public function __construct($module, $user)
27    {
28        $this->attribute = plugin_load('helper', 'attribute');
29        if ($this->attribute === null) throw new \RuntimeException('attribute plugin not found');
30
31        $this->providerID = $module;
32        $this->user = $user;
33    }
34
35    /**
36     * Return a list of users that have settings for the given module
37     *
38     * @param $module
39     * @return array|bool
40     */
41    static public function findUsers($module)
42    {
43        /** @var \helper_plugin_attribute $attribute */
44        $attribute = plugin_load('helper', 'attribute');
45        if ($attribute === null) throw new \RuntimeException('attribute plugin not found');
46
47        return $attribute->enumerateUsers($module);
48    }
49
50    /**
51     * Check if a setting exists
52     *
53     * @param string $key Settings key
54     * @return bool
55     */
56    public function has($key)
57    {
58        return $this->attribute->exists($this->providerID, $key, $this->user);
59    }
60
61    /**
62     * Get a stored setting
63     *
64     * @param string $key Settings key
65     * @param mixed $default Default to return when no setting available
66     * @return mixed
67     */
68    public function get($key, $default = null)
69    {
70        $success = false;
71        $data = $this->attribute->get($this->providerID, $key, $success, $this->user);
72        if (!$success) return $default;
73        return $data;
74    }
75
76    /**
77     * Store a settings value
78     *
79     * @param string $key Settings key
80     * @param mixed $value Value to store
81     * @return bool
82     */
83    public function set($key, $value)
84    {
85        return $this->attribute->set($this->providerID, $key, $value, $this->user);
86    }
87
88    /**
89     * Delete a settings value
90     *
91     * @param string $key Settings key
92     * @return bool
93     */
94    public function delete($key)
95    {
96        return $this->attribute->del($this->providerID, $key, $this->user);
97    }
98
99    /**
100     * Remove all settings
101     *
102     * @return bool
103     */
104    public function purge()
105    {
106        return $this->attribute->purge($this->providerID, $this->user);
107    }
108
109}
110