View Javadoc

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   * @FQCN com.atlassian.jira.web.actions.ProjectAdministrate
20   * @description AJAX Actions for AdminProjectTabPanel  
21   * @author kaamelot
22   * @since 2006
23   * @version 3.x.1.34 
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  	 * @param _projectId
53  	 * @param _userName
54  	 * @param _fullName
55  	 * @param _email
56  	 * @throws EntityNotFoundException
57  	 * @throws PermissionException
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()) ) { // && apm.getManageableGroups(_projectId, new HashMap(), 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  	 * Evaluate if the current user can Administrate Group for its project
91  	 * 
92  	 * @return True, if current has required Permission or Rule
93  	 * @throws Exception
94  	 */
95  	protected boolean isHasUserCreatePermission(Long _projectId, final User _user) {
96  		return getAdminProjectManager(_projectId).isHasUserCreatePermission(_projectId, _user);
97  	}
98  
99  	/**
100 	 * Evaluate if the current user can Administrate Group for its project
101 	 * 
102 	 * @return True, if current has required Permission or Rule
103 	 * @throws Exception
104 	 */
105 	protected boolean isHasManagerPermission(Long _projectId, final User _user) {
106 		return getAdminProjectManager(_projectId).isHasManagerPermission(_projectId, _user);
107 	}
108 
109 	/**
110 	 * @param _projectId Project Id. 
111 	 * @return AdminProjectManager in charge of Administration for passes project
112 	 */
113 	public AdminProjectManager getAdminProjectManager(Long _projectId) {
114 		if (adminProjectManager==null) {
115 			adminProjectManager = adminProjectManagerDirectory.getAdminProjectManagerByProject(_projectId); 
116 		}
117 		return adminProjectManager;
118 	}
119 
120 }