jobs.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 {
  24. static private $groupsFromDB;
  25. static private $groupBE;
  26. static private $connector;
  27. static public function updateGroups() {
  28. \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG);
  29. $lastUpdate = \OCP\Config::getAppValue('user_ldap', 'bgjUpdateGroupsLastRun', 0);
  30. if((time() - $lastUpdate) < self::getRefreshInterval()) {
  31. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – last run too fresh, aborting.', \OCP\Util::DEBUG);
  32. //komm runter Werner die Maurer geben ein aus
  33. return;
  34. }
  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. \OCP\Config::setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time());
  42. return;
  43. }
  44. self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  45. self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  46. self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  47. \OCP\Config::setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time());
  48. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG);
  49. }
  50. static private function getRefreshInterval() {
  51. //defaults to every hour
  52. return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
  53. }
  54. static private function handleKnownGroups($groups) {
  55. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG);
  56. $query = \OCP\DB::prepare('
  57. UPDATE `*PREFIX*ldap_group_members`
  58. SET `owncloudusers` = ?
  59. WHERE `owncloudname` = ?
  60. ');
  61. foreach($groups as $group) {
  62. //we assume, that self::$groupsFromDB has been retrieved already
  63. $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
  64. $actualUsers = self::getGroupBE()->usersInGroup($group);
  65. $hasChanged = false;
  66. foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
  67. \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group));
  68. \OCP\Util::writeLog('user_ldap',
  69. 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
  70. \OCP\Util::INFO);
  71. $hasChanged = true;
  72. }
  73. foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
  74. \OCP\Util::emitHook('OC_User', 'post_addFromGroup', array('uid' => $addedUser, 'gid' => $group));
  75. \OCP\Util::writeLog('user_ldap',
  76. 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
  77. \OCP\Util::INFO);
  78. $hasChanged = true;
  79. }
  80. if($hasChanged) {
  81. $query->execute(array(serialize($actualUsers), $group));
  82. }
  83. }
  84. \OCP\Util::writeLog('user_ldap',
  85. 'bgJ "updateGroups" – FINISHED dealing with known Groups.',
  86. \OCP\Util::DEBUG);
  87. }
  88. static private function handleCreatedGroups($createdGroups) {
  89. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG);
  90. $query = \OCP\DB::prepare('
  91. INSERT
  92. INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
  93. VALUES (?, ?)
  94. ');
  95. foreach($createdGroups as $createdGroup) {
  96. \OCP\Util::writeLog('user_ldap',
  97. 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
  98. \OCP\Util::INFO);
  99. $users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
  100. $query->execute(array($createdGroup, $users));
  101. }
  102. \OCP\Util::writeLog('user_ldap',
  103. 'bgJ "updateGroups" – FINISHED dealing with created Groups.',
  104. \OCP\Util::DEBUG);
  105. }
  106. static private function handleRemovedGroups($removedGroups) {
  107. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG);
  108. $query = \OCP\DB::prepare('
  109. DELETE
  110. FROM `*PREFIX*ldap_group_members`
  111. WHERE `owncloudname` = ?
  112. ');
  113. foreach($removedGroups as $removedGroup) {
  114. \OCP\Util::writeLog('user_ldap',
  115. 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
  116. \OCP\Util::INFO);
  117. $query->execute(array($removedGroup));
  118. }
  119. \OCP\Util::writeLog('user_ldap',
  120. 'bgJ "updateGroups" – FINISHED dealing with removed groups.',
  121. \OCP\Util::DEBUG);
  122. }
  123. static private function getConnector() {
  124. if(!is_null(self::$connector)) {
  125. return self::$connector;
  126. }
  127. self::$connector = new \OCA\user_ldap\lib\Connection('user_ldap');
  128. return self::$connector;
  129. }
  130. static private function getGroupBE() {
  131. if(!is_null(self::$groupBE)) {
  132. return self::$groupBE;
  133. }
  134. self::getConnector();
  135. self::$groupBE = new \OCA\user_ldap\GROUP_LDAP();
  136. self::$groupBE->setConnector(self::$connector);
  137. return self::$groupBE;
  138. }
  139. static private function getKnownGroups() {
  140. if(is_array(self::$groupsFromDB)) {
  141. return self::$groupsFromDB;
  142. }
  143. $query = \OCP\DB::prepare('
  144. SELECT `owncloudname`, `owncloudusers`
  145. FROM `*PREFIX*ldap_group_members`
  146. ');
  147. $result = $query->execute()->fetchAll();
  148. self::$groupsFromDB = array();
  149. foreach($result as $dataset) {
  150. self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
  151. }
  152. return self::$groupsFromDB;
  153. }
  154. }