jobs.php 6.5 KB

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