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