jobs.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. class Jobs extends \OC\BackgroundJob\TimedJob {
  24. static private $groupsFromDB;
  25. static private $groupBE;
  26. static private $connector;
  27. public function __construct(){
  28. $this->interval = self::getRefreshInterval();
  29. }
  30. public function run($argument){
  31. Jobs::updateGroups();
  32. }
  33. static public function updateGroups() {
  34. \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG);
  35. $knownGroups = array_keys(self::getKnownGroups());
  36. $actualGroups = self::getGroupBE()->getGroups();
  37. if(empty($actualGroups) && empty($knownGroups)) {
  38. \OCP\Util::writeLog('user_ldap',
  39. 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
  40. \OCP\Util::INFO);
  41. return;
  42. }
  43. self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  44. self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  45. self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  46. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG);
  47. }
  48. static private function getRefreshInterval() {
  49. //defaults to every hour
  50. return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
  51. }
  52. static private function handleKnownGroups($groups) {
  53. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG);
  54. $query = \OCP\DB::prepare('
  55. UPDATE `*PREFIX*ldap_group_members`
  56. SET `owncloudusers` = ?
  57. WHERE `owncloudname` = ?
  58. ');
  59. foreach($groups as $group) {
  60. //we assume, that self::$groupsFromDB has been retrieved already
  61. $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
  62. $actualUsers = self::getGroupBE()->usersInGroup($group);
  63. $hasChanged = false;
  64. foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
  65. \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group));
  66. \OCP\Util::writeLog('user_ldap',
  67. 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
  68. \OCP\Util::INFO);
  69. $hasChanged = true;
  70. }
  71. foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
  72. \OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group));
  73. \OCP\Util::writeLog('user_ldap',
  74. 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
  75. \OCP\Util::INFO);
  76. $hasChanged = true;
  77. }
  78. if($hasChanged) {
  79. $query->execute(array(serialize($actualUsers), $group));
  80. }
  81. }
  82. \OCP\Util::writeLog('user_ldap',
  83. 'bgJ "updateGroups" – FINISHED dealing with known Groups.',
  84. \OCP\Util::DEBUG);
  85. }
  86. static private function handleCreatedGroups($createdGroups) {
  87. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG);
  88. $query = \OCP\DB::prepare('
  89. INSERT
  90. INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
  91. VALUES (?, ?)
  92. ');
  93. foreach($createdGroups as $createdGroup) {
  94. \OCP\Util::writeLog('user_ldap',
  95. 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
  96. \OCP\Util::INFO);
  97. $users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
  98. $query->execute(array($createdGroup, $users));
  99. }
  100. \OCP\Util::writeLog('user_ldap',
  101. 'bgJ "updateGroups" – FINISHED dealing with created Groups.',
  102. \OCP\Util::DEBUG);
  103. }
  104. static private function handleRemovedGroups($removedGroups) {
  105. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG);
  106. $query = \OCP\DB::prepare('
  107. DELETE
  108. FROM `*PREFIX*ldap_group_members`
  109. WHERE `owncloudname` = ?
  110. ');
  111. foreach($removedGroups as $removedGroup) {
  112. \OCP\Util::writeLog('user_ldap',
  113. 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
  114. \OCP\Util::INFO);
  115. $query->execute(array($removedGroup));
  116. }
  117. \OCP\Util::writeLog('user_ldap',
  118. 'bgJ "updateGroups" – FINISHED dealing with removed groups.',
  119. \OCP\Util::DEBUG);
  120. }
  121. static private function getGroupBE() {
  122. if(!is_null(self::$groupBE)) {
  123. return self::$groupBE;
  124. }
  125. $configPrefixes = Helper::getServerConfigurationPrefixes(true);
  126. $ldapWrapper = new LDAP();
  127. if(count($configPrefixes) === 1) {
  128. //avoid the proxy when there is only one LDAP server configured
  129. $connector = new Connection($ldapWrapper, $configPrefixes[0]);
  130. $ldapAccess = new Access($connector, $ldapWrapper);
  131. self::$groupBE = new \OCA\user_ldap\GROUP_LDAP($ldapAccess);
  132. } else {
  133. self::$groupBE = new \OCA\user_ldap\Group_Proxy($configPrefixes, $ldapWrapper);
  134. }
  135. return self::$groupBE;
  136. }
  137. static private function getKnownGroups() {
  138. if(is_array(self::$groupsFromDB)) {
  139. return self::$groupsFromDB;
  140. }
  141. $query = \OCP\DB::prepare('
  142. SELECT `owncloudname`, `owncloudusers`
  143. FROM `*PREFIX*ldap_group_members`
  144. ');
  145. $result = $query->execute()->fetchAll();
  146. self::$groupsFromDB = array();
  147. foreach($result as $dataset) {
  148. self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
  149. }
  150. return self::$groupsFromDB;
  151. }
  152. }