PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-plugins/jira-sal-plugin/src/main/java/com/atlassian/sal/jira/user/DefaultUserManager.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 273 lines | 223 code | 37 blank | 13 comment | 43 complexity | 83a948864307bd9dcd980d57881ac06c MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.sal.jira.user;
  2. import com.atlassian.crowd.embedded.api.CrowdService;
  3. import com.atlassian.crowd.embedded.api.Group;
  4. import com.atlassian.crowd.embedded.api.User;
  5. import com.atlassian.crowd.exception.FailedAuthenticationException;
  6. import com.atlassian.crowd.model.group.GroupType;
  7. import com.atlassian.crowd.search.builder.Restriction;
  8. import com.atlassian.crowd.search.query.entity.GroupQuery;
  9. import com.atlassian.crowd.search.query.entity.restriction.NullRestrictionImpl;
  10. import com.atlassian.crowd.search.query.entity.restriction.constants.GroupTermKeys;
  11. import com.atlassian.jira.avatar.Avatar;
  12. import com.atlassian.jira.avatar.AvatarService;
  13. import com.atlassian.jira.security.GlobalPermissionManager;
  14. import com.atlassian.jira.security.JiraAuthenticationContext;
  15. import com.atlassian.jira.security.Permissions;
  16. import com.atlassian.jira.user.ApplicationUser;
  17. import com.atlassian.sal.api.user.UserKey;
  18. import com.atlassian.sal.api.user.UserManager;
  19. import com.atlassian.sal.api.user.UserProfile;
  20. import com.atlassian.sal.api.user.UserResolutionException;
  21. import org.apache.commons.lang.StringUtils;
  22. import webwork.util.URLCodec;
  23. import javax.annotation.Nullable;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.io.UnsupportedEncodingException;
  26. import java.net.URI;
  27. import java.net.URISyntaxException;
  28. import java.security.Principal;
  29. /**
  30. * User operations
  31. */
  32. public class DefaultUserManager implements UserManager {
  33. private final GlobalPermissionManager globalPermissionManager;
  34. private final JiraAuthenticationContext jiraAuthenticationContext;
  35. private final CrowdService crowdService;
  36. private final AvatarService avatarService;
  37. private final com.atlassian.jira.user.util.UserManager userManager;
  38. public DefaultUserManager(final GlobalPermissionManager globalPermissionManager,
  39. final JiraAuthenticationContext jiraAuthenticationContext, final CrowdService crowdService,
  40. AvatarService avatarService, final com.atlassian.jira.user.util.UserManager userManager) {
  41. this.globalPermissionManager = globalPermissionManager;
  42. this.jiraAuthenticationContext = jiraAuthenticationContext;
  43. this.crowdService = crowdService;
  44. this.avatarService = avatarService;
  45. this.userManager = userManager;
  46. }
  47. @Override
  48. public String getRemoteUsername() {
  49. final ApplicationUser user = jiraAuthenticationContext.getUser();
  50. if (user == null) {
  51. return null;
  52. } else {
  53. return user.getUsername();
  54. }
  55. }
  56. @Override
  57. public UserProfile getRemoteUser() {
  58. final ApplicationUser user = jiraAuthenticationContext.getUser();
  59. if (user == null) {
  60. return null;
  61. } else {
  62. return new JiraUserProfile(user);
  63. }
  64. }
  65. @Nullable
  66. @Override
  67. public UserKey getRemoteUserKey() {
  68. final ApplicationUser user = jiraAuthenticationContext.getUser();
  69. if (user == null) {
  70. return null;
  71. } else {
  72. return new UserKey(user.getKey());
  73. }
  74. }
  75. @Override
  76. public String getRemoteUsername(final HttpServletRequest request) {
  77. return getRemoteUsername();
  78. }
  79. @Override
  80. public UserProfile getRemoteUser(final HttpServletRequest httpServletRequest) {
  81. return getRemoteUser();
  82. }
  83. @Nullable
  84. @Override
  85. public UserKey getRemoteUserKey(final HttpServletRequest request) {
  86. return getRemoteUserKey();
  87. }
  88. @Override
  89. public boolean isSystemAdmin(final String username) {
  90. if (StringUtils.isNotEmpty(username)) {
  91. final ApplicationUser user = userManager.getUserByName(username);
  92. return user != null && globalPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, user);
  93. }
  94. return false;
  95. }
  96. @Override
  97. public boolean isSystemAdmin(final UserKey userKey) {
  98. if (userKey != null) {
  99. final ApplicationUser user = userManager.getUserByKey(userKey.getStringValue());
  100. return user != null && globalPermissionManager.hasPermission(Permissions.SYSTEM_ADMIN, user);
  101. }
  102. return false;
  103. }
  104. @Override
  105. public boolean isAdmin(final String username) {
  106. if (StringUtils.isNotEmpty(username)) {
  107. final ApplicationUser user = userManager.getUserByName(username);
  108. return user != null && globalPermissionManager.hasPermission(Permissions.ADMINISTER, user);
  109. }
  110. return false;
  111. }
  112. @Override
  113. public boolean isAdmin(final UserKey userKey) {
  114. if (userKey != null) {
  115. final ApplicationUser user = userManager.getUserByKey(userKey.getStringValue());
  116. return user != null && globalPermissionManager.hasPermission(Permissions.ADMINISTER, user);
  117. }
  118. return false;
  119. }
  120. @Override
  121. public boolean authenticate(final String username, final String password) {
  122. try {
  123. return crowdService.authenticate(username, password) != null;
  124. } catch (FailedAuthenticationException e) {
  125. return false;
  126. }
  127. }
  128. @Override
  129. public Principal resolve(final String username) throws UserResolutionException {
  130. return crowdService.getUser(username);
  131. }
  132. @Override
  133. public Iterable<String> findGroupNamesByPrefix(String prefix, int startIndex, int maxResults) {
  134. return crowdService.search(getGroupQuery(prefix, startIndex, maxResults));
  135. }
  136. private GroupQuery<String> getGroupQuery(String prefix, int startIndex, int maxResults) {
  137. return new GroupQuery<String>(String.class,
  138. GroupType.GROUP,
  139. StringUtils.isBlank(prefix) ? NullRestrictionImpl.INSTANCE : Restriction.on(GroupTermKeys.NAME).startingWith(prefix),
  140. startIndex,
  141. maxResults);
  142. }
  143. /**
  144. * Returns whether the user is in the specify group
  145. *
  146. * @param username The username to check
  147. * @param groupName The group to check
  148. * @return True if the user is in the specified group
  149. */
  150. @Override
  151. public boolean isUserInGroup(final String username, final String groupName) {
  152. final User user = crowdService.getUser(username);
  153. final Group group = crowdService.getGroup(groupName);
  154. return user != null && group != null && crowdService.isUserMemberOfGroup(user, group);
  155. }
  156. @Override
  157. public boolean isUserInGroup(final UserKey userKey, final String groupName) {
  158. final ApplicationUser user = userManager.getUserByKey(userKey.getStringValue());
  159. final Group group = crowdService.getGroup(groupName);
  160. return user != null && group != null && crowdService.isUserMemberOfGroup(user.getDirectoryUser(), group);
  161. }
  162. @Override
  163. public UserProfile getUserProfile(String username) {
  164. final ApplicationUser user = userManager.getUserByName(username);
  165. if (user != null) {
  166. return new JiraUserProfile(user);
  167. }
  168. return null;
  169. }
  170. @Override
  171. public UserProfile getUserProfile(final UserKey userKey) {
  172. if (userKey != null) {
  173. final ApplicationUser user = userManager.getUserByKey(userKey.getStringValue());
  174. if (user != null) {
  175. return new JiraUserProfile(user);
  176. }
  177. }
  178. return null;
  179. }
  180. class JiraUserProfile implements UserProfile {
  181. private final ApplicationUser user;
  182. /**
  183. * @param user the application user
  184. */
  185. JiraUserProfile(final ApplicationUser user) {
  186. this.user = user;
  187. }
  188. @Override
  189. public UserKey getUserKey() {
  190. return new UserKey(user.getKey());
  191. }
  192. @Override
  193. public String getUsername() {
  194. return user.getUsername();
  195. }
  196. @Override
  197. public String getFullName() {
  198. return user.getDisplayName();
  199. }
  200. @Override
  201. public String getEmail() {
  202. return user.getEmailAddress();
  203. }
  204. @Override
  205. public URI getProfilePictureUri(int width, int height) {
  206. Avatar.Size theSize = Avatar.Size.biggerThan(Math.max(width, height));
  207. if (null == theSize) {
  208. return null;
  209. } else {
  210. return getProfilePictureUri(theSize);
  211. }
  212. }
  213. @Override
  214. public URI getProfilePictureUri() {
  215. return getProfilePictureUri(Avatar.Size.LARGE);
  216. }
  217. private URI getProfilePictureUri(Avatar.Size size) {
  218. final ApplicationUser remoteUser = jiraAuthenticationContext.getUser();
  219. return avatarService.getAvatarURL(remoteUser, user, size);
  220. }
  221. @Override
  222. public URI getProfilePageUri() {
  223. final String username = getUsername();
  224. if (username == null) {
  225. return null;
  226. }
  227. try {
  228. return new URI(String.format("/secure/ViewProfile.jspa?name=%s", URLCodec.encode(username, "UTF-8")));
  229. } catch (URISyntaxException e) {
  230. return null;
  231. } catch (UnsupportedEncodingException e) {
  232. return null;
  233. }
  234. }
  235. }
  236. }