/src/main/java/de/theit/hudson/crowd/CrowdConfigurationService.java

https://github.com/theit/hudson-crowd2-plugin · Java · 243 lines · 147 code · 25 blank · 71 comment · 22 complexity · 8688b14fbb71d0ea7cdb11568f99b922 MD5 · raw file

  1. /*
  2. * @(#)CrowdConfigurationService.java
  3. * Copyright (C)2011 Thorsten Heit.
  4. * All rights reserved.
  5. */
  6. package de.theit.hudson.crowd;
  7. import static de.theit.hudson.crowd.ErrorMessages.applicationPermission;
  8. import static de.theit.hudson.crowd.ErrorMessages.groupNotFound;
  9. import static de.theit.hudson.crowd.ErrorMessages.invalidAuthentication;
  10. import static de.theit.hudson.crowd.ErrorMessages.operationFailed;
  11. import static de.theit.hudson.crowd.ErrorMessages.specifyGroup;
  12. import static de.theit.hudson.crowd.ErrorMessages.userNotFound;
  13. import java.util.Collection;
  14. import java.util.Comparator;
  15. import java.util.HashSet;
  16. import java.util.List;
  17. import java.util.TreeSet;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import org.acegisecurity.GrantedAuthority;
  21. import org.acegisecurity.GrantedAuthorityImpl;
  22. import com.atlassian.crowd.exception.ApplicationPermissionException;
  23. import com.atlassian.crowd.exception.GroupNotFoundException;
  24. import com.atlassian.crowd.exception.InvalidAuthenticationException;
  25. import com.atlassian.crowd.exception.OperationFailedException;
  26. import com.atlassian.crowd.exception.UserNotFoundException;
  27. import com.atlassian.crowd.integration.http.CrowdHttpAuthenticator;
  28. import com.atlassian.crowd.integration.http.util.CrowdHttpTokenHelper;
  29. import com.atlassian.crowd.model.group.Group;
  30. import com.atlassian.crowd.service.client.ClientProperties;
  31. import com.atlassian.crowd.service.client.CrowdClient;
  32. /**
  33. * This class contains all objects that are necessary to access the REST
  34. * services on the remote Crowd server. In addition to this it contains some
  35. * helper methods
  36. *
  37. * @author <a href="mailto:theit@gmx.de">Thorsten Heit (theit@gmx.de)</a>
  38. * @since 08.09.2011
  39. * @version $Id$
  40. */
  41. public class CrowdConfigurationService {
  42. /** Used for logging purposes. */
  43. private static final Logger LOG = Logger
  44. .getLogger(CrowdConfigurationService.class.getName());
  45. /**
  46. * The maximum number of groups that can be fetched from the Crowd server
  47. * for a user in one request.
  48. */
  49. private static final int MAX_GROUPS = 500;
  50. /** Holds the Crowd client properties. */
  51. ClientProperties clientProperties;
  52. /** The Crowd client to access the REST services on the remote Crowd server. */
  53. CrowdClient crowdClient;
  54. /** The helper class for Crowd SSO token operations. */
  55. CrowdHttpTokenHelper tokenHelper;
  56. /**
  57. * The interface used to manage HTTP authentication and web/SSO
  58. * authentication integration.
  59. */
  60. CrowdHttpAuthenticator crowdHttpAuthenticator;
  61. /** The group name a user must belong to to be allowed to login into Hudson. */
  62. private String groupName;
  63. /** Specifies whether nested groups may be used. */
  64. private boolean nestedGroups;
  65. /**
  66. * Creates a new Crowd configuration object.
  67. *
  68. * @param pGroupName
  69. * The group name to use when authenticating Crowd users. May not
  70. * be <code>null</code>.
  71. * @param pNestedGroups
  72. * Specifies whether nested groups should be used when validating
  73. * users against the group name.
  74. */
  75. public CrowdConfigurationService(String pGroupName, boolean pNestedGroups) {
  76. this.groupName = pGroupName.trim();
  77. if (0 == this.groupName.length()) {
  78. throw new IllegalArgumentException(specifyGroup());
  79. }
  80. this.nestedGroups = pNestedGroups;
  81. }
  82. /**
  83. * Checks whether the user is a member of a certain Crowd group whose
  84. * members are allowed to login into Hudson.
  85. *
  86. * @param username
  87. * The name of the user to check. May not be <code>null</code>.
  88. * @return <code>true</code> if and only if the group exists, is active and
  89. * the user is either a direct group member or, if nested groups may
  90. * be used, a nested group member. <code>false</code> else.
  91. */
  92. public boolean isGroupMember(String username) {
  93. boolean retval = false;
  94. try {
  95. if (this.crowdClient.isUserDirectGroupMember(username,
  96. this.groupName)) {
  97. retval = true;
  98. } else if (this.nestedGroups
  99. && this.crowdClient.isUserNestedGroupMember(username,
  100. this.groupName)) {
  101. retval = true;
  102. }
  103. } catch (ApplicationPermissionException ex) {
  104. LOG.log(Level.WARNING, applicationPermission(), ex);
  105. } catch (InvalidAuthenticationException ex) {
  106. LOG.log(Level.WARNING, invalidAuthentication(), ex);
  107. } catch (OperationFailedException ex) {
  108. LOG.log(Level.SEVERE, operationFailed(), ex);
  109. }
  110. return retval;
  111. }
  112. /**
  113. * Checks if the group exists on the remote Crowd server and is active.
  114. *
  115. * @return <code>true</code> if and only if:
  116. * <ul>
  117. * <li>The group name is empty or</li>
  118. * <li>The group name is not empty, does exist on the remote Crowd
  119. * server and is active.</li>
  120. * </ul>
  121. * <code>false</code> else.
  122. */
  123. public boolean isGroupActive() {
  124. boolean retval = false;
  125. try {
  126. Group group = this.crowdClient.getGroup(this.groupName);
  127. if (null != group) {
  128. retval = group.isActive();
  129. }
  130. } catch (GroupNotFoundException ex) {
  131. LOG.log(Level.INFO, groupNotFound(), ex);
  132. } catch (InvalidAuthenticationException ex) {
  133. LOG.log(Level.WARNING, invalidAuthentication(), ex);
  134. } catch (ApplicationPermissionException ex) {
  135. LOG.log(Level.WARNING, applicationPermission(), ex);
  136. } catch (OperationFailedException ex) {
  137. LOG.log(Level.SEVERE, operationFailed(), ex);
  138. }
  139. return retval;
  140. }
  141. /**
  142. * Retrieves the list of all (nested) groups from the Crowd server that the
  143. * user is a member of.
  144. *
  145. * @param username
  146. * The name of the user. May not be <code>null</code>.
  147. * @return The list of all groups that the user is a member of. Always
  148. * non-null.
  149. */
  150. public Collection<GrantedAuthority> getAuthoritiesForUser(String username) {
  151. Collection<GrantedAuthority> authorities = new TreeSet<GrantedAuthority>(
  152. new Comparator<GrantedAuthority>() {
  153. @Override
  154. public int compare(GrantedAuthority ga1,
  155. GrantedAuthority ga2) {
  156. return ga1.getAuthority().compareTo(ga2.getAuthority());
  157. }
  158. });
  159. HashSet<String> groupNames = new HashSet<String>();
  160. // load the names of all groups the user is a direct member of
  161. try {
  162. int index = 0;
  163. while (true) {
  164. List<Group> groups = this.crowdClient.getGroupsForUser(
  165. username, index, MAX_GROUPS);
  166. if (null == groups || groups.isEmpty()) {
  167. break;
  168. }
  169. for (Group group : groups) {
  170. if (group.isActive()) {
  171. groupNames.add(group.getName());
  172. }
  173. }
  174. index += MAX_GROUPS;
  175. }
  176. } catch (UserNotFoundException ex) {
  177. LOG.log(Level.INFO, userNotFound(), ex);
  178. } catch (InvalidAuthenticationException ex) {
  179. LOG.log(Level.WARNING, invalidAuthentication(), ex);
  180. } catch (ApplicationPermissionException ex) {
  181. LOG.log(Level.WARNING, applicationPermission(), ex);
  182. } catch (OperationFailedException ex) {
  183. LOG.log(Level.SEVERE, operationFailed(), ex);
  184. }
  185. // load the names of all groups the user is a nester member of
  186. if (this.nestedGroups) {
  187. try {
  188. int index = 0;
  189. while (true) {
  190. List<Group> groups = this.crowdClient
  191. .getGroupsForNestedUser(username, index, MAX_GROUPS);
  192. if (null == groups || groups.isEmpty()) {
  193. break;
  194. }
  195. for (Group group : groups) {
  196. if (group.isActive()) {
  197. groupNames.add(group.getName());
  198. }
  199. }
  200. index += MAX_GROUPS;
  201. }
  202. } catch (UserNotFoundException ex) {
  203. LOG.log(Level.INFO, userNotFound(), ex);
  204. } catch (InvalidAuthenticationException ex) {
  205. LOG.log(Level.WARNING, invalidAuthentication(), ex);
  206. } catch (ApplicationPermissionException ex) {
  207. LOG.log(Level.WARNING, applicationPermission(), ex);
  208. } catch (OperationFailedException ex) {
  209. LOG.log(Level.SEVERE, operationFailed(), ex);
  210. }
  211. }
  212. // now create the list of authorities
  213. for (String str : groupNames) {
  214. authorities.add(new GrantedAuthorityImpl(str));
  215. }
  216. return authorities;
  217. }
  218. }