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'); 29b6119621SAndreas 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 /** 36*6d69ca71SAndreas Gohr * Return a list of users that have settings for the given module 37*6d69ca71SAndreas Gohr * 38*6d69ca71SAndreas Gohr * @param $module 39*6d69ca71SAndreas Gohr * @return array|bool 40*6d69ca71SAndreas Gohr */ 41*6d69ca71SAndreas Gohr static public function findUsers($module) 42*6d69ca71SAndreas Gohr { 43*6d69ca71SAndreas Gohr /** @var \helper_plugin_attribute $attribute */ 44*6d69ca71SAndreas Gohr $attribute = plugin_load('helper', 'attribute'); 45*6d69ca71SAndreas Gohr if ($attribute === null) throw new \RuntimeException('attribute plugin not found'); 46*6d69ca71SAndreas Gohr 47*6d69ca71SAndreas Gohr return $attribute->enumerateUsers($module); 48*6d69ca71SAndreas Gohr } 49*6d69ca71SAndreas Gohr 50*6d69ca71SAndreas Gohr /** 51a635cb20SAndreas Gohr * Check if a setting exists 52a635cb20SAndreas Gohr * 53a635cb20SAndreas Gohr * @param string $key Settings key 54a635cb20SAndreas Gohr * @return bool 55a635cb20SAndreas Gohr */ 56a635cb20SAndreas Gohr public function has($key) 57a635cb20SAndreas Gohr { 58a635cb20SAndreas Gohr return $this->attribute->exists($this->providerID, $key, $this->user); 59a635cb20SAndreas Gohr } 60a635cb20SAndreas Gohr 61a635cb20SAndreas Gohr /** 62a635cb20SAndreas Gohr * Get a stored setting 63a635cb20SAndreas Gohr * 64a635cb20SAndreas Gohr * @param string $key Settings key 65a635cb20SAndreas Gohr * @param mixed $default Default to return when no setting available 66a635cb20SAndreas Gohr * @return mixed 67a635cb20SAndreas Gohr */ 68a635cb20SAndreas Gohr public function get($key, $default = null) 69a635cb20SAndreas Gohr { 70a635cb20SAndreas Gohr $success = false; 71a635cb20SAndreas Gohr $data = $this->attribute->get($this->providerID, $key, $success, $this->user); 72a635cb20SAndreas Gohr if (!$success) return $default; 73a635cb20SAndreas Gohr return $data; 74a635cb20SAndreas Gohr } 75a635cb20SAndreas Gohr 76a635cb20SAndreas Gohr /** 77a635cb20SAndreas Gohr * Store a settings value 78a635cb20SAndreas Gohr * 79a635cb20SAndreas Gohr * @param string $key Settings key 80a635cb20SAndreas Gohr * @param mixed $value Value to store 81a635cb20SAndreas Gohr * @return bool 82a635cb20SAndreas Gohr */ 83a635cb20SAndreas Gohr public function set($key, $value) 84a635cb20SAndreas Gohr { 85a635cb20SAndreas Gohr return $this->attribute->set($this->providerID, $key, $value, $this->user); 86a635cb20SAndreas Gohr } 87a635cb20SAndreas Gohr 88a635cb20SAndreas Gohr /** 89a635cb20SAndreas Gohr * Delete a settings value 90a635cb20SAndreas Gohr * 91a635cb20SAndreas Gohr * @param string $key Settings key 92a635cb20SAndreas Gohr * @return bool 93a635cb20SAndreas Gohr */ 94a635cb20SAndreas Gohr public function delete($key) 95a635cb20SAndreas Gohr { 96a635cb20SAndreas Gohr return $this->attribute->del($this->providerID, $key, $this->user); 97a635cb20SAndreas Gohr } 98a635cb20SAndreas Gohr 99a635cb20SAndreas Gohr /** 100a635cb20SAndreas Gohr * Remove all settings 101a635cb20SAndreas Gohr * 102a635cb20SAndreas Gohr * @return bool 103a635cb20SAndreas Gohr */ 104a635cb20SAndreas Gohr public function purge() 105a635cb20SAndreas Gohr { 106a635cb20SAndreas Gohr return $this->attribute->purge($this->providerID, $this->user); 107a635cb20SAndreas Gohr } 108a635cb20SAndreas Gohr 109a635cb20SAndreas Gohr} 110