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.

AppManager.php 12KB

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