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