|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of the Joomla Framework Github Package |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
|
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Joomla\Github\Package\Orgs; |
|
10
|
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* GitHub API Orgs Members class for the Joomla Framework. |
|
15
|
|
|
* |
|
16
|
|
|
* @documentation http://developer.github.com/v3/orgs/members/ |
|
17
|
|
|
* |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Members extends AbstractPackage |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Members list. |
|
24
|
|
|
* |
|
25
|
|
|
* List all users who are members of an organization. |
|
26
|
|
|
* A member is a user that belongs to at least 1 team in the organization. |
|
27
|
|
|
* If the authenticated user is also a member of this organization then |
|
28
|
|
|
* both concealed and public members will be returned. |
|
29
|
|
|
* If the requester is not a member of the organization the query will be |
|
30
|
|
|
* redirected to the public members list. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $org The name of the organization. |
|
33
|
|
|
* |
|
34
|
|
|
* @throws \UnexpectedValueException |
|
35
|
|
|
* @since 1.0 |
|
36
|
|
|
* |
|
37
|
|
|
* @return boolean|mixed |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function getList($org) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
// Build the request path. |
|
42
|
|
|
$path = '/orgs/' . $org . '/members'; |
|
43
|
|
|
|
|
44
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
|
45
|
|
|
|
|
46
|
|
|
switch ($response->code) |
|
47
|
|
|
{ |
|
48
|
|
|
case 302 : |
|
49
|
|
|
// Requester is not an organization member. |
|
50
|
|
|
return false; |
|
51
|
|
|
break; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
case 200 : |
|
54
|
|
|
return json_decode($response->body); |
|
55
|
|
|
break; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
default : |
|
58
|
|
|
throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); |
|
59
|
|
|
break; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Check membership. |
|
65
|
|
|
* |
|
66
|
|
|
* Check if a user is, publicly or privately, a member of the organization. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $org The name of the organization. |
|
69
|
|
|
* @param string $user The name of the user. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \UnexpectedValueException |
|
72
|
|
|
* @since 1.0 |
|
73
|
|
|
* |
|
74
|
|
|
* @return boolean |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function check($org, $user) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
// Build the request path. |
|
79
|
|
|
$path = '/orgs/' . $org . '/members/' . $user; |
|
80
|
|
|
|
|
81
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
|
82
|
|
|
|
|
83
|
|
|
switch ($response->code) |
|
84
|
|
|
{ |
|
85
|
|
|
case 204 : |
|
86
|
|
|
// Requester is an organization member and user is a member. |
|
87
|
|
|
return true; |
|
88
|
|
|
break; |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
case 404 : |
|
91
|
|
|
// Requester is an organization member and user is not a member. |
|
92
|
|
|
// Requester is not an organization member and is inquiring about themselves. |
|
93
|
|
|
return false; |
|
94
|
|
|
break; |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
case 302 : |
|
97
|
|
|
// Requester is not an organization member. |
|
98
|
|
|
return false; |
|
99
|
|
|
break; |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
default : |
|
102
|
|
|
throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); |
|
103
|
|
|
break; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add a member. |
|
109
|
|
|
* |
|
110
|
|
|
* To add someone as a member to an org, you must add them to a team. |
|
111
|
|
|
*/ |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Remove a member. |
|
115
|
|
|
* |
|
116
|
|
|
* Removing a user from this list will remove them from all teams and they will no longer have |
|
117
|
|
|
* any access to the organization’s repositories. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $org The name of the organization. |
|
120
|
|
|
* @param string $user The name of the user. |
|
121
|
|
|
* |
|
122
|
|
|
* @since 1.0 |
|
123
|
|
|
* |
|
124
|
|
|
* @return object |
|
125
|
|
|
*/ |
|
126
|
|
|
public function remove($org, $user) |
|
127
|
|
|
{ |
|
128
|
|
|
// Build the request path. |
|
129
|
|
|
$path = '/orgs/' . $org . '/members/' . $user; |
|
130
|
|
|
|
|
131
|
|
|
return $this->processResponse( |
|
132
|
|
|
$this->client->delete($this->fetchUrl($path)), |
|
133
|
|
|
204 |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Public members list. |
|
139
|
|
|
* |
|
140
|
|
|
* Members of an organization can choose to have their membership publicized or not. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $org The name of the organization. |
|
143
|
|
|
* |
|
144
|
|
|
* @since 1.0 |
|
145
|
|
|
* |
|
146
|
|
|
* @return object |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
public function getListPublic($org) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
// Build the request path. |
|
151
|
|
|
$path = '/orgs/' . $org . '/public_members'; |
|
152
|
|
|
|
|
153
|
|
|
return $this->processResponse( |
|
154
|
|
|
$this->client->get($this->fetchUrl($path)) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Check public membership. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $org The name of the organization. |
|
162
|
|
|
* @param string $user The name of the user. |
|
163
|
|
|
* |
|
164
|
|
|
* @throws \UnexpectedValueException |
|
165
|
|
|
* @since 1.0 |
|
166
|
|
|
* |
|
167
|
|
|
* @return boolean |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public function checkPublic($org, $user) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
// Build the request path. |
|
172
|
|
|
$path = '/orgs/' . $org . '/public_members/' . $user; |
|
173
|
|
|
|
|
174
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
|
175
|
|
|
|
|
176
|
|
|
switch ($response->code) |
|
177
|
|
|
{ |
|
178
|
|
|
case 204 : |
|
179
|
|
|
// Response if user is a public member. |
|
180
|
|
|
return true; |
|
181
|
|
|
break; |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
case 404 : |
|
184
|
|
|
// Response if user is not a public member. |
|
185
|
|
|
return false; |
|
186
|
|
|
break; |
|
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
default : |
|
189
|
|
|
throw new \UnexpectedValueException('Unexpected response code: ' . $response->code); |
|
190
|
|
|
break; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Publicize a user’s membership. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $org The name of the organization. |
|
198
|
|
|
* @param string $user The name of the user. |
|
199
|
|
|
* |
|
200
|
|
|
* @since 1.0 |
|
201
|
|
|
* |
|
202
|
|
|
* @return object |
|
203
|
|
|
*/ |
|
204
|
|
|
public function publicize($org, $user) |
|
205
|
|
|
{ |
|
206
|
|
|
// Build the request path. |
|
207
|
|
|
$path = '/orgs/' . $org . '/public_members/' . $user; |
|
208
|
|
|
|
|
209
|
|
|
return $this->processResponse( |
|
210
|
|
|
$this->client->put($this->fetchUrl($path), ''), |
|
211
|
|
|
204 |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Conceal a user’s membership. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $org The name of the organization. |
|
219
|
|
|
* @param string $user The name of the user. |
|
220
|
|
|
* |
|
221
|
|
|
* @since 1.0 |
|
222
|
|
|
* |
|
223
|
|
|
* @return object |
|
224
|
|
|
*/ |
|
225
|
|
|
public function conceal($org, $user) |
|
226
|
|
|
{ |
|
227
|
|
|
// Build the request path. |
|
228
|
|
|
$path = '/orgs/' . $org . '/public_members/' . $user; |
|
229
|
|
|
|
|
230
|
|
|
return $this->processResponse( |
|
231
|
|
|
$this->client->delete($this->fetchUrl($path)), |
|
232
|
|
|
204 |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Get organization membership |
|
238
|
|
|
* |
|
239
|
|
|
* In order to get a user's membership with an organization, the authenticated user must be an organization owner. |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $org The name of the organization. |
|
242
|
|
|
* @param string $user The name of the user. |
|
243
|
|
|
* |
|
244
|
|
|
* @return object |
|
245
|
|
|
* |
|
246
|
|
|
* @since 1.4.0 |
|
247
|
|
|
*/ |
|
248
|
|
|
public function getMembership($org, $user) |
|
249
|
|
|
{ |
|
250
|
|
|
// Build the request path. |
|
251
|
|
|
$path = '/orgs/' . $org . '/memberships/' . $user; |
|
252
|
|
|
|
|
253
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Add or update organization membership |
|
258
|
|
|
* |
|
259
|
|
|
* In order to create or update a user's membership with an organization, the authenticated user must be an organization owner. |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $org The name of the organization. |
|
262
|
|
|
* @param string $user The name of the user. |
|
263
|
|
|
* @param string $role The role to give the user in the organization. Can be either 'member' or 'admin'. |
|
264
|
|
|
* |
|
265
|
|
|
* @return object |
|
266
|
|
|
* |
|
267
|
|
|
* @since 1.4.0 |
|
268
|
|
|
*/ |
|
269
|
|
|
public function updateMembership($org, $user, $role = 'member') |
|
270
|
|
|
{ |
|
271
|
|
|
$allowedRoles = array('member', 'admin'); |
|
272
|
|
|
|
|
273
|
|
|
if (!in_array($role, $allowedRoles)) |
|
274
|
|
|
{ |
|
275
|
|
|
throw new \InvalidArgumentException(sprintf("The user's role must be: %s", implode(', ', $allowedRoles))); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
// Build the request path. |
|
279
|
|
|
$path = "/orgs/$org/memberships/$user"; |
|
280
|
|
|
|
|
281
|
|
|
$data = array( |
|
282
|
|
|
'role' => $role, |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
|
|
return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Remove organization membership |
|
290
|
|
|
* |
|
291
|
|
|
* In order to remove a user's membership with an organization, the authenticated user must be an organization owner. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $org The name of the organization. |
|
294
|
|
|
* @param string $user The name of the user. |
|
295
|
|
|
* |
|
296
|
|
|
* @return object |
|
297
|
|
|
* |
|
298
|
|
|
* @since 1.4.0 |
|
299
|
|
|
*/ |
|
300
|
|
|
public function removeMembership($org, $user) |
|
301
|
|
|
{ |
|
302
|
|
|
// Build the request path. |
|
303
|
|
|
$path = '/orgs/' . $org . '/memberships/' . $user; |
|
304
|
|
|
|
|
305
|
|
|
return $this->processResponse( |
|
306
|
|
|
$this->client->delete($this->fetchUrl($path)), |
|
307
|
|
|
204 |
|
308
|
|
|
); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* List your organization memberships |
|
313
|
|
|
* |
|
314
|
|
|
* @return object |
|
315
|
|
|
* |
|
316
|
|
|
* @since 1.4.0 |
|
317
|
|
|
*/ |
|
318
|
|
|
public function listMemberships() |
|
319
|
|
|
{ |
|
320
|
|
|
// Build the request path. |
|
321
|
|
|
$path = '/user/memberships/orgs'; |
|
322
|
|
|
|
|
323
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Get your organization membership |
|
328
|
|
|
* |
|
329
|
|
|
* @param string $org The name of the organization. |
|
330
|
|
|
* |
|
331
|
|
|
* @return object |
|
332
|
|
|
* |
|
333
|
|
|
* @since 1.4.0 |
|
334
|
|
|
*/ |
|
335
|
|
|
public function listOrganizationMembership($org) |
|
336
|
|
|
{ |
|
337
|
|
|
// Build the request path. |
|
338
|
|
|
$path = '/user/memberships/orgs/' . $org; |
|
339
|
|
|
|
|
340
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Edit your organization membership |
|
345
|
|
|
* |
|
346
|
|
|
* @param string $org The name of the organization. |
|
347
|
|
|
* @param string $state The state that the membership should be in. |
|
348
|
|
|
* |
|
349
|
|
|
* @return object |
|
350
|
|
|
* |
|
351
|
|
|
* @since 1.4.0 |
|
352
|
|
|
*/ |
|
353
|
|
View Code Duplication |
public function editOrganizationMembership($org, $state) |
|
|
|
|
|
|
354
|
|
|
{ |
|
355
|
|
|
// The API only accepts $state == 'active' at present |
|
356
|
|
|
if ($state != 'active') |
|
357
|
|
|
{ |
|
358
|
|
|
throw new \InvalidArgumentException('The state must be "active".'); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
// Build the request path. |
|
362
|
|
|
$path = '/user/memberships/orgs/' . $org; |
|
363
|
|
|
|
|
364
|
|
|
return $this->processResponse($this->client->patch($this->fetchUrl($path), array('state' => $state))); |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.