1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.assembly.old; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.LinkedHashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.apache.log4j.Logger; |
25 | |
import org.kuali.rice.kim.api.permission.Permission; |
26 | |
import org.kuali.rice.kim.api.permission.PermissionService; |
27 | |
import org.kuali.student.common.assembly.data.AssemblyException; |
28 | |
import org.kuali.student.common.assembly.data.Data; |
29 | |
import org.kuali.student.common.assembly.data.Metadata; |
30 | |
import org.kuali.student.common.assembly.dictionary.old.MetadataServiceImpl; |
31 | |
import org.kuali.student.common.rice.authorization.PermissionType; |
32 | |
import org.kuali.student.common.util.security.SecurityUtils; |
33 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
34 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
35 | |
|
36 | |
@Deprecated |
37 | 0 | public abstract class BaseAssembler<TargetType, SourceType> implements Assembler<TargetType, SourceType> { |
38 | 0 | protected final Logger LOG = Logger.getLogger(getClass()); |
39 | |
|
40 | |
protected PermissionService permissionService; |
41 | |
protected MetadataServiceImpl metadataService; |
42 | |
|
43 | 0 | public enum PermissionEnum { |
44 | 0 | EDIT("edit"), READ_ONLY("readonly"), UNMASK("unmask"); |
45 | |
final String kimName; |
46 | 0 | private PermissionEnum(String kimName) { |
47 | 0 | this.kimName = kimName; |
48 | 0 | } |
49 | |
@Override |
50 | |
public String toString() { |
51 | 0 | return kimName; |
52 | |
} |
53 | |
public static PermissionEnum kimValueOf(String kimName) { |
54 | 0 | for(PermissionEnum p : values()) { |
55 | 0 | if(p.kimName.equals(kimName)) { |
56 | 0 | return p; |
57 | |
} |
58 | |
} |
59 | |
|
60 | 0 | throw new IllegalArgumentException("The value " + kimName + " is not enumerated in Permission"); |
61 | |
} |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
protected Map<String, String> getFieldAccessPermissions(String dtoName, String idType, String id) { |
67 | |
try { |
68 | |
|
69 | 0 | String principalId = SecurityUtils.getCurrentUserId(); |
70 | 0 | Map<String,String> qualification = getQualification(idType, id); |
71 | 0 | Map<String,String> permissionDetails = new LinkedHashMap <String,String> (); |
72 | 0 | permissionDetails.put ("dtoName", dtoName); |
73 | |
|
74 | |
|
75 | 0 | Map<String, String> permMap = new HashMap<String, String>(); |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 0 | return permMap; |
85 | 0 | } catch (Exception e) { |
86 | 0 | LOG.warn("Error calling permission service.", e); |
87 | |
} |
88 | 0 | return null; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private void setReadOnly(Metadata metadata, boolean readOnly) { |
96 | 0 | metadata.setCanEdit(!readOnly); |
97 | 0 | Map<String, Metadata> childProperties = metadata.getProperties(); |
98 | 0 | if (childProperties != null && childProperties.size() > 0) { |
99 | 0 | for (Metadata child : childProperties.values()) { |
100 | 0 | setReadOnly(child, readOnly); |
101 | |
} |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public Metadata getMetadata(String idType, String id, String type, String state) throws AssemblyException { |
107 | 0 | Metadata metadata = metadataService.getMetadata(getDataType(), type, state); |
108 | 0 | applyPermissionsToMetadata(metadata,idType, id); |
109 | 0 | return metadata; |
110 | |
} |
111 | |
|
112 | |
protected void applyPermissionsToMetadata(Metadata metadata, String idType, String id){ |
113 | 0 | Boolean authorized = null; |
114 | 0 | if (StringUtils.isNotBlank(id) && checkDocumentLevelPermissions()) { |
115 | 0 | Map<String,String> qualification = getQualification(idType, id); |
116 | 0 | String currentUser = SecurityUtils.getCurrentUserId(); |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 0 | metadata.setCanEdit(authorized.booleanValue()); |
122 | |
} |
123 | 0 | if(metadata != null && metadata.getProperties() != null) { |
124 | 0 | for(Metadata child : metadata.getProperties().values()) { |
125 | 0 | if(!child.isCanEdit()) { |
126 | 0 | setReadOnly(child, true); |
127 | |
} |
128 | |
} |
129 | |
} |
130 | |
|
131 | 0 | if (checkDocumentLevelPermissions() && Boolean.FALSE.equals(authorized)) { |
132 | 0 | setReadOnly(metadata, true); |
133 | |
} |
134 | |
|
135 | |
else { |
136 | 0 | Map<String, String> permissions = getFieldAccessPermissions(getDtoName(),idType,id); |
137 | 0 | if (permissions != null) { |
138 | 0 | for (Map.Entry<String, String> permission : permissions.entrySet()) { |
139 | 0 | String dtoFieldPath = permission.getKey(); |
140 | 0 | String fieldAccessLevel = permission.getValue(); |
141 | 0 | String[] fieldPathTokens = getPathTokens(dtoFieldPath); |
142 | 0 | Metadata fieldMetadata = metadata.getProperties().get(fieldPathTokens[0]); |
143 | 0 | for(int i = 1; i < fieldPathTokens.length; i++) { |
144 | 0 | if(fieldMetadata == null) { |
145 | 0 | break; |
146 | |
} |
147 | 0 | fieldMetadata = fieldMetadata.getProperties().get(fieldPathTokens[i]); |
148 | |
} |
149 | 0 | if (fieldMetadata != null) { |
150 | 0 | PermissionEnum perm = PermissionEnum.kimValueOf(fieldAccessLevel); |
151 | 0 | if (PermissionEnum.EDIT.equals(perm)) { |
152 | 0 | setReadOnly(fieldMetadata, false); |
153 | |
|
154 | |
} |
155 | |
} |
156 | 0 | } |
157 | |
} |
158 | |
} |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
public Metadata getDefaultMetadata() { |
163 | 0 | return metadataService.getMetadata(getDataType(), null, null); |
164 | |
} |
165 | |
|
166 | |
protected boolean hasValidationErrors(List<ValidationResultInfo> validationResults) { |
167 | 0 | boolean result = false; |
168 | 0 | if (validationResults != null) { |
169 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
170 | 0 | if (validationResult.getLevel() == ErrorLevel.ERROR) { |
171 | 0 | result = true; |
172 | 0 | break; |
173 | |
} |
174 | |
} |
175 | |
} |
176 | 0 | return result; |
177 | |
} |
178 | |
|
179 | |
public List<ValidationResultInfo> validate(Data data) throws AssemblyException { |
180 | 0 | List<ValidationResultInfo> validationResults = null; |
181 | |
|
182 | 0 | return validationResults; |
183 | |
} |
184 | |
|
185 | |
private static String[] getPathTokens(String fieldPath) { |
186 | 0 | return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath}); |
187 | |
} |
188 | |
|
189 | |
public boolean checkDocumentLevelPermissions() { |
190 | 0 | return false; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
protected abstract String getDataType(); |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
protected abstract String getDtoName(); |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected abstract String getDocumentPropertyName(); |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
protected abstract Map<String,String> getQualification(String idType, String id); |
224 | |
|
225 | |
public void setPermissionService(PermissionService permissionService) { |
226 | 0 | this.permissionService = permissionService; |
227 | 0 | } |
228 | |
|
229 | |
public void setMetadataService(MetadataServiceImpl metadataService) { |
230 | 0 | this.metadataService = metadataService; |
231 | 0 | } |
232 | |
|
233 | |
} |