1 package com.atlassian.jira.web.actions;
2
3 import javax.servlet.http.HttpServletRequest;
4
5 import org.apache.log4j.Category;
6
7 import uk.ltd.getahead.dwr.WebContextFactory;
8
9 import com.atlassian.jira.exception.PermissionException;
10 import com.atlassian.jira.project.AdminProjectManager;
11 import com.atlassian.jira.project.AdminProjectManagerDirectory;
12 import com.atlassian.jira.user.util.UserUtil;
13 import com.atlassian.seraph.auth.DefaultAuthenticator;
14 import com.opensymphony.user.DuplicateEntityException;
15 import com.opensymphony.user.EntityNotFoundException;
16 import com.opensymphony.user.User;
17
18
19
20
21
22
23
24
25 public class ProjectAdministrate {
26
27 private static final Category log = Category.getInstance(ProjectAdministrate.class);
28
29 protected UserUtil userUtil;
30 protected AdminProjectManagerDirectory adminProjectManagerDirectory;
31 protected AdminProjectManager adminProjectManager;
32
33 private User remoteUser = null;
34
35 public ProjectAdministrate(UserUtil userUtil, AdminProjectManagerDirectory adminProjectManagerDirectory) {
36 this.userUtil = userUtil;
37 this.adminProjectManagerDirectory = adminProjectManagerDirectory;
38 }
39
40 protected HttpServletRequest getRequest() {
41 return WebContextFactory.get().getHttpServletRequest();
42 }
43
44 protected User getRemoteUser() {
45 if (remoteUser == null) {
46 remoteUser = (User) getRequest().getSession().getAttribute(DefaultAuthenticator.LOGGED_IN_KEY);
47 }
48 return remoteUser;
49 }
50
51
52
53
54
55
56
57
58
59 public User createUser(final Long _projectId, final String _userName, final String _fullName, final String _email) throws EntityNotFoundException, PermissionException {
60 User user = null;
61 if (isHasUserCreatePermission(_projectId, getRemoteUser())) {
62 user = getAdminProjectManager(_projectId).createUser(_projectId, _userName, _fullName, _email);
63 } else {
64 throw new PermissionException("[" + getRemoteUser() + "] not authorized to administrate Group's Project");
65 }
66 return user;
67 }
68
69 public User addUserToGroup(final Long _projectId, final String _group, final String _user) throws EntityNotFoundException, PermissionException, DuplicateEntityException {
70 User user;
71 AdminProjectManager apm = getAdminProjectManager(_projectId);
72 if (isHasManagerPermission(_projectId, getRemoteUser()) ) {
73 user = apm.addUserToGroup(_projectId, _group, _user);
74 log.warn("User " + user.getFullName() + " [" + user.getName() + "] added as member of [" + _group + "] successfully ...");
75 } else {
76 throw new PermissionException("[" + getRemoteUser() + "] not authorized to administrate Group's Project");
77 }
78 return user;
79 }
80
81 public void removeUserFromGroup(final Long _projectId, final String _group, final String _user) throws EntityNotFoundException, PermissionException {
82 if (isHasManagerPermission(_projectId, getRemoteUser())) {
83 getAdminProjectManager(_projectId).removeUserFromGroup(_projectId, _group, _user);
84 } else {
85 throw new PermissionException("[" + getRemoteUser() + "] not authorized to administrate Group's Project");
86 }
87 }
88
89
90
91
92
93
94
95 protected boolean isHasUserCreatePermission(Long _projectId, final User _user) {
96 return getAdminProjectManager(_projectId).isHasUserCreatePermission(_projectId, _user);
97 }
98
99
100
101
102
103
104
105 protected boolean isHasManagerPermission(Long _projectId, final User _user) {
106 return getAdminProjectManager(_projectId).isHasManagerPermission(_projectId, _user);
107 }
108
109
110
111
112
113 public AdminProjectManager getAdminProjectManager(Long _projectId) {
114 if (adminProjectManager==null) {
115 adminProjectManager = adminProjectManagerDirectory.getAdminProjectManagerByProject(_projectId);
116 }
117 return adminProjectManager;
118 }
119
120 }