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.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.ole.sys.FinancialSystemModuleConfiguration;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.batch.BatchFile;
26  import org.kuali.ole.sys.batch.BatchFileUtils;
27  import org.kuali.ole.sys.batch.service.BatchFileAdminAuthorizationService;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.ole.sys.identity.OleKimAttributes;
30  import org.kuali.rice.kim.api.KimConstants;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.services.IdentityManagementService;
33  import org.kuali.rice.krad.bo.ModuleConfiguration;
34  import org.kuali.rice.krad.service.KualiModuleService;
35  import org.kuali.rice.krad.service.ModuleService;
36  import org.kuali.rice.krad.util.KRADConstants;
37  
38  public class BatchFileAdminAuthorizationServiceImpl implements BatchFileAdminAuthorizationService {
39  
40      private IdentityManagementService identityManagementService;
41      private KualiModuleService kualiModuleService;
42      
43      @Override
44      public boolean canDownload(BatchFile batchFile, Person user) {
45          return getIdentityManagementService().isAuthorizedByTemplateName(user.getPrincipalId(),
46                  OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.namespace, OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.name,
47                  generateDownloadCheckPermissionDetails(batchFile, user), generateDownloadCheckRoleQualifiers(batchFile, user));
48      }
49  
50      @Override
51      public boolean canDelete(BatchFile batchFile, Person user) {
52          return getIdentityManagementService().isAuthorizedByTemplateName(user.getPrincipalId(),
53                  OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.namespace, OLEConstants.PermissionTemplate.VIEW_BATCH_FILES.name,
54                  generateDownloadCheckPermissionDetails(batchFile, user), generateDownloadCheckRoleQualifiers(batchFile, user));
55      }
56      
57      protected String determineNamespaceCode(BatchFile batchFile) {
58          for (ModuleService moduleService : getKualiModuleService().getInstalledModuleServices()) {
59              ModuleConfiguration moduleConfiguration = moduleService.getModuleConfiguration();
60              if (moduleConfiguration instanceof FinancialSystemModuleConfiguration) {
61                  List<String> batchFileDirectories = ((FinancialSystemModuleConfiguration) moduleConfiguration).getBatchFileDirectories();
62                  for (String batchFileDirectoryName : batchFileDirectories) {
63                      File directory = new File(batchFileDirectoryName).getAbsoluteFile();
64                      if (BatchFileUtils.isSuperDirectoryOf(directory, batchFile.retrieveFile())) {
65                          return moduleConfiguration.getNamespaceCode();
66                      }
67                  }
68              }
69          }
70          return null;
71      }
72  
73      protected Map<String,String> generateDownloadCheckPermissionDetails(BatchFile batchFile, Person user) {
74          return generatePermissionDetails(batchFile, user);
75      }
76      
77      protected Map<String,String> generateDownloadCheckRoleQualifiers(BatchFile batchFile, Person user) {
78          return generateRoleQualifiers(batchFile, user);
79      }
80  
81      protected Map<String,String> generateDeleteCheckPermissionDetails(BatchFile batchFile, Person user) {
82          return generatePermissionDetails(batchFile, user);
83      }
84      
85      protected Map<String,String> generateDeleteCheckRoleQualifiers(BatchFile batchFile, Person user) {
86          return generateRoleQualifiers(batchFile, user);
87      }
88  
89      protected Map<String,String> generatePermissionDetails(BatchFile batchFile, Person user) {
90          Map<String,String> permissionDetails = new HashMap<String,String>();
91          permissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, determineNamespaceCode(batchFile));
92          permissionDetails.put(OleKimAttributes.FILE_PATH, replaceSlashes(batchFile.getPath() + File.separator + batchFile.getFileName()));
93          return permissionDetails;
94      }
95      
96      /**
97       * The permissions for the filePath will be added using '/' directory separators.
98       * This method will replace any '\\' directory separators with '/'
99       *
100      * @param filePath
101      * @return
102      */
103     private String replaceSlashes(String filePath) {
104 
105         if (File.separatorChar == '\\') {
106             filePath = filePath.replace(File.separatorChar, '/');
107         }
108 
109         return filePath;
110     }
111 
112     protected Map<String,String> generateRoleQualifiers(BatchFile batchFile, Person user) {
113         return new HashMap<String,String>();
114     }
115     
116     protected IdentityManagementService getIdentityManagementService() {
117         if (identityManagementService == null) {
118             identityManagementService = SpringContext.getBean(IdentityManagementService.class);
119         }
120         return identityManagementService;
121     }
122 
123     public KualiModuleService getKualiModuleService() {
124         if (kualiModuleService == null) {
125             kualiModuleService = SpringContext.getBean(KualiModuleService.class);
126         }
127         return kualiModuleService;
128     }
129 }