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
|
|
View Code Duplication |
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
|
|
|
* Adds members (users or other groups) to group |
89
|
|
|
* The main fields in $data: |
90
|
|
|
* 'user' => array that contain users that will be added |
91
|
|
|
* 'group' => array that contain groups that will be added |
92
|
|
|
* If $data is a string, will be user uid |
93
|
|
|
* |
94
|
|
|
* @param string $group_name group name |
95
|
|
|
* @param array|string $data See explanation above |
96
|
|
|
* @return mixed Array containing information about processing and group data OR false on error |
97
|
|
|
* @since GIT: 0.1.0 |
98
|
|
|
* @version GIT: 0.1.0 |
99
|
|
|
* @see ../../docs/return_samples/group_add_member.txt |
100
|
|
|
* @see \FreeIPA\APIAccess\Connection\buildRequest() |
101
|
|
|
* @throws \Exception if the request was not completed successfully |
102
|
|
|
*/ |
103
|
|
|
public function addMember($group_name = null, $data = array()) |
104
|
|
|
{ |
105
|
|
|
if (!$group_name || !$data) { |
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Obtained with the command: ipa -vv group_add_member group_one --users="stallman" |
110
|
|
|
$args = array($group_name); |
111
|
|
|
$default_options = array( |
112
|
|
|
'all' => true, |
113
|
|
|
'no_members' => false, |
114
|
|
|
'raw' => false, |
115
|
|
|
); |
116
|
|
View Code Duplication |
if (is_array($data)) { |
|
|
|
|
117
|
|
|
$final_options = array_merge($default_options, $data); |
118
|
|
|
} else if (is_string($data)) { |
119
|
|
|
$final_options = array_merge($default_options, array('user' => array($data))); |
120
|
|
|
} else { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$response = $this->getConnection()->buildRequest('group_add_member', $args, $final_options); //returns json and http code of response |
125
|
|
|
if (!$response) { |
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
$returned_json = $response[0]; |
129
|
|
|
if (!$returned_json->result->completed) { |
130
|
|
|
$message = "Error while inserting members in group \"$group_name\"."; |
131
|
|
View Code Duplication |
if (!empty($returned_json->result->failed->member->group) || |
|
|
|
|
132
|
|
|
!empty($returned_json->result->failed->member->user)) { |
133
|
|
|
$message .= 'Details: '; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
if (!empty($returned_json->result->failed->member->group)) { |
|
|
|
|
137
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->group[0]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
if (!empty($returned_json->result->failed->member->user)) { |
|
|
|
|
141
|
|
|
$message .= implode(' ', $returned_json->result->failed->member->user[0]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw new \Exception($message); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Unlike other methods, where $returned_json->result->result is returned, |
148
|
|
|
// here the $returned_json->result contain usefull information |
149
|
|
|
return $returned_json->result; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.