View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.batch.service.impl;
17  
18  import java.io.File;
19  import java.util.*;
20  
21  import org.kuali.ole.sys.FinancialSystemModuleConfiguration;
22  import org.kuali.ole.sys.OLEConstants;
23  import org.kuali.ole.sys.batch.BatchFile;
24  import org.kuali.ole.sys.batch.BatchFileUtils;
25  import org.kuali.ole.sys.batch.service.BatchFileAdminAuthorizationService;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.identity.OleKimAttributes;
28  import org.kuali.rice.kim.api.KimConstants;
29  import org.kuali.rice.kim.api.identity.Person;
30  import org.kuali.rice.kim.api.services.IdentityManagementService;
31  import org.kuali.rice.krad.bo.ModuleConfiguration;
32  import org.kuali.rice.krad.service.KualiModuleService;
33  import org.kuali.rice.krad.service.ModuleService;
34  import org.kuali.rice.krad.util.KRADConstants;
35  
36  public class BatchFileAdminAuthorizationServiceImpl implements BatchFileAdminAuthorizationService {
37  
38      private IdentityManagementService identityManagementService;
39      private KualiModuleService kualiModuleService;
40      
41      @Override
42      public boolean canDownload(BatchFile batchFile, Person user) {
43          return getIdentityManagementService().isAuthorizedByTemplateName(user.getPrincipalId(),
44                  OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.namespace, OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.name,
45                  generateDownloadCheckPermissionDetails(batchFile, user), generateDownloadCheckRoleQualifiers(batchFile, user));
46      }
47  
48      @Override
49      public boolean canDelete(BatchFile batchFile, Person user) {
50          return getIdentityManagementService().isAuthorizedByTemplateName(user.getPrincipalId(),
51                  OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.namespace, OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.name,
52                  generateDownloadCheckPermissionDetails(batchFile, user), generateDownloadCheckRoleQualifiers(batchFile, user));
53      }
54      
55      protected String determineNamespaceCode(BatchFile batchFile) {
56          for (ModuleService moduleService : getKualiModuleService().getInstalledModuleServices()) {
57              ModuleConfiguration moduleConfiguration = moduleService.getModuleConfiguration();
58              if (moduleConfiguration instanceof FinancialSystemModuleConfiguration) {
59                  List<String> batchFileDirectories = ((FinancialSystemModuleConfiguration) moduleConfiguration).getBatchFileDirectories();
60                  for (String batchFileDirectoryName : batchFileDirectories) {
61                      File directory = new File(batchFileDirectoryName).getAbsoluteFile();
62                      String fileName = batchFile.getFileName();
63                      if(isFileNamePatternAvailable(fileName)){
64                          return moduleConfiguration.getNamespaceCode();
65                      } else if (BatchFileUtils.isSuperDirectoryOf(directory, batchFile.retrieveFile())) {
66                          return moduleConfiguration.getNamespaceCode();
67                      }
68                  }
69              }
70          }
71          return null;
72      }
73  
74      private boolean isFileNamePatternAvailable(String fileName){
75          //Set<String> filePatterns = new HashSet();
76          List<String> filePatterns = new ArrayList<String>();
77          filePatterns.add(OLEConstants.REENCUM_RECURR);
78          filePatterns.add(OLEConstants.PO_BULK_AMEND_OUT_FILE_NM);
79          filePatterns.add(OLEConstants.POBA_FILE);
80          filePatterns.add(OLEConstants.POBA_LOG_FILE);
81  
82          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.PROFILE_JOB);
83          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.PROFILE_SCHEDULE);
84          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.BATCH_JOB);
85          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.RECORDS_CREATED_WITHOUT_LINK);
86          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.RECORDS_CREATED_WITH_MORE_THAN_ONE_LINK);
87          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.BIBS_MATCHED);
88          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.HOLDINGS_MATCHED);
89          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.ITEMS_MATCHED);
90          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.BIBS_NO_MATCHED);
91          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.HOLDINGS_NO_MATCHED);
92          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.ITEMS_NO_MATCHED);
93          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.MATCHED_BIB_IDS_FILE_NAME);
94          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.MATCHED_HOLDINGS_IDS_FILE_NAME);
95          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.MATCHED_ITEM_IDS_FILE_NAME);
96          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.NO_MATCHED_BIB_FILE_NAME);
97          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.NO_MATCHED_HOLDINGS_FILE_NAME);
98          filePatterns.add(org.kuali.ole.OLEConstants.OLEBatchProcess.NO_MATCHED_ITEM_FILE_NAME);
99  
100         for(String allowedFilePattern:filePatterns){
101             if (fileName.contains(allowedFilePattern)) {
102                 return true;
103             }
104         }
105         return false;
106     }
107     protected Map<String,String> generateDownloadCheckPermissionDetails(BatchFile batchFile, Person user) {
108         return generatePermissionDetails(batchFile, user);
109     }
110     
111     protected Map<String,String> generateDownloadCheckRoleQualifiers(BatchFile batchFile, Person user) {
112         return generateRoleQualifiers(batchFile, user);
113     }
114 
115     protected Map<String,String> generateDeleteCheckPermissionDetails(BatchFile batchFile, Person user) {
116         return generatePermissionDetails(batchFile, user);
117     }
118     
119     protected Map<String,String> generateDeleteCheckRoleQualifiers(BatchFile batchFile, Person user) {
120         return generateRoleQualifiers(batchFile, user);
121     }
122 
123     protected Map<String,String> generatePermissionDetails(BatchFile batchFile, Person user) {
124         Map<String,String> permissionDetails = new HashMap<String,String>();
125         permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, determineNamespaceCode(batchFile));
126         permissionDetails.put(OleKimAttributes.FILE_PATH, replaceSlashes(batchFile.getPath() + File.separator + batchFile.getFileName()));
127         return permissionDetails;
128     }
129     
130     /**
131      * The permissions for the filePath will be added using '/' directory separators.
132      * This method will replace any '\\' directory separators with '/'
133      *
134      * @param filePath
135      * @return
136      */
137     private String replaceSlashes(String filePath) {
138 
139         if (File.separatorChar == '\\') {
140             filePath = filePath.replace(File.separatorChar, '/');
141         }
142 
143         return filePath;
144     }
145 
146     protected Map<String,String> generateRoleQualifiers(BatchFile batchFile, Person user) {
147         return new HashMap<String,String>();
148     }
149     
150     protected IdentityManagementService getIdentityManagementService() {
151         if (identityManagementService == null) {
152             identityManagementService = SpringContext.getBean(IdentityManagementService.class);
153         }
154         return identityManagementService;
155     }
156 
157     public KualiModuleService getKualiModuleService() {
158         if (kualiModuleService == null) {
159             kualiModuleService = SpringContext.getBean(KualiModuleService.class);
160         }
161         return kualiModuleService;
162     }
163 }