Passed
Push — master ( a89c8a...40eabf )
by Blizzz
16:59 queued 04:40
created
apps/user_ldap/lib/Jobs/UpdateGroups.php 1 patch
Indentation   +173 added lines, -173 removed lines patch added patch discarded remove patch
@@ -49,192 +49,192 @@
 block discarded – undo
49 49
 use OCP\ILogger;
50 50
 
51 51
 class UpdateGroups extends \OC\BackgroundJob\TimedJob {
52
-	private static $groupsFromDB;
53
-
54
-	private static $groupBE;
55
-
56
-	public function __construct() {
57
-		$this->interval = self::getRefreshInterval();
58
-	}
59
-
60
-	/**
61
-	 * @param mixed $argument
62
-	 */
63
-	public function run($argument) {
64
-		self::updateGroups();
65
-	}
66
-
67
-	public static function updateGroups() {
68
-		\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
69
-
70
-		$knownGroups = array_keys(self::getKnownGroups());
71
-		$actualGroups = self::getGroupBE()->getGroups();
72
-
73
-		if (empty($actualGroups) && empty($knownGroups)) {
74
-			\OCP\Util::writeLog('user_ldap',
75
-				'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
76
-				ILogger::INFO);
77
-			return;
78
-		}
79
-
80
-		self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
81
-		self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
82
-		self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
83
-
84
-		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
85
-	}
86
-
87
-	/**
88
-	 * @return int
89
-	 */
90
-	private static function getRefreshInterval() {
91
-		//defaults to every hour
92
-		return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
93
-	}
94
-
95
-	/**
96
-	 * @param string[] $groups
97
-	 */
98
-	private static function handleKnownGroups($groups) {
99
-		/** @var IEventDispatcher $dispatcher */
100
-		$dispatcher = \OC::$server->query(IEventDispatcher::class);
101
-		$groupManager = \OC::$server->getGroupManager();
102
-		$userManager = \OC::$server->getUserManager();
103
-
104
-		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
105
-		$query = \OC_DB::prepare('
52
+    private static $groupsFromDB;
53
+
54
+    private static $groupBE;
55
+
56
+    public function __construct() {
57
+        $this->interval = self::getRefreshInterval();
58
+    }
59
+
60
+    /**
61
+     * @param mixed $argument
62
+     */
63
+    public function run($argument) {
64
+        self::updateGroups();
65
+    }
66
+
67
+    public static function updateGroups() {
68
+        \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
69
+
70
+        $knownGroups = array_keys(self::getKnownGroups());
71
+        $actualGroups = self::getGroupBE()->getGroups();
72
+
73
+        if (empty($actualGroups) && empty($knownGroups)) {
74
+            \OCP\Util::writeLog('user_ldap',
75
+                'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
76
+                ILogger::INFO);
77
+            return;
78
+        }
79
+
80
+        self::handleKnownGroups(array_intersect($actualGroups, $knownGroups));
81
+        self::handleCreatedGroups(array_diff($actualGroups, $knownGroups));
82
+        self::handleRemovedGroups(array_diff($knownGroups, $actualGroups));
83
+
84
+        \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
85
+    }
86
+
87
+    /**
88
+     * @return int
89
+     */
90
+    private static function getRefreshInterval() {
91
+        //defaults to every hour
92
+        return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
93
+    }
94
+
95
+    /**
96
+     * @param string[] $groups
97
+     */
98
+    private static function handleKnownGroups($groups) {
99
+        /** @var IEventDispatcher $dispatcher */
100
+        $dispatcher = \OC::$server->query(IEventDispatcher::class);
101
+        $groupManager = \OC::$server->getGroupManager();
102
+        $userManager = \OC::$server->getUserManager();
103
+
104
+        \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
105
+        $query = \OC_DB::prepare('
106 106
 			UPDATE `*PREFIX*ldap_group_members`
107 107
 			SET `owncloudusers` = ?
108 108
 			WHERE `owncloudname` = ?
109 109
 		');
110
-		foreach ($groups as $group) {
111
-			//we assume, that self::$groupsFromDB has been retrieved already
112
-			$knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
113
-			$actualUsers = self::getGroupBE()->usersInGroup($group);
114
-			$hasChanged = false;
115
-
116
-			$groupObject = $groupManager->get($group);
117
-			foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
118
-				$userObject = $userManager->get($removedUser);
119
-				$dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
120
-				\OCP\Util::writeLog('user_ldap',
121
-				'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
122
-					ILogger::INFO);
123
-				$hasChanged = true;
124
-			}
125
-			foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
126
-				$userObject = $userManager->get($addedUser);
127
-				$dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
128
-				\OCP\Util::writeLog('user_ldap',
129
-				'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
130
-					ILogger::INFO);
131
-				$hasChanged = true;
132
-			}
133
-			if ($hasChanged) {
134
-				$query->execute([serialize($actualUsers), $group]);
135
-			}
136
-		}
137
-		\OCP\Util::writeLog('user_ldap',
138
-			'bgJ "updateGroups" – FINISHED dealing with known Groups.',
139
-			ILogger::DEBUG);
140
-	}
141
-
142
-	/**
143
-	 * @param string[] $createdGroups
144
-	 */
145
-	private static function handleCreatedGroups($createdGroups) {
146
-		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
147
-		$query = \OC_DB::prepare('
110
+        foreach ($groups as $group) {
111
+            //we assume, that self::$groupsFromDB has been retrieved already
112
+            $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']);
113
+            $actualUsers = self::getGroupBE()->usersInGroup($group);
114
+            $hasChanged = false;
115
+
116
+            $groupObject = $groupManager->get($group);
117
+            foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
118
+                $userObject = $userManager->get($removedUser);
119
+                $dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
120
+                \OCP\Util::writeLog('user_ldap',
121
+                'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
122
+                    ILogger::INFO);
123
+                $hasChanged = true;
124
+            }
125
+            foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
126
+                $userObject = $userManager->get($addedUser);
127
+                $dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
128
+                \OCP\Util::writeLog('user_ldap',
129
+                'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
130
+                    ILogger::INFO);
131
+                $hasChanged = true;
132
+            }
133
+            if ($hasChanged) {
134
+                $query->execute([serialize($actualUsers), $group]);
135
+            }
136
+        }
137
+        \OCP\Util::writeLog('user_ldap',
138
+            'bgJ "updateGroups" – FINISHED dealing with known Groups.',
139
+            ILogger::DEBUG);
140
+    }
141
+
142
+    /**
143
+     * @param string[] $createdGroups
144
+     */
145
+    private static function handleCreatedGroups($createdGroups) {
146
+        \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
147
+        $query = \OC_DB::prepare('
148 148
 			INSERT
149 149
 			INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
150 150
 			VALUES (?, ?)
151 151
 		');
152
-		foreach ($createdGroups as $createdGroup) {
153
-			\OCP\Util::writeLog('user_ldap',
154
-				'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
155
-				ILogger::INFO);
156
-			$users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
157
-			$query->execute([$createdGroup, $users]);
158
-		}
159
-		\OCP\Util::writeLog('user_ldap',
160
-			'bgJ "updateGroups" – FINISHED dealing with created Groups.',
161
-			ILogger::DEBUG);
162
-	}
163
-
164
-	/**
165
-	 * @param string[] $removedGroups
166
-	 */
167
-	private static function handleRemovedGroups($removedGroups) {
168
-		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
169
-		$query = \OC_DB::prepare('
152
+        foreach ($createdGroups as $createdGroup) {
153
+            \OCP\Util::writeLog('user_ldap',
154
+                'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
155
+                ILogger::INFO);
156
+            $users = serialize(self::getGroupBE()->usersInGroup($createdGroup));
157
+            $query->execute([$createdGroup, $users]);
158
+        }
159
+        \OCP\Util::writeLog('user_ldap',
160
+            'bgJ "updateGroups" – FINISHED dealing with created Groups.',
161
+            ILogger::DEBUG);
162
+    }
163
+
164
+    /**
165
+     * @param string[] $removedGroups
166
+     */
167
+    private static function handleRemovedGroups($removedGroups) {
168
+        \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
169
+        $query = \OC_DB::prepare('
170 170
 			DELETE
171 171
 			FROM `*PREFIX*ldap_group_members`
172 172
 			WHERE `owncloudname` = ?
173 173
 		');
174
-		foreach ($removedGroups as $removedGroup) {
175
-			\OCP\Util::writeLog('user_ldap',
176
-				'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
177
-				ILogger::INFO);
178
-			$query->execute([$removedGroup]);
179
-		}
180
-		\OCP\Util::writeLog('user_ldap',
181
-			'bgJ "updateGroups" – FINISHED dealing with removed groups.',
182
-			ILogger::DEBUG);
183
-	}
184
-
185
-	/**
186
-	 * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy
187
-	 */
188
-	private static function getGroupBE() {
189
-		if (!is_null(self::$groupBE)) {
190
-			return self::$groupBE;
191
-		}
192
-		$helper = new Helper(\OC::$server->getConfig());
193
-		$configPrefixes = $helper->getServerConfigurationPrefixes(true);
194
-		$ldapWrapper = new LDAP();
195
-		if (count($configPrefixes) === 1) {
196
-			//avoid the proxy when there is only one LDAP server configured
197
-			$dbc = \OC::$server->getDatabaseConnection();
198
-			$userManager = new Manager(
199
-				\OC::$server->getConfig(),
200
-				new FilesystemHelper(),
201
-				new LogWrapper(),
202
-				\OC::$server->getAvatarManager(),
203
-				new \OCP\Image(),
204
-				$dbc,
205
-				\OC::$server->getUserManager(),
206
-				\OC::$server->getNotificationManager());
207
-			$connector = new Connection($ldapWrapper, $configPrefixes[0]);
208
-			$ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager());
209
-			$groupMapper = new GroupMapping($dbc);
210
-			$userMapper  = new UserMapping($dbc);
211
-			$ldapAccess->setGroupMapper($groupMapper);
212
-			$ldapAccess->setUserMapper($userMapper);
213
-			self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query(GroupPluginManager::class));
214
-		} else {
215
-			self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query(GroupPluginManager::class));
216
-		}
217
-
218
-		return self::$groupBE;
219
-	}
220
-
221
-	/**
222
-	 * @return array
223
-	 */
224
-	private static function getKnownGroups() {
225
-		if (is_array(self::$groupsFromDB)) {
226
-			return self::$groupsFromDB;
227
-		}
228
-		$query = \OC_DB::prepare('
174
+        foreach ($removedGroups as $removedGroup) {
175
+            \OCP\Util::writeLog('user_ldap',
176
+                'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
177
+                ILogger::INFO);
178
+            $query->execute([$removedGroup]);
179
+        }
180
+        \OCP\Util::writeLog('user_ldap',
181
+            'bgJ "updateGroups" – FINISHED dealing with removed groups.',
182
+            ILogger::DEBUG);
183
+    }
184
+
185
+    /**
186
+     * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy
187
+     */
188
+    private static function getGroupBE() {
189
+        if (!is_null(self::$groupBE)) {
190
+            return self::$groupBE;
191
+        }
192
+        $helper = new Helper(\OC::$server->getConfig());
193
+        $configPrefixes = $helper->getServerConfigurationPrefixes(true);
194
+        $ldapWrapper = new LDAP();
195
+        if (count($configPrefixes) === 1) {
196
+            //avoid the proxy when there is only one LDAP server configured
197
+            $dbc = \OC::$server->getDatabaseConnection();
198
+            $userManager = new Manager(
199
+                \OC::$server->getConfig(),
200
+                new FilesystemHelper(),
201
+                new LogWrapper(),
202
+                \OC::$server->getAvatarManager(),
203
+                new \OCP\Image(),
204
+                $dbc,
205
+                \OC::$server->getUserManager(),
206
+                \OC::$server->getNotificationManager());
207
+            $connector = new Connection($ldapWrapper, $configPrefixes[0]);
208
+            $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper, \OC::$server->getConfig(), \OC::$server->getUserManager());
209
+            $groupMapper = new GroupMapping($dbc);
210
+            $userMapper  = new UserMapping($dbc);
211
+            $ldapAccess->setGroupMapper($groupMapper);
212
+            $ldapAccess->setUserMapper($userMapper);
213
+            self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess, \OC::$server->query(GroupPluginManager::class));
214
+        } else {
215
+            self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper, \OC::$server->query(GroupPluginManager::class));
216
+        }
217
+
218
+        return self::$groupBE;
219
+    }
220
+
221
+    /**
222
+     * @return array
223
+     */
224
+    private static function getKnownGroups() {
225
+        if (is_array(self::$groupsFromDB)) {
226
+            return self::$groupsFromDB;
227
+        }
228
+        $query = \OC_DB::prepare('
229 229
 			SELECT `owncloudname`, `owncloudusers`
230 230
 			FROM `*PREFIX*ldap_group_members`
231 231
 		');
232
-		$result = $query->execute()->fetchAll();
233
-		self::$groupsFromDB = [];
234
-		foreach ($result as $dataset) {
235
-			self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
236
-		}
237
-
238
-		return self::$groupsFromDB;
239
-	}
232
+        $result = $query->execute()->fetchAll();
233
+        self::$groupsFromDB = [];
234
+        foreach ($result as $dataset) {
235
+            self::$groupsFromDB[$dataset['owncloudname']] = $dataset;
236
+        }
237
+
238
+        return self::$groupsFromDB;
239
+    }
240 240
 }
Please login to merge, or discard this patch.