Passed
Push — master ( 48395b...d6de8e )
by John
10:56
created
apps/dav/lib/DAV/GroupPrincipalBackend.php 2 patches
Indentation   +283 added lines, -283 removed lines patch added patch discarded remove patch
@@ -35,287 +35,287 @@
 block discarded – undo
35 35
 
36 36
 class GroupPrincipalBackend implements BackendInterface {
37 37
 
38
-	const PRINCIPAL_PREFIX = 'principals/groups';
39
-
40
-	/** @var IGroupManager */
41
-	private $groupManager;
42
-
43
-	/** @var IUserSession */
44
-	private $userSession;
45
-
46
-	/** @var IShareManager */
47
-	private $shareManager;
48
-
49
-	/**
50
-	 * @param IGroupManager $IGroupManager
51
-	 * @param IUserSession $userSession
52
-	 * @param IShareManager $shareManager
53
-	 */
54
-	public function __construct(IGroupManager $IGroupManager,
55
-								IUserSession $userSession,
56
-								IShareManager $shareManager) {
57
-		$this->groupManager = $IGroupManager;
58
-		$this->userSession = $userSession;
59
-		$this->shareManager = $shareManager;
60
-	}
61
-
62
-	/**
63
-	 * Returns a list of principals based on a prefix.
64
-	 *
65
-	 * This prefix will often contain something like 'principals'. You are only
66
-	 * expected to return principals that are in this base path.
67
-	 *
68
-	 * You are expected to return at least a 'uri' for every user, you can
69
-	 * return any additional properties if you wish so. Common properties are:
70
-	 *   {DAV:}displayname
71
-	 *
72
-	 * @param string $prefixPath
73
-	 * @return string[]
74
-	 */
75
-	public function getPrincipalsByPrefix($prefixPath) {
76
-		$principals = [];
77
-
78
-		if ($prefixPath === self::PRINCIPAL_PREFIX) {
79
-			foreach($this->groupManager->search('') as $user) {
80
-				$principals[] = $this->groupToPrincipal($user);
81
-			}
82
-		}
83
-
84
-		return $principals;
85
-	}
86
-
87
-	/**
88
-	 * Returns a specific principal, specified by it's path.
89
-	 * The returned structure should be the exact same as from
90
-	 * getPrincipalsByPrefix.
91
-	 *
92
-	 * @param string $path
93
-	 * @return array
94
-	 */
95
-	public function getPrincipalByPath($path) {
96
-		$elements = explode('/', $path,  3);
97
-		if ($elements[0] !== 'principals') {
98
-			return null;
99
-		}
100
-		if ($elements[1] !== 'groups') {
101
-			return null;
102
-		}
103
-		$name = urldecode($elements[2]);
104
-		$group = $this->groupManager->get($name);
105
-
106
-		if (!is_null($group)) {
107
-			return $this->groupToPrincipal($group);
108
-		}
109
-
110
-		return null;
111
-	}
112
-
113
-	/**
114
-	 * Returns the list of members for a group-principal
115
-	 *
116
-	 * @param string $principal
117
-	 * @return string[]
118
-	 * @throws Exception
119
-	 */
120
-	public function getGroupMemberSet($principal) {
121
-		$elements = explode('/', $principal);
122
-		if ($elements[0] !== 'principals') {
123
-			return [];
124
-		}
125
-		if ($elements[1] !== 'groups') {
126
-			return [];
127
-		}
128
-		$name = $elements[2];
129
-		$group = $this->groupManager->get($name);
130
-
131
-		if (is_null($group)) {
132
-			return [];
133
-		}
134
-
135
-		return array_map(function($user) {
136
-			return $this->userToPrincipal($user);
137
-		}, $group->getUsers());
138
-	}
139
-
140
-	/**
141
-	 * Returns the list of groups a principal is a member of
142
-	 *
143
-	 * @param string $principal
144
-	 * @return array
145
-	 * @throws Exception
146
-	 */
147
-	public function getGroupMembership($principal) {
148
-		return [];
149
-	}
150
-
151
-	/**
152
-	 * Updates the list of group members for a group principal.
153
-	 *
154
-	 * The principals should be passed as a list of uri's.
155
-	 *
156
-	 * @param string $principal
157
-	 * @param string[] $members
158
-	 * @throws Exception
159
-	 */
160
-	public function setGroupMemberSet($principal, array $members) {
161
-		throw new Exception('Setting members of the group is not supported yet');
162
-	}
163
-
164
-	/**
165
-	 * @param string $path
166
-	 * @param PropPatch $propPatch
167
-	 * @return int
168
-	 */
169
-	function updatePrincipal($path, PropPatch $propPatch) {
170
-		return 0;
171
-	}
172
-
173
-	/**
174
-	 * @param string $prefixPath
175
-	 * @param array $searchProperties
176
-	 * @param string $test
177
-	 * @return array
178
-	 */
179
-	function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
180
-		$results = [];
181
-
182
-		if (\count($searchProperties) === 0) {
183
-			return [];
184
-		}
185
-		if ($prefixPath !== self::PRINCIPAL_PREFIX) {
186
-			return [];
187
-		}
188
-		// If sharing is disabled, return the empty array
189
-		$shareAPIEnabled = $this->shareManager->shareApiEnabled();
190
-		if (!$shareAPIEnabled) {
191
-			return [];
192
-		}
193
-
194
-		// If sharing is restricted to group members only,
195
-		// return only members that have groups in common
196
-		$restrictGroups = false;
197
-		if ($this->shareManager->shareWithGroupMembersOnly()) {
198
-			$user = $this->userSession->getUser();
199
-			if (!$user) {
200
-				return [];
201
-			}
202
-
203
-			$restrictGroups = $this->groupManager->getUserGroupIds($user);
204
-		}
205
-
206
-		foreach ($searchProperties as $prop => $value) {
207
-			switch ($prop) {
208
-				case '{DAV:}displayname':
209
-					$groups = $this->groupManager->search($value);
210
-
211
-					$results[] = array_reduce($groups, function(array $carry, IGroup $group) use ($restrictGroups) {
212
-						$gid = $group->getGID();
213
-						// is sharing restricted to groups only?
214
-						if ($restrictGroups !== false) {
215
-							if (!\in_array($gid, $restrictGroups, true)) {
216
-								return $carry;
217
-							}
218
-						}
219
-
220
-						$carry[] = self::PRINCIPAL_PREFIX . '/' . $gid;
221
-						return $carry;
222
-					}, []);
223
-					break;
224
-
225
-				default:
226
-					$results[] = [];
227
-					break;
228
-			}
229
-		}
230
-
231
-		// results is an array of arrays, so this is not the first search result
232
-		// but the results of the first searchProperty
233
-		if (count($results) === 1) {
234
-			return $results[0];
235
-		}
236
-
237
-		switch ($test) {
238
-			case 'anyof':
239
-				return array_values(array_unique(array_merge(...$results)));
240
-
241
-			case 'allof':
242
-			default:
243
-				return array_values(array_intersect(...$results));
244
-		}
245
-	}
246
-
247
-	/**
248
-	 * @param string $uri
249
-	 * @param string $principalPrefix
250
-	 * @return string
251
-	 */
252
-	function findByUri($uri, $principalPrefix) {
253
-		// If sharing is disabled, return the empty array
254
-		$shareAPIEnabled = $this->shareManager->shareApiEnabled();
255
-		if (!$shareAPIEnabled) {
256
-			return null;
257
-		}
258
-
259
-		// If sharing is restricted to group members only,
260
-		// return only members that have groups in common
261
-		$restrictGroups = false;
262
-		if ($this->shareManager->shareWithGroupMembersOnly()) {
263
-			$user = $this->userSession->getUser();
264
-			if (!$user) {
265
-				return null;
266
-			}
267
-
268
-			$restrictGroups = $this->groupManager->getUserGroupIds($user);
269
-		}
270
-
271
-		if (strpos($uri, 'principal:principals/groups/') === 0) {
272
-			$name = urlencode(substr($uri, 28));
273
-			if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) {
274
-				return null;
275
-			}
276
-
277
-			return substr($uri, 10);
278
-		}
279
-
280
-		return null;
281
-	}
282
-
283
-	/**
284
-	 * @param IGroup $group
285
-	 * @return array
286
-	 */
287
-	protected function groupToPrincipal($group) {
288
-		$groupId = $group->getGID();
289
-		// getDisplayName returns UID if none
290
-		$displayName = $group->getDisplayName();
291
-
292
-		return [
293
-			'uri' => 'principals/groups/' . urlencode($groupId),
294
-			'{DAV:}displayname' => $displayName,
295
-			'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
296
-		];
297
-	}
298
-
299
-	/**
300
-	 * @param IUser $user
301
-	 * @return array
302
-	 */
303
-	protected function userToPrincipal($user) {
304
-		$userId = $user->getUID();
305
-		// getDisplayName returns UID if none
306
-		$displayName = $user->getDisplayName();
307
-
308
-		$principal = [
309
-			'uri' => 'principals/users/' . $userId,
310
-			'{DAV:}displayname' => $displayName,
311
-			'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
312
-		];
313
-
314
-		$email = $user->getEMailAddress();
315
-		if (!empty($email)) {
316
-			$principal['{http://sabredav.org/ns}email-address'] = $email;
317
-		}
318
-
319
-		return $principal;
320
-	}
38
+    const PRINCIPAL_PREFIX = 'principals/groups';
39
+
40
+    /** @var IGroupManager */
41
+    private $groupManager;
42
+
43
+    /** @var IUserSession */
44
+    private $userSession;
45
+
46
+    /** @var IShareManager */
47
+    private $shareManager;
48
+
49
+    /**
50
+     * @param IGroupManager $IGroupManager
51
+     * @param IUserSession $userSession
52
+     * @param IShareManager $shareManager
53
+     */
54
+    public function __construct(IGroupManager $IGroupManager,
55
+                                IUserSession $userSession,
56
+                                IShareManager $shareManager) {
57
+        $this->groupManager = $IGroupManager;
58
+        $this->userSession = $userSession;
59
+        $this->shareManager = $shareManager;
60
+    }
61
+
62
+    /**
63
+     * Returns a list of principals based on a prefix.
64
+     *
65
+     * This prefix will often contain something like 'principals'. You are only
66
+     * expected to return principals that are in this base path.
67
+     *
68
+     * You are expected to return at least a 'uri' for every user, you can
69
+     * return any additional properties if you wish so. Common properties are:
70
+     *   {DAV:}displayname
71
+     *
72
+     * @param string $prefixPath
73
+     * @return string[]
74
+     */
75
+    public function getPrincipalsByPrefix($prefixPath) {
76
+        $principals = [];
77
+
78
+        if ($prefixPath === self::PRINCIPAL_PREFIX) {
79
+            foreach($this->groupManager->search('') as $user) {
80
+                $principals[] = $this->groupToPrincipal($user);
81
+            }
82
+        }
83
+
84
+        return $principals;
85
+    }
86
+
87
+    /**
88
+     * Returns a specific principal, specified by it's path.
89
+     * The returned structure should be the exact same as from
90
+     * getPrincipalsByPrefix.
91
+     *
92
+     * @param string $path
93
+     * @return array
94
+     */
95
+    public function getPrincipalByPath($path) {
96
+        $elements = explode('/', $path,  3);
97
+        if ($elements[0] !== 'principals') {
98
+            return null;
99
+        }
100
+        if ($elements[1] !== 'groups') {
101
+            return null;
102
+        }
103
+        $name = urldecode($elements[2]);
104
+        $group = $this->groupManager->get($name);
105
+
106
+        if (!is_null($group)) {
107
+            return $this->groupToPrincipal($group);
108
+        }
109
+
110
+        return null;
111
+    }
112
+
113
+    /**
114
+     * Returns the list of members for a group-principal
115
+     *
116
+     * @param string $principal
117
+     * @return string[]
118
+     * @throws Exception
119
+     */
120
+    public function getGroupMemberSet($principal) {
121
+        $elements = explode('/', $principal);
122
+        if ($elements[0] !== 'principals') {
123
+            return [];
124
+        }
125
+        if ($elements[1] !== 'groups') {
126
+            return [];
127
+        }
128
+        $name = $elements[2];
129
+        $group = $this->groupManager->get($name);
130
+
131
+        if (is_null($group)) {
132
+            return [];
133
+        }
134
+
135
+        return array_map(function($user) {
136
+            return $this->userToPrincipal($user);
137
+        }, $group->getUsers());
138
+    }
139
+
140
+    /**
141
+     * Returns the list of groups a principal is a member of
142
+     *
143
+     * @param string $principal
144
+     * @return array
145
+     * @throws Exception
146
+     */
147
+    public function getGroupMembership($principal) {
148
+        return [];
149
+    }
150
+
151
+    /**
152
+     * Updates the list of group members for a group principal.
153
+     *
154
+     * The principals should be passed as a list of uri's.
155
+     *
156
+     * @param string $principal
157
+     * @param string[] $members
158
+     * @throws Exception
159
+     */
160
+    public function setGroupMemberSet($principal, array $members) {
161
+        throw new Exception('Setting members of the group is not supported yet');
162
+    }
163
+
164
+    /**
165
+     * @param string $path
166
+     * @param PropPatch $propPatch
167
+     * @return int
168
+     */
169
+    function updatePrincipal($path, PropPatch $propPatch) {
170
+        return 0;
171
+    }
172
+
173
+    /**
174
+     * @param string $prefixPath
175
+     * @param array $searchProperties
176
+     * @param string $test
177
+     * @return array
178
+     */
179
+    function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
180
+        $results = [];
181
+
182
+        if (\count($searchProperties) === 0) {
183
+            return [];
184
+        }
185
+        if ($prefixPath !== self::PRINCIPAL_PREFIX) {
186
+            return [];
187
+        }
188
+        // If sharing is disabled, return the empty array
189
+        $shareAPIEnabled = $this->shareManager->shareApiEnabled();
190
+        if (!$shareAPIEnabled) {
191
+            return [];
192
+        }
193
+
194
+        // If sharing is restricted to group members only,
195
+        // return only members that have groups in common
196
+        $restrictGroups = false;
197
+        if ($this->shareManager->shareWithGroupMembersOnly()) {
198
+            $user = $this->userSession->getUser();
199
+            if (!$user) {
200
+                return [];
201
+            }
202
+
203
+            $restrictGroups = $this->groupManager->getUserGroupIds($user);
204
+        }
205
+
206
+        foreach ($searchProperties as $prop => $value) {
207
+            switch ($prop) {
208
+                case '{DAV:}displayname':
209
+                    $groups = $this->groupManager->search($value);
210
+
211
+                    $results[] = array_reduce($groups, function(array $carry, IGroup $group) use ($restrictGroups) {
212
+                        $gid = $group->getGID();
213
+                        // is sharing restricted to groups only?
214
+                        if ($restrictGroups !== false) {
215
+                            if (!\in_array($gid, $restrictGroups, true)) {
216
+                                return $carry;
217
+                            }
218
+                        }
219
+
220
+                        $carry[] = self::PRINCIPAL_PREFIX . '/' . $gid;
221
+                        return $carry;
222
+                    }, []);
223
+                    break;
224
+
225
+                default:
226
+                    $results[] = [];
227
+                    break;
228
+            }
229
+        }
230
+
231
+        // results is an array of arrays, so this is not the first search result
232
+        // but the results of the first searchProperty
233
+        if (count($results) === 1) {
234
+            return $results[0];
235
+        }
236
+
237
+        switch ($test) {
238
+            case 'anyof':
239
+                return array_values(array_unique(array_merge(...$results)));
240
+
241
+            case 'allof':
242
+            default:
243
+                return array_values(array_intersect(...$results));
244
+        }
245
+    }
246
+
247
+    /**
248
+     * @param string $uri
249
+     * @param string $principalPrefix
250
+     * @return string
251
+     */
252
+    function findByUri($uri, $principalPrefix) {
253
+        // If sharing is disabled, return the empty array
254
+        $shareAPIEnabled = $this->shareManager->shareApiEnabled();
255
+        if (!$shareAPIEnabled) {
256
+            return null;
257
+        }
258
+
259
+        // If sharing is restricted to group members only,
260
+        // return only members that have groups in common
261
+        $restrictGroups = false;
262
+        if ($this->shareManager->shareWithGroupMembersOnly()) {
263
+            $user = $this->userSession->getUser();
264
+            if (!$user) {
265
+                return null;
266
+            }
267
+
268
+            $restrictGroups = $this->groupManager->getUserGroupIds($user);
269
+        }
270
+
271
+        if (strpos($uri, 'principal:principals/groups/') === 0) {
272
+            $name = urlencode(substr($uri, 28));
273
+            if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) {
274
+                return null;
275
+            }
276
+
277
+            return substr($uri, 10);
278
+        }
279
+
280
+        return null;
281
+    }
282
+
283
+    /**
284
+     * @param IGroup $group
285
+     * @return array
286
+     */
287
+    protected function groupToPrincipal($group) {
288
+        $groupId = $group->getGID();
289
+        // getDisplayName returns UID if none
290
+        $displayName = $group->getDisplayName();
291
+
292
+        return [
293
+            'uri' => 'principals/groups/' . urlencode($groupId),
294
+            '{DAV:}displayname' => $displayName,
295
+            '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
296
+        ];
297
+    }
298
+
299
+    /**
300
+     * @param IUser $user
301
+     * @return array
302
+     */
303
+    protected function userToPrincipal($user) {
304
+        $userId = $user->getUID();
305
+        // getDisplayName returns UID if none
306
+        $displayName = $user->getDisplayName();
307
+
308
+        $principal = [
309
+            'uri' => 'principals/users/' . $userId,
310
+            '{DAV:}displayname' => $displayName,
311
+            '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
312
+        ];
313
+
314
+        $email = $user->getEMailAddress();
315
+        if (!empty($email)) {
316
+            $principal['{http://sabredav.org/ns}email-address'] = $email;
317
+        }
318
+
319
+        return $principal;
320
+    }
321 321
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -76,7 +76,7 @@  discard block
 block discarded – undo
76 76
 		$principals = [];
77 77
 
78 78
 		if ($prefixPath === self::PRINCIPAL_PREFIX) {
79
-			foreach($this->groupManager->search('') as $user) {
79
+			foreach ($this->groupManager->search('') as $user) {
80 80
 				$principals[] = $this->groupToPrincipal($user);
81 81
 			}
82 82
 		}
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 	 * @return array
94 94
 	 */
95 95
 	public function getPrincipalByPath($path) {
96
-		$elements = explode('/', $path,  3);
96
+		$elements = explode('/', $path, 3);
97 97
 		if ($elements[0] !== 'principals') {
98 98
 			return null;
99 99
 		}
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 							}
218 218
 						}
219 219
 
220
-						$carry[] = self::PRINCIPAL_PREFIX . '/' . $gid;
220
+						$carry[] = self::PRINCIPAL_PREFIX.'/'.$gid;
221 221
 						return $carry;
222 222
 					}, []);
223 223
 					break;
@@ -290,7 +290,7 @@  discard block
 block discarded – undo
290 290
 		$displayName = $group->getDisplayName();
291 291
 
292 292
 		return [
293
-			'uri' => 'principals/groups/' . urlencode($groupId),
293
+			'uri' => 'principals/groups/'.urlencode($groupId),
294 294
 			'{DAV:}displayname' => $displayName,
295 295
 			'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP',
296 296
 		];
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
 		$displayName = $user->getDisplayName();
307 307
 
308 308
 		$principal = [
309
-			'uri' => 'principals/users/' . $userId,
309
+			'uri' => 'principals/users/'.$userId,
310 310
 			'{DAV:}displayname' => $displayName,
311 311
 			'{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
312 312
 		];
Please login to merge, or discard this patch.