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'); 30*c8525a21SAndreas Gohr $this->attribute->setSecure(false); 31a635cb20SAndreas Gohr 32a635cb20SAndreas Gohr $this->providerID = $module; 33a635cb20SAndreas Gohr $this->user = $user; 34a635cb20SAndreas Gohr } 35a635cb20SAndreas Gohr 36a635cb20SAndreas Gohr /** 376d69ca71SAndreas Gohr * Return a list of users that have settings for the given module 386d69ca71SAndreas Gohr * 396d69ca71SAndreas Gohr * @param $module 406d69ca71SAndreas Gohr * @return array|bool 416d69ca71SAndreas Gohr */ 426d69ca71SAndreas Gohr static public function findUsers($module) 436d69ca71SAndreas Gohr { 446d69ca71SAndreas Gohr /** @var \helper_plugin_attribute $attribute */ 456d69ca71SAndreas Gohr $attribute = plugin_load('helper', 'attribute'); 466d69ca71SAndreas Gohr if ($attribute === null) throw new \RuntimeException('attribute plugin not found'); 476d69ca71SAndreas Gohr 486d69ca71SAndreas Gohr return $attribute->enumerateUsers($module); 496d69ca71SAndreas Gohr } 506d69ca71SAndreas Gohr 516d69ca71SAndreas Gohr /** 52*c8525a21SAndreas Gohr * Get the user these settings are for 53*c8525a21SAndreas Gohr * 54*c8525a21SAndreas Gohr * @return string 55*c8525a21SAndreas Gohr */ 56*c8525a21SAndreas Gohr public function getUser() 57*c8525a21SAndreas Gohr { 58*c8525a21SAndreas Gohr return $this->user; 59*c8525a21SAndreas Gohr } 60*c8525a21SAndreas Gohr 61*c8525a21SAndreas Gohr /** 62a635cb20SAndreas Gohr * Check if a setting exists 63a635cb20SAndreas Gohr * 64a635cb20SAndreas Gohr * @param string $key Settings key 65a635cb20SAndreas Gohr * @return bool 66a635cb20SAndreas Gohr */ 67a635cb20SAndreas Gohr public function has($key) 68a635cb20SAndreas Gohr { 69a635cb20SAndreas Gohr return $this->attribute->exists($this->providerID, $key, $this->user); 70a635cb20SAndreas Gohr } 71a635cb20SAndreas Gohr 72a635cb20SAndreas Gohr /** 73a635cb20SAndreas Gohr * Get a stored setting 74a635cb20SAndreas Gohr * 75a635cb20SAndreas Gohr * @param string $key Settings key 76a635cb20SAndreas Gohr * @param mixed $default Default to return when no setting available 77a635cb20SAndreas Gohr * @return mixed 78a635cb20SAndreas Gohr */ 79a635cb20SAndreas Gohr public function get($key, $default = null) 80a635cb20SAndreas Gohr { 81a635cb20SAndreas Gohr $success = false; 82a635cb20SAndreas Gohr $data = $this->attribute->get($this->providerID, $key, $success, $this->user); 83a635cb20SAndreas Gohr if (!$success) return $default; 84a635cb20SAndreas Gohr return $data; 85a635cb20SAndreas Gohr } 86a635cb20SAndreas Gohr 87a635cb20SAndreas Gohr /** 88a635cb20SAndreas Gohr * Store a settings value 89a635cb20SAndreas Gohr * 90a635cb20SAndreas Gohr * @param string $key Settings key 91a635cb20SAndreas Gohr * @param mixed $value Value to store 92a635cb20SAndreas Gohr * @return bool 93a635cb20SAndreas Gohr */ 94a635cb20SAndreas Gohr public function set($key, $value) 95a635cb20SAndreas Gohr { 96a635cb20SAndreas Gohr return $this->attribute->set($this->providerID, $key, $value, $this->user); 97a635cb20SAndreas Gohr } 98a635cb20SAndreas Gohr 99a635cb20SAndreas Gohr /** 100a635cb20SAndreas Gohr * Delete a settings value 101a635cb20SAndreas Gohr * 102a635cb20SAndreas Gohr * @param string $key Settings key 103a635cb20SAndreas Gohr * @return bool 104a635cb20SAndreas Gohr */ 105a635cb20SAndreas Gohr public function delete($key) 106a635cb20SAndreas Gohr { 107a635cb20SAndreas Gohr return $this->attribute->del($this->providerID, $key, $this->user); 108a635cb20SAndreas Gohr } 109a635cb20SAndreas Gohr 110a635cb20SAndreas Gohr /** 111a635cb20SAndreas Gohr * Remove all settings 112a635cb20SAndreas Gohr * 113a635cb20SAndreas Gohr * @return bool 114a635cb20SAndreas Gohr */ 115a635cb20SAndreas Gohr public function purge() 116a635cb20SAndreas Gohr { 117a635cb20SAndreas Gohr return $this->attribute->purge($this->providerID, $this->user); 118a635cb20SAndreas Gohr } 119a635cb20SAndreas Gohr 120a635cb20SAndreas Gohr} 121