ModuleSettingRepository.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Repositories;
  3. use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model;
  4. /**
  5. * Class ModuleSettingsRepository
  6. * @package ModulesGarden\ProxmoxAddon\App\Repositories
  7. * @property $consoleHost
  8. * @method isPermissionStart()
  9. * @method isPermissionReboot()
  10. * @method isPermissionStop()
  11. * @method isPermissionShutdown()
  12. * @method isPermissionNovnc()
  13. * @method isPermissionSpice()
  14. * @method isPermissionXtermjs()
  15. * @method isPermissionMigrate()
  16. * @method isPermissionInformation()
  17. * @method isPermissionIpAddresses()
  18. * @method isPermissionIpSet()
  19. * @method isPermissionReinstall()
  20. * @method isPermissionGraph()
  21. * @method isPermissionUserDetails()
  22. * @method isPermissionAccountResources()
  23. * @method isPermissionVms()
  24. */
  25. class ModuleSettingRepository
  26. {
  27. protected $fillable = ['consoleHost','consoleApiKey','permissionStart', 'permissionReboot','permissionStop','permissionShutdown','permissionNovnc','permissionSpice',
  28. 'permissionXtermjs','permissionMigrate','permissionInformation','permissionIpAddresses','permissionIpSet','permissionReinstall','permissionGraph','permissionUserDetails',
  29. 'permissionAccountResources','permissionVms'];
  30. protected $encryption = ['consoleApiKey'];
  31. /**
  32. *
  33. * @var Model[]
  34. */
  35. protected $entries;
  36. protected $force = false;
  37. public function getForce()
  38. {
  39. return $this->force;
  40. }
  41. public function setForce($force)
  42. {
  43. $this->force = $force;
  44. return $this;
  45. }
  46. public function isEmpty()
  47. {
  48. return empty($this->entries);
  49. }
  50. public function flush()
  51. {
  52. Model::delete();
  53. return $this;
  54. }
  55. /**
  56. * Delete keys
  57. * @param string $keys
  58. * @param array $keys
  59. * @return $this
  60. */
  61. public function forget($keys)
  62. {
  63. if (is_array($keys))
  64. {
  65. Model::whereIn('setting', $keys)
  66. ->delete();
  67. foreach ($keys as $k)
  68. {
  69. unset($this->entries[$k]);
  70. }
  71. }
  72. else
  73. {
  74. Model::where('setting', $keys)
  75. ->limit(1)
  76. ->delete();
  77. unset($this->entries[$keys]);
  78. }
  79. return $this;
  80. }
  81. public function all($default = [])
  82. {
  83. $this->force = true;
  84. foreach (Model::pluck('value', 'setting')->all() as $key => $value)
  85. {
  86. if(in_array($key, $this->encryption)){
  87. $value = decrypt($value);
  88. }
  89. $this->entries[$key] = $value;
  90. }
  91. if (!$this->isEmpty())
  92. {
  93. return $this->entries;
  94. }
  95. return $default;
  96. }
  97. public function store(array $values)
  98. {
  99. foreach ($values as $k => $v)
  100. {
  101. $this->set($k, $v);
  102. }
  103. return $this;
  104. }
  105. public function exist($key)
  106. {
  107. return Model::where('setting', $key)
  108. ->count() > 0;
  109. }
  110. public function set($key, $value)
  111. {
  112. $this->entries[$key] = $value;
  113. }
  114. public function __isset($key)
  115. {
  116. return isset($this->entries[$key]);
  117. }
  118. public function get($key, $default = null)
  119. {
  120. if (isset($this->entries[$key]))
  121. {
  122. return $this->entries[$key];
  123. }
  124. else
  125. {
  126. if (!$this->force)
  127. {
  128. $this->getEntery($key);
  129. if (isset($this->entries[$key]))
  130. {
  131. return $this->entries[$key];
  132. }
  133. }
  134. }
  135. return $default;
  136. }
  137. /**
  138. *
  139. * @param string $key
  140. * @return Model
  141. */
  142. protected function getEntery($key)
  143. {
  144. if (isset($this->entries[$key]))
  145. {
  146. return $this->entries[$key];
  147. }
  148. else
  149. {
  150. if (!$this->force)
  151. {
  152. $this->entries[$key] = Model::ofSetting($key)
  153. ->value("value");
  154. return $this->entries[$key];
  155. }
  156. }
  157. }
  158. public function fill(array $setting)
  159. {
  160. $this->entries = $setting;
  161. return $this;
  162. }
  163. public function fillAndSave(array $setting)
  164. {
  165. $this->entries = $setting;
  166. foreach ($this->entries as $key => $value)
  167. {
  168. //fillable
  169. if(!in_array($key, $this->fillable)){
  170. unset($this->entries[$key]);
  171. continue;
  172. }
  173. //encrypt
  174. if(in_array($key, $this->encryption)){
  175. $value = encrypt($value);
  176. }
  177. $setting = Model::firstOrNew([
  178. 'setting' => $key
  179. ]);
  180. $setting->value = $value;
  181. $setting->save();
  182. }
  183. return $this;
  184. }
  185. public function save()
  186. {
  187. foreach ($this->entries as $key => $value)
  188. {
  189. $setting = new Model();
  190. $setting->setting = $key;
  191. $setting->value = $value;
  192. $setting->save();
  193. }
  194. }
  195. public function __get($name)
  196. {
  197. return $this->get($name);
  198. }
  199. public function setConsoleApiKey($consoleApiKey){
  200. $this->set('consoleApiKey',encrypt($consoleApiKey));
  201. return $this;
  202. }
  203. public function getConsoleApiKey()
  204. {
  205. return decrypt($this->consoleApiKey);
  206. }
  207. public function hasConsoleApiKey()
  208. {
  209. return decrypt($this->consoleApiKey) !="";
  210. }
  211. public function __call($method, $args)
  212. {
  213. $name = preg_replace('/^is/',"",$method);
  214. $name = lcfirst($name );
  215. return $this->get($name) != "off";
  216. }
  217. public function isPermissionServiceActions(){
  218. return $this->isPermissionStart() ||
  219. $this->isPermissionReboot() ||
  220. $this->isPermissionStop() ||
  221. $this->isPermissionShutdown() ||
  222. $this->isPermissionNovnc() ||
  223. $this->isPermissionSpice() ||
  224. $this->isPermissionXtermjs() ||
  225. $this->isPermissionMigrate() ;
  226. }
  227. }