PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/web/action/admin/user/GroupBrowser.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 262 lines | 201 code | 35 blank | 26 comment | 23 complexity | d6a27cd02e1b43cc96b79356ed70404f MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.admin.user;
  2. import com.atlassian.core.util.StringUtils;
  3. import com.atlassian.crowd.embedded.api.CrowdDirectoryService;
  4. import com.atlassian.crowd.embedded.api.CrowdService;
  5. import com.atlassian.crowd.embedded.api.Directory;
  6. import com.atlassian.crowd.embedded.api.Group;
  7. import com.atlassian.crowd.embedded.impl.ImmutableGroup;
  8. import com.atlassian.crowd.exception.OperationNotPermittedException;
  9. import com.atlassian.crowd.exception.embedded.InvalidGroupException;
  10. import com.atlassian.crowd.exception.runtime.OperationFailedException;
  11. import com.atlassian.jira.bc.JiraServiceContext;
  12. import com.atlassian.jira.bc.JiraServiceContextImpl;
  13. import com.atlassian.jira.bc.group.GroupService;
  14. import com.atlassian.jira.component.ComponentAccessor;
  15. import com.atlassian.jira.scheme.Scheme;
  16. import com.atlassian.jira.security.groups.GroupManager;
  17. import com.atlassian.jira.security.util.GroupToPermissionSchemeMapper;
  18. import com.atlassian.jira.security.xsrf.RequiresXsrfCheck;
  19. import com.atlassian.jira.user.util.UserManager;
  20. import com.atlassian.jira.util.ErrorCollection;
  21. import com.atlassian.jira.util.GlobalPermissionGroupAssociationUtil;
  22. import com.atlassian.jira.util.SimpleErrorCollection;
  23. import com.atlassian.jira.web.SessionKeys;
  24. import com.atlassian.jira.web.action.AbstractBrowser;
  25. import com.atlassian.jira.web.bean.GroupBrowserFilter;
  26. import com.atlassian.jira.web.bean.PagerFilter;
  27. import com.atlassian.jira.web.component.admin.group.GroupLabelView;
  28. import com.atlassian.jira.web.component.admin.group.GroupLabelsService;
  29. import com.atlassian.sal.api.websudo.WebSudoRequired;
  30. import org.ofbiz.core.entity.GenericEntityException;
  31. import webwork.action.ActionContext;
  32. import webwork.util.BeanUtil;
  33. import javax.annotation.Nonnull;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.List;
  37. import java.util.Optional;
  38. @WebSudoRequired
  39. public class GroupBrowser extends AbstractBrowser {
  40. private List<Group> groups;
  41. private String addName;
  42. private GroupToPermissionSchemeMapper groupPermissionSchemeMapper;
  43. private final GlobalPermissionGroupAssociationUtil globalPermissionGroupAssociationUtil;
  44. private final CrowdService crowdService;
  45. private final GroupManager groupManager;
  46. private final UserManager userManager;
  47. private final CrowdDirectoryService crowdDirectoryService;
  48. private final GroupService groupService;
  49. private final GroupLabelsService groupLabels;
  50. public GroupBrowser(GroupToPermissionSchemeMapper groupToPermissionSchemeMapper, UserManager userManager,
  51. GlobalPermissionGroupAssociationUtil globalPermissionGroupAssociationUtil, CrowdService crowdService,
  52. CrowdDirectoryService crowdDirectoryService, GroupManager groupManager, GroupService groupService,
  53. GroupLabelsService groupLabels) {
  54. this.globalPermissionGroupAssociationUtil = globalPermissionGroupAssociationUtil;
  55. this.crowdService = crowdService;
  56. this.crowdDirectoryService = crowdDirectoryService;
  57. this.groupService = groupService;
  58. this.groupManager = groupManager;
  59. this.userManager = userManager;
  60. this.groupLabels = groupLabels;
  61. if (groupToPermissionSchemeMapper != null) {
  62. this.groupPermissionSchemeMapper = groupToPermissionSchemeMapper;
  63. } else {
  64. addErrorMessage(getText("groupbrowser.error.retrieve.group"));
  65. }
  66. }
  67. public GroupBrowser(GroupManager groupManager, UserManager userManager, CrowdService crowdService,
  68. CrowdDirectoryService crowdDirectoryService,
  69. GlobalPermissionGroupAssociationUtil globalPermissionGroupAssociationUtil, GroupService groupService,
  70. GroupLabelsService groupLabels)
  71. throws GenericEntityException {
  72. this(new GroupToPermissionSchemeMapper(ComponentAccessor.getPermissionSchemeManager(),
  73. ComponentAccessor.getPermissionManager()),
  74. userManager,
  75. globalPermissionGroupAssociationUtil,
  76. crowdService,
  77. crowdDirectoryService,
  78. groupManager,
  79. groupService,
  80. groupLabels);
  81. }
  82. protected String doExecute() throws Exception {
  83. resetPager();
  84. BeanUtil.setProperties(params, getFilter());
  85. return SUCCESS;
  86. }
  87. @RequiresXsrfCheck
  88. public String doAdd() throws Exception {
  89. if (!addNewGroup()) {
  90. return ERROR;
  91. }
  92. return doExecute();
  93. }
  94. private boolean addNewGroup() {
  95. if (org.apache.commons.lang.StringUtils.isEmpty(addName)) {
  96. addError("addName", getText("admin.errors.cannot.add.groups.invalid.group.name"));
  97. return false;
  98. }
  99. //JRA-12112: If external *user* management is enabled, we do not allow the addtion of a new user.
  100. if (!userManager.hasGroupWritableDirectory()) {
  101. addErrorMessage(getText("admin.errors.cannot.add.groups.directories.read.only"));
  102. return false;
  103. }
  104. if (crowdService.getGroup(addName) == null) {
  105. try {
  106. crowdService.addGroup(new ImmutableGroup(addName));
  107. } catch (OperationNotPermittedException | InvalidGroupException | OperationFailedException e) {
  108. addError("addName", getText("groupbrowser.error.add", addName));
  109. log.error("Error occurred adding group : " + addName, e);
  110. }
  111. addName = null;
  112. } else {
  113. addError("addName", getText("groupbrowser.error.group.exists"));
  114. }
  115. return true;
  116. }
  117. public PagerFilter getPager() {
  118. return getFilter();
  119. }
  120. public void resetPager() {
  121. ActionContext.getSession().put(SessionKeys.GROUP_FILTER, null);
  122. }
  123. public GroupBrowserFilter getFilter() {
  124. GroupBrowserFilter filter = (GroupBrowserFilter) ActionContext.getSession().get(SessionKeys.GROUP_FILTER);
  125. if (filter == null) {
  126. filter = new GroupBrowserFilter();
  127. ActionContext.getSession().put(SessionKeys.GROUP_FILTER, filter);
  128. }
  129. return filter;
  130. }
  131. /**
  132. * Return the current 'page' of issues (given max and start) for the current filter
  133. */
  134. public List getCurrentPage() {
  135. return getFilter().getCurrentPage(getBrowsableItems());
  136. }
  137. public List getBrowsableItems() {
  138. if (groups == null) {
  139. try {
  140. groups = getFilter().getFilteredGroups();
  141. } catch (Exception e) {
  142. log.error("Exception getting groups: " + e, e);
  143. return Collections.emptyList();
  144. }
  145. }
  146. return groups;
  147. }
  148. public String getAddName() {
  149. return addName;
  150. }
  151. public void setAddName(String addName) {
  152. this.addName = addName.trim();
  153. }
  154. public String escapeAmpersand(String str) {
  155. return StringUtils.replaceAll(str, "&", "%26");
  156. }
  157. public int getUsersInGroupCount(Group group) {
  158. return groupManager.getUsersInGroupCount(group);
  159. }
  160. public Collection<Scheme> getPermissionSchemes(String groupName) {
  161. if (groupPermissionSchemeMapper != null) {
  162. return groupPermissionSchemeMapper.getMappedValues(groupName);
  163. } else {
  164. return Collections.emptyList();
  165. }
  166. }
  167. private Boolean hasGroupWritableDirectory = null;
  168. public boolean hasGroupWritableDirectory() {
  169. if (hasGroupWritableDirectory == null) {
  170. hasGroupWritableDirectory = new Boolean(userManager.hasGroupWritableDirectory());
  171. }
  172. return hasGroupWritableDirectory.booleanValue();
  173. }
  174. /**
  175. * Test if the current user has the necessary permissions to permit deletion of given group.
  176. */
  177. public boolean isUserAbleToDeleteGroup(String groupName) {
  178. return globalPermissionGroupAssociationUtil.isUserAbleToDeleteGroup(
  179. getLoggedInUser(), groupName);
  180. }
  181. /**
  182. * Returns the empty string if the given group (name) is potentially deletable, otherwise returns the
  183. * reason why it's not. Note that this is a different test than {@link #isUserAbleToDeleteGroup(String)}.
  184. *
  185. * @return reason why group would not be deletable, otherwise the empty string.
  186. * @see GroupService#validateDelete
  187. */
  188. @Nonnull
  189. public String isGroupDeletable(@Nonnull String groupName) {
  190. JiraServiceContext currentRequest = getJiraServiceContext();
  191. ErrorCollection potentialDeleteErrors = new SimpleErrorCollection();
  192. JiraServiceContext potentialDelete = new JiraServiceContextImpl(
  193. currentRequest.getLoggedInApplicationUser(), potentialDeleteErrors, currentRequest.getI18nBean());
  194. // if group delete would fail, then passed ErrorCollection will be non-empty
  195. groupService.validateDelete(potentialDelete, groupName, null);
  196. if (potentialDeleteErrors.hasAnyErrors()) {
  197. // then delete is expected to fail.
  198. // special case: if there are comments/worklogs associated to this group, then validateDelete
  199. // will return false. however the group is still potentially deletable on the delete confirm
  200. // page via assignment to a swap group.
  201. if (groupService.getCommentsAndWorklogsGuardedByGroupCount(groupName) > 0) {
  202. // delete could still succeed
  203. return "";
  204. }
  205. return potentialDeleteErrors.getErrorMessages().iterator().next();
  206. } else {
  207. // then delete should succeed
  208. return "";
  209. }
  210. }
  211. /**
  212. * Return true if any directory supports nested groups.
  213. *
  214. * @return true if any directory supports nested groups.
  215. */
  216. public boolean isNestedGroupsEnabledForAnyDirectory() {
  217. for (Directory directory : crowdDirectoryService.findAllDirectories()) {
  218. if (crowdDirectoryService.supportsNestedGroups(directory.getId())) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. public List<GroupLabelView> getGroupLabels(Group group) {
  225. return groupLabels.getGroupLabels(group, Optional.<Long>empty());
  226. }
  227. }