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