Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Manager.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Settings;
  24. use OCP\AppFramework\QueryException;
  25. use OCP\Encryption\IManager as EncryptionManager;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use OCP\IL10N;
  29. use OCP\ILogger;
  30. use OCP\IUserManager;
  31. use OCP\Lock\ILockingProvider;
  32. use OCP\Settings\ISettings;
  33. use OCP\Settings\IManager;
  34. use OCP\Settings\ISection;
  35. class Manager implements IManager {
  36. const TABLE_ADMIN_SETTINGS = 'admin_settings';
  37. const TABLE_ADMIN_SECTIONS = 'admin_sections';
  38. /** @var ILogger */
  39. private $log;
  40. /** @var IDBConnection */
  41. private $dbc;
  42. /** @var IL10N */
  43. private $l;
  44. /** @var IConfig */
  45. private $config;
  46. /** @var EncryptionManager */
  47. private $encryptionManager;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var ILockingProvider */
  51. private $lockingProvider;
  52. /**
  53. * @param ILogger $log
  54. * @param IDBConnection $dbc
  55. * @param IL10N $l
  56. * @param IConfig $config
  57. * @param EncryptionManager $encryptionManager
  58. * @param IUserManager $userManager
  59. * @param ILockingProvider $lockingProvider
  60. */
  61. public function __construct(
  62. ILogger $log,
  63. IDBConnection $dbc,
  64. IL10N $l,
  65. IConfig $config,
  66. EncryptionManager $encryptionManager,
  67. IUserManager $userManager,
  68. ILockingProvider $lockingProvider
  69. ) {
  70. $this->log = $log;
  71. $this->dbc = $dbc;
  72. $this->l = $l;
  73. $this->config = $config;
  74. $this->encryptionManager = $encryptionManager;
  75. $this->userManager = $userManager;
  76. $this->lockingProvider = $lockingProvider;
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public function setupSettings(array $settings) {
  82. if(isset($settings[IManager::KEY_ADMIN_SECTION])) {
  83. $this->setupAdminSection($settings[IManager::KEY_ADMIN_SECTION]);
  84. }
  85. if(isset($settings[IManager::KEY_ADMIN_SETTINGS])) {
  86. $this->setupAdminSettings($settings[IManager::KEY_ADMIN_SETTINGS]);
  87. }
  88. }
  89. /**
  90. * attempts to remove an apps section and/or settings entry. A listener is
  91. * added centrally making sure that this method is called ones an app was
  92. * disabled.
  93. *
  94. * @param string $appId
  95. * @since 9.1.0
  96. */
  97. public function onAppDisabled($appId) {
  98. $appInfo = \OC_App::getAppInfo($appId); // hello static legacy
  99. if(isset($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
  100. $this->remove(self::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
  101. }
  102. if(isset($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
  103. $this->remove(self::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
  104. }
  105. }
  106. public function checkForOrphanedClassNames() {
  107. $tables = [ self::TABLE_ADMIN_SECTIONS, self::TABLE_ADMIN_SETTINGS ];
  108. foreach ($tables as $table) {
  109. $classes = $this->getClasses($table);
  110. foreach($classes as $className) {
  111. try {
  112. \OC::$server->query($className);
  113. } catch (QueryException $e) {
  114. $this->remove($table, $className);
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * returns the registerd classes in the given table
  121. *
  122. * @param $table
  123. * @return string[]
  124. */
  125. private function getClasses($table) {
  126. $q = $this->dbc->getQueryBuilder();
  127. $resultStatement = $q->select('class')
  128. ->from($table)
  129. ->execute();
  130. $data = $resultStatement->fetchAll();
  131. $resultStatement->closeCursor();
  132. return array_map(function($row) { return $row['class']; }, $data);
  133. }
  134. /**
  135. * @param string $sectionClassName
  136. */
  137. private function setupAdminSection($sectionClassName) {
  138. if(!class_exists($sectionClassName)) {
  139. $this->log->debug('Could not find admin section class ' . $sectionClassName);
  140. return;
  141. }
  142. try {
  143. $section = $this->query($sectionClassName);
  144. } catch (QueryException $e) {
  145. // cancel
  146. return;
  147. }
  148. if(!$section instanceof ISection) {
  149. $this->log->error(
  150. 'Admin section instance must implement \OCP\ISection. Invalid class: {class}',
  151. ['class' => $sectionClassName]
  152. );
  153. return;
  154. }
  155. if(!$this->hasAdminSection(get_class($section))) {
  156. $this->addAdminSection($section);
  157. } else {
  158. $this->updateAdminSection($section);
  159. }
  160. }
  161. private function addAdminSection(ISection $section) {
  162. $this->add(self::TABLE_ADMIN_SECTIONS, [
  163. 'id' => $section->getID(),
  164. 'class' => get_class($section),
  165. 'priority' => $section->getPriority(),
  166. ]);
  167. }
  168. private function addAdminSettings(ISettings $settings) {
  169. $this->add(self::TABLE_ADMIN_SETTINGS, [
  170. 'class' => get_class($settings),
  171. 'section' => $settings->getSection(),
  172. 'priority' => $settings->getPriority(),
  173. ]);
  174. }
  175. /**
  176. * @param string $table
  177. * @param array $values
  178. */
  179. private function add($table, array $values) {
  180. $query = $this->dbc->getQueryBuilder();
  181. $values = array_map(function($value) use ($query) {
  182. return $query->createNamedParameter($value);
  183. }, $values);
  184. $query->insert($table)->values($values);
  185. $query->execute();
  186. }
  187. private function updateAdminSettings(ISettings $settings) {
  188. $this->update(
  189. self::TABLE_ADMIN_SETTINGS,
  190. 'class',
  191. get_class($settings),
  192. [
  193. 'section' => $settings->getSection(),
  194. 'priority' => $settings->getPriority(),
  195. ]
  196. );
  197. }
  198. private function updateAdminSection(ISection $section) {
  199. $this->update(
  200. self::TABLE_ADMIN_SECTIONS,
  201. 'class',
  202. get_class($section),
  203. [
  204. 'id' => $section->getID(),
  205. 'priority' => $section->getPriority(),
  206. ]
  207. );
  208. }
  209. private function update($table, $idCol, $id, $values) {
  210. $query = $this->dbc->getQueryBuilder();
  211. $query->update($table);
  212. foreach($values as $key => $value) {
  213. $query->set($key, $query->createNamedParameter($value));
  214. }
  215. $query
  216. ->where($query->expr()->eq($idCol, $query->createParameter($idCol)))
  217. ->setParameter($idCol, $id)
  218. ->execute();
  219. }
  220. /**
  221. * @param string $className
  222. * @return bool
  223. */
  224. private function hasAdminSection($className) {
  225. return $this->has(self::TABLE_ADMIN_SECTIONS, $className);
  226. }
  227. /**
  228. * @param string $className
  229. * @return bool
  230. */
  231. private function hasAdminSettings($className) {
  232. return $this->has(self::TABLE_ADMIN_SETTINGS, $className);
  233. }
  234. /**
  235. * @param string $table
  236. * @param string $className
  237. * @return bool
  238. */
  239. private function has($table, $className) {
  240. $query = $this->dbc->getQueryBuilder();
  241. $query->select('class')
  242. ->from($table)
  243. ->where($query->expr()->eq('class', $query->createNamedParameter($className)))
  244. ->setMaxResults(1);
  245. $result = $query->execute();
  246. $row = $result->fetch();
  247. $result->closeCursor();
  248. return (bool) $row;
  249. }
  250. /**
  251. * deletes an settings or admin entry from the given table
  252. *
  253. * @param $table
  254. * @param $className
  255. */
  256. private function remove($table, $className) {
  257. $query = $this->dbc->getQueryBuilder();
  258. $query->delete($table)
  259. ->where($query->expr()->eq('class', $query->createNamedParameter($className)));
  260. $query->execute();
  261. }
  262. private function setupAdminSettings($settingsClassName) {
  263. if(!class_exists($settingsClassName)) {
  264. $this->log->debug('Could not find admin section class ' . $settingsClassName);
  265. return;
  266. }
  267. try {
  268. /** @var ISettings $settings */
  269. $settings = $this->query($settingsClassName);
  270. } catch (QueryException $e) {
  271. // cancel
  272. return;
  273. }
  274. if(!$settings instanceof ISettings) {
  275. $this->log->error(
  276. 'Admin section instance must implement \OCP\Settings\ISection. Invalid class: {class}',
  277. ['class' => $settingsClassName]
  278. );
  279. return;
  280. }
  281. if(!$this->hasAdminSettings(get_class($settings))) {
  282. $this->addAdminSettings($settings);
  283. } else {
  284. $this->updateAdminSettings($settings);
  285. }
  286. }
  287. private function query($className) {
  288. try {
  289. return \OC::$server->query($className);
  290. } catch (QueryException $e) {
  291. $this->log->logException($e);
  292. throw $e;
  293. }
  294. }
  295. /**
  296. * @inheritdoc
  297. */
  298. public function getAdminSections() {
  299. // built-in sections
  300. $sections = [
  301. 0 => [new Section('server', $this->l->t('Server settings'), 0)],
  302. 5 => [new Section('sharing', $this->l->t('Sharing'), 0)],
  303. 45 => [new Section('encryption', $this->l->t('Encryption'), 0)],
  304. 90 => [new Section('logging', $this->l->t('Logging'), 0)],
  305. 98 => [new Section('additional', $this->l->t('Additional settings'), 0)],
  306. 99 => [new Section('tips-tricks', $this->l->t('Tips & tricks'), 0)],
  307. ];
  308. $query = $this->dbc->getQueryBuilder();
  309. $query->selectDistinct('s.class')
  310. ->addSelect('s.priority')
  311. ->from(self::TABLE_ADMIN_SECTIONS, 's')
  312. ->from(self::TABLE_ADMIN_SETTINGS, 'f')
  313. ->where($query->expr()->eq('s.id', 'f.section'))
  314. ;
  315. $result = $query->execute();
  316. while($row = $result->fetch()) {
  317. if(!isset($sections[$row['priority']])) {
  318. $sections[$row['priority']] = [];
  319. }
  320. try {
  321. $sections[$row['priority']][] = $this->query($row['class']);
  322. } catch (QueryException $e) {
  323. // skip
  324. }
  325. }
  326. $result->closeCursor();
  327. ksort($sections);
  328. return $sections;
  329. }
  330. private function getBuiltInAdminSettings($section) {
  331. $forms = [];
  332. try {
  333. if($section === 'server') {
  334. /** @var ISettings $form */
  335. $form = new Admin\Server($this->dbc, $this->config, $this->lockingProvider, $this->l);
  336. $forms[$form->getPriority()] = [$form];
  337. }
  338. if($section === 'encryption') {
  339. /** @var ISettings $form */
  340. $form = new Admin\Encryption($this->encryptionManager, $this->userManager);
  341. $forms[$form->getPriority()] = [$form];
  342. }
  343. if($section === 'sharing') {
  344. /** @var ISettings $form */
  345. $form = new Admin\Sharing($this->config);
  346. $forms[$form->getPriority()] = [$form];
  347. }
  348. if($section === 'logging') {
  349. /** @var ISettings $form */
  350. $form = new Admin\Logging($this->config);
  351. $forms[$form->getPriority()] = [$form];
  352. }
  353. if($section === 'additional') {
  354. /** @var ISettings $form */
  355. $form = new Admin\Additional($this->config);
  356. $forms[$form->getPriority()] = [$form];
  357. }
  358. if($section === 'tips-tricks') {
  359. /** @var ISettings $form */
  360. $form = new Admin\TipsTricks($this->config);
  361. $forms[$form->getPriority()] = [$form];
  362. }
  363. } catch (QueryException $e) {
  364. // skip
  365. }
  366. return $forms;
  367. }
  368. private function getAdminSettingsFromDB($section, &$settings) {
  369. $query = $this->dbc->getQueryBuilder();
  370. $query->select(['class', 'priority'])
  371. ->from(self::TABLE_ADMIN_SETTINGS)
  372. ->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section')))
  373. ->setParameter('section', $section);
  374. $result = $query->execute();
  375. while($row = $result->fetch()) {
  376. if(!isset($settings[$row['priority']])) {
  377. $settings[$row['priority']] = [];
  378. }
  379. try {
  380. $settings[$row['priority']][] = $this->query($row['class']);
  381. } catch (QueryException $e) {
  382. // skip
  383. }
  384. }
  385. $result->closeCursor();
  386. ksort($settings);
  387. }
  388. /**
  389. * @inheritdoc
  390. */
  391. public function getAdminSettings($section) {
  392. $settings = $this->getBuiltInAdminSettings($section);
  393. $this->getAdminSettingsFromDB($section, $settings);
  394. return $settings;
  395. }
  396. }