UpdateGroups.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Roger Szabo <roger.szabo@web.de>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\User_LDAP\Jobs;
  35. use OCA\User_LDAP\Access;
  36. use OCA\User_LDAP\Connection;
  37. use OCA\User_LDAP\FilesystemHelper;
  38. use OCA\User_LDAP\Helper;
  39. use OCA\User_LDAP\LDAP;
  40. use OCA\User_LDAP\LogWrapper;
  41. use OCA\User_LDAP\Mapping\GroupMapping;
  42. use OCA\User_LDAP\Mapping\UserMapping;
  43. use OCA\User_LDAP\User\Manager;
  44. use OCP\ILogger;
  45. class UpdateGroups extends \OC\BackgroundJob\TimedJob {
  46. static private $groupsFromDB;
  47. static private $groupBE;
  48. public function __construct(){
  49. $this->interval = self::getRefreshInterval();
  50. }
  51. /**
  52. * @param mixed $argument
  53. */
  54. public function run($argument){
  55. self::updateGroups();
  56. }
  57. static public function updateGroups() {
  58. \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
  59. $knownGroups = array_keys(self::getKnownGroups());
  60. $actualGroups = self::getGroupBE()->getGroups();
  61. if(empty($actualGroups) && empty($knownGroups)) {
  62. \OCP\Util::writeLog('user_ldap',
  63. 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
  64. ILogger::INFO);
  65. return;
  66. }
  67. self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  68. self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  69. self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  70. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
  71. }
  72. /**
  73. * @return int
  74. */
  75. static private function getRefreshInterval() {
  76. //defaults to every hour
  77. return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
  78. }
  79. /**
  80. * @param string[] $groups
  81. */
  82. static private function handleKnownGroups($groups) {
  83. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
  84. $query = \OC_DB::prepare('
  85. UPDATE `*PREFIX*ldap_group_members`
  86. SET `owncloudusers` = ?
  87. WHERE `owncloudname` = ?
  88. ');
  89. foreach($groups as $group) {
  90. //we assume, that self::$groupsFromDB has been retrieved already
  91. $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
  92. $actualUsers = self::getGroupBE()->usersInGroup($group);
  93. $hasChanged = false;
  94. foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
  95. \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', ['uid' => $removedUser, 'gid' => $group]);
  96. \OCP\Util::writeLog('user_ldap',
  97. 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
  98. ILogger::INFO);
  99. $hasChanged = true;
  100. }
  101. foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
  102. \OCP\Util::emitHook('OC_User', 'post_addToGroup', ['uid' => $addedUser, 'gid' => $group]);
  103. \OCP\Util::writeLog('user_ldap',
  104. 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
  105. ILogger::INFO);
  106. $hasChanged = true;
  107. }
  108. if($hasChanged) {
  109. $query->execute([serialize($actualUsers), $group]);
  110. }
  111. }
  112. \OCP\Util::writeLog('user_ldap',
  113. 'bgJ "updateGroups" – FINISHED dealing with known Groups.',
  114. ILogger::DEBUG);
  115. }
  116. /**
  117. * @param string[] $createdGroups
  118. */
  119. static private function handleCreatedGroups($createdGroups) {
  120. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
  121. $query = \OC_DB::prepare('
  122. INSERT
  123. INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
  124. VALUES (?, ?)
  125. ');
  126. foreach($createdGroups as $createdGroup) {
  127. \OCP\Util::writeLog('user_ldap',
  128. 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
  129. ILogger::INFO);
  130. $users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
  131. $query->execute([$createdGroup, $users]);
  132. }
  133. \OCP\Util::writeLog('user_ldap',
  134. 'bgJ "updateGroups" – FINISHED dealing with created Groups.',
  135. ILogger::DEBUG);
  136. }
  137. /**
  138. * @param string[] $removedGroups
  139. */
  140. static private function handleRemovedGroups($removedGroups) {
  141. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
  142. $query = \OC_DB::prepare('
  143. DELETE
  144. FROM `*PREFIX*ldap_group_members`
  145. WHERE `owncloudname` = ?
  146. ');
  147. foreach($removedGroups as $removedGroup) {
  148. \OCP\Util::writeLog('user_ldap',
  149. 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
  150. ILogger::INFO);
  151. $query->execute([$removedGroup]);
  152. }
  153. \OCP\Util::writeLog('user_ldap',
  154. 'bgJ "updateGroups" – FINISHED dealing with removed groups.',
  155. ILogger::DEBUG);
  156. }
  157. /**
  158. * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy
  159. */
  160. static private function getGroupBE() {
  161. if(!is_null(self::$groupBE)) {
  162. return self::$groupBE;
  163. }
  164. $helper = new Helper(\OC::$server->getConfig());
  165. $configPrefixes = $helper->getServerConfigurationPrefixes(true);
  166. $ldapWrapper = new LDAP();
  167. if(count($configPrefixes) === 1) {
  168. //avoid the proxy when there is only one LDAP server configured
  169. $dbc = \OC::$server->getDatabaseConnection();
  170. $userManager = new Manager(
  171. \OC::$server->getConfig(),
  172. new FilesystemHelper(),
  173. new LogWrapper(),
  174. \OC::$server->getAvatarManager(),
  175. new \OCP\Image(),
  176. $dbc,
  177. \OC::$server->getUserManager(),
  178. \OC::$server->getNotificationManager());
  179. $connector = new Connection($ldapWrapper, $configPrefixes[0]);
  180. $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager());
  181. $groupMapper = new GroupMapping($dbc);
  182. $userMapper = new UserMapping($dbc);
  183. $ldapAccess->setGroupMapper($groupMapper);
  184. $ldapAccess->setUserMapper($userMapper);
  185. self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query('LDAPGroupPluginManager'));
  186. } else {
  187. self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query('LDAPGroupPluginManager'));
  188. }
  189. return self::$groupBE;
  190. }
  191. /**
  192. * @return array
  193. */
  194. static private function getKnownGroups() {
  195. if(is_array(self::$groupsFromDB)) {
  196. return self::$groupsFromDB;
  197. }
  198. $query = \OC_DB::prepare('
  199. SELECT `owncloudname`, `owncloudusers`
  200. FROM `*PREFIX*ldap_group_members`
  201. ');
  202. $result = $query->execute()->fetchAll();
  203. self::$groupsFromDB = [];
  204. foreach($result as $dataset) {
  205. self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
  206. }
  207. return self::$groupsFromDB;
  208. }
  209. }