View Javadoc

1   package com.atlassian.jira.plugins.projectpanel.admin;
2   
3   import java.util.Collection;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.apache.log4j.Category;
8   import org.ofbiz.core.entity.GenericValue;
9   
10  import webwork.action.ServletActionContext;
11  
12  import com.atlassian.core.util.ClassLoaderUtils;
13  import com.atlassian.jira.plugin.projectpanel.ProjectTabPanelModuleDescriptor;
14  import com.atlassian.jira.plugin.projectpanel.impl.AbstractProjectTabPanel;
15  import com.atlassian.jira.plugin.webfragment.JiraWebInterfaceManager;
16  import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
17  import com.atlassian.jira.plugin.workflow.JiraWorkflowPluginConstants;
18  import com.atlassian.jira.project.AdminProjectManager;
19  import com.atlassian.jira.project.AdminProjectManagerDirectory;
20  import com.atlassian.jira.project.Project;
21  import com.atlassian.jira.security.JiraAuthenticationContext;
22  import com.atlassian.jira.util.JiraUtils;
23  import com.atlassian.jira.web.action.ProjectActionSupport;
24  import com.atlassian.jira.web.action.browser.Browser;
25  import com.atlassian.jira.web.bean.FieldVisibilityBean;
26  import com.atlassian.plugin.PluginParseException;
27  import com.atlassian.plugin.elements.ResourceLocation;
28  import com.atlassian.plugin.web.Condition;
29  import com.opensymphony.user.User;
30  import com.opensymphony.user.UserManager;
31  
32  /**
33   * FQCN : com.atlassian.jira.plugins.projectpanel.admin.ProjectGroupAdminTabPanel
34   * @author Kaamelot - 2006
35   * @since
36   * Description : Provide a Project Tab Panel dedicated to Group Management
37   *
38   */
39  public class ProjectGroupAdminTabPanel extends AbstractProjectTabPanel {
40  
41  	private static final Category log = Category.getInstance(ProjectGroupAdminTabPanel.class);
42  
43  	protected static final String ADMINPROJECTMANAGER_PARAM_KEY = "adminProjectManager";
44  	protected static final String CONDITION_PARAM_KEY = "condition";
45  	
46  	protected static final String DEFAULT_VIEW = "view";
47  	protected static final String NEW_USER_VIEW = "viewNewUser";
48  	
49  	private JiraAuthenticationContext authenticationContext;
50  	protected AdminProjectManager adminProjectManager;
51  	protected AdminProjectManagerDirectory adminProjectManagerDirectory;
52  	protected UserManager userManager;
53  	
54  	private Browser browser;
55  
56  	public ProjectGroupAdminTabPanel(JiraAuthenticationContext authenticationContext, AdminProjectManager adminProjectManager, UserManager userManager, AdminProjectManagerDirectory _adminProjectManagerDirectory) {
57  		this.authenticationContext = authenticationContext;
58  		this.adminProjectManagerDirectory = _adminProjectManagerDirectory;
59  		this.adminProjectManager = adminProjectManager;
60  		this.userManager = userManager;
61  	}
62  	
63  	public void init(ProjectTabPanelModuleDescriptor descriptor) {
64  		super.init(descriptor);
65  		AdminProjectManager apm = adminProjectManagerDirectory.getAdminProjectManager(getAdminProjectManagerKey());
66  		if (apm!=null) {
67  			this.adminProjectManager = apm; 
68  		}		
69  	}
70  	
71  	protected ProjectTabPanelModuleDescriptor getDescriptor() {
72  		return descriptor;
73  	}
74  	
75  	/**
76  	 * @return  AdminProjectManager Key to use to retrieve AdminProjectManager from AdminProjectManagerDirectory  
77  	 */	
78  	protected String getAdminProjectManagerKey() {
79  		return (String) getDescriptor().getParams().get(ADMINPROJECTMANAGER_PARAM_KEY);
80  	}
81  
82  	/**
83  	 * @return  AdminProjectManager Key to use to retrieve AdminProjectManager from AdminProjectManagerDirectory  
84  	 */	
85  	protected Condition getCondition() {
86  		Condition condition = null;
87  		String conditionFQCN = (String) getDescriptor().getParams().get(CONDITION_PARAM_KEY);
88  		if (conditionFQCN != null) {
89  			try {
90  				Class clazz = ClassLoaderUtils.loadClass(conditionFQCN, this.getClass());
91  				if (Condition.class.isAssignableFrom(clazz)) {
92  					condition = (Condition) JiraUtils.loadComponent(conditionFQCN, this.getClass());
93  //					condition = (Condition)ComponentManager.getInstance().getContainer().getComponentInstance(clazz);
94  				} else {
95  					log.warn("Condition [" + conditionFQCN + "] is not valid ...");
96  				}
97  			} catch (ClassNotFoundException e) {
98  				log.warn("Condition class [" + conditionFQCN + "] not found.", e);
99  			}
100 		}
101 		if (condition == null) {
102 			log.warn("True Condition assumed ...");
103 			condition = new Condition(){
104 				public void init(Map params) throws PluginParseException {
105 				}
106 				public boolean shouldDisplay(Map context) {
107 					return true;
108 				}
109 			};
110 		}
111 		return condition;
112 	}
113 
114 	public String getHtml(Browser browser) {
115 		this.browser = browser;
116 		Map velocityParams = getVelocityParameters();
117 		try {
118 			velocityParams.put("browser", browser);
119 		} catch (Exception e) {
120 			e.printStackTrace();
121 		}
122 		return descriptor.getHtml("view", velocityParams);
123 	}
124 
125 	/**
126 	 * @param _project
127 	 * @return Collection of Groups providing the ability to manage current Project Group Members 
128 	 */
129 	protected Collection getAuthorizingGroups(final GenericValue _project) {
130 		return adminProjectManager.getAuthorizingGroups(_project, authenticationContext.getUser());
131 	}
132 	/**
133 	 * @param _project
134 	 * @return Collection of Groups providing the ability to manage current Project Group Members 
135 	 */
136 	protected Collection getCreateUserAuthorizingGroups(final GenericValue _project) {
137 		return adminProjectManager.getCreateUserAuthorizingGroups(_project, authenticationContext.getUser());
138 	}
139 
140 	/**
141 	 * @return List of Groups identified in Project associated PermissionScheme
142 	 * and having _permission Permissions 
143 	 */
144 	protected Collection getAuthorizedGroups(final GenericValue _project, final Map _params) {
145 		return adminProjectManager.getAuthorizedGroups(_project, _params);
146 	}
147 
148 	/**
149 	 * @return List of Groups identified in Project associated PermissionScheme
150 	 * and having _permission Permissions 
151 	 */
152 	protected Collection getManageableGroups(final GenericValue _project, final Map _params) {
153 		return adminProjectManager.getManageableGroups(_project, _params, authenticationContext.getUser());
154 	}
155 
156 	/**
157 	 * @return Map of Parameters required to identified Manageable Group 
158 	 */
159 	public Map getManageableGroupParameters() {
160 		return adminProjectManager.getManageableGroupParameters();
161 	}
162 
163 	/**
164 	 * @return Filtering Group to user for UserPicker selection 
165 	 */
166 	public String getFilteringGroupName() {
167 		return adminProjectManager.getFilteringGroupName();
168 	}
169 
170 	public Map getVelocityParameters() {
171 		Map velocityParams = new HashMap();
172 		try {
173 			Project project = browser.getProjectObject();
174 			GenericValue gvProject = project.getGenericValue();
175 			adminProjectManagerDirectory.setAdminProjectManagerByProject(project.getId(), adminProjectManager);
176 			velocityParams.put("userManager", userManager);
177 			velocityParams.put("fieldVisibility", new FieldVisibilityBean());
178 			velocityParams.put("i18n", descriptor.getI18nBean());
179 			velocityParams.put("manageGroupDueTo", getAuthorizingGroups(gvProject));
180 			velocityParams.put("createUserDueTo", getCreateUserAuthorizingGroups(gvProject));			
181 			velocityParams.put("manageableGroups", getManageableGroups(gvProject,getManageableGroupParameters()));
182 			velocityParams.put("authorizedGroups", getAuthorizedGroups(gvProject,getManageableGroupParameters()));
183 			velocityParams.put("filteringGroup", getFilteringGroupName());
184 			
185 			if (adminProjectManager.isHasUserCreatePermission(project.getId(), authenticationContext.getUser())) {
186 				ResourceLocation rl = getDescriptor().getResourceLocation(JiraWorkflowPluginConstants.RESOURCE_TYPE_VELOCITY, NEW_USER_VIEW);
187 				if (rl!=null) {
188 					velocityParams.put(NEW_USER_VIEW, rl.getLocation() );
189 				}
190 			}
191 		} catch (Exception e) {
192 			e.printStackTrace();
193 		}
194 		return velocityParams;
195 	}
196 
197 	public boolean showPanel(ProjectActionSupport action, GenericValue _project) {
198 		boolean showPanel = true;
199 		User user = authenticationContext.getUser();
200 		Condition condition = getCondition(); 
201 		if (condition != null) {
202 			Map context = new HashMap();
203 			JiraHelper jh = new JiraHelper(ServletActionContext.getRequest(),_project);
204 			context.put(JiraWebInterfaceManager.CONTEXT_KEY_USER, user);
205 			context.put(JiraWebInterfaceManager.CONTEXT_KEY_HELPER, jh);
206 			showPanel = condition.shouldDisplay(context);
207 		}
208 		if (showPanel) {
209 			showPanel = adminProjectManager.isHasManagerPermission(_project, user);
210 		}
211 		return showPanel;
212 	}
213 
214 }