appmanager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\App;
  25. use OCP\App\IAppManager;
  26. use OCP\IAppConfig;
  27. use OCP\ICacheFactory;
  28. use OCP\IGroupManager;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. class AppManager implements IAppManager {
  32. /**
  33. * @var \OCP\IUserSession
  34. */
  35. private $userSession;
  36. /**
  37. * @var \OCP\IAppConfig
  38. */
  39. private $appConfig;
  40. /**
  41. * @var \OCP\IGroupManager
  42. */
  43. private $groupManager;
  44. /** @var \OCP\ICacheFactory */
  45. private $memCacheFactory;
  46. /**
  47. * @var string[] $appId => $enabled
  48. */
  49. private $installedAppsCache;
  50. /**
  51. * @param \OCP\IUserSession $userSession
  52. * @param \OCP\IAppConfig $appConfig
  53. * @param \OCP\IGroupManager $groupManager
  54. * @param \OCP\ICacheFactory $memCacheFactory
  55. */
  56. public function __construct(IUserSession $userSession,
  57. IAppConfig $appConfig,
  58. IGroupManager $groupManager,
  59. ICacheFactory $memCacheFactory) {
  60. $this->userSession = $userSession;
  61. $this->appConfig = $appConfig;
  62. $this->groupManager = $groupManager;
  63. $this->memCacheFactory = $memCacheFactory;
  64. }
  65. /**
  66. * @return string[] $appId => $enabled
  67. */
  68. private function getInstalledAppsValues() {
  69. if (!$this->installedAppsCache) {
  70. $values = $this->appConfig->getValues(false, 'enabled');
  71. $this->installedAppsCache = array_filter($values, function ($value) {
  72. return $value !== 'no';
  73. });
  74. ksort($this->installedAppsCache);
  75. }
  76. return $this->installedAppsCache;
  77. }
  78. /**
  79. * List all installed apps
  80. *
  81. * @return string[]
  82. */
  83. public function getInstalledApps() {
  84. return array_keys($this->getInstalledAppsValues());
  85. }
  86. /**
  87. * List all apps enabled for a user
  88. *
  89. * @param \OCP\IUser $user
  90. * @return string[]
  91. */
  92. public function getEnabledAppsForUser(IUser $user) {
  93. $apps = $this->getInstalledAppsValues();
  94. $appsForUser = array_filter($apps, function ($enabled) use ($user) {
  95. return $this->checkAppForUser($enabled, $user);
  96. });
  97. return array_keys($appsForUser);
  98. }
  99. /**
  100. * Check if an app is enabled for user
  101. *
  102. * @param string $appId
  103. * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
  104. * @return bool
  105. */
  106. public function isEnabledForUser($appId, $user = null) {
  107. if (is_null($user)) {
  108. $user = $this->userSession->getUser();
  109. }
  110. $installedApps = $this->getInstalledAppsValues();
  111. if (isset($installedApps[$appId])) {
  112. return $this->checkAppForUser($installedApps[$appId], $user);
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * @param string $enabled
  119. * @param IUser $user
  120. * @return bool
  121. */
  122. private function checkAppForUser($enabled, $user) {
  123. if ($enabled === 'yes') {
  124. return true;
  125. } elseif (is_null($user)) {
  126. return false;
  127. } else {
  128. $groupIds = json_decode($enabled);
  129. $userGroups = $this->groupManager->getUserGroupIds($user);
  130. foreach ($userGroups as $groupId) {
  131. if (array_search($groupId, $groupIds) !== false) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. }
  138. /**
  139. * Check if an app is installed in the instance
  140. *
  141. * @param string $appId
  142. * @return bool
  143. */
  144. public function isInstalled($appId) {
  145. $installedApps = $this->getInstalledAppsValues();
  146. return isset($installedApps[$appId]);
  147. }
  148. /**
  149. * Enable an app for every user
  150. *
  151. * @param string $appId
  152. */
  153. public function enableApp($appId) {
  154. $this->installedAppsCache[$appId] = 'yes';
  155. $this->appConfig->setValue($appId, 'enabled', 'yes');
  156. $this->clearAppsCache();
  157. }
  158. /**
  159. * Enable an app only for specific groups
  160. *
  161. * @param string $appId
  162. * @param \OCP\IGroup[] $groups
  163. */
  164. public function enableAppForGroups($appId, $groups) {
  165. $groupIds = array_map(function ($group) {
  166. /** @var \OCP\IGroup $group */
  167. return $group->getGID();
  168. }, $groups);
  169. $this->installedAppsCache[$appId] = json_encode($groupIds);
  170. $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
  171. $this->clearAppsCache();
  172. }
  173. /**
  174. * Disable an app for every user
  175. *
  176. * @param string $appId
  177. * @throws \Exception if app can't be disabled
  178. */
  179. public function disableApp($appId) {
  180. if ($appId === 'files') {
  181. throw new \Exception("files can't be disabled.");
  182. }
  183. unset($this->installedAppsCache[$appId]);
  184. $this->appConfig->setValue($appId, 'enabled', 'no');
  185. $this->clearAppsCache();
  186. }
  187. /**
  188. * Clear the cached list of apps when enabling/disabling an app
  189. */
  190. public function clearAppsCache() {
  191. $settingsMemCache = $this->memCacheFactory->create('settings');
  192. $settingsMemCache->clear('listApps');
  193. }
  194. /**
  195. * Returns a list of apps that need upgrade
  196. *
  197. * @param array $version ownCloud version as array of version components
  198. * @return array list of app info from apps that need an upgrade
  199. *
  200. * @internal
  201. */
  202. public function getAppsNeedingUpgrade($ocVersion) {
  203. $appsToUpgrade = [];
  204. $apps = $this->getInstalledApps();
  205. foreach ($apps as $appId) {
  206. $appInfo = $this->getAppInfo($appId);
  207. $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
  208. if ($appDbVersion
  209. && isset($appInfo['version'])
  210. && version_compare($appInfo['version'], $appDbVersion, '>')
  211. && \OC_App::isAppCompatible($ocVersion, $appInfo)
  212. ) {
  213. $appsToUpgrade[] = $appInfo;
  214. }
  215. }
  216. return $appsToUpgrade;
  217. }
  218. /**
  219. * Returns the app information from "appinfo/info.xml".
  220. *
  221. * If no version was present in "appinfo/info.xml", reads it
  222. * from the external "appinfo/version" file instead.
  223. *
  224. * @param string $appId app id
  225. *
  226. * @return array app iinfo
  227. *
  228. * @internal
  229. */
  230. public function getAppInfo($appId) {
  231. $appInfo = \OC_App::getAppInfo($appId);
  232. if (!isset($appInfo['version'])) {
  233. // read version from separate file
  234. $appInfo['version'] = \OC_App::getAppVersion($appId);
  235. }
  236. return $appInfo;
  237. }
  238. /**
  239. * Returns a list of apps incompatible with the given version
  240. *
  241. * @param array $version ownCloud version as array of version components
  242. *
  243. * @return array list of app info from incompatible apps
  244. *
  245. * @internal
  246. */
  247. public function getIncompatibleApps($version) {
  248. $apps = $this->getInstalledApps();
  249. $incompatibleApps = array();
  250. foreach ($apps as $appId) {
  251. $info = $this->getAppInfo($appId);
  252. if (!\OC_App::isAppCompatible($version, $info)) {
  253. $incompatibleApps[] = $info;
  254. }
  255. }
  256. return $incompatibleApps;
  257. }
  258. }