/
GroupController.groovy
309 lines (277 loc) · 11.2 KB
/
GroupController.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import org.hyperic.hq.hqapi1.ErrorCode
import org.hyperic.hq.authz.shared.PermissionException
import org.hyperic.hq.common.VetoException
class GroupController extends ApiController {
private Closure getGroupXML(g) {
{ doc ->
Group(id : g.id,
resourceId : g.resource.id,
name : g.name,
description : g.description,
location : g.location) {
if (g.resourcePrototype) {
ResourcePrototype(id : g.resourcePrototype.id,
name : g.resourcePrototype.name)
}
for (r in g.resources) {
Resource(id : r.id,
name : r.name)
}
for (r in g.roles) {
Role(id : r.id,
name : r.name)
}
}
}
}
def get(params) {
def id = params.getOne('id')?.toInteger()
def name = params.getOne('name')
def group
def failureXml = null
if (id == null && name == null) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
} else {
group = getGroup(id, name)
if (!group) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Group with id=" + id + " name='" +
name + "' not found")
}
}
renderXml() {
GroupResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
out << getGroupXML(group)
}
}
}
}
def delete(params) {
def id = params.getOne('id')?.toInteger()
if (id == null) {
renderXml() {
out << StatusResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
}
}
return
}
def group = getGroup(id, null)
def failureXml = null
if (!group) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Group with id " + id + " not found")
} else {
if (group.system) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Cannot delete system group " +
group.name)
} else {
try {
group.remove(user)
} catch (PermissionException e) {
failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED)
} catch (Exception e) {
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
}
renderXml() {
out << StatusResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
}
}
}
}
def sync(params) {
def syncRequest = new XmlParser().parseText(getPostData())
def groups = []
for (xmlGroup in syncRequest['Group']) {
// Check for existance
def existing = getGroup(xmlGroup.'@id'?.toInteger(),
xmlGroup.'@name')
def failureXml = null
def roles = [] as Set
def resources = []
def prototype = null
if (!xmlGroup.'@name') {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Group name required.")
}
if (existing?.isSystem()) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Cannot update system group " +
"with id=" + existing.id)
}
// Look up prototype
def xmlPrototype = xmlGroup.'ResourcePrototype'
if (!xmlPrototype) {
log.debug("No prototype found for " + xmlGroup.'@name')
} else {
prototype = resourceHelper.find(prototype:xmlPrototype.'@name')
if (!prototype) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Unable to find prototype with " +
"name " + xmlPrototype.'@name')
} else {
if (existing) {
if (existing.resourcePrototype) {
// If already compatible - ensure the same type.
if (!existing.resourcePrototype.name.equals(prototype.name)) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Cannot change group type from " +
existing.resourcePrototype.name +
" to " + prototype.name)
}
}
}
}
}
// Look up roles
for (xmlRole in xmlGroup['Role']) {
log.debug("Found role "+ xmlRole.'@name')
def role = getRole(xmlRole.'@id'?.toInteger(),
xmlRole.'@name')
if (!role) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Unable to find role with id " +
xmlRole.'@id' + " and name " +
xmlRole.'@name')
} else {
roles.add(role)
}
}
// Look up resources
for (xmlResource in xmlGroup['Resource']) {
log.debug("Found resource " + xmlResource.'@name')
def resource = null
try {
resource = getResource(xmlResource.'@id'?.toInteger());
} catch (PermissionException e) {
// Ignore resources the user cannot see.
}
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Unable to find resource with id " +
xmlResource.'@id')
} else {
if (prototype) {
if (!resource.prototype.name.equals(prototype.name)) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Resource " + resource.name +
" is not of type " +
prototype.name)
}
}
// Avoid duplicate resources which causes constraint violations
// TODO: Backend should handle this case.
if (!resources.contains(resource)) {
resources.add(resource)
}
}
}
if (!failureXml) {
if (existing) {
// TODO: This needs to be moved out to the Manager.
try {
existing.setResources(user, resources)
existing.setRoles(roles)
existing.updateGroup(user,
xmlGroup.'@name',
xmlGroup.'@description',
xmlGroup.'@location')
groups << existing
} catch (VetoException ve) {
failureXml = getFailureXML(ErrorCode.OPERATION_DENIED,
ve.getMessage())
}
} else {
// TODO: private groups
def group = resourceHelper.createGroup(xmlGroup.'@name',
xmlGroup.'@description',
xmlGroup.'@location',
prototype, roles,
resources,
false)
groups << group
}
}
// If any group is unable to be synced exit with an error.
if (failureXml) {
renderXml() {
out << GroupsResponse() {
out << failureXml
}
}
return
}
}
renderXml() {
out << GroupsResponse() {
out << getSuccessXML()
for (g in groups) {
out << getGroupXML(g)
}
}
}
}
def list(params) {
def compatible = params.getOne('compatible')?.toBoolean()
def containing = params.getOne('containing')?.toBoolean()
def roleId = params.getOne('roleId')?.toInteger()
def groups = null
def failureXml = null
if (containing != null) {
def resourceId = params.getOne('resourceId')?.toInteger()
def resource = getResource(resourceId)
if (resource) {
if (containing) {
groups = resource.getGroupsContaining(user)
} else {
groups = resource.getGroupsNotContaining(user)
}
} else {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id=" + resourceId +
" not found")
}
} else if (roleId != null) {
def role = getRole(roleId, null)
if (!role) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Role id=" + roleId +
" not found")
} else {
groups = role.getGroups(user)
}
} else {
groups = resourceHelper.findViewableGroups()
if (compatible != null) {
if (compatible) {
groups = groups.grep { it.resourcePrototype != null }
} else {
groups = groups.grep { it.resourcePrototype == null }
}
}
}
renderXml() {
out << GroupsResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (g in groups.sort {a, b -> a.name <=> b.name}) {
out << getGroupXML(g)
}
}
}
}
}
}