jobs.php 6.8 KB

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