View Javadoc

1   /*------------------------------------
2    * Kaamelot - 2006
3    * ------------------------------------
4    * @author Kaamelot
5    * Projet  : Myrddin
6    * Fichier : AAdminProjectManager.java
7    * Description :
8    * This Class implements and Abstracion of AdminProjectManager contracts.  
9    */
10  package com.atlassian.jira.project;
11  
12  import java.util.ArrayList;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Map;
17  
18  import org.apache.log4j.Category;
19  import org.ofbiz.core.entity.GenericEntityException;
20  import org.ofbiz.core.entity.GenericValue;
21  
22  import com.atlassian.core.user.GroupUtils;
23  import com.atlassian.core.user.UserUtils;
24  import com.atlassian.core.util.ClassLoaderUtils;
25  import com.atlassian.jira.ManagerFactory;
26  import com.atlassian.jira.event.user.UserEventDispatcher;
27  import com.atlassian.jira.event.user.UserEventType;
28  import com.atlassian.jira.exception.PermissionException;
29  import com.atlassian.jira.permission.PermissionSchemeManager;
30  import com.atlassian.jira.security.GlobalPermissionManager;
31  import com.atlassian.jira.security.PermissionManager;
32  import com.atlassian.jira.security.Permissions;
33  import com.atlassian.jira.user.util.UserUtil;
34  import com.atlassian.jira.util.JiraUtils;
35  import com.atlassian.jira.util.map.EasyMap;
36  import com.atlassian.plugin.PluginParseException;
37  import com.atlassian.plugin.web.Condition;
38  import com.opensymphony.user.DuplicateEntityException;
39  import com.opensymphony.user.EntityNotFoundException;
40  import com.opensymphony.user.Group;
41  import com.opensymphony.user.User;
42  
43  /**
44   * ------------------------------------ � Kaamelot - 2006 ------------------------------------
45   * 
46   * @author Kaamelot Projet : Myrddin Fichier : AAdminProjectManager.java Description : This Class provides information concerning Users and Groups for the Project Administration
47   */
48  public abstract class AAdminProjectManager implements AdminProjectManager {
49  
50  	private static final Category log = Category.getInstance(AAdminProjectManager.class);
51  
52  	protected AdminProjectManagerDirectory adminProjectManagerDirectory;
53  
54  	protected PermissionManager permissionManager;
55  
56  	protected PermissionSchemeManager permissionSchemeManager;
57  
58  	protected ProjectManager projectManager;
59  
60  	protected UserUtil userUtil;
61  
62  	protected Condition defaultActivationCondition;
63  
64  	/**
65  	 * @param permissionManager
66  	 */
67  	public AAdminProjectManager(final PermissionManager _permissionManager, final PermissionSchemeManager _permissionSchemeManager, final ProjectManager _projectManager, final UserUtil _userUtil,
68  				final AdminProjectManagerDirectory _adminProjectManagerDirectory) {
69  		this.permissionManager = _permissionManager;
70  		this.permissionSchemeManager = _permissionSchemeManager;
71  		this.projectManager = _projectManager;
72  		this.userUtil = _userUtil;
73  		this.adminProjectManagerDirectory = _adminProjectManagerDirectory;
74  		adminProjectManagerDirectory.addAdminProjectManager(this);
75  	}
76  
77  	public String getId() {
78  		return this.getClass().getName();
79  	}
80  
81  	public Condition getDefaultActivationCondition() {
82  		if (defaultActivationCondition == null) {
83  			String conditionFQCN = getDefaultActivationConditionFQCN();
84  			if (conditionFQCN != null) {
85  				try {
86  					Class clazz = ClassLoaderUtils.loadClass(conditionFQCN, this.getClass());
87  					if (Condition.class.isAssignableFrom(clazz)) {
88  						defaultActivationCondition = (Condition) JiraUtils.loadComponent(conditionFQCN, this.getClass());
89  					} else {
90  						log.warn("Condition [" + conditionFQCN + "] is not valid ...");
91  					}
92  				} catch (ClassNotFoundException e) {
93  					log.warn("Condition class [" + conditionFQCN + "] not found.", e);
94  				}
95  			}
96  			if (defaultActivationCondition == null) {
97  				log.warn("True Condition assumed ...");
98  				defaultActivationCondition = new Condition() {
99  					public void init(Map params) throws PluginParseException {
100 					}
101 
102 					public boolean shouldDisplay(Map context) {
103 						return true;
104 					}
105 				};
106 			}
107 		}
108 		return defaultActivationCondition;
109 	}
110 
111 	/**
112 	 * @param _project
113 	 *           Concerned Project
114 	 * @param _user
115 	 *           Concerned User
116 	 * @return A list of Rule given the permission to administrate the Project
117 	 */
118 	public Collection getAuthorizingGroups(final GenericValue _project, final User _user) {
119 		Collection list = new ArrayList();
120 		try {
121 			if (isAdmin(_user))
122 				list.add("jira-administrator");
123 			if (isProjectLead(_project, _user))
124 				list.add("project-lead");
125 			if (isProjectAdmin(_project, _user))
126 				list.add("project-administrator");
127 		} catch (Exception e) {
128 			e.printStackTrace();
129 		}
130 		return list;
131 	}
132 
133 	/**
134 	 * @param _project
135 	 *           Concerned Project
136 	 * @param _user
137 	 *           Concerned User
138 	 * @return A list of Rule given the permission to create User
139 	 */
140 	public Collection getCreateUserAuthorizingGroups(final GenericValue _project, final User _user) {
141 		Collection list = new ArrayList();
142 		try {
143 			if (isAdmin(_user))
144 				list.add("jira-administrator");
145 			if (isAllowToCreateUser(_user))
146 				list.add("user-creator");
147 		} catch (Exception e) {
148 			e.printStackTrace();
149 		}
150 		return list;
151 	}
152 
153 	/**
154 	 * @param _project
155 	 *           Concerned Project
156 	 * @param _params
157 	 *           List of Parameters required to defined the list of Groups
158 	 * @return List of Groups identified as authorized to access the Project
159 	 */
160 	public abstract Collection getAuthorizedGroups(final GenericValue _project, final Map _params);
161 
162 	/**
163 	 * @param _project
164 	 *           Concerned Project
165 	 * @param _params
166 	 *           List of Parameters required to defined the list of Groups
167 	 * @return List of Groups Manageable in Project by Current User
168 	 */
169 	public Collection getManageableGroups(final GenericValue _project, final Map _params, final User _user) {
170 		// GlobalPermissionManager gpm = ManagerFactory.getGlobalPermissionManager();
171 		Collection authorizedGroups = getAuthorizedGroups(_project, _params);
172 		Collection administerGroups = getAdministerGroups();
173 		List manageableGroups = new ArrayList();
174 		for (Iterator iter = authorizedGroups.iterator(); iter.hasNext();) {
175 			Group group = (Group) iter.next();
176 			// if (!gpm.hasPermission(Permissions.ADMINISTER, group.getName())) {
177 			if (!administerGroups.contains(group)) {
178 				manageableGroups.add(group);
179 			}
180 		}
181 		return manageableGroups;
182 	}
183 
184 	/**
185 	 */
186 	public Collection getWorkableGroups(GenericValue _project) {
187 		int permission = Permissions.WORK_ISSUE;
188 		Collection list = new ArrayList();
189 		List schemes;
190 		try {
191 			schemes = permissionSchemeManager.getSchemes(_project);
192 			for (Iterator si = schemes.iterator(); si.hasNext();) {
193 				GenericValue scheme = (GenericValue) si.next();
194 				List l = permissionSchemeManager.getEntities(scheme);
195 				for (int n = 0; n < l.size(); n++) {
196 					GenericValue schemePermission = (GenericValue) l.get(n);
197 					int pid = schemePermission.getLong("permission").intValue();
198 					if (pid == permission && schemePermission.getString("type").equals("group")) {
199 						String groupName = schemePermission.getString("parameter");
200 						list.add(GroupUtils.getGroup(groupName));
201 					}
202 				}
203 			}
204 		} catch (GenericEntityException e) {
205 			e.printStackTrace();
206 		} catch (Exception e) {
207 			e.printStackTrace();
208 		}
209 		return list;
210 	}
211 
212 	protected Collection getAdministerGroups() {
213 		GlobalPermissionManager gpm = ManagerFactory.getGlobalPermissionManager();
214 		return gpm.getGroups(Permissions.ADMINISTER);
215 	}
216 
217 	/**
218 	 * Evaluate if the current user can Administer Group for its project
219 	 * 
220 	 * @return True, if current has required Permission or Rule
221 	 * @throws Exception
222 	 */
223 	public boolean isHasManagerPermission(final Long _projectId, final User _user) {
224 		return isHasManagerPermission(projectManager.getProject(_projectId), _user);
225 	}
226 
227 	/**
228 	 * Evaluate if the current user can Administrate Group for its project
229 	 * 
230 	 * @return True, if current has required Permission or Rule
231 	 * @throws Exception
232 	 */
233 	public boolean isHasManagerPermission(final GenericValue _project, final User _user) {
234 		return isAdmin(_user) || isProjectLead(_project, _user) || isProjectAdmin(_project, _user);
235 	}
236 
237 	public boolean isHasUserCreatePermission(Long _projectId, User _user) {
238 		return isHasUserCreatePermission(projectManager.getProject(_projectId), _user);
239 	}
240 
241 	public boolean isHasUserCreatePermission(final GenericValue _project, final User _user) {
242 		return isAdmin(_user) || isAllowToCreateUser(_user);
243 	}
244 
245 	/**
246 	 * Evaluate if the current user is a Project Leader
247 	 * 
248 	 * @param _project
249 	 *           Concerned Project
250 	 * @param _user
251 	 *           Concerned User
252 	 * @return True, if current has the mentionned Rule
253 	 * @throws Exception
254 	 */
255 	public boolean isProjectLead(final GenericValue project, final User _user) {
256 		String lead = project.getString("lead");
257 		return lead != null && _user != null && lead.equals(_user.getName());
258 	}
259 
260 	/**
261 	 * Evaluate if the current user is a JIRA Administrateur
262 	 * 
263 	 * @param _project
264 	 *           Concerned Project
265 	 * @param _user
266 	 *           Concerned User
267 	 * @return True, if current has the mentionned Permission
268 	 * @throws Exception
269 	 */
270 	public boolean isAdmin(final User _user) {
271 		return permissionManager.hasPermission(Permissions.ADMINISTER, _user);
272 	}
273 
274 	/**
275 	 * Evaluate if the current user has the Permission of Administrate the Project
276 	 * 
277 	 * @param _project
278 	 *           Concerned Project
279 	 * @param _user
280 	 *           Concerned User
281 	 * @return True, if current has the mentionned Permission
282 	 * @throws Exception
283 	 */
284 	public boolean isProjectAdmin(final GenericValue _project, final User _user) {
285 		return permissionManager.hasPermission(Permissions.PROJECT_ADMIN, _project, _user);
286 	}
287 
288 	/**
289 	 * Evaluate if the current user has the Permission of Administrate the Project
290 	 * 
291 	 * @param _project
292 	 *           Concerned Project
293 	 * @param _user
294 	 *           Concerned User
295 	 * @return True, if current has the mentionned Permission
296 	 * @throws Exception
297 	 */
298 	public boolean isAllowToCreateUser(final User _user) {
299 		return permissionManager.hasPermission(Permissions.ADMINISTER, _user);
300 	}
301 
302 	/**
303 	 * No Filter implemented
304 	 */
305 	public String getFilteringGroupName() {
306 		// return "jira-users";
307 		return "";
308 	}
309 
310 	/** 
311 	 */
312 	public User createUser(Long _projectId, String _userName, String _fullName, String _email) throws PermissionException {
313 		User user = null;
314 		try {
315 			user = userUtil.createJiraUser(_userName, null, _email, _fullName);
316 			String password = UserUtils.resetPassword(user);
317 
318 			// Send always password if the user has not disabled when creating.
319 			dispatchEvent(user, EasyMap.build("username", _userName, "password.new", password, "email", _email, "fullname", _fullName));
320 		} catch (Exception e) {
321 			throw new PermissionException(e);
322 		}
323 		return user;
324 	}
325 
326 	protected void dispatchEvent(User user, Map args) {
327 		UserEventDispatcher.dispatchEvent(UserEventType.USER_CREATED, user, args);
328 	}
329 
330 	public void addUserToGroup(final Group _groupToAdd, final User _user) {
331 		try {
332 			userUtil.addUserToGroup(_groupToAdd, _user);
333 			// } catch (IndexException e) {
334 			// e.printStackTrace();
335 			// } catch (GenericEntityException e) {
336 			// e.printStackTrace();
337 		} catch (Exception e) {
338 			e.printStackTrace();
339 		}
340 	}
341 
342 	public User getUser(final String _user) throws EntityNotFoundException {
343 		return UserUtils.getUser(_user);
344 	}
345 
346 	public User addUserToGroup(final Long _projectId, final String _group, final String _user) throws EntityNotFoundException, PermissionException, DuplicateEntityException {
347 		User user;
348 		try {
349 			user = getUser(_user);
350 			Group groupToAdd = GroupUtils.getGroup(_group);
351 			if (groupToAdd.containsUser(user)) {
352 				throw new DuplicateEntityException("User already member ...");
353 			}
354 			addUserToGroup(groupToAdd, user);
355 		} catch (EntityNotFoundException enfe) {
356 			log.warn(enfe.getMessage());
357 			throw enfe;
358 		}
359 		return user;
360 	}
361 
362 	public void removeUserFromGroup(final Group _groupToRemove, final User _user) {
363 		try {
364 			userUtil.removeUserFromGroup(_groupToRemove, _user);
365 			// } catch (IndexException e) {
366 			// e.printStackTrace();
367 			// } catch (GenericEntityException e) {
368 			// e.printStackTrace();
369 		} catch (Exception e) {
370 			e.printStackTrace();
371 		}
372 	}
373 
374 	public void removeUserFromGroup(final Long _projectId, final String _group, final String _user) throws EntityNotFoundException, PermissionException {
375 		User user;
376 		try {
377 			user = UserUtils.getUser(_user);
378 			Group groupToRemove = GroupUtils.getGroup(_group);
379 			removeUserFromGroup(groupToRemove, user);
380 		} catch (EntityNotFoundException enfe) {
381 			log.warn(enfe.getMessage());
382 			throw enfe;
383 		}
384 	}
385 
386 }