diff --git a/apps/user_ldap/appinfo/app.php b/apps/user_ldap/appinfo/app.php
index 3c6da47d71..0eec7829a4 100644
--- a/apps/user_ldap/appinfo/app.php
+++ b/apps/user_ldap/appinfo/app.php
@@ -40,3 +40,5 @@ $entry = array(
'href' => OCP\Util::linkTo( 'user_ldap', 'settings.php' ),
'name' => 'LDAP'
);
+
+OCP\Backgroundjob::addRegularTask('OCA\user_ldap\lib\Jobs', 'updateGroups');
diff --git a/apps/user_ldap/appinfo/database.xml b/apps/user_ldap/appinfo/database.xml
index b228fa2796..3bbd2b09a4 100644
--- a/apps/user_ldap/appinfo/database.xml
+++ b/apps/user_ldap/appinfo/database.xml
@@ -92,4 +92,37 @@
+
+
+
+ *dbprefix*ldap_group_members
+
+
+
+
+ owncloudname
+ text
+ true
+ 255
+
+
+
+
+ owncloudusers
+ clob
+ true
+
+
+
+ ldap_group_members
+ true
+
+ owncloudname
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apps/user_ldap/appinfo/version b/apps/user_ldap/appinfo/version
index 5f021e960e..1683a26615 100644
--- a/apps/user_ldap/appinfo/version
+++ b/apps/user_ldap/appinfo/version
@@ -1 +1 @@
-0.2.0.5
\ No newline at end of file
+0.2.0.8
\ No newline at end of file
diff --git a/apps/user_ldap/lib/jobs.php b/apps/user_ldap/lib/jobs.php
new file mode 100644
index 0000000000..76a8e3cbb2
--- /dev/null
+++ b/apps/user_ldap/lib/jobs.php
@@ -0,0 +1,156 @@
+.
+ *
+ */
+
+namespace OCA\user_ldap\lib;
+
+class Jobs {
+
+ //refresh groups every hour
+ static private $refreshInterval = 36;//00;
+
+ static private $groupsFromDB;
+
+ static private $groupBE;
+ static private $connector;
+
+ static public function updateGroups() {
+ \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG);
+ $lastUpdate = \OCP\Config::getAppValue('user_ldap', 'bgjUpdateGroupsLastRun', 0);
+ if((time() - $lastUpdate) < self::$refreshInterval) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – last run too fresh, aborting.', \OCP\Util::DEBUG);
+ //komm runter Werner die Maurer geben ein aus
+ return;
+ }
+
+ $knownGroups = array_keys(self::getKnownGroups());
+ $actualGroups = self::getGroupBE()->getGroups();
+
+ if(empty($actualGroups) && empty($knownGroups)) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', \OCP\Util::INFO);
+ \OCP\setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time());
+ return;
+ }
+
+ self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
+ self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
+ self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
+
+ \OCP\Config::setAppValue('user_ldap', 'bgjUpdateGroupsLastRun', time());
+
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG);
+ }
+
+ static private function handleKnownGroups($groups) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG);
+ $query = \OCP\DB::prepare('
+ UPDATE *PREFIX*ldap_group_members
+ SET owncloudusers = ?
+ WHERE owncloudname = ?
+ ');
+ foreach($groups as $group) {
+ //we assume, that self::$groupsFromDB has been retrieved already
+ $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
+ $actualUsers = self::getGroupBE()->usersInGroup($group);
+ $hasChanged = false;
+ foreach(array_diff($knownUsers, $actualUsers) as $removedUser) {
+ \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group));
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".', \OCP\Util::INFO);
+ $hasChanged = true;
+ }
+ foreach(array_diff($actualUsers, $knownUsers) as $addedUser) {
+ \OCP\Util::emitHook('OC_User', 'post_addFromGroup', array('uid' => $addedUser, 'gid' => $group));
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".', \OCP\Util::INFO);
+ $hasChanged = true;
+ }
+ if($hasChanged) {
+ $query->execute(array(serialize($actualUsers), $group));
+ }
+ }
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – FINISHED dealing with known Groups.', \OCP\Util::DEBUG);
+ }
+
+ static private function handleCreatedGroups($createdGroups) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG);
+ $query = \OCP\DB::prepare('
+ INSERT
+ INTO *PREFIX*ldap_group_members (owncloudname, owncloudusers)
+ VALUES (?, ?)
+ ');
+ foreach($createdGroups as $createdGroup) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.', \OCP\Util::INFO);
+ $users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
+ $query->execute(array($createdGroup, $users));
+ }
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – FINISHED dealing with created Groups.', \OCP\Util::DEBUG);
+ }
+
+ static private function handleRemovedGroups($removedGroups) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG);
+ $query = \OCP\DB::prepare('
+ DELETE
+ FROM *PREFIX*ldap_group_members
+ WHERE owncloudname = ?
+ ');
+ foreach($removedGroups as $removedGroup) {
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – group "'.$createdGroup.'" was removed.', \OCP\Util::INFO);
+ $query->execute(array($removedGroup));
+ }
+ \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – FINISHED dealing with removed groups.', \OCP\Util::DEBUG);
+ }
+
+ static private function getConnector() {
+ if(!is_null(self::$connector)) {
+ return self::$connector;
+ }
+ self::$connector = new \OCA\user_ldap\lib\Connection('user_ldap');
+ return self::$connector;
+ }
+
+ static private function getGroupBE() {
+ if(!is_null(self::$groupBE)) {
+ return self::$groupBE;
+ }
+ self::getConnector();
+ self::$groupBE = new \OCA\user_ldap\GROUP_LDAP();
+ self::$groupBE->setConnector(self::$connector);
+
+ return self::$groupBE;
+ }
+
+ static private function getKnownGroups() {
+ if(is_array(self::$groupsFromDB)) {
+ return self::$groupsFromDB;
+ }
+ $query = \OCP\DB::prepare('
+ SELECT owncloudname, owncloudusers
+ FROM *PREFIX*ldap_group_members
+ ');
+ $result = $query->execute()->fetchAll();
+ self::$groupsFromDB = array();
+ foreach($result as $dataset) {
+ self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
+ }
+
+ return self::$groupsFromDB;
+ }
+}
\ No newline at end of file