| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kim.service.impl; |
| 17 | |
|
| 18 | |
import org.kuali.rice.kim.bo.group.dto.GroupInfo; |
| 19 | |
import org.kuali.rice.kim.bo.group.impl.GroupMemberImpl; |
| 20 | |
import org.kuali.rice.kim.bo.impl.GroupImpl; |
| 21 | |
import org.kuali.rice.kim.service.IdentityManagementNotificationService; |
| 22 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
| 23 | |
import org.kuali.rice.kim.util.KimConstants; |
| 24 | |
import org.kuali.rice.kim.util.KimConstants.KimGroupMemberTypes; |
| 25 | |
import org.kuali.rice.kns.service.*; |
| 26 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
| 27 | |
|
| 28 | |
import javax.xml.namespace.QName; |
| 29 | |
import java.util.*; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 0 | public abstract class GroupServiceBase { |
| 38 | |
private BusinessObjectService businessObjectService; |
| 39 | |
private LookupService lookupService; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public GroupInfo getGroupInfo(String groupId) { |
| 45 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 46 | 0 | return toGroupInfo(group); |
| 47 | |
} |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) { |
| 55 | 0 | if( groupId == null || groupMemberId == null) { |
| 56 | 0 | return false; |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | return isGroupMemberOfGroupInternal(groupMemberId, groupId); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public boolean isMemberOfGroup(String principalId, String groupId) { |
| 69 | 0 | if ( principalId == null || groupId == null ) { |
| 70 | 0 | return false; |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 75 | 0 | if ( group == null || !group.isActive() ) { |
| 76 | 0 | return false; |
| 77 | |
} |
| 78 | |
|
| 79 | 0 | for (String groupPrincipalId : group.getMemberPrincipalIds() ) { |
| 80 | 0 | if (groupPrincipalId.equals(principalId)) { |
| 81 | 0 | return true; |
| 82 | |
} |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | 0 | for ( String memberGroupId : group.getMemberGroupIds() ) { |
| 87 | 0 | if ( isMemberOfGroup( principalId, memberGroupId ) ) { |
| 88 | 0 | return true; |
| 89 | |
} |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | 0 | return false; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public GroupInfo getGroupInfoByName(String namespaceCode, String groupName) { |
| 100 | 0 | return toGroupInfo(getGroupByName(namespaceCode, groupName)); |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public Map<String, GroupInfo> getGroupInfos(Collection<String> groupIds) { |
| 107 | 0 | Map<String, GroupInfo> result = new HashMap<String, GroupInfo>(); |
| 108 | |
|
| 109 | |
|
| 110 | 0 | for (String s : groupIds) { |
| 111 | 0 | GroupImpl group = getGroupImpl(s); |
| 112 | 0 | if (group != null) { |
| 113 | 0 | result.put(s, toGroupInfo(group)); |
| 114 | |
} |
| 115 | 0 | } |
| 116 | |
|
| 117 | 0 | return result; |
| 118 | |
} |
| 119 | |
|
| 120 | |
protected GroupImpl getGroupImpl(String groupId) { |
| 121 | 0 | if ( groupId == null ) { |
| 122 | 0 | return null; |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | 0 | return (GroupImpl) getBusinessObjectService().findBySinglePrimaryKey(GroupImpl.class, groupId); |
| 127 | |
} |
| 128 | |
|
| 129 | |
@SuppressWarnings("unchecked") |
| 130 | |
protected GroupImpl getGroupByName(String namespaceCode, String groupName) { |
| 131 | 0 | if ( namespaceCode == null || groupName == null ) { |
| 132 | 0 | return null; |
| 133 | |
} |
| 134 | 0 | Map<String,String> criteria = new HashMap<String,String>(); |
| 135 | 0 | criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode); |
| 136 | 0 | criteria.put(KimConstants.UniqueKeyConstants.GROUP_NAME, groupName); |
| 137 | 0 | Collection<GroupImpl> groups = getBusinessObjectService().findMatching(GroupImpl.class, criteria); |
| 138 | 0 | if ( groups.size() > 0 ) { |
| 139 | 0 | return groups.iterator().next(); |
| 140 | |
} |
| 141 | 0 | return null; |
| 142 | |
} |
| 143 | |
|
| 144 | |
protected GroupInfo toGroupInfo(GroupImpl kimGroup) { |
| 145 | 0 | GroupInfo info = null; |
| 146 | |
|
| 147 | 0 | if (kimGroup != null) { |
| 148 | 0 | info = new GroupInfo(); |
| 149 | |
|
| 150 | 0 | info.setActive(kimGroup.isActive()); |
| 151 | 0 | info.setGroupDescription(kimGroup.getGroupDescription()); |
| 152 | 0 | info.setGroupId(kimGroup.getGroupId()); |
| 153 | 0 | info.setGroupName(kimGroup.getGroupName()); |
| 154 | 0 | info.setKimTypeId(kimGroup.getKimTypeId()); |
| 155 | 0 | info.setNamespaceCode(kimGroup.getNamespaceCode()); |
| 156 | |
|
| 157 | 0 | info.setAttributes(kimGroup.getAttributes()); |
| 158 | |
} |
| 159 | |
|
| 160 | 0 | return info; |
| 161 | |
} |
| 162 | |
|
| 163 | |
protected List<GroupInfo> toGroupInfo(List<GroupImpl> kimGroups){ |
| 164 | 0 | List<GroupInfo> lRet = null; |
| 165 | |
|
| 166 | 0 | if(kimGroups != null){ |
| 167 | 0 | lRet = new ArrayList<GroupInfo>(); |
| 168 | |
|
| 169 | 0 | for(GroupImpl gi: kimGroups){ |
| 170 | 0 | lRet.add(this.toGroupInfo(gi)); |
| 171 | |
} |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | return lRet; |
| 175 | |
} |
| 176 | |
|
| 177 | |
protected boolean isGroupMemberOfGroupInternal(String groupMemberId, String groupId) { |
| 178 | |
|
| 179 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 180 | 0 | if (group == null) { |
| 181 | 0 | return false; |
| 182 | |
} |
| 183 | 0 | if( !group.isActive() ) { |
| 184 | 0 | return false; |
| 185 | |
} |
| 186 | |
|
| 187 | 0 | for( String memberGroupId : group.getMemberGroupIds()) { |
| 188 | 0 | if(memberGroupId.equals(groupMemberId)) { |
| 189 | 0 | return true; |
| 190 | |
} |
| 191 | 0 | else if(isGroupMemberOfGroup(groupMemberId, memberGroupId)) { |
| 192 | 0 | return true; |
| 193 | |
} |
| 194 | |
} |
| 195 | |
|
| 196 | 0 | return false; |
| 197 | |
} |
| 198 | |
|
| 199 | |
@SuppressWarnings("unchecked") |
| 200 | |
protected Map<String,GroupInfo> getDirectParentGroups(String groupId) { |
| 201 | 0 | if ( groupId == null ) { |
| 202 | 0 | return Collections.emptyMap(); |
| 203 | |
} |
| 204 | 0 | Map<String,String> criteria = new HashMap<String,String>(); |
| 205 | 0 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, groupId); |
| 206 | 0 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
| 207 | |
|
| 208 | 0 | List<GroupMemberImpl> groupMembers = (List<GroupMemberImpl>)getBusinessObjectService().findMatching(GroupMemberImpl.class, criteria); |
| 209 | 0 | Set<String> matchingGroupIds = new HashSet<String>(); |
| 210 | |
|
| 211 | 0 | for ( GroupMemberImpl gm : groupMembers ) { |
| 212 | 0 | if ( gm.isActive() ) { |
| 213 | 0 | matchingGroupIds.add(gm.getGroupId()); |
| 214 | |
} |
| 215 | |
} |
| 216 | 0 | return getGroupInfos(matchingGroupIds); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
protected List<GroupInfo> getParentGroups(String groupId) { |
| 223 | 0 | if ( groupId == null ) { |
| 224 | 0 | return Collections.emptyList(); |
| 225 | |
} |
| 226 | 0 | Set<GroupInfo> groups = new HashSet<GroupInfo>(); |
| 227 | 0 | getParentGroupsInternal( groupId, groups ); |
| 228 | 0 | return new ArrayList<GroupInfo>( groups ); |
| 229 | |
} |
| 230 | |
|
| 231 | |
protected void getParentGroupsInternal( String groupId, Set<GroupInfo> groups ) { |
| 232 | 0 | Map<String,GroupInfo> parentGroups = getDirectParentGroups( groupId ); |
| 233 | 0 | for ( GroupInfo group : parentGroups.values() ) { |
| 234 | 0 | if ( !groups.contains( group ) ) { |
| 235 | 0 | groups.add( group ); |
| 236 | 0 | getParentGroupsInternal( group.getGroupId(), groups ); |
| 237 | |
} |
| 238 | |
} |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
public List<String> getMemberPrincipalIds(String groupId) { |
| 242 | 0 | if ( groupId == null ) { |
| 243 | 0 | return Collections.emptyList(); |
| 244 | |
} |
| 245 | 0 | Set<String> ids = new HashSet<String>(); |
| 246 | 0 | Set<String> groupIds = new HashSet<String>(); |
| 247 | |
|
| 248 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 249 | 0 | if ( group == null ) { |
| 250 | 0 | return Collections.emptyList(); |
| 251 | |
} |
| 252 | |
|
| 253 | 0 | ids.addAll( group.getMemberPrincipalIds() ); |
| 254 | 0 | groupIds.add(group.getGroupId()); |
| 255 | |
|
| 256 | 0 | for (String memberGroupId : group.getMemberGroupIds()) { |
| 257 | 0 | if (!groupIds.contains(memberGroupId)){ |
| 258 | 0 | ids.addAll(getMemberPrincipalIds(memberGroupId)); |
| 259 | |
} |
| 260 | |
} |
| 261 | |
|
| 262 | 0 | return new ArrayList<String>(ids); |
| 263 | |
} |
| 264 | |
|
| 265 | |
protected List<String> getMemberPrincipalIdsInternal(String groupId, Set<String> visitedGroupIds) { |
| 266 | 0 | if ( groupId == null ) { |
| 267 | 0 | return Collections.emptyList(); |
| 268 | |
} |
| 269 | 0 | Set<String> ids = new HashSet<String>(); |
| 270 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 271 | 0 | if ( group == null ) { |
| 272 | 0 | return Collections.emptyList(); |
| 273 | |
} |
| 274 | |
|
| 275 | 0 | ids.addAll( group.getMemberPrincipalIds() ); |
| 276 | 0 | visitedGroupIds.add(group.getGroupId()); |
| 277 | |
|
| 278 | 0 | for (String memberGroupId : group.getMemberGroupIds()) { |
| 279 | 0 | if (!visitedGroupIds.contains(memberGroupId)){ |
| 280 | 0 | ids.addAll(getMemberPrincipalIdsInternal(memberGroupId, visitedGroupIds)); |
| 281 | |
} |
| 282 | |
} |
| 283 | |
|
| 284 | 0 | return new ArrayList<String>(ids); |
| 285 | |
} |
| 286 | |
|
| 287 | |
public boolean isMemberOfGroupInternal(String principalId, String groupId, Set<String> visitedGroupIds) { |
| 288 | 0 | if ( principalId == null || groupId == null ) { |
| 289 | 0 | return false; |
| 290 | |
} |
| 291 | |
|
| 292 | |
|
| 293 | 0 | GroupImpl group = getGroupImpl(groupId); |
| 294 | 0 | if ( group == null || !group.isActive() ) { |
| 295 | 0 | return false; |
| 296 | |
} |
| 297 | |
|
| 298 | 0 | for (String groupPrincipalId : group.getMemberPrincipalIds() ) { |
| 299 | 0 | if (groupPrincipalId.equals(principalId)) { |
| 300 | 0 | return true; |
| 301 | |
} |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | 0 | for ( String memberGroupId : group.getMemberGroupIds() ) { |
| 306 | 0 | if (!visitedGroupIds.contains(memberGroupId)){ |
| 307 | 0 | visitedGroupIds.add(memberGroupId); |
| 308 | 0 | if ( isMemberOfGroupInternal( principalId, memberGroupId, visitedGroupIds ) ) { |
| 309 | 0 | return true; |
| 310 | |
} |
| 311 | |
} |
| 312 | |
} |
| 313 | |
|
| 314 | |
|
| 315 | 0 | return false; |
| 316 | |
} |
| 317 | |
|
| 318 | |
protected IdentityManagementNotificationService getIdentityManagementNotificationService() { |
| 319 | 0 | return (IdentityManagementNotificationService)KSBServiceLocator.getMessageHelper().getServiceAsynchronously(new QName("KIM", "kimIdentityManagementNotificationService")); |
| 320 | |
} |
| 321 | |
|
| 322 | |
protected BusinessObjectService getBusinessObjectService() { |
| 323 | 0 | if ( businessObjectService == null ) { |
| 324 | 0 | businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
| 325 | |
} |
| 326 | 0 | return businessObjectService; |
| 327 | |
} |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
protected LookupService getLookupService() { |
| 333 | 0 | if(lookupService == null) { |
| 334 | 0 | lookupService = KNSServiceLocatorWeb.getLookupService(); |
| 335 | |
} |
| 336 | 0 | return lookupService; |
| 337 | |
} |
| 338 | |
} |