Manager.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author sualko <klaus@jsxc.org>
  14. * @author Carl Schwan <carl@carlschwan.eu>
  15. * @author Kate Döen <kate.doeen@nextcloud.com>
  16. *
  17. * @license GNU AGPL version 3 or any later version
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License as
  21. * published by the Free Software Foundation, either version 3 of the
  22. * License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. */
  33. namespace OC\Settings;
  34. use Closure;
  35. use OCP\AppFramework\QueryException;
  36. use OCP\Group\ISubAdmin;
  37. use OCP\IGroupManager;
  38. use OCP\IL10N;
  39. use OCP\IServerContainer;
  40. use OCP\IURLGenerator;
  41. use OCP\IUser;
  42. use OCP\L10N\IFactory;
  43. use OCP\Settings\IIconSection;
  44. use OCP\Settings\IManager;
  45. use OCP\Settings\ISettings;
  46. use OCP\Settings\ISubAdminSettings;
  47. use Psr\Log\LoggerInterface;
  48. class Manager implements IManager {
  49. /** @var LoggerInterface */
  50. private $log;
  51. /** @var IL10N */
  52. private $l;
  53. /** @var IFactory */
  54. private $l10nFactory;
  55. /** @var IURLGenerator */
  56. private $url;
  57. /** @var IServerContainer */
  58. private $container;
  59. /** @var AuthorizedGroupMapper $mapper */
  60. private $mapper;
  61. /** @var IGroupManager $groupManager */
  62. private $groupManager;
  63. /** @var ISubAdmin $subAdmin */
  64. private $subAdmin;
  65. public function __construct(
  66. LoggerInterface $log,
  67. IFactory $l10nFactory,
  68. IURLGenerator $url,
  69. IServerContainer $container,
  70. AuthorizedGroupMapper $mapper,
  71. IGroupManager $groupManager,
  72. ISubAdmin $subAdmin
  73. ) {
  74. $this->log = $log;
  75. $this->l10nFactory = $l10nFactory;
  76. $this->url = $url;
  77. $this->container = $container;
  78. $this->mapper = $mapper;
  79. $this->groupManager = $groupManager;
  80. $this->subAdmin = $subAdmin;
  81. }
  82. /** @var array<self::SETTINGS_*, list<class-string<IIconSection>>> */
  83. protected $sectionClasses = [];
  84. /** @var array<self::SETTINGS_*, array<string, IIconSection>> */
  85. protected $sections = [];
  86. /**
  87. * @inheritdoc
  88. */
  89. public function registerSection(string $type, string $section) {
  90. if (!isset($this->sectionClasses[$type])) {
  91. $this->sectionClasses[$type] = [];
  92. }
  93. $this->sectionClasses[$type][] = $section;
  94. }
  95. /**
  96. * @psalm-param self::SETTINGS_* $type
  97. *
  98. * @return IIconSection[]
  99. */
  100. protected function getSections(string $type): array {
  101. if (!isset($this->sections[$type])) {
  102. $this->sections[$type] = [];
  103. }
  104. if (!isset($this->sectionClasses[$type])) {
  105. return $this->sections[$type];
  106. }
  107. foreach (array_unique($this->sectionClasses[$type]) as $index => $class) {
  108. try {
  109. /** @var IIconSection $section */
  110. $section = $this->container->get($class);
  111. } catch (QueryException $e) {
  112. $this->log->info($e->getMessage(), ['exception' => $e]);
  113. continue;
  114. }
  115. $sectionID = $section->getID();
  116. if (!$this->isKnownDuplicateSectionId($sectionID) && isset($this->sections[$type][$sectionID])) {
  117. $e = new \InvalidArgumentException('Section with the same ID already registered: ' . $sectionID . ', class: ' . $class);
  118. $this->log->info($e->getMessage(), ['exception' => $e]);
  119. continue;
  120. }
  121. $this->sections[$type][$sectionID] = $section;
  122. unset($this->sectionClasses[$type][$index]);
  123. }
  124. return $this->sections[$type];
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function getSection(string $type, string $sectionId): ?IIconSection {
  130. if (isset($this->sections[$type]) && isset($this->sections[$type][$sectionId])) {
  131. return $this->sections[$type][$sectionId];
  132. }
  133. return null;
  134. }
  135. protected function isKnownDuplicateSectionId(string $sectionID): bool {
  136. return in_array($sectionID, [
  137. 'connected-accounts',
  138. 'notifications',
  139. ], true);
  140. }
  141. /** @var array<class-string<ISettings>, self::SETTINGS_*> */
  142. protected $settingClasses = [];
  143. /** @var array<self::SETTINGS_*, array<string, list<ISettings>>> */
  144. protected $settings = [];
  145. /**
  146. * @inheritdoc
  147. */
  148. public function registerSetting(string $type, string $setting) {
  149. $this->settingClasses[$setting] = $type;
  150. }
  151. /**
  152. * @psalm-param self::SETTINGS_* $type The type of the setting.
  153. * @param string $section
  154. * @param ?Closure $filter optional filter to apply on all loaded ISettings
  155. *
  156. * @return ISettings[]
  157. */
  158. protected function getSettings(string $type, string $section, ?Closure $filter = null): array {
  159. if (!isset($this->settings[$type])) {
  160. $this->settings[$type] = [];
  161. }
  162. if (!isset($this->settings[$type][$section])) {
  163. $this->settings[$type][$section] = [];
  164. }
  165. foreach ($this->settingClasses as $class => $settingsType) {
  166. if ($type !== $settingsType) {
  167. continue;
  168. }
  169. try {
  170. /** @var ISettings $setting */
  171. $setting = $this->container->get($class);
  172. } catch (QueryException $e) {
  173. $this->log->info($e->getMessage(), ['exception' => $e]);
  174. continue;
  175. }
  176. if (!$setting instanceof ISettings) {
  177. $e = new \InvalidArgumentException('Invalid settings setting registered (' . $class . ')');
  178. $this->log->info($e->getMessage(), ['exception' => $e]);
  179. continue;
  180. }
  181. if ($filter !== null && !$filter($setting)) {
  182. continue;
  183. }
  184. if ($setting->getSection() === null) {
  185. continue;
  186. }
  187. if (!isset($this->settings[$settingsType][$setting->getSection()])) {
  188. $this->settings[$settingsType][$setting->getSection()] = [];
  189. }
  190. $this->settings[$settingsType][$setting->getSection()][] = $setting;
  191. unset($this->settingClasses[$class]);
  192. }
  193. return $this->settings[$type][$section];
  194. }
  195. /**
  196. * @inheritdoc
  197. */
  198. public function getAdminSections(): array {
  199. // built-in sections
  200. $sections = [];
  201. $appSections = $this->getSections('admin');
  202. foreach ($appSections as $section) {
  203. /** @var IIconSection $section */
  204. if (!isset($sections[$section->getPriority()])) {
  205. $sections[$section->getPriority()] = [];
  206. }
  207. $sections[$section->getPriority()][] = $section;
  208. }
  209. ksort($sections);
  210. return $sections;
  211. }
  212. /**
  213. * @inheritdoc
  214. */
  215. public function getAdminSettings(string $section, bool $subAdminOnly = false): array {
  216. if ($subAdminOnly) {
  217. $subAdminSettingsFilter = function (ISettings $settings) {
  218. return $settings instanceof ISubAdminSettings;
  219. };
  220. $appSettings = $this->getSettings('admin', $section, $subAdminSettingsFilter);
  221. } else {
  222. $appSettings = $this->getSettings('admin', $section);
  223. }
  224. $settings = [];
  225. foreach ($appSettings as $setting) {
  226. if (!isset($settings[$setting->getPriority()])) {
  227. $settings[$setting->getPriority()] = [];
  228. }
  229. $settings[$setting->getPriority()][] = $setting;
  230. }
  231. ksort($settings);
  232. return $settings;
  233. }
  234. /**
  235. * @inheritdoc
  236. */
  237. public function getPersonalSections(): array {
  238. if ($this->l === null) {
  239. $this->l = $this->l10nFactory->get('lib');
  240. }
  241. $sections = [];
  242. $legacyForms = \OC_App::getForms('personal');
  243. if ((!empty($legacyForms) && $this->hasLegacyPersonalSettingsToRender($legacyForms))
  244. || count($this->getPersonalSettings('additional')) > 1) {
  245. $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
  246. }
  247. $appSections = $this->getSections('personal');
  248. foreach ($appSections as $section) {
  249. /** @var IIconSection $section */
  250. if (!isset($sections[$section->getPriority()])) {
  251. $sections[$section->getPriority()] = [];
  252. }
  253. $sections[$section->getPriority()][] = $section;
  254. }
  255. ksort($sections);
  256. return $sections;
  257. }
  258. /**
  259. * @param string[] $forms
  260. *
  261. * @return bool
  262. */
  263. private function hasLegacyPersonalSettingsToRender(array $forms): bool {
  264. foreach ($forms as $form) {
  265. if (trim($form) !== '') {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * @inheritdoc
  273. */
  274. public function getPersonalSettings(string $section): array {
  275. $settings = [];
  276. $appSettings = $this->getSettings('personal', $section);
  277. foreach ($appSettings as $setting) {
  278. if (!isset($settings[$setting->getPriority()])) {
  279. $settings[$setting->getPriority()] = [];
  280. }
  281. $settings[$setting->getPriority()][] = $setting;
  282. }
  283. ksort($settings);
  284. return $settings;
  285. }
  286. /**
  287. * @inheritdoc
  288. */
  289. public function getAllowedAdminSettings(string $section, IUser $user): array {
  290. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  291. if ($isAdmin) {
  292. $appSettings = $this->getSettings('admin', $section);
  293. } else {
  294. $authorizedSettingsClasses = $this->mapper->findAllClassesForUser($user);
  295. if ($this->subAdmin->isSubAdmin($user)) {
  296. $authorizedGroupFilter = function (ISettings $settings) use ($authorizedSettingsClasses) {
  297. return $settings instanceof ISubAdminSettings
  298. || in_array(get_class($settings), $authorizedSettingsClasses) === true;
  299. };
  300. } else {
  301. $authorizedGroupFilter = function (ISettings $settings) use ($authorizedSettingsClasses) {
  302. return in_array(get_class($settings), $authorizedSettingsClasses) === true;
  303. };
  304. }
  305. $appSettings = $this->getSettings('admin', $section, $authorizedGroupFilter);
  306. }
  307. $settings = [];
  308. foreach ($appSettings as $setting) {
  309. if (!isset($settings[$setting->getPriority()])) {
  310. $settings[$setting->getPriority()] = [];
  311. }
  312. $settings[$setting->getPriority()][] = $setting;
  313. }
  314. ksort($settings);
  315. return $settings;
  316. }
  317. /**
  318. * @inheritdoc
  319. */
  320. public function getAllAllowedAdminSettings(IUser $user): array {
  321. $this->getSettings('admin', ''); // Make sure all the settings are loaded
  322. $settings = [];
  323. $authorizedSettingsClasses = $this->mapper->findAllClassesForUser($user);
  324. foreach ($this->settings['admin'] as $section) {
  325. foreach ($section as $setting) {
  326. if (in_array(get_class($setting), $authorizedSettingsClasses) === true) {
  327. $settings[] = $setting;
  328. }
  329. }
  330. }
  331. return $settings;
  332. }
  333. }