appmanager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\App;
  23. use OCP\App\IAppManager;
  24. use OCP\IAppConfig;
  25. use OCP\ICacheFactory;
  26. use OCP\IGroupManager;
  27. use OCP\IUser;
  28. use OCP\IUserSession;
  29. class AppManager implements IAppManager {
  30. /**
  31. * @var \OCP\IUserSession
  32. */
  33. private $userSession;
  34. /**
  35. * @var \OCP\IAppConfig
  36. */
  37. private $appConfig;
  38. /**
  39. * @var \OCP\IGroupManager
  40. */
  41. private $groupManager;
  42. /** @var \OCP\ICacheFactory */
  43. private $memCacheFactory;
  44. /**
  45. * @var string[] $appId => $enabled
  46. */
  47. private $installedAppsCache;
  48. /**
  49. * @param \OCP\IUserSession $userSession
  50. * @param \OCP\IAppConfig $appConfig
  51. * @param \OCP\IGroupManager $groupManager
  52. * @param \OCP\ICacheFactory $memCacheFactory
  53. */
  54. public function __construct(IUserSession $userSession,
  55. IAppConfig $appConfig,
  56. IGroupManager $groupManager,
  57. ICacheFactory $memCacheFactory) {
  58. $this->userSession = $userSession;
  59. $this->appConfig = $appConfig;
  60. $this->groupManager = $groupManager;
  61. $this->memCacheFactory = $memCacheFactory;
  62. }
  63. /**
  64. * @return string[] $appId => $enabled
  65. */
  66. private function getInstalledAppsValues() {
  67. if (!$this->installedAppsCache) {
  68. $values = $this->appConfig->getValues(false, 'enabled');
  69. $this->installedAppsCache = array_filter($values, function ($value) {
  70. return $value !== 'no';
  71. });
  72. ksort($this->installedAppsCache);
  73. }
  74. return $this->installedAppsCache;
  75. }
  76. /**
  77. * List all installed apps
  78. *
  79. * @return string[]
  80. */
  81. public function getInstalledApps() {
  82. return array_keys($this->getInstalledAppsValues());
  83. }
  84. /**
  85. * List all apps enabled for a user
  86. *
  87. * @param \OCP\IUser $user
  88. * @return string[]
  89. */
  90. public function getEnabledAppsForUser(IUser $user) {
  91. $apps = $this->getInstalledAppsValues();
  92. $appsForUser = array_filter($apps, function ($enabled) use ($user) {
  93. return $this->checkAppForUser($enabled, $user);
  94. });
  95. return array_keys($appsForUser);
  96. }
  97. /**
  98. * Check if an app is enabled for user
  99. *
  100. * @param string $appId
  101. * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
  102. * @return bool
  103. */
  104. public function isEnabledForUser($appId, $user = null) {
  105. if (is_null($user)) {
  106. $user = $this->userSession->getUser();
  107. }
  108. $installedApps = $this->getInstalledAppsValues();
  109. if (isset($installedApps[$appId])) {
  110. return $this->checkAppForUser($installedApps[$appId], $user);
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * @param string $enabled
  117. * @param IUser $user
  118. * @return bool
  119. */
  120. private function checkAppForUser($enabled, $user) {
  121. if ($enabled === 'yes') {
  122. return true;
  123. } elseif (is_null($user)) {
  124. return false;
  125. } else {
  126. $groupIds = json_decode($enabled);
  127. $userGroups = $this->groupManager->getUserGroupIds($user);
  128. foreach ($userGroups as $groupId) {
  129. if (array_search($groupId, $groupIds) !== false) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. }
  136. /**
  137. * Check if an app is installed in the instance
  138. *
  139. * @param string $appId
  140. * @return bool
  141. */
  142. public function isInstalled($appId) {
  143. $installedApps = $this->getInstalledAppsValues();
  144. return isset($installedApps[$appId]);
  145. }
  146. /**
  147. * Enable an app for every user
  148. *
  149. * @param string $appId
  150. */
  151. public function enableApp($appId) {
  152. $this->installedAppsCache[$appId] = 'yes';
  153. $this->appConfig->setValue($appId, 'enabled', 'yes');
  154. $this->clearAppsCache();
  155. }
  156. /**
  157. * Enable an app only for specific groups
  158. *
  159. * @param string $appId
  160. * @param \OCP\IGroup[] $groups
  161. */
  162. public function enableAppForGroups($appId, $groups) {
  163. $groupIds = array_map(function ($group) {
  164. /** @var \OCP\IGroup $group */
  165. return $group->getGID();
  166. }, $groups);
  167. $this->installedAppsCache[$appId] = json_encode($groupIds);
  168. $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
  169. $this->clearAppsCache();
  170. }
  171. /**
  172. * Disable an app for every user
  173. *
  174. * @param string $appId
  175. * @throws \Exception if app can't be disabled
  176. */
  177. public function disableApp($appId) {
  178. if ($appId === 'files') {
  179. throw new \Exception("files can't be disabled.");
  180. }
  181. unset($this->installedAppsCache[$appId]);
  182. $this->appConfig->setValue($appId, 'enabled', 'no');
  183. $this->clearAppsCache();
  184. }
  185. /**
  186. * Clear the cached list of apps when enabling/disabling an app
  187. */
  188. protected function clearAppsCache() {
  189. $settingsMemCache = $this->memCacheFactory->create('settings');
  190. $settingsMemCache->clear('listApps');
  191. }
  192. }