AppManager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Schaefer <christophł@wolkesicher.de>
  8. * @author Christoph Wurst <christoph@owncloud.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\App;
  32. use OCP\App\AppPathNotFoundException;
  33. use OCP\App\IAppManager;
  34. use OCP\App\ManagerEvent;
  35. use OCP\IAppConfig;
  36. use OCP\ICacheFactory;
  37. use OCP\IGroupManager;
  38. use OCP\IUser;
  39. use OCP\IUserSession;
  40. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  41. class AppManager implements IAppManager {
  42. /**
  43. * Apps with these types can not be enabled for certain groups only
  44. * @var string[]
  45. */
  46. protected $protectedAppTypes = [
  47. 'filesystem',
  48. 'prelogin',
  49. 'authentication',
  50. 'logging',
  51. 'prevent_group_restriction',
  52. ];
  53. /** @var IUserSession */
  54. private $userSession;
  55. /** @var IAppConfig */
  56. private $appConfig;
  57. /** @var IGroupManager */
  58. private $groupManager;
  59. /** @var ICacheFactory */
  60. private $memCacheFactory;
  61. /** @var EventDispatcherInterface */
  62. private $dispatcher;
  63. /** @var string[] $appId => $enabled */
  64. private $installedAppsCache;
  65. /** @var string[] */
  66. private $shippedApps;
  67. /** @var string[] */
  68. private $alwaysEnabled;
  69. /**
  70. * @param IUserSession $userSession
  71. * @param IAppConfig $appConfig
  72. * @param IGroupManager $groupManager
  73. * @param ICacheFactory $memCacheFactory
  74. * @param EventDispatcherInterface $dispatcher
  75. */
  76. public function __construct(IUserSession $userSession,
  77. IAppConfig $appConfig,
  78. IGroupManager $groupManager,
  79. ICacheFactory $memCacheFactory,
  80. EventDispatcherInterface $dispatcher) {
  81. $this->userSession = $userSession;
  82. $this->appConfig = $appConfig;
  83. $this->groupManager = $groupManager;
  84. $this->memCacheFactory = $memCacheFactory;
  85. $this->dispatcher = $dispatcher;
  86. }
  87. /**
  88. * @return string[] $appId => $enabled
  89. */
  90. private function getInstalledAppsValues() {
  91. if (!$this->installedAppsCache) {
  92. $values = $this->appConfig->getValues(false, 'enabled');
  93. $alwaysEnabledApps = $this->getAlwaysEnabledApps();
  94. foreach($alwaysEnabledApps as $appId) {
  95. $values[$appId] = 'yes';
  96. }
  97. $this->installedAppsCache = array_filter($values, function ($value) {
  98. return $value !== 'no';
  99. });
  100. ksort($this->installedAppsCache);
  101. }
  102. return $this->installedAppsCache;
  103. }
  104. /**
  105. * List all installed apps
  106. *
  107. * @return string[]
  108. */
  109. public function getInstalledApps() {
  110. return array_keys($this->getInstalledAppsValues());
  111. }
  112. /**
  113. * List all apps enabled for a user
  114. *
  115. * @param \OCP\IUser $user
  116. * @return string[]
  117. */
  118. public function getEnabledAppsForUser(IUser $user) {
  119. $apps = $this->getInstalledAppsValues();
  120. $appsForUser = array_filter($apps, function ($enabled) use ($user) {
  121. return $this->checkAppForUser($enabled, $user);
  122. });
  123. return array_keys($appsForUser);
  124. }
  125. /**
  126. * Check if an app is enabled for user
  127. *
  128. * @param string $appId
  129. * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
  130. * @return bool
  131. */
  132. public function isEnabledForUser($appId, $user = null) {
  133. if ($this->isAlwaysEnabled($appId)) {
  134. return true;
  135. }
  136. if ($user === null) {
  137. $user = $this->userSession->getUser();
  138. }
  139. $installedApps = $this->getInstalledAppsValues();
  140. if (isset($installedApps[$appId])) {
  141. return $this->checkAppForUser($installedApps[$appId], $user);
  142. } else {
  143. return false;
  144. }
  145. }
  146. /**
  147. * @param string $enabled
  148. * @param IUser $user
  149. * @return bool
  150. */
  151. private function checkAppForUser($enabled, $user) {
  152. if ($enabled === 'yes') {
  153. return true;
  154. } elseif ($user === null) {
  155. return false;
  156. } else {
  157. if(empty($enabled)){
  158. return false;
  159. }
  160. $groupIds = json_decode($enabled);
  161. if (!is_array($groupIds)) {
  162. $jsonError = json_last_error();
  163. \OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
  164. return false;
  165. }
  166. $userGroups = $this->groupManager->getUserGroupIds($user);
  167. foreach ($userGroups as $groupId) {
  168. if (in_array($groupId, $groupIds, true)) {
  169. return true;
  170. }
  171. }
  172. return false;
  173. }
  174. }
  175. /**
  176. * Check if an app is installed in the instance
  177. *
  178. * @param string $appId
  179. * @return bool
  180. */
  181. public function isInstalled($appId) {
  182. $installedApps = $this->getInstalledAppsValues();
  183. return isset($installedApps[$appId]);
  184. }
  185. /**
  186. * Enable an app for every user
  187. *
  188. * @param string $appId
  189. * @throws AppPathNotFoundException
  190. */
  191. public function enableApp($appId) {
  192. // Check if app exists
  193. $this->getAppPath($appId);
  194. $this->installedAppsCache[$appId] = 'yes';
  195. $this->appConfig->setValue($appId, 'enabled', 'yes');
  196. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
  197. ManagerEvent::EVENT_APP_ENABLE, $appId
  198. ));
  199. $this->clearAppsCache();
  200. }
  201. /**
  202. * Whether a list of types contains a protected app type
  203. *
  204. * @param string[] $types
  205. * @return bool
  206. */
  207. public function hasProtectedAppType($types) {
  208. if (empty($types)) {
  209. return false;
  210. }
  211. $protectedTypes = array_intersect($this->protectedAppTypes, $types);
  212. return !empty($protectedTypes);
  213. }
  214. /**
  215. * Enable an app only for specific groups
  216. *
  217. * @param string $appId
  218. * @param \OCP\IGroup[] $groups
  219. * @throws \Exception if app can't be enabled for groups
  220. */
  221. public function enableAppForGroups($appId, $groups) {
  222. $info = $this->getAppInfo($appId);
  223. if (!empty($info['types'])) {
  224. $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
  225. if (!empty($protectedTypes)) {
  226. throw new \Exception("$appId can't be enabled for groups.");
  227. }
  228. }
  229. $groupIds = array_map(function ($group) {
  230. /** @var \OCP\IGroup $group */
  231. return $group->getGID();
  232. }, $groups);
  233. $this->installedAppsCache[$appId] = json_encode($groupIds);
  234. $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
  235. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
  236. ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
  237. ));
  238. $this->clearAppsCache();
  239. }
  240. /**
  241. * Disable an app for every user
  242. *
  243. * @param string $appId
  244. * @throws \Exception if app can't be disabled
  245. */
  246. public function disableApp($appId) {
  247. if ($this->isAlwaysEnabled($appId)) {
  248. throw new \Exception("$appId can't be disabled.");
  249. }
  250. unset($this->installedAppsCache[$appId]);
  251. $this->appConfig->setValue($appId, 'enabled', 'no');
  252. $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
  253. ManagerEvent::EVENT_APP_DISABLE, $appId
  254. ));
  255. $this->clearAppsCache();
  256. }
  257. /**
  258. * Get the directory for the given app.
  259. *
  260. * @param string $appId
  261. * @return string
  262. * @throws AppPathNotFoundException if app folder can't be found
  263. */
  264. public function getAppPath($appId) {
  265. $appPath = \OC_App::getAppPath($appId);
  266. if($appPath === false) {
  267. throw new AppPathNotFoundException('Could not find path for ' . $appId);
  268. }
  269. return $appPath;
  270. }
  271. /**
  272. * Clear the cached list of apps when enabling/disabling an app
  273. */
  274. public function clearAppsCache() {
  275. $settingsMemCache = $this->memCacheFactory->create('settings');
  276. $settingsMemCache->clear('listApps');
  277. }
  278. /**
  279. * Returns a list of apps that need upgrade
  280. *
  281. * @param string $version Nextcloud version as array of version components
  282. * @return array list of app info from apps that need an upgrade
  283. *
  284. * @internal
  285. */
  286. public function getAppsNeedingUpgrade($version) {
  287. $appsToUpgrade = [];
  288. $apps = $this->getInstalledApps();
  289. foreach ($apps as $appId) {
  290. $appInfo = $this->getAppInfo($appId);
  291. $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
  292. if ($appDbVersion
  293. && isset($appInfo['version'])
  294. && version_compare($appInfo['version'], $appDbVersion, '>')
  295. && \OC_App::isAppCompatible($version, $appInfo)
  296. ) {
  297. $appsToUpgrade[] = $appInfo;
  298. }
  299. }
  300. return $appsToUpgrade;
  301. }
  302. /**
  303. * Returns the app information from "appinfo/info.xml".
  304. *
  305. * @param string $appId app id
  306. *
  307. * @return array app info
  308. *
  309. * @internal
  310. */
  311. public function getAppInfo($appId) {
  312. $appInfo = \OC_App::getAppInfo($appId);
  313. if (!isset($appInfo['version'])) {
  314. // read version from separate file
  315. $appInfo['version'] = \OC_App::getAppVersion($appId);
  316. }
  317. return $appInfo;
  318. }
  319. /**
  320. * Returns a list of apps incompatible with the given version
  321. *
  322. * @param string $version Nextcloud version as array of version components
  323. *
  324. * @return array list of app info from incompatible apps
  325. *
  326. * @internal
  327. */
  328. public function getIncompatibleApps($version) {
  329. $apps = $this->getInstalledApps();
  330. $incompatibleApps = array();
  331. foreach ($apps as $appId) {
  332. $info = $this->getAppInfo($appId);
  333. if (!\OC_App::isAppCompatible($version, $info)) {
  334. $incompatibleApps[] = $info;
  335. }
  336. }
  337. return $incompatibleApps;
  338. }
  339. /**
  340. * @inheritdoc
  341. */
  342. public function isShipped($appId) {
  343. $this->loadShippedJson();
  344. return in_array($appId, $this->shippedApps, true);
  345. }
  346. private function isAlwaysEnabled($appId) {
  347. $alwaysEnabled = $this->getAlwaysEnabledApps();
  348. return in_array($appId, $alwaysEnabled, true);
  349. }
  350. private function loadShippedJson() {
  351. if ($this->shippedApps === null) {
  352. $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
  353. if (!file_exists($shippedJson)) {
  354. throw new \Exception("File not found: $shippedJson");
  355. }
  356. $content = json_decode(file_get_contents($shippedJson), true);
  357. $this->shippedApps = $content['shippedApps'];
  358. $this->alwaysEnabled = $content['alwaysEnabled'];
  359. }
  360. }
  361. /**
  362. * @inheritdoc
  363. */
  364. public function getAlwaysEnabledApps() {
  365. $this->loadShippedJson();
  366. return $this->alwaysEnabled;
  367. }
  368. }