/kns/src/main/java/org/kuali/rice/kns/kim/role/RoleTypeServiceBase.java

https://github.com/sbower/kuali-rice-1 · Java · 249 lines · 156 code · 37 blank · 56 comment · 45 complexity · ebdf68df5ae47e677559720a5f98843d MD5 · raw file

  1. /*
  2. * Copyright 2007-2008 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.rice.kns.kim.role;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
  19. import org.kuali.rice.kim.api.role.Role;
  20. import org.kuali.rice.kim.api.role.RoleMembership;
  21. import org.kuali.rice.kim.framework.common.delegate.DelegationTypeService;
  22. import org.kuali.rice.kim.framework.role.RoleTypeService;
  23. import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceBase;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * @deprecated A krad integrated type service base class will be provided in the future.
  31. */
  32. @Deprecated
  33. public class RoleTypeServiceBase extends DataDictionaryTypeServiceBase implements RoleTypeService, DelegationTypeService {
  34. /**
  35. * Performs a simple check that the qualifier on the role matches the qualification.
  36. * Extra qualification attributes are ignored.
  37. */
  38. @Override
  39. public boolean doesRoleQualifierMatchQualification(Map<String, String> qualification, Map<String, String> roleQualifier) {
  40. if (qualification == null) {
  41. throw new RiceIllegalArgumentException("qualification was null or blank");
  42. }
  43. if (roleQualifier == null) {
  44. throw new RiceIllegalArgumentException("roleQualifier was null or blank");
  45. }
  46. Map<String, String> translatedQualification = translateInputAttributes(qualification);
  47. validateRequiredAttributesAgainstReceived(translatedQualification);
  48. return performMatch(translatedQualification, roleQualifier);
  49. }
  50. @Override
  51. public List<RoleMembership> getMatchingRoleMemberships(Map<String, String> qualification,
  52. List<RoleMembership> roleMemberList) {
  53. if (qualification == null) {
  54. throw new RiceIllegalArgumentException("qualification was null or blank");
  55. }
  56. if (roleMemberList == null) {
  57. throw new RiceIllegalArgumentException("roleMemberList was null or blank");
  58. }
  59. Map<String, String> translatedQualification = translateInputAttributes(qualification);
  60. validateRequiredAttributesAgainstReceived(translatedQualification);
  61. List<RoleMembership> matchingMemberships = new ArrayList<RoleMembership>();
  62. for ( RoleMembership roleMembership : roleMemberList ) {
  63. if ( performMatch( translatedQualification, roleMembership.getQualifier() ) ) {
  64. matchingMemberships.add( roleMembership );
  65. }
  66. }
  67. return Collections.unmodifiableList(matchingMemberships);
  68. }
  69. /**
  70. * Return an empty list since this method should not be called by the role service for this service type.
  71. * Subclasses which are application role types should override this method.
  72. *
  73. */
  74. protected List<RoleMembership> getRoleMembersFromApplicationRole(String namespaceCode, String roleName, Map<String, String> qualification) {
  75. if (StringUtils.isBlank(namespaceCode)) {
  76. throw new RiceIllegalArgumentException("namespaceCode was null or blank");
  77. }
  78. if (StringUtils.isBlank(roleName)) {
  79. throw new RiceIllegalArgumentException("roleName was null or blank");
  80. }
  81. if (qualification == null) {
  82. throw new RiceIllegalArgumentException("qualification was null or blank");
  83. }
  84. validateRequiredAttributesAgainstReceived(qualification);
  85. if ( !isApplicationRoleType() ) {
  86. throw new UnsupportedOperationException( this.getClass().getName() + " is not an application role." );
  87. } else {
  88. throw new UnsupportedOperationException( this.getClass().getName() + " is an application role type but has not overridden this method." );
  89. }
  90. }
  91. /**
  92. * This simple initial implementation just calls
  93. * {@link #getRoleMembersFromApplicationRole(String, String, Map<String, String>)} and checks the results.
  94. *
  95. */
  96. @Override
  97. public boolean hasApplicationRole(String principalId, List<String> groupIds, String namespaceCode, String roleName, Map<String, String> qualification) {
  98. if (StringUtils.isBlank(principalId)) {
  99. throw new RiceIllegalArgumentException("principalId was null or blank");
  100. }
  101. if (groupIds == null) {
  102. throw new RiceIllegalArgumentException("groupIds was null or blank");
  103. }
  104. if (StringUtils.isBlank(namespaceCode)) {
  105. throw new RiceIllegalArgumentException("namespaceCode was null or blank");
  106. }
  107. if (StringUtils.isBlank(roleName)) {
  108. throw new RiceIllegalArgumentException("roleName was null or blank");
  109. }
  110. if (qualification == null) {
  111. throw new RiceIllegalArgumentException("qualification was null or blank");
  112. }
  113. if ( !isApplicationRoleType() ) {
  114. throw new UnsupportedOperationException( this.getClass().getName() + " is not an application role." );
  115. }
  116. // if principal ID given, check if it is in the list generated from the getPrincipalIdsFromApplicationRole method
  117. if ( StringUtils.isNotBlank( principalId ) ) {
  118. List<RoleMembership> members = getRoleMembersFromApplicationRole(namespaceCode, roleName, qualification);
  119. for ( RoleMembership rm : members ) {
  120. if ( StringUtils.isBlank( rm.getRoleMemberId() ) ) {
  121. continue;
  122. }
  123. if ( rm.getMemberTypeCode().equals( Role.PRINCIPAL_MEMBER_TYPE ) ) {
  124. if ( rm.getMemberId().equals( principalId ) ) {
  125. return true;
  126. }
  127. } else { // groups
  128. if ( groupIds != null
  129. && groupIds.contains(rm.getMemberId())) {
  130. return true;
  131. }
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. /**
  138. * Default to not being an application role type. Always returns false.
  139. *
  140. * @see org.kuali.rice.kim.framework.role.RoleTypeService#isApplicationRoleType()
  141. */
  142. @Override
  143. public boolean isApplicationRoleType() {
  144. return false;
  145. }
  146. /**
  147. * This base implementation simply returns the passed in Attributes.
  148. *
  149. * @see org.kuali.rice.kim.framework.role.RoleTypeService#convertQualificationForMemberRoles(String, String, String, String, Map<String, String>)
  150. */
  151. @Override
  152. public Map<String, String> convertQualificationForMemberRoles(String namespaceCode, String roleName, String memberRoleNamespaceCode, String memberRoleName, Map<String, String> qualification) {
  153. if (StringUtils.isBlank(namespaceCode)) {
  154. throw new RiceIllegalArgumentException("namespaceCode was null or blank");
  155. }
  156. if (StringUtils.isBlank(roleName)) {
  157. throw new RiceIllegalArgumentException("roleName was null or blank");
  158. }
  159. if (StringUtils.isBlank(memberRoleNamespaceCode)) {
  160. throw new RiceIllegalArgumentException("memberRoleNamespaceCode was null or blank");
  161. }
  162. if (StringUtils.isBlank(memberRoleName)) {
  163. throw new RiceIllegalArgumentException("memberRoleName was null or blank");
  164. }
  165. if (qualification == null) {
  166. throw new RiceIllegalArgumentException("qualification was null or blank");
  167. }
  168. return Collections.unmodifiableMap(new HashMap<String, String>(qualification));
  169. }
  170. /**
  171. * Base implementation: no sorting. Just returns the input list.
  172. */
  173. protected List<RoleMembership> sortRoleMembers(List<RoleMembership> roleMembers) {
  174. return Collections.unmodifiableList(new ArrayList<RoleMembership>(roleMembers));
  175. }
  176. /**
  177. * Performs a simple check that the qualifier on the delegation matches the qualification.
  178. * Extra qualification attributes are ignored.
  179. *
  180. */
  181. @Override
  182. public boolean doesDelegationQualifierMatchQualification(Map<String, String> qualification, Map<String, String> roleQualifier) {
  183. if (qualification == null) {
  184. throw new RiceIllegalArgumentException("qualification was null or blank");
  185. }
  186. if (roleQualifier == null) {
  187. throw new RiceIllegalArgumentException("roleQualifier was null or blank");
  188. }
  189. Map<String, String> translatedQualification = translateInputAttributes(qualification);
  190. validateRequiredAttributesAgainstReceived(translatedQualification);
  191. return performMatch(translatedQualification, roleQualifier);
  192. }
  193. /**
  194. * Returns true as a default
  195. *
  196. * @see org.kuali.rice.kim.framework.role.RoleTypeService#dynamicRoleMembership(java.lang.String, java.lang.String)
  197. */
  198. @Override
  199. public boolean dynamicRoleMembership(String namespaceCode, String roleName) {
  200. if (StringUtils.isBlank(namespaceCode)) {
  201. throw new RiceIllegalArgumentException("namespaceCode was null or blank");
  202. }
  203. if (StringUtils.isBlank(roleName)) {
  204. throw new RiceIllegalArgumentException("roleName was null or blank");
  205. }
  206. return true;
  207. }
  208. @Override
  209. public List<String> getQualifiersForExactMatch() {
  210. return Collections.emptyList();
  211. }
  212. }