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