You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 10KB

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