Passed
Push — master ( 5216c9...a74458 )
by Blizzz
19:31 queued 13s
created

UpdateGroups   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 79
dl 0
loc 156
rs 10
c 1
b 0
f 1
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getKnownGroups() 0 15 3
B handleKnownGroups() 0 44 6
A updateGroups() 0 18 3
A __construct() 0 3 1
A getRefreshInterval() 0 3 1
A run() 0 2 1
A handleCreatedGroups() 0 17 2
A handleRemovedGroups() 0 16 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Robin McCorkell <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Roger Szabo <[email protected]>
16
 * @author Thomas Müller <[email protected]>
17
 *
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program. If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
34
namespace OCA\User_LDAP\Jobs;
35
36
use OC\BackgroundJob\TimedJob;
37
use OCA\User_LDAP\Group_Proxy;
38
use OCP\EventDispatcher\IEventDispatcher;
39
use OCP\Group\Events\UserAddedEvent;
40
use OCP\Group\Events\UserRemovedEvent;
41
use OCP\ILogger;
42
43
class UpdateGroups extends TimedJob {
44
	private $groupsFromDB;
45
46
	/** @var Group_Proxy */
47
	private $groupBackend;
48
49
	public function __construct(Group_Proxy $groupBackend) {
50
		$this->interval = $this->getRefreshInterval();
51
		$this->groupBackend = $groupBackend;
52
	}
53
54
	/**
55
	 * @param mixed $argument
56
	 */
57
	public function run($argument) {
58
		$this->updateGroups();
59
	}
60
61
	public function updateGroups() {
62
		\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
		\OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', /** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
63
64
		$knownGroups = array_keys($this->getKnownGroups());
65
		$actualGroups = $this->groupBackend->getGroups();
66
67
		if (empty($actualGroups) && empty($knownGroups)) {
68
			\OCP\Util::writeLog('user_ldap',
69
				'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
70
				ILogger::INFO);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
				/** @scrutinizer ignore-deprecated */ ILogger::INFO);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
71
			return;
72
		}
73
74
		$this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
75
		$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
76
		$this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
77
78
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', /** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
79
	}
80
81
	/**
82
	 * @return int
83
	 */
84
	private function getRefreshInterval() {
85
		//defaults to every hour
86
		return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
0 ignored issues
show
Bug Best Practice introduced by
The expression return OC::server->getCo...RefreshInterval', 3600) returns the type string which is incompatible with the documented return type integer.
Loading history...
87
	}
88
89
	/**
90
	 * @param string[] $groups
91
	 */
92
	private function handleKnownGroups($groups) {
93
		/** @var IEventDispatcher $dispatcher */
94
		$dispatcher = \OC::$server->query(IEventDispatcher::class);
95
		$groupManager = \OC::$server->getGroupManager();
96
		$userManager = \OC::$server->getUserManager();
97
98
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

98
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', /** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
99
		$query = \OC_DB::prepare('
100
			UPDATE `*PREFIX*ldap_group_members`
101
			SET `owncloudusers` = ?
102
			WHERE `owncloudname` = ?
103
		');
104
		if (!is_array($this->groupsFromDB)) {
105
			$this->getKnownGroups();
106
		}
107
		foreach ($groups as $group) {
108
			$knownUsers = unserialize($this->groupsFromDB[$group]['owncloudusers']);
109
			$actualUsers = $this->groupBackend->usersInGroup($group);
110
			$hasChanged = false;
111
112
			$groupObject = $groupManager->get($group);
113
			foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
114
				$userObject = $userManager->get($removedUser);
115
				$dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
116
				\OCP\Util::writeLog('user_ldap',
117
				'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
118
					ILogger::INFO);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

118
					/** @scrutinizer ignore-deprecated */ ILogger::INFO);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
119
				$hasChanged = true;
120
			}
121
			foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
122
				$userObject = $userManager->get($addedUser);
123
				$dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
124
				\OCP\Util::writeLog('user_ldap',
125
				'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
126
					ILogger::INFO);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

126
					/** @scrutinizer ignore-deprecated */ ILogger::INFO);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
127
				$hasChanged = true;
128
			}
129
			if ($hasChanged) {
130
				$query->execute([serialize($actualUsers), $group]);
131
			}
132
		}
133
		\OCP\Util::writeLog('user_ldap',
134
			'bgJ "updateGroups" – FINISHED dealing with known Groups.',
135
			ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
			/** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
136
	}
137
138
	/**
139
	 * @param string[] $createdGroups
140
	 */
141
	private function handleCreatedGroups($createdGroups) {
142
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

142
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', /** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
143
		$query = \OC_DB::prepare('
144
			INSERT
145
			INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
146
			VALUES (?, ?)
147
		');
148
		foreach ($createdGroups as $createdGroup) {
149
			\OCP\Util::writeLog('user_ldap',
150
				'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
151
				ILogger::INFO);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

151
				/** @scrutinizer ignore-deprecated */ ILogger::INFO);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
152
			$users = serialize($this->groupBackend->usersInGroup($createdGroup));
153
			$query->execute([$createdGroup, $users]);
154
		}
155
		\OCP\Util::writeLog('user_ldap',
156
			'bgJ "updateGroups" – FINISHED dealing with created Groups.',
157
			ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

157
			/** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
158
	}
159
160
	/**
161
	 * @param string[] $removedGroups
162
	 */
163
	private function handleRemovedGroups($removedGroups) {
164
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

164
		\OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', /** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
165
		$query = \OC_DB::prepare('
166
			DELETE
167
			FROM `*PREFIX*ldap_group_members`
168
			WHERE `owncloudname` = ?
169
		');
170
		foreach ($removedGroups as $removedGroup) {
171
			\OCP\Util::writeLog('user_ldap',
172
				'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
173
				ILogger::INFO);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::INFO has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

173
				/** @scrutinizer ignore-deprecated */ ILogger::INFO);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
174
			$query->execute([$removedGroup]);
175
		}
176
		\OCP\Util::writeLog('user_ldap',
177
			'bgJ "updateGroups" – FINISHED dealing with removed groups.',
178
			ILogger::DEBUG);
0 ignored issues
show
Deprecated Code introduced by
The constant OCP\ILogger::DEBUG has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

178
			/** @scrutinizer ignore-deprecated */ ILogger::DEBUG);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
179
	}
180
181
	/**
182
	 * @return array
183
	 */
184
	private function getKnownGroups() {
185
		if (is_array($this->groupsFromDB)) {
186
			$this->groupsFromDB;
187
		}
188
		$query = \OC_DB::prepare('
189
			SELECT `owncloudname`, `owncloudusers`
190
			FROM `*PREFIX*ldap_group_members`
191
		');
192
		$result = $query->execute()->fetchAll();
193
		$this->groupsFromDB = [];
194
		foreach ($result as $dataset) {
195
			$this->groupsFromDB[$dataset['owncloudname']] = $dataset;
196
		}
197
198
		return $this->groupsFromDB;
199
	}
200
}
201