-
Notifications
You must be signed in to change notification settings - Fork 188
/
ModelUtils.java
185 lines (164 loc) · 7.41 KB
/
ModelUtils.java
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
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.model.impl.controller;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.model.api.ModelAuthorizationAction;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectPolicyConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectTemplateType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ServiceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
/**
*
* @author lazyman
*
*/
public class ModelUtils {
private static final Trace LOGGER = TraceManager.getTrace(ModelUtils.class);
public static void validatePaging(ObjectPaging paging) {
if (paging == null) {
return;
}
if (paging.getMaxSize() != null && paging.getMaxSize().longValue() < 0) {
throw new IllegalArgumentException("Paging max size must be more than 0.");
}
if (paging.getOffset() != null && paging.getOffset().longValue() < 0) {
throw new IllegalArgumentException("Paging offset index must be more than 0.");
}
}
public static void recordFatalError(OperationResult result, Throwable e) {
recordFatalError(result, e.getMessage(), e);
}
public static void recordFatalError(OperationResult result, String message, Throwable e) {
// Do not log at ERROR level. This is too harsh. Especially in object not found case.
// What model considers an error may be just a normal situation for the code is using model API.
// If this is really an error then it should be logged by the invoking code.
LoggingUtils.logExceptionOnDebugLevel(LOGGER, message, e);
result.recordFatalError(message, e);
result.cleanupResult(e);
}
public static void recordPartialError(OperationResult result, Throwable e) {
recordPartialError(result, e.getMessage(), e);
}
public static void recordPartialError(OperationResult result, String message, Throwable e) {
// Do not log at ERROR level. This is too harsh. Especially in object not found case.
// What model considers an error may be just a normal situation for the code is using model API.
// If this is really an error then it should be logged by the invoking code.
LoggingUtils.logExceptionOnDebugLevel(LOGGER, message, e);
result.recordPartialError(message, e);
result.cleanupResult(e);
}
public static <O extends ObjectType> String getOperationUrlFromDelta(ObjectDelta<O> delta) {
if (delta == null) {
return null;
}
if (delta.isAdd()) {
return ModelAuthorizationAction.ADD.getUrl();
}
if (delta.isModify()) {
return ModelAuthorizationAction.MODIFY.getUrl();
}
if (delta.isDelete()) {
return ModelAuthorizationAction.DELETE.getUrl();
}
throw new IllegalArgumentException("Unknown delta type "+delta);
}
public static <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(PrismObject<O> object, SystemConfigurationType systemConfigurationType) throws ConfigurationException {
List<String> subTypes = determineSubTypes(object);
return determineObjectPolicyConfiguration(object.getCompileTimeClass(), subTypes, systemConfigurationType);
}
public static <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(Class<O> objectClass, List<String> objectSubtypes, SystemConfigurationType systemConfigurationType) throws ConfigurationException {
ObjectPolicyConfigurationType applicablePolicyConfigurationType = null;
for (ObjectPolicyConfigurationType aPolicyConfigurationType: systemConfigurationType.getDefaultObjectPolicyConfiguration()) {
QName typeQName = aPolicyConfigurationType.getType();
ObjectTypes objectType = ObjectTypes.getObjectTypeFromTypeQName(typeQName);
if (objectType == null) {
throw new ConfigurationException("Unknown type "+typeQName+" in default object policy definition in system configuration");
}
if (objectType.getClassDefinition() == objectClass) {
String aSubType = aPolicyConfigurationType.getSubType();
if (aSubType == null) {
if (applicablePolicyConfigurationType == null) {
applicablePolicyConfigurationType = aPolicyConfigurationType;
}
} else if (objectSubtypes != null && objectSubtypes.contains(aSubType)) {
applicablePolicyConfigurationType = aPolicyConfigurationType;
}
}
}
if (applicablePolicyConfigurationType != null) {
return applicablePolicyConfigurationType;
}
// Deprecated
for (ObjectPolicyConfigurationType aPolicyConfigurationType: systemConfigurationType.getObjectTemplate()) {
QName typeQName = aPolicyConfigurationType.getType();
ObjectTypes objectType = ObjectTypes.getObjectTypeFromTypeQName(typeQName);
if (objectType == null) {
throw new ConfigurationException("Unknown type "+typeQName+" in object template definition in system configuration");
}
if (objectType.getClassDefinition() == objectClass) {
return aPolicyConfigurationType;
}
}
// Deprecated method to specify user template. For compatibility only
if (objectClass == UserType.class) {
ObjectReferenceType templateRef = systemConfigurationType.getDefaultUserTemplateRef();
if (templateRef == null) {
return null;
}
ObjectPolicyConfigurationType policy = new ObjectPolicyConfigurationType();
policy.setObjectTemplateRef(templateRef.clone());
return policy;
}
return null;
}
public static <O extends ObjectType> List<String> determineSubTypes(PrismObject<O> object) {
if (object == null) {
return null;
}
// TODO: get subType (from ObjectType)
if (object.canRepresent(UserType.class)) {
return (((UserType)object.asObjectable()).getEmployeeType());
}
if (object.canRepresent(OrgType.class)) {
return (((OrgType)object.asObjectable()).getOrgType());
}
if (object.canRepresent(RoleType.class)) {
List<String> roleTypes = new ArrayList<>(1);
roleTypes.add((((RoleType)object.asObjectable()).getRoleType()));
return roleTypes;
}
if (object.canRepresent(ServiceType.class)) {
return (((ServiceType)object.asObjectable()).getServiceType());
}
return null;
}
}