001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kew.identity.service.impl; 017 018 import org.apache.commons.logging.Log; 019 import org.apache.commons.logging.LogFactory; 020 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 021 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; 022 import org.kuali.rice.kew.actionrequest.Recipient; 023 import org.kuali.rice.kew.api.KewApiConstants; 024 import org.kuali.rice.kew.api.identity.EmployeeId; 025 import org.kuali.rice.kew.api.identity.PrincipalId; 026 import org.kuali.rice.kew.api.identity.PrincipalName; 027 import org.kuali.rice.kew.api.user.UserId; 028 import org.kuali.rice.kew.identity.service.IdentityHelperService; 029 import org.kuali.rice.kew.workgroup.GroupId; 030 import org.kuali.rice.kew.workgroup.GroupNameId; 031 import org.kuali.rice.kew.workgroup.WorkflowGroupId; 032 import org.kuali.rice.kim.api.group.Group; 033 import org.kuali.rice.kim.api.identity.Person; 034 import org.kuali.rice.kim.api.identity.principal.Principal; 035 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 036 037 /** 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 * 041 */ 042 public class IdentityHelperServiceImpl implements IdentityHelperService { 043 044 private static final Log logger = LogFactory.getLog(IdentityHelperServiceImpl.class); 045 046 public String getIdForPrincipalName(String principalName) { 047 if (principalName == null) { 048 throw new RiceIllegalArgumentException("Can't lookup a principal ID for a null principal name."); 049 } 050 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName); 051 if (principal == null) { 052 throw new RiceIllegalArgumentException("Given principal name of '" + principalName + "' was invalid. Failed to lookup a corresponding principal ID."); 053 } 054 return principal.getPrincipalId(); 055 } 056 057 public void validatePrincipalId(String principalId) { 058 // the getPrincipal method attempts to load the principal with the id and throws an exception if it fails 059 getPrincipal(principalId); 060 } 061 062 public String getIdForGroupName(String namespace, String groupName) { 063 Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespace, groupName); 064 if (group == null) { 065 throw new RiceIllegalArgumentException("Given namespace of '" + namespace + "' and name of '" + groupName + "' was invalid. Failed to lookup a corresponding group ID."); 066 } 067 return group.getId(); 068 } 069 070 071 public Recipient getPrincipalRecipient(String principalId) { 072 Principal principal = getPrincipal(principalId); 073 return new KimPrincipalRecipient(principal); 074 } 075 076 public Principal getPrincipal(String principalId) { 077 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId); 078 if (principal == null) { 079 throw new RiceIllegalArgumentException("Could not locate a principal with the given principalId of " + principalId); 080 } 081 return principal; 082 } 083 084 public Principal getPrincipalByPrincipalName(String principalName) { 085 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName); 086 if (principal == null) { 087 throw new RiceIllegalArgumentException("Could not locate a principal with the given principalName of " + principalName); 088 } 089 return principal; 090 } 091 092 public Group getGroupByName(String namespaceCode, String name) { 093 Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(namespaceCode, name); 094 if (group == null) { 095 throw new RiceIllegalArgumentException("Could not locate a group with the given namspace of '" + namespaceCode + "' and group name of '" + name + "'"); 096 } 097 return group; 098 } 099 100 public Person getPerson(String principalId) { 101 Person person = KimApiServiceLocator.getPersonService().getPerson(principalId); 102 if (person == null) { 103 throw new RiceIllegalArgumentException("Could not locate a person with the given principal id of " + principalId); 104 } 105 return person; 106 } 107 108 public Person getPersonByPrincipalName(String principalName) { 109 Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName); 110 if (person == null) { 111 throw new RiceIllegalArgumentException("Could not locate a person with the given principal name of " + principalName); 112 } 113 return person; 114 } 115 116 public Person getPersonByEmployeeId(String employeeId) { 117 Person person = KimApiServiceLocator.getPersonService().getPersonByEmployeeId(employeeId); 118 if (person == null) { 119 throw new RiceIllegalArgumentException("Could not locate a person with the given employee id of " + employeeId); 120 } 121 return person; 122 } 123 124 125 public Group getGroup(String groupId) { 126 Group group = KimApiServiceLocator.getGroupService().getGroup(groupId); 127 if (group == null) { 128 throw new RiceIllegalArgumentException("Could not locate a group with the given groupId of " + groupId); 129 } 130 return group; 131 } 132 133 public Group getGroup(GroupId groupId) { 134 if (groupId == null || groupId.isEmpty()) { 135 return null; 136 } else if (groupId instanceof WorkflowGroupId) { 137 return KimApiServiceLocator.getGroupService().getGroup(""+((WorkflowGroupId)groupId).getGroupId()); 138 } else if (groupId instanceof GroupNameId) { 139 return KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName( 140 ((GroupNameId) groupId).getNamespace(), ((GroupNameId) groupId).getNameId()); 141 } 142 throw new RiceIllegalArgumentException("Invalid GroupId type was passed: " + groupId); 143 } 144 145 public Principal getPrincipal(UserId userId) { 146 if (userId == null) { 147 return null; 148 } else if (userId instanceof PrincipalId) { 149 String principalId = ((PrincipalId)userId).getPrincipalId(); 150 return KimApiServiceLocator.getIdentityService().getPrincipal(principalId); 151 } else if (userId instanceof PrincipalName) { 152 String principalName = ((PrincipalName)userId).getId(); 153 return KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName); 154 } else if (userId instanceof EmployeeId) { 155 String employeeId = ((EmployeeId)userId).getEmployeeId(); 156 Person person = getPersonByEmployeeId(employeeId); 157 return getPrincipal(person.getPrincipalId()); 158 } 159 throw new RiceIllegalArgumentException("Invalid UserIdDTO type was passed: " + userId); 160 } 161 162 public Principal getSystemPrincipal() { 163 return getPrincipalByPrincipalName(KewApiConstants.SYSTEM_USER); 164 } 165 166 }