1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
FreeIPA library for PHP |
4
|
|
|
Copyright (C) 2015 Tobias Sette <[email protected]> |
5
|
|
|
|
6
|
|
|
This program is free software: you can redistribute it and/or modify |
7
|
|
|
it under the terms of the GNU Lesser General Public License as published by |
8
|
|
|
the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
(at your option) any later version. |
10
|
|
|
|
11
|
|
|
This program is distributed in the hope that it will be useful, |
12
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
GNU Lesser General Public License for more details. |
15
|
|
|
|
16
|
|
|
You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
// Dependencies: |
21
|
|
|
//require_once('Base.php'); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Classes for access to FreeIPA API |
25
|
|
|
* @since GIT: 0.1.0 |
26
|
|
|
*/ |
27
|
|
|
namespace FreeIPA\APIAccess; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class to access group resources |
31
|
|
|
* |
32
|
|
|
* @author Tobias Sette <[email protected]> |
33
|
|
|
* @copyright Copyright (c) 2015 Tobias Sette <[email protected]> |
34
|
|
|
* @license LGPLv3 |
35
|
|
|
* @package php-freeipa |
36
|
|
|
* @since GIT: 0.1.0 |
37
|
|
|
* @version GIT: 0.2.0 |
38
|
|
|
*/ |
39
|
|
|
class Group extends \FreeIPA\APIAccess\Base |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Adds a group |
43
|
|
|
* The main fields in $data: |
44
|
|
|
* 'description' => group description |
45
|
|
|
* If $data is string will be a group description |
46
|
|
|
* |
47
|
|
|
* @param string $name group name |
48
|
|
|
* @param array|string $data see above |
49
|
|
|
* @return object|bool Object with new group data or false if the group was not found |
50
|
|
|
* @since GIT: 0.1.0 |
51
|
|
|
* @version GIT: 0.1.0 |
52
|
|
|
* @see ../../docs/return_samples/group_add.txt |
53
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
54
|
|
|
*/ |
55
|
|
|
public function add($name = null, $data = array()) |
56
|
|
|
{ |
57
|
|
|
if (!$name || !$data) { |
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Obtained with the command: ipa -vv group-add group_one --desc="Group one" --all |
62
|
|
|
$args = array($name); |
63
|
|
|
$default_options = array( |
64
|
|
|
'all' => false, |
65
|
|
|
'external' => false, |
66
|
|
|
'no_members' => false, |
67
|
|
|
'nonposix' => false, |
68
|
|
|
'raw' => false, |
69
|
|
|
); |
70
|
|
|
if (is_array($data)) { |
71
|
|
|
$final_options = array_merge($default_options, $data); |
72
|
|
|
} else if (is_string($data)) { |
73
|
|
|
$final_options = array_merge($default_options, array('description' => $data)); |
74
|
|
|
} else { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// The buildRequest() method already checks the field 'error', which is the only relevant to this API method |
79
|
|
|
$response = $this->getConnection()->buildRequest('group_add', $args, $final_options); //returns json and http code of response |
80
|
|
|
if (!$response) { |
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $response[0]->result->result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Delete a group |
89
|
|
|
* |
90
|
|
|
* @param string $name group name |
91
|
|
|
* @param array|string $data see above |
92
|
|
|
* @return object|bool Object with new group data or false if the group was not found |
93
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
94
|
|
|
*/ |
95
|
|
|
public function del($name = null, $data = array()) |
96
|
|
|
{ |
97
|
|
|
if (!$name || !$data) { |
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Obtained with the command: ipa -vv group-add group_one --desc="Group one" --all |
102
|
|
|
$args = array($name); |
103
|
|
|
$default_options = array( |
104
|
|
|
'continue' => false, |
105
|
|
|
); |
106
|
|
|
if (is_array($data)) { |
107
|
|
|
$final_options = array_merge($default_options, $data); |
108
|
|
|
} else { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// The buildRequest() method already checks the field 'error', which is the only relevant to this API method |
113
|
|
|
$response = $this->getConnection()->buildRequest('group_del', $args, $final_options); //returns json and http code of response |
114
|
|
|
if (!$response) { |
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $response[0]->result->result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Adds members (users or other groups) to group |
123
|
|
|
* The main fields in $data: |
124
|
|
|
* 'user' => array that contain users that will be added |
125
|
|
|
* 'group' => array that contain groups that will be added |
126
|
|
|
* If $data is a string, will be user uid |
127
|
|
|
* |
128
|
|
|
* @param string $group_name group name |
129
|
|
|
* @param array|string $data See explanation above |
130
|
|
|
* @return mixed Array containing information about processing and group data OR false on error |
131
|
|
|
* @since GIT: 0.1.0 |
132
|
|
|
* @version GIT: 0.1.0 |
133
|
|
|
* @see ../../docs/return_samples/group_add_member.txt |
134
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
135
|
|
|
* @throws \Exception if the request was not completed successfully |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function addMember($group_name = null, $data = array()) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
if (!$group_name || !$data) { |
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Obtained with the command: ipa -vv group_add_member group_one --users="stallman" |
144
|
|
|
$args = array($group_name); |
145
|
|
|
$default_options = array( |
146
|
|
|
'all' => true, |
147
|
|
|
'no_members' => false, |
148
|
|
|
'raw' => false, |
149
|
|
|
); |
150
|
|
|
if (is_array($data)) { |
151
|
|
|
$final_options = array_merge($default_options, $data); |
152
|
|
|
} else if (is_string($data)) { |
153
|
|
|
$final_options = array_merge($default_options, array('user' => array($data))); |
154
|
|
|
} else { |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$response = $this->getConnection()->buildRequest('group_add_member', $args, $final_options); //returns json and http code of response |
159
|
|
|
if (!$response) { |
|
|
|
|
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
$returned_json = $response[0]; |
163
|
|
|
if (!$returned_json->result->completed) { |
164
|
|
|
$message = "Error while inserting members in group \"$group_name\"."; |
165
|
|
|
if (!empty($returned_json->result->failed->member->group) || |
166
|
|
|
!empty($returned_json->result->failed->member->user)) { |
167
|
|
|
$message .= 'Details: '; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!empty($returned_json->result->failed->member->group)) { |
171
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->group[0]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!empty($returned_json->result->failed->member->user)) { |
175
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->user[0]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
throw new \Exception($message); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Unlike other methods, where $returned_json->result->result is returned, |
182
|
|
|
// here the $returned_json->result contain usefull information |
183
|
|
|
return $returned_json->result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Deletes members (users or other groups) to group |
188
|
|
|
* The main fields in $data: |
189
|
|
|
* 'user' => array that contain users that will be added |
190
|
|
|
* 'group' => array that contain groups that will be added |
191
|
|
|
* If $data is a string, will be user uid |
192
|
|
|
* |
193
|
|
|
* @param string $group_name group name |
194
|
|
|
* @param array|string $data See explanation above |
195
|
|
|
* @return mixed Array containing information about processing and group data OR false on error |
196
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
197
|
|
|
* @throws \Exception if the request was not completed successfully |
198
|
|
|
*/ |
199
|
|
View Code Duplication |
public function removeMember($group_name = null, $data = array()) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
if (!$group_name || !$data) { |
|
|
|
|
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Obtained with the command: ipa -vv group_remove_member group_one --users="stallman" |
206
|
|
|
$args = array($group_name); |
207
|
|
|
$default_options = array( |
208
|
|
|
'all' => true, |
209
|
|
|
'no_members' => false, |
210
|
|
|
'raw' => false, |
211
|
|
|
); |
212
|
|
|
if (is_array($data)) { |
213
|
|
|
$final_options = array_merge($default_options, $data); |
214
|
|
|
} else if (is_string($data)) { |
215
|
|
|
$final_options = array_merge($default_options, array('user' => array($data))); |
216
|
|
|
} else { |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$response = $this->getConnection()->buildRequest('group_remove_member', $args, $final_options); //returns json and http code of response |
221
|
|
|
if (!$response) { |
|
|
|
|
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
$returned_json = $response[0]; |
225
|
|
|
if (!$returned_json->result->completed) { |
226
|
|
|
$message = "Error while removing members in group \"$group_name\"."; |
227
|
|
|
if (!empty($returned_json->result->failed->member->group) || |
228
|
|
|
!empty($returned_json->result->failed->member->user)) { |
229
|
|
|
$message .= 'Details: '; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if (!empty($returned_json->result->failed->member->group)) { |
233
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->group[0]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (!empty($returned_json->result->failed->member->user)) { |
237
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->user[0]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
throw new \Exception($message); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Unlike other methods, where $returned_json->result->result is returned, |
244
|
|
|
// here the $returned_json->result contain usefull information |
245
|
|
|
return $returned_json->result; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Search group through of group_find method. |
250
|
|
|
* |
251
|
|
|
* @param array $args arguments for user_find method |
252
|
|
|
* @param array $options options for user_find method |
253
|
|
|
* @return array|bool false if the group was not found |
254
|
|
|
* @throws \Exception if error in json return |
255
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
256
|
|
|
*/ |
257
|
|
|
public function find($args = array(), $options = array()) |
258
|
|
|
{ |
259
|
|
|
if (!is_array($args) || !is_array($options)) { |
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// Obtained with the command ipa -vv group-find --all |
264
|
|
|
$default_options = array( |
265
|
|
|
'all' => true, |
266
|
|
|
'raw' => false, |
267
|
|
|
'private' => false, |
268
|
|
|
'posix' => false, |
269
|
|
|
'external' => false, |
270
|
|
|
'nonposix' => false, |
271
|
|
|
'no_members' => false |
272
|
|
|
); |
273
|
|
|
$final_options = array_merge($default_options, $options); |
274
|
|
|
|
275
|
|
|
$return_request = $this->getConnection()->buildRequest('group_find', $args, $final_options); //returns json and http code of response |
276
|
|
|
$json = $return_request[0]; |
277
|
|
|
|
278
|
|
|
if (empty($json->result) || !isset($json->result->count)) { |
279
|
|
|
throw new \Exception('Malformed json'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ($json->result->count < 1) { |
283
|
|
|
return false; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $json->result->result; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Search group by field |
291
|
|
|
* |
292
|
|
|
* @param array $field field name. |
293
|
|
|
* @param string $value field value |
294
|
|
|
* @return array|bool false if the group was not found |
295
|
|
|
* @see find() |
296
|
|
|
*/ |
297
|
|
View Code Duplication |
public function findBy($field = null, $value = null) |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
if (!$field || !$value) { |
|
|
|
|
300
|
|
|
return false; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$options = array($field => $value); |
304
|
|
|
return $this->find(array(), $options); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get group data by cn through group_show method |
309
|
|
|
* |
310
|
|
|
* @param string|array $params cn or some parameters |
311
|
|
|
* @param array $options options for group_show method |
312
|
|
|
* @return array|bool false if the group was not found |
313
|
|
|
* @throws \Exception se houver erro no retorno json |
314
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function get($params = null, $options = array()) |
|
|
|
|
317
|
|
|
{ |
318
|
|
|
if (!is_array($options)) { |
319
|
|
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if (is_string($params)) { |
323
|
|
|
$final_params = array($params); |
324
|
|
|
} else if (is_array($params)) { |
325
|
|
|
$final_params = $params; |
326
|
|
|
} else { |
327
|
|
|
return false; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// Obtained with the command ipa -vv user-show admin |
331
|
|
|
$default_options = array( |
332
|
|
|
'all' => true, |
333
|
|
|
'no_members' => false, |
334
|
|
|
'raw' => false, |
335
|
|
|
'rights' => false, |
336
|
|
|
|
337
|
|
|
); |
338
|
|
|
$final_options = array_merge($options, $default_options); |
339
|
|
|
|
340
|
|
|
$return_request = $this->getConnection()->buildRequest('group_show', $final_params, $final_options, false); |
341
|
|
|
$json = $return_request[0]; |
342
|
|
|
|
343
|
|
|
if (!empty($json->error) && strtolower($json->error->name) == 'notfound') { |
344
|
|
|
// group not found |
345
|
|
|
return false; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if (empty($json->result)) { |
349
|
|
|
throw new \Exception('Malformed json'); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// #TODO erase this code? |
353
|
|
|
if (!isset($json->result->result)) { |
354
|
|
|
return false; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return $json->result->result; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Change group data |
362
|
|
|
* |
363
|
|
|
* If group does not exists, the \FreeIPA\APIAccess\Connection\buildRequest() method will return \Exception. |
364
|
|
|
* |
365
|
|
|
* @param string $cn of group that will be changed |
366
|
|
|
* @param array $data See above |
367
|
|
|
* @return object|bool Object with new group data or false if the group was not found |
368
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
369
|
|
|
*/ |
370
|
|
View Code Duplication |
public function modify($cn = null, $data = array()) |
|
|
|
|
371
|
|
|
{ |
372
|
|
|
if (!$cn || !$data) { |
|
|
|
|
373
|
|
|
return false; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
// Obtained with the command: ipa -vv group_mod tobias --first="testaaaaaa" |
377
|
|
|
$args = array($cn); |
378
|
|
|
$default_options = array( |
379
|
|
|
'all' => false, |
380
|
|
|
'no_members' => false, |
381
|
|
|
'posix' => false, |
382
|
|
|
'raw' => false, |
383
|
|
|
'external' => false, |
384
|
|
|
'rights' => false, |
385
|
|
|
); |
386
|
|
|
$final_options = array_merge($default_options, $data); |
387
|
|
|
|
388
|
|
|
// The buildRequest() method already checks the field 'error', which is the only relevant to this API method |
389
|
|
|
$return_request = $this->getConnection()->buildRequest('group_mod', $args, $final_options); //returns json and http code of response |
390
|
|
|
if (!$return_request) { |
|
|
|
|
391
|
|
|
return false; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
return $return_request[0]->result->result; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: