Passed
Push — master ( 28e255...7ed11b )
by Morris
24:23 queued 10:03
created
lib/public/Group/Events/SubAdminAddedEvent.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -36,32 +36,32 @@
 block discarded – undo
36 36
  */
37 37
 class SubAdminAddedEvent extends Event {
38 38
 
39
-	/** @var IGroup */
40
-	private $group;
39
+    /** @var IGroup */
40
+    private $group;
41 41
 
42
-	/*** @var IUser */
43
-	private $user;
42
+    /*** @var IUser */
43
+    private $user;
44 44
 
45
-	/**
46
-	 * @since 21.0.0
47
-	 */
48
-	public function __construct(IGroup $group, IUser $user) {
49
-		parent::__construct();
50
-		$this->group = $group;
51
-		$this->user = $user;
52
-	}
45
+    /**
46
+     * @since 21.0.0
47
+     */
48
+    public function __construct(IGroup $group, IUser $user) {
49
+        parent::__construct();
50
+        $this->group = $group;
51
+        $this->user = $user;
52
+    }
53 53
 
54
-	/**
55
-	 * @since 21.0.0
56
-	 */
57
-	public function getGroup(): IGroup {
58
-		return $this->group;
59
-	}
54
+    /**
55
+     * @since 21.0.0
56
+     */
57
+    public function getGroup(): IGroup {
58
+        return $this->group;
59
+    }
60 60
 
61
-	/**
62
-	 * @since 21.0.0
63
-	 */
64
-	public function getUser(): IUser {
65
-		return $this->user;
66
-	}
61
+    /**
62
+     * @since 21.0.0
63
+     */
64
+    public function getUser(): IUser {
65
+        return $this->user;
66
+    }
67 67
 }
Please login to merge, or discard this patch.
lib/public/Group/Events/SubAdminRemovedEvent.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -36,32 +36,32 @@
 block discarded – undo
36 36
  */
37 37
 class SubAdminRemovedEvent extends Event {
38 38
 
39
-	/** @var IGroup */
40
-	private $group;
39
+    /** @var IGroup */
40
+    private $group;
41 41
 
42
-	/*** @var IUser */
43
-	private $user;
42
+    /*** @var IUser */
43
+    private $user;
44 44
 
45
-	/**
46
-	 * @since 21.0.0
47
-	 */
48
-	public function __construct(IGroup $group, IUser $user) {
49
-		parent::__construct();
50
-		$this->group = $group;
51
-		$this->user = $user;
52
-	}
45
+    /**
46
+     * @since 21.0.0
47
+     */
48
+    public function __construct(IGroup $group, IUser $user) {
49
+        parent::__construct();
50
+        $this->group = $group;
51
+        $this->user = $user;
52
+    }
53 53
 
54
-	/**
55
-	 * @since 21.0.0
56
-	 */
57
-	public function getGroup(): IGroup {
58
-		return $this->group;
59
-	}
54
+    /**
55
+     * @since 21.0.0
56
+     */
57
+    public function getGroup(): IGroup {
58
+        return $this->group;
59
+    }
60 60
 
61
-	/**
62
-	 * @since 21.0.0
63
-	 */
64
-	public function getUser(): IUser {
65
-		return $this->user;
66
-	}
61
+    /**
62
+     * @since 21.0.0
63
+     */
64
+    public function getUser(): IUser {
65
+        return $this->user;
66
+    }
67 67
 }
Please login to merge, or discard this patch.
lib/private/SubAdmin.php 2 patches
Indentation   +269 added lines, -269 removed lines patch added patch discarded remove patch
@@ -44,275 +44,275 @@
 block discarded – undo
44 44
 
45 45
 class SubAdmin extends PublicEmitter implements ISubAdmin {
46 46
 
47
-	/** @var IUserManager */
48
-	private $userManager;
49
-
50
-	/** @var IGroupManager */
51
-	private $groupManager;
52
-
53
-	/** @var IDBConnection */
54
-	private $dbConn;
55
-
56
-	/** @var IEventDispatcher */
57
-	private $eventDispatcher;
58
-
59
-	/**
60
-	 * @param IUserManager $userManager
61
-	 * @param IGroupManager $groupManager
62
-	 * @param IDBConnection $dbConn
63
-	 */
64
-	public function __construct(IUserManager $userManager,
65
-								IGroupManager $groupManager,
66
-								IDBConnection $dbConn,
67
-								IEventDispatcher $eventDispatcher) {
68
-		$this->userManager = $userManager;
69
-		$this->groupManager = $groupManager;
70
-		$this->dbConn = $dbConn;
71
-		$this->eventDispatcher = $eventDispatcher;
72
-
73
-		$this->userManager->listen('\OC\User', 'postDelete', function ($user) {
74
-			$this->post_deleteUser($user);
75
-		});
76
-		$this->groupManager->listen('\OC\Group', 'postDelete', function ($group) {
77
-			$this->post_deleteGroup($group);
78
-		});
79
-	}
80
-
81
-	/**
82
-	 * add a SubAdmin
83
-	 * @param IUser $user user to be SubAdmin
84
-	 * @param IGroup $group group $user becomes subadmin of
85
-	 */
86
-	public function createSubAdmin(IUser $user, IGroup $group): void {
87
-		$qb = $this->dbConn->getQueryBuilder();
88
-
89
-		$qb->insert('group_admin')
90
-			->values([
91
-				'gid' => $qb->createNamedParameter($group->getGID()),
92
-				'uid' => $qb->createNamedParameter($user->getUID())
93
-			])
94
-			->execute();
95
-
96
-		/** @depreacted 21.0.0 - use type SubAdminAddedEvent instead  */
97
-		$this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
98
-		$event = new SubAdminAddedEvent($group, $user);
99
-		$this->eventDispatcher->dispatchTyped($event);
100
-	}
101
-
102
-	/**
103
-	 * delete a SubAdmin
104
-	 * @param IUser $user the user that is the SubAdmin
105
-	 * @param IGroup $group the group
106
-	 */
107
-	public function deleteSubAdmin(IUser $user, IGroup $group): void {
108
-		$qb = $this->dbConn->getQueryBuilder();
109
-
110
-		$qb->delete('group_admin')
111
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
112
-			->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
113
-			->execute();
114
-
115
-		/** @depreacted 21.0.0 - use type SubAdminRemovedEvent instead  */
116
-		$this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
117
-		$event = new SubAdminRemovedEvent($group, $user);
118
-		$this->eventDispatcher->dispatchTyped($event);
119
-	}
120
-
121
-	/**
122
-	 * get groups of a SubAdmin
123
-	 * @param IUser $user the SubAdmin
124
-	 * @return IGroup[]
125
-	 */
126
-	public function getSubAdminsGroups(IUser $user): array {
127
-		$groupIds = $this->getSubAdminsGroupIds($user);
128
-
129
-		$groups = [];
130
-		foreach ($groupIds as $groupId) {
131
-			$group = $this->groupManager->get($groupId);
132
-			if ($group !== null) {
133
-				$groups[$group->getGID()] = $group;
134
-			}
135
-		}
136
-
137
-		return $groups;
138
-	}
139
-
140
-	/**
141
-	 * Get group ids of a SubAdmin
142
-	 * @param IUser $user the SubAdmin
143
-	 * @return string[]
144
-	 */
145
-	public function getSubAdminsGroupIds(IUser $user): array {
146
-		$qb = $this->dbConn->getQueryBuilder();
147
-
148
-		$result = $qb->select('gid')
149
-			->from('group_admin')
150
-			->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
151
-			->execute();
152
-
153
-		$groups = [];
154
-		while ($row = $result->fetch()) {
155
-			$groups[] = $row['gid'];
156
-		}
157
-		$result->closeCursor();
158
-
159
-		return $groups;
160
-	}
161
-
162
-	/**
163
-	 * get an array of groupid and displayName for a user
164
-	 * @param IUser $user
165
-	 * @return array ['displayName' => displayname]
166
-	 */
167
-	public function getSubAdminsGroupsName(IUser $user): array {
168
-		return array_map(function ($group) {
169
-			return ['displayName' => $group->getDisplayName()];
170
-		}, $this->getSubAdminsGroups($user));
171
-	}
172
-
173
-	/**
174
-	 * get SubAdmins of a group
175
-	 * @param IGroup $group the group
176
-	 * @return IUser[]
177
-	 */
178
-	public function getGroupsSubAdmins(IGroup $group): array {
179
-		$qb = $this->dbConn->getQueryBuilder();
180
-
181
-		$result = $qb->select('uid')
182
-			->from('group_admin')
183
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
184
-			->execute();
185
-
186
-		$users = [];
187
-		while ($row = $result->fetch()) {
188
-			$user = $this->userManager->get($row['uid']);
189
-			if (!is_null($user)) {
190
-				$users[] = $user;
191
-			}
192
-		}
193
-		$result->closeCursor();
194
-
195
-		return $users;
196
-	}
197
-
198
-	/**
199
-	 * get all SubAdmins
200
-	 * @return array
201
-	 */
202
-	public function getAllSubAdmins(): array {
203
-		$qb = $this->dbConn->getQueryBuilder();
204
-
205
-		$result = $qb->select('*')
206
-			->from('group_admin')
207
-			->execute();
208
-
209
-		$subadmins = [];
210
-		while ($row = $result->fetch()) {
211
-			$user = $this->userManager->get($row['uid']);
212
-			$group = $this->groupManager->get($row['gid']);
213
-			if (!is_null($user) && !is_null($group)) {
214
-				$subadmins[] = [
215
-					'user' => $user,
216
-					'group' => $group
217
-				];
218
-			}
219
-		}
220
-		$result->closeCursor();
221
-
222
-		return $subadmins;
223
-	}
224
-
225
-	/**
226
-	 * checks if a user is a SubAdmin of a group
227
-	 * @param IUser $user
228
-	 * @param IGroup $group
229
-	 * @return bool
230
-	 */
231
-	public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {
232
-		$qb = $this->dbConn->getQueryBuilder();
233
-
234
-		/*
47
+    /** @var IUserManager */
48
+    private $userManager;
49
+
50
+    /** @var IGroupManager */
51
+    private $groupManager;
52
+
53
+    /** @var IDBConnection */
54
+    private $dbConn;
55
+
56
+    /** @var IEventDispatcher */
57
+    private $eventDispatcher;
58
+
59
+    /**
60
+     * @param IUserManager $userManager
61
+     * @param IGroupManager $groupManager
62
+     * @param IDBConnection $dbConn
63
+     */
64
+    public function __construct(IUserManager $userManager,
65
+                                IGroupManager $groupManager,
66
+                                IDBConnection $dbConn,
67
+                                IEventDispatcher $eventDispatcher) {
68
+        $this->userManager = $userManager;
69
+        $this->groupManager = $groupManager;
70
+        $this->dbConn = $dbConn;
71
+        $this->eventDispatcher = $eventDispatcher;
72
+
73
+        $this->userManager->listen('\OC\User', 'postDelete', function ($user) {
74
+            $this->post_deleteUser($user);
75
+        });
76
+        $this->groupManager->listen('\OC\Group', 'postDelete', function ($group) {
77
+            $this->post_deleteGroup($group);
78
+        });
79
+    }
80
+
81
+    /**
82
+     * add a SubAdmin
83
+     * @param IUser $user user to be SubAdmin
84
+     * @param IGroup $group group $user becomes subadmin of
85
+     */
86
+    public function createSubAdmin(IUser $user, IGroup $group): void {
87
+        $qb = $this->dbConn->getQueryBuilder();
88
+
89
+        $qb->insert('group_admin')
90
+            ->values([
91
+                'gid' => $qb->createNamedParameter($group->getGID()),
92
+                'uid' => $qb->createNamedParameter($user->getUID())
93
+            ])
94
+            ->execute();
95
+
96
+        /** @depreacted 21.0.0 - use type SubAdminAddedEvent instead  */
97
+        $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
98
+        $event = new SubAdminAddedEvent($group, $user);
99
+        $this->eventDispatcher->dispatchTyped($event);
100
+    }
101
+
102
+    /**
103
+     * delete a SubAdmin
104
+     * @param IUser $user the user that is the SubAdmin
105
+     * @param IGroup $group the group
106
+     */
107
+    public function deleteSubAdmin(IUser $user, IGroup $group): void {
108
+        $qb = $this->dbConn->getQueryBuilder();
109
+
110
+        $qb->delete('group_admin')
111
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
112
+            ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
113
+            ->execute();
114
+
115
+        /** @depreacted 21.0.0 - use type SubAdminRemovedEvent instead  */
116
+        $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
117
+        $event = new SubAdminRemovedEvent($group, $user);
118
+        $this->eventDispatcher->dispatchTyped($event);
119
+    }
120
+
121
+    /**
122
+     * get groups of a SubAdmin
123
+     * @param IUser $user the SubAdmin
124
+     * @return IGroup[]
125
+     */
126
+    public function getSubAdminsGroups(IUser $user): array {
127
+        $groupIds = $this->getSubAdminsGroupIds($user);
128
+
129
+        $groups = [];
130
+        foreach ($groupIds as $groupId) {
131
+            $group = $this->groupManager->get($groupId);
132
+            if ($group !== null) {
133
+                $groups[$group->getGID()] = $group;
134
+            }
135
+        }
136
+
137
+        return $groups;
138
+    }
139
+
140
+    /**
141
+     * Get group ids of a SubAdmin
142
+     * @param IUser $user the SubAdmin
143
+     * @return string[]
144
+     */
145
+    public function getSubAdminsGroupIds(IUser $user): array {
146
+        $qb = $this->dbConn->getQueryBuilder();
147
+
148
+        $result = $qb->select('gid')
149
+            ->from('group_admin')
150
+            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
151
+            ->execute();
152
+
153
+        $groups = [];
154
+        while ($row = $result->fetch()) {
155
+            $groups[] = $row['gid'];
156
+        }
157
+        $result->closeCursor();
158
+
159
+        return $groups;
160
+    }
161
+
162
+    /**
163
+     * get an array of groupid and displayName for a user
164
+     * @param IUser $user
165
+     * @return array ['displayName' => displayname]
166
+     */
167
+    public function getSubAdminsGroupsName(IUser $user): array {
168
+        return array_map(function ($group) {
169
+            return ['displayName' => $group->getDisplayName()];
170
+        }, $this->getSubAdminsGroups($user));
171
+    }
172
+
173
+    /**
174
+     * get SubAdmins of a group
175
+     * @param IGroup $group the group
176
+     * @return IUser[]
177
+     */
178
+    public function getGroupsSubAdmins(IGroup $group): array {
179
+        $qb = $this->dbConn->getQueryBuilder();
180
+
181
+        $result = $qb->select('uid')
182
+            ->from('group_admin')
183
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
184
+            ->execute();
185
+
186
+        $users = [];
187
+        while ($row = $result->fetch()) {
188
+            $user = $this->userManager->get($row['uid']);
189
+            if (!is_null($user)) {
190
+                $users[] = $user;
191
+            }
192
+        }
193
+        $result->closeCursor();
194
+
195
+        return $users;
196
+    }
197
+
198
+    /**
199
+     * get all SubAdmins
200
+     * @return array
201
+     */
202
+    public function getAllSubAdmins(): array {
203
+        $qb = $this->dbConn->getQueryBuilder();
204
+
205
+        $result = $qb->select('*')
206
+            ->from('group_admin')
207
+            ->execute();
208
+
209
+        $subadmins = [];
210
+        while ($row = $result->fetch()) {
211
+            $user = $this->userManager->get($row['uid']);
212
+            $group = $this->groupManager->get($row['gid']);
213
+            if (!is_null($user) && !is_null($group)) {
214
+                $subadmins[] = [
215
+                    'user' => $user,
216
+                    'group' => $group
217
+                ];
218
+            }
219
+        }
220
+        $result->closeCursor();
221
+
222
+        return $subadmins;
223
+    }
224
+
225
+    /**
226
+     * checks if a user is a SubAdmin of a group
227
+     * @param IUser $user
228
+     * @param IGroup $group
229
+     * @return bool
230
+     */
231
+    public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {
232
+        $qb = $this->dbConn->getQueryBuilder();
233
+
234
+        /*
235 235
 		 * Primary key is ('gid', 'uid') so max 1 result possible here
236 236
 		 */
237
-		$result = $qb->select('*')
238
-			->from('group_admin')
239
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
240
-			->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
241
-			->execute();
242
-
243
-		$fetch = $result->fetch();
244
-		$result->closeCursor();
245
-		$result = !empty($fetch) ? true : false;
246
-
247
-		return $result;
248
-	}
249
-
250
-	/**
251
-	 * checks if a user is a SubAdmin
252
-	 * @param IUser $user
253
-	 * @return bool
254
-	 */
255
-	public function isSubAdmin(IUser $user): bool {
256
-		// Check if the user is already an admin
257
-		if ($this->groupManager->isAdmin($user->getUID())) {
258
-			return true;
259
-		}
260
-
261
-		$qb = $this->dbConn->getQueryBuilder();
262
-
263
-		$result = $qb->select('gid')
264
-			->from('group_admin')
265
-			->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
266
-			->setMaxResults(1)
267
-			->execute();
268
-
269
-		$isSubAdmin = $result->fetch();
270
-		$result->closeCursor();
271
-
272
-		return $isSubAdmin !== false;
273
-	}
274
-
275
-	/**
276
-	 * checks if a user is a accessible by a subadmin
277
-	 * @param IUser $subadmin
278
-	 * @param IUser $user
279
-	 * @return bool
280
-	 */
281
-	public function isUserAccessible(IUser $subadmin, IUser $user): bool {
282
-		if (!$this->isSubAdmin($subadmin)) {
283
-			return false;
284
-		}
285
-		if ($this->groupManager->isAdmin($user->getUID())) {
286
-			return false;
287
-		}
288
-
289
-		$accessibleGroups = $this->getSubAdminsGroupIds($subadmin);
290
-		$userGroups = $this->groupManager->getUserGroupIds($user);
291
-
292
-		return !empty(array_intersect($accessibleGroups, $userGroups));
293
-	}
294
-
295
-	/**
296
-	 * delete all SubAdmins by $user
297
-	 * @param IUser $user
298
-	 */
299
-	private function post_deleteUser(IUser $user) {
300
-		$qb = $this->dbConn->getQueryBuilder();
301
-
302
-		$qb->delete('group_admin')
303
-			->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
304
-			->execute();
305
-	}
306
-
307
-	/**
308
-	 * delete all SubAdmins by $group
309
-	 * @param IGroup $group
310
-	 */
311
-	private function post_deleteGroup(IGroup $group) {
312
-		$qb = $this->dbConn->getQueryBuilder();
313
-
314
-		$qb->delete('group_admin')
315
-			->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
316
-			->execute();
317
-	}
237
+        $result = $qb->select('*')
238
+            ->from('group_admin')
239
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
240
+            ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
241
+            ->execute();
242
+
243
+        $fetch = $result->fetch();
244
+        $result->closeCursor();
245
+        $result = !empty($fetch) ? true : false;
246
+
247
+        return $result;
248
+    }
249
+
250
+    /**
251
+     * checks if a user is a SubAdmin
252
+     * @param IUser $user
253
+     * @return bool
254
+     */
255
+    public function isSubAdmin(IUser $user): bool {
256
+        // Check if the user is already an admin
257
+        if ($this->groupManager->isAdmin($user->getUID())) {
258
+            return true;
259
+        }
260
+
261
+        $qb = $this->dbConn->getQueryBuilder();
262
+
263
+        $result = $qb->select('gid')
264
+            ->from('group_admin')
265
+            ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
266
+            ->setMaxResults(1)
267
+            ->execute();
268
+
269
+        $isSubAdmin = $result->fetch();
270
+        $result->closeCursor();
271
+
272
+        return $isSubAdmin !== false;
273
+    }
274
+
275
+    /**
276
+     * checks if a user is a accessible by a subadmin
277
+     * @param IUser $subadmin
278
+     * @param IUser $user
279
+     * @return bool
280
+     */
281
+    public function isUserAccessible(IUser $subadmin, IUser $user): bool {
282
+        if (!$this->isSubAdmin($subadmin)) {
283
+            return false;
284
+        }
285
+        if ($this->groupManager->isAdmin($user->getUID())) {
286
+            return false;
287
+        }
288
+
289
+        $accessibleGroups = $this->getSubAdminsGroupIds($subadmin);
290
+        $userGroups = $this->groupManager->getUserGroupIds($user);
291
+
292
+        return !empty(array_intersect($accessibleGroups, $userGroups));
293
+    }
294
+
295
+    /**
296
+     * delete all SubAdmins by $user
297
+     * @param IUser $user
298
+     */
299
+    private function post_deleteUser(IUser $user) {
300
+        $qb = $this->dbConn->getQueryBuilder();
301
+
302
+        $qb->delete('group_admin')
303
+            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
304
+            ->execute();
305
+    }
306
+
307
+    /**
308
+     * delete all SubAdmins by $group
309
+     * @param IGroup $group
310
+     */
311
+    private function post_deleteGroup(IGroup $group) {
312
+        $qb = $this->dbConn->getQueryBuilder();
313
+
314
+        $qb->delete('group_admin')
315
+            ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
316
+            ->execute();
317
+    }
318 318
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -70,10 +70,10 @@  discard block
 block discarded – undo
70 70
 		$this->dbConn = $dbConn;
71 71
 		$this->eventDispatcher = $eventDispatcher;
72 72
 
73
-		$this->userManager->listen('\OC\User', 'postDelete', function ($user) {
73
+		$this->userManager->listen('\OC\User', 'postDelete', function($user) {
74 74
 			$this->post_deleteUser($user);
75 75
 		});
76
-		$this->groupManager->listen('\OC\Group', 'postDelete', function ($group) {
76
+		$this->groupManager->listen('\OC\Group', 'postDelete', function($group) {
77 77
 			$this->post_deleteGroup($group);
78 78
 		});
79 79
 	}
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 	 * @return array ['displayName' => displayname]
166 166
 	 */
167 167
 	public function getSubAdminsGroupsName(IUser $user): array {
168
-		return array_map(function ($group) {
168
+		return array_map(function($group) {
169 169
 			return ['displayName' => $group->getDisplayName()];
170 170
 		}, $this->getSubAdminsGroups($user));
171 171
 	}
Please login to merge, or discard this patch.
lib/private/Group/Manager.php 1 patch
Indentation   +359 added lines, -359 removed lines patch added patch discarded remove patch
@@ -65,363 +65,363 @@
 block discarded – undo
65 65
  * @package OC\Group
66 66
  */
67 67
 class Manager extends PublicEmitter implements IGroupManager {
68
-	/** @var GroupInterface[] */
69
-	private $backends = [];
70
-
71
-	/** @var \OC\User\Manager */
72
-	private $userManager;
73
-	/** @var EventDispatcherInterface */
74
-	private $dispatcher;
75
-	/** @var ILogger */
76
-	private $logger;
77
-
78
-	/** @var \OC\Group\Group[] */
79
-	private $cachedGroups = [];
80
-
81
-	/** @var (string[])[] */
82
-	private $cachedUserGroups = [];
83
-
84
-	/** @var \OC\SubAdmin */
85
-	private $subAdmin = null;
86
-
87
-	/**
88
-	 * @param \OC\User\Manager $userManager
89
-	 * @param EventDispatcherInterface $dispatcher
90
-	 * @param ILogger $logger
91
-	 */
92
-	public function __construct(\OC\User\Manager $userManager,
93
-								EventDispatcherInterface $dispatcher,
94
-								ILogger $logger) {
95
-		$this->userManager = $userManager;
96
-		$this->dispatcher = $dispatcher;
97
-		$this->logger = $logger;
98
-
99
-		$cachedGroups = &$this->cachedGroups;
100
-		$cachedUserGroups = &$this->cachedUserGroups;
101
-		$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
102
-			/**
103
-			 * @var \OC\Group\Group $group
104
-			 */
105
-			unset($cachedGroups[$group->getGID()]);
106
-			$cachedUserGroups = [];
107
-		});
108
-		$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
109
-			/**
110
-			 * @var \OC\Group\Group $group
111
-			 */
112
-			$cachedUserGroups = [];
113
-		});
114
-		$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
115
-			/**
116
-			 * @var \OC\Group\Group $group
117
-			 */
118
-			$cachedUserGroups = [];
119
-		});
120
-	}
121
-
122
-	/**
123
-	 * Checks whether a given backend is used
124
-	 *
125
-	 * @param string $backendClass Full classname including complete namespace
126
-	 * @return bool
127
-	 */
128
-	public function isBackendUsed($backendClass) {
129
-		$backendClass = strtolower(ltrim($backendClass, '\\'));
130
-
131
-		foreach ($this->backends as $backend) {
132
-			if (strtolower(get_class($backend)) === $backendClass) {
133
-				return true;
134
-			}
135
-		}
136
-
137
-		return false;
138
-	}
139
-
140
-	/**
141
-	 * @param \OCP\GroupInterface $backend
142
-	 */
143
-	public function addBackend($backend) {
144
-		$this->backends[] = $backend;
145
-		$this->clearCaches();
146
-	}
147
-
148
-	public function clearBackends() {
149
-		$this->backends = [];
150
-		$this->clearCaches();
151
-	}
152
-
153
-	/**
154
-	 * Get the active backends
155
-	 *
156
-	 * @return \OCP\GroupInterface[]
157
-	 */
158
-	public function getBackends() {
159
-		return $this->backends;
160
-	}
161
-
162
-
163
-	protected function clearCaches() {
164
-		$this->cachedGroups = [];
165
-		$this->cachedUserGroups = [];
166
-	}
167
-
168
-	/**
169
-	 * @param string $gid
170
-	 * @return IGroup|null
171
-	 */
172
-	public function get($gid) {
173
-		if (isset($this->cachedGroups[$gid])) {
174
-			return $this->cachedGroups[$gid];
175
-		}
176
-		return $this->getGroupObject($gid);
177
-	}
178
-
179
-	/**
180
-	 * @param string $gid
181
-	 * @param string $displayName
182
-	 * @return \OCP\IGroup|null
183
-	 */
184
-	protected function getGroupObject($gid, $displayName = null) {
185
-		$backends = [];
186
-		foreach ($this->backends as $backend) {
187
-			if ($backend->implementsActions(Backend::GROUP_DETAILS)) {
188
-				$groupData = $backend->getGroupDetails($gid);
189
-				if (is_array($groupData) && !empty($groupData)) {
190
-					// take the display name from the first backend that has a non-null one
191
-					if (is_null($displayName) && isset($groupData['displayName'])) {
192
-						$displayName = $groupData['displayName'];
193
-					}
194
-					$backends[] = $backend;
195
-				}
196
-			} elseif ($backend->groupExists($gid)) {
197
-				$backends[] = $backend;
198
-			}
199
-		}
200
-		if (count($backends) === 0) {
201
-			return null;
202
-		}
203
-		$this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
204
-		return $this->cachedGroups[$gid];
205
-	}
206
-
207
-	/**
208
-	 * @param string $gid
209
-	 * @return bool
210
-	 */
211
-	public function groupExists($gid) {
212
-		return $this->get($gid) instanceof IGroup;
213
-	}
214
-
215
-	/**
216
-	 * @param string $gid
217
-	 * @return IGroup|null
218
-	 */
219
-	public function createGroup($gid) {
220
-		if ($gid === '' || $gid === null) {
221
-			return null;
222
-		} elseif ($group = $this->get($gid)) {
223
-			return $group;
224
-		} else {
225
-			$this->emit('\OC\Group', 'preCreate', [$gid]);
226
-			foreach ($this->backends as $backend) {
227
-				if ($backend->implementsActions(Backend::CREATE_GROUP)) {
228
-					if ($backend->createGroup($gid)) {
229
-						$group = $this->getGroupObject($gid);
230
-						$this->emit('\OC\Group', 'postCreate', [$group]);
231
-						return $group;
232
-					}
233
-				}
234
-			}
235
-			return null;
236
-		}
237
-	}
238
-
239
-	/**
240
-	 * @param string $search
241
-	 * @param int $limit
242
-	 * @param int $offset
243
-	 * @return \OC\Group\Group[]
244
-	 */
245
-	public function search($search, $limit = null, $offset = null) {
246
-		$groups = [];
247
-		foreach ($this->backends as $backend) {
248
-			$groupIds = $backend->getGroups($search, $limit, $offset);
249
-			foreach ($groupIds as $groupId) {
250
-				$aGroup = $this->get($groupId);
251
-				if ($aGroup instanceof IGroup) {
252
-					$groups[$groupId] = $aGroup;
253
-				} else {
254
-					$this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
255
-				}
256
-			}
257
-			if (!is_null($limit) and $limit <= 0) {
258
-				return array_values($groups);
259
-			}
260
-		}
261
-		return array_values($groups);
262
-	}
263
-
264
-	/**
265
-	 * @param IUser|null $user
266
-	 * @return \OC\Group\Group[]
267
-	 */
268
-	public function getUserGroups(IUser $user = null) {
269
-		if (!$user instanceof IUser) {
270
-			return [];
271
-		}
272
-		return $this->getUserIdGroups($user->getUID());
273
-	}
274
-
275
-	/**
276
-	 * @param string $uid the user id
277
-	 * @return \OC\Group\Group[]
278
-	 */
279
-	public function getUserIdGroups($uid) {
280
-		$groups = [];
281
-
282
-		foreach ($this->getUserIdGroupIds($uid) as $groupId) {
283
-			$aGroup = $this->get($groupId);
284
-			if ($aGroup instanceof IGroup) {
285
-				$groups[$groupId] = $aGroup;
286
-			} else {
287
-				$this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
288
-			}
289
-		}
290
-
291
-		return $groups;
292
-	}
293
-
294
-	/**
295
-	 * Checks if a userId is in the admin group
296
-	 *
297
-	 * @param string $userId
298
-	 * @return bool if admin
299
-	 */
300
-	public function isAdmin($userId) {
301
-		foreach ($this->backends as $backend) {
302
-			if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
303
-				return true;
304
-			}
305
-		}
306
-		return $this->isInGroup($userId, 'admin');
307
-	}
308
-
309
-	/**
310
-	 * Checks if a userId is in a group
311
-	 *
312
-	 * @param string $userId
313
-	 * @param string $group
314
-	 * @return bool if in group
315
-	 */
316
-	public function isInGroup($userId, $group) {
317
-		return array_search($group, $this->getUserIdGroupIds($userId)) !== false;
318
-	}
319
-
320
-	/**
321
-	 * get a list of group ids for a user
322
-	 *
323
-	 * @param IUser $user
324
-	 * @return array with group ids
325
-	 */
326
-	public function getUserGroupIds(IUser $user) {
327
-		return $this->getUserIdGroupIds($user->getUID());
328
-	}
329
-
330
-	/**
331
-	 * @param string $uid the user id
332
-	 * @return GroupInterface[]
333
-	 */
334
-	private function getUserIdGroupIds($uid) {
335
-		if (!isset($this->cachedUserGroups[$uid])) {
336
-			$groups = [];
337
-			foreach ($this->backends as $backend) {
338
-				if ($groupIds = $backend->getUserGroups($uid)) {
339
-					$groups = array_merge($groups, $groupIds);
340
-				}
341
-			}
342
-			$this->cachedUserGroups[$uid] = $groups;
343
-		}
344
-
345
-		return $this->cachedUserGroups[$uid];
346
-	}
347
-
348
-	/**
349
-	 * get an array of groupid and displayName for a user
350
-	 *
351
-	 * @param IUser $user
352
-	 * @return array ['displayName' => displayname]
353
-	 */
354
-	public function getUserGroupNames(IUser $user) {
355
-		return array_map(function ($group) {
356
-			return ['displayName' => $group->getDisplayName()];
357
-		}, $this->getUserGroups($user));
358
-	}
359
-
360
-	/**
361
-	 * get a list of all display names in a group
362
-	 *
363
-	 * @param string $gid
364
-	 * @param string $search
365
-	 * @param int $limit
366
-	 * @param int $offset
367
-	 * @return array an array of display names (value) and user ids (key)
368
-	 */
369
-	public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
370
-		$group = $this->get($gid);
371
-		if (is_null($group)) {
372
-			return [];
373
-		}
374
-
375
-		$search = trim($search);
376
-		$groupUsers = [];
377
-
378
-		if (!empty($search)) {
379
-			// only user backends have the capability to do a complex search for users
380
-			$searchOffset = 0;
381
-			$searchLimit = $limit * 100;
382
-			if ($limit === -1) {
383
-				$searchLimit = 500;
384
-			}
385
-
386
-			do {
387
-				$filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
388
-				foreach ($filteredUsers as $filteredUser) {
389
-					if ($group->inGroup($filteredUser)) {
390
-						$groupUsers[] = $filteredUser;
391
-					}
392
-				}
393
-				$searchOffset += $searchLimit;
394
-			} while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit);
395
-
396
-			if ($limit === -1) {
397
-				$groupUsers = array_slice($groupUsers, $offset);
398
-			} else {
399
-				$groupUsers = array_slice($groupUsers, $offset, $limit);
400
-			}
401
-		} else {
402
-			$groupUsers = $group->searchUsers('', $limit, $offset);
403
-		}
404
-
405
-		$matchingUsers = [];
406
-		foreach ($groupUsers as $groupUser) {
407
-			$matchingUsers[(string) $groupUser->getUID()] = $groupUser->getDisplayName();
408
-		}
409
-		return $matchingUsers;
410
-	}
411
-
412
-	/**
413
-	 * @return \OC\SubAdmin
414
-	 */
415
-	public function getSubAdmin() {
416
-		if (!$this->subAdmin) {
417
-			$this->subAdmin = new \OC\SubAdmin(
418
-				$this->userManager,
419
-				$this,
420
-				\OC::$server->getDatabaseConnection(),
421
-				\OC::$server->get(IEventDispatcher::class)
422
-			);
423
-		}
424
-
425
-		return $this->subAdmin;
426
-	}
68
+    /** @var GroupInterface[] */
69
+    private $backends = [];
70
+
71
+    /** @var \OC\User\Manager */
72
+    private $userManager;
73
+    /** @var EventDispatcherInterface */
74
+    private $dispatcher;
75
+    /** @var ILogger */
76
+    private $logger;
77
+
78
+    /** @var \OC\Group\Group[] */
79
+    private $cachedGroups = [];
80
+
81
+    /** @var (string[])[] */
82
+    private $cachedUserGroups = [];
83
+
84
+    /** @var \OC\SubAdmin */
85
+    private $subAdmin = null;
86
+
87
+    /**
88
+     * @param \OC\User\Manager $userManager
89
+     * @param EventDispatcherInterface $dispatcher
90
+     * @param ILogger $logger
91
+     */
92
+    public function __construct(\OC\User\Manager $userManager,
93
+                                EventDispatcherInterface $dispatcher,
94
+                                ILogger $logger) {
95
+        $this->userManager = $userManager;
96
+        $this->dispatcher = $dispatcher;
97
+        $this->logger = $logger;
98
+
99
+        $cachedGroups = &$this->cachedGroups;
100
+        $cachedUserGroups = &$this->cachedUserGroups;
101
+        $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
102
+            /**
103
+             * @var \OC\Group\Group $group
104
+             */
105
+            unset($cachedGroups[$group->getGID()]);
106
+            $cachedUserGroups = [];
107
+        });
108
+        $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
109
+            /**
110
+             * @var \OC\Group\Group $group
111
+             */
112
+            $cachedUserGroups = [];
113
+        });
114
+        $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
115
+            /**
116
+             * @var \OC\Group\Group $group
117
+             */
118
+            $cachedUserGroups = [];
119
+        });
120
+    }
121
+
122
+    /**
123
+     * Checks whether a given backend is used
124
+     *
125
+     * @param string $backendClass Full classname including complete namespace
126
+     * @return bool
127
+     */
128
+    public function isBackendUsed($backendClass) {
129
+        $backendClass = strtolower(ltrim($backendClass, '\\'));
130
+
131
+        foreach ($this->backends as $backend) {
132
+            if (strtolower(get_class($backend)) === $backendClass) {
133
+                return true;
134
+            }
135
+        }
136
+
137
+        return false;
138
+    }
139
+
140
+    /**
141
+     * @param \OCP\GroupInterface $backend
142
+     */
143
+    public function addBackend($backend) {
144
+        $this->backends[] = $backend;
145
+        $this->clearCaches();
146
+    }
147
+
148
+    public function clearBackends() {
149
+        $this->backends = [];
150
+        $this->clearCaches();
151
+    }
152
+
153
+    /**
154
+     * Get the active backends
155
+     *
156
+     * @return \OCP\GroupInterface[]
157
+     */
158
+    public function getBackends() {
159
+        return $this->backends;
160
+    }
161
+
162
+
163
+    protected function clearCaches() {
164
+        $this->cachedGroups = [];
165
+        $this->cachedUserGroups = [];
166
+    }
167
+
168
+    /**
169
+     * @param string $gid
170
+     * @return IGroup|null
171
+     */
172
+    public function get($gid) {
173
+        if (isset($this->cachedGroups[$gid])) {
174
+            return $this->cachedGroups[$gid];
175
+        }
176
+        return $this->getGroupObject($gid);
177
+    }
178
+
179
+    /**
180
+     * @param string $gid
181
+     * @param string $displayName
182
+     * @return \OCP\IGroup|null
183
+     */
184
+    protected function getGroupObject($gid, $displayName = null) {
185
+        $backends = [];
186
+        foreach ($this->backends as $backend) {
187
+            if ($backend->implementsActions(Backend::GROUP_DETAILS)) {
188
+                $groupData = $backend->getGroupDetails($gid);
189
+                if (is_array($groupData) && !empty($groupData)) {
190
+                    // take the display name from the first backend that has a non-null one
191
+                    if (is_null($displayName) && isset($groupData['displayName'])) {
192
+                        $displayName = $groupData['displayName'];
193
+                    }
194
+                    $backends[] = $backend;
195
+                }
196
+            } elseif ($backend->groupExists($gid)) {
197
+                $backends[] = $backend;
198
+            }
199
+        }
200
+        if (count($backends) === 0) {
201
+            return null;
202
+        }
203
+        $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
204
+        return $this->cachedGroups[$gid];
205
+    }
206
+
207
+    /**
208
+     * @param string $gid
209
+     * @return bool
210
+     */
211
+    public function groupExists($gid) {
212
+        return $this->get($gid) instanceof IGroup;
213
+    }
214
+
215
+    /**
216
+     * @param string $gid
217
+     * @return IGroup|null
218
+     */
219
+    public function createGroup($gid) {
220
+        if ($gid === '' || $gid === null) {
221
+            return null;
222
+        } elseif ($group = $this->get($gid)) {
223
+            return $group;
224
+        } else {
225
+            $this->emit('\OC\Group', 'preCreate', [$gid]);
226
+            foreach ($this->backends as $backend) {
227
+                if ($backend->implementsActions(Backend::CREATE_GROUP)) {
228
+                    if ($backend->createGroup($gid)) {
229
+                        $group = $this->getGroupObject($gid);
230
+                        $this->emit('\OC\Group', 'postCreate', [$group]);
231
+                        return $group;
232
+                    }
233
+                }
234
+            }
235
+            return null;
236
+        }
237
+    }
238
+
239
+    /**
240
+     * @param string $search
241
+     * @param int $limit
242
+     * @param int $offset
243
+     * @return \OC\Group\Group[]
244
+     */
245
+    public function search($search, $limit = null, $offset = null) {
246
+        $groups = [];
247
+        foreach ($this->backends as $backend) {
248
+            $groupIds = $backend->getGroups($search, $limit, $offset);
249
+            foreach ($groupIds as $groupId) {
250
+                $aGroup = $this->get($groupId);
251
+                if ($aGroup instanceof IGroup) {
252
+                    $groups[$groupId] = $aGroup;
253
+                } else {
254
+                    $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
255
+                }
256
+            }
257
+            if (!is_null($limit) and $limit <= 0) {
258
+                return array_values($groups);
259
+            }
260
+        }
261
+        return array_values($groups);
262
+    }
263
+
264
+    /**
265
+     * @param IUser|null $user
266
+     * @return \OC\Group\Group[]
267
+     */
268
+    public function getUserGroups(IUser $user = null) {
269
+        if (!$user instanceof IUser) {
270
+            return [];
271
+        }
272
+        return $this->getUserIdGroups($user->getUID());
273
+    }
274
+
275
+    /**
276
+     * @param string $uid the user id
277
+     * @return \OC\Group\Group[]
278
+     */
279
+    public function getUserIdGroups($uid) {
280
+        $groups = [];
281
+
282
+        foreach ($this->getUserIdGroupIds($uid) as $groupId) {
283
+            $aGroup = $this->get($groupId);
284
+            if ($aGroup instanceof IGroup) {
285
+                $groups[$groupId] = $aGroup;
286
+            } else {
287
+                $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
288
+            }
289
+        }
290
+
291
+        return $groups;
292
+    }
293
+
294
+    /**
295
+     * Checks if a userId is in the admin group
296
+     *
297
+     * @param string $userId
298
+     * @return bool if admin
299
+     */
300
+    public function isAdmin($userId) {
301
+        foreach ($this->backends as $backend) {
302
+            if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
303
+                return true;
304
+            }
305
+        }
306
+        return $this->isInGroup($userId, 'admin');
307
+    }
308
+
309
+    /**
310
+     * Checks if a userId is in a group
311
+     *
312
+     * @param string $userId
313
+     * @param string $group
314
+     * @return bool if in group
315
+     */
316
+    public function isInGroup($userId, $group) {
317
+        return array_search($group, $this->getUserIdGroupIds($userId)) !== false;
318
+    }
319
+
320
+    /**
321
+     * get a list of group ids for a user
322
+     *
323
+     * @param IUser $user
324
+     * @return array with group ids
325
+     */
326
+    public function getUserGroupIds(IUser $user) {
327
+        return $this->getUserIdGroupIds($user->getUID());
328
+    }
329
+
330
+    /**
331
+     * @param string $uid the user id
332
+     * @return GroupInterface[]
333
+     */
334
+    private function getUserIdGroupIds($uid) {
335
+        if (!isset($this->cachedUserGroups[$uid])) {
336
+            $groups = [];
337
+            foreach ($this->backends as $backend) {
338
+                if ($groupIds = $backend->getUserGroups($uid)) {
339
+                    $groups = array_merge($groups, $groupIds);
340
+                }
341
+            }
342
+            $this->cachedUserGroups[$uid] = $groups;
343
+        }
344
+
345
+        return $this->cachedUserGroups[$uid];
346
+    }
347
+
348
+    /**
349
+     * get an array of groupid and displayName for a user
350
+     *
351
+     * @param IUser $user
352
+     * @return array ['displayName' => displayname]
353
+     */
354
+    public function getUserGroupNames(IUser $user) {
355
+        return array_map(function ($group) {
356
+            return ['displayName' => $group->getDisplayName()];
357
+        }, $this->getUserGroups($user));
358
+    }
359
+
360
+    /**
361
+     * get a list of all display names in a group
362
+     *
363
+     * @param string $gid
364
+     * @param string $search
365
+     * @param int $limit
366
+     * @param int $offset
367
+     * @return array an array of display names (value) and user ids (key)
368
+     */
369
+    public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
370
+        $group = $this->get($gid);
371
+        if (is_null($group)) {
372
+            return [];
373
+        }
374
+
375
+        $search = trim($search);
376
+        $groupUsers = [];
377
+
378
+        if (!empty($search)) {
379
+            // only user backends have the capability to do a complex search for users
380
+            $searchOffset = 0;
381
+            $searchLimit = $limit * 100;
382
+            if ($limit === -1) {
383
+                $searchLimit = 500;
384
+            }
385
+
386
+            do {
387
+                $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
388
+                foreach ($filteredUsers as $filteredUser) {
389
+                    if ($group->inGroup($filteredUser)) {
390
+                        $groupUsers[] = $filteredUser;
391
+                    }
392
+                }
393
+                $searchOffset += $searchLimit;
394
+            } while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit);
395
+
396
+            if ($limit === -1) {
397
+                $groupUsers = array_slice($groupUsers, $offset);
398
+            } else {
399
+                $groupUsers = array_slice($groupUsers, $offset, $limit);
400
+            }
401
+        } else {
402
+            $groupUsers = $group->searchUsers('', $limit, $offset);
403
+        }
404
+
405
+        $matchingUsers = [];
406
+        foreach ($groupUsers as $groupUser) {
407
+            $matchingUsers[(string) $groupUser->getUID()] = $groupUser->getDisplayName();
408
+        }
409
+        return $matchingUsers;
410
+    }
411
+
412
+    /**
413
+     * @return \OC\SubAdmin
414
+     */
415
+    public function getSubAdmin() {
416
+        if (!$this->subAdmin) {
417
+            $this->subAdmin = new \OC\SubAdmin(
418
+                $this->userManager,
419
+                $this,
420
+                \OC::$server->getDatabaseConnection(),
421
+                \OC::$server->get(IEventDispatcher::class)
422
+            );
423
+        }
424
+
425
+        return $this->subAdmin;
426
+    }
427 427
 }
Please login to merge, or discard this patch.