001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.login.deputy.management.web; 023 024import static org.jboss.seam.ScopeType.*; 025 026import java.io.IOException; 027import java.io.Serializable; 028import java.security.Principal; 029import java.util.ArrayList; 030import java.util.List; 031import java.util.Map; 032 033import javax.faces.application.FacesMessage; 034import javax.faces.context.ExternalContext; 035import javax.faces.context.FacesContext; 036import javax.servlet.ServletException; 037import javax.servlet.http.HttpServletRequest; 038import javax.servlet.http.HttpServletResponse; 039 040import org.jboss.seam.ScopeType; 041import org.jboss.seam.annotations.Factory; 042import org.jboss.seam.annotations.In; 043import org.jboss.seam.annotations.Name; 044import org.jboss.seam.annotations.Scope; 045import org.nuxeo.ecm.core.api.DocumentModel; 046import org.nuxeo.ecm.core.api.NuxeoPrincipal; 047import org.nuxeo.ecm.platform.login.deputy.management.DeputyManager; 048import org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants; 049import org.nuxeo.ecm.platform.usermanager.UserManager; 050 051@Name("deputyActions") 052@Scope(ScopeType.CONVERSATION) 053public class DeputyActionsBean implements Serializable { 054 055 private static final long serialVersionUID = 23167576454986L; 056 057 public static final String VIEW_DEPUTIES = "view_deputies"; 058 059 public static final String NEW_DEPUTY_ID = "new-deputy-id"; 060 061 @In(create = true, required = false) 062 private transient DeputyManager deputyManager; 063 064 @In(create = true) 065 private transient UserManager userManager; 066 067 @In 068 private transient Principal currentUser; 069 070 @In(create = true) 071 private transient Map<String, String> messages; 072 073 // Forms parameters 074 075 protected String adminLogin; 076 077 // Back-end Model 078 079 protected DocumentModel editableDeputy; 080 081 public String createDeputy() { 082 editableDeputy = deputyManager.newMandate(currentUser.getName(), null); 083 return VIEW_DEPUTIES; 084 } 085 086 public String setNewDeputy(String deputyId) { 087 String schemaName = deputyManager.getDeputySchemaName(); 088 editableDeputy.setProperty(schemaName, "deputy", deputyId); 089 return VIEW_DEPUTIES; 090 } 091 092 public String cancelDeputy() { 093 editableDeputy = null; 094 return VIEW_DEPUTIES; 095 } 096 097 public String saveDeputy() { 098 if (editableDeputy == null) { 099 return null; 100 } 101 102 deputyManager.addMandate(editableDeputy); 103 104 editableDeputy = null; 105 106 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, messages.get("message.deputy.created"), 107 messages.get("message.deputy.created")); 108 FacesContext.getCurrentInstance().addMessage(null, message); 109 110 return VIEW_DEPUTIES; 111 } 112 113 public String editDeputy(String deputyId) { 114 if (deputyId == null) { 115 return null; 116 } 117 118 editableDeputy = null; 119 120 List<DocumentModel> deputies = getUserDeputies(); 121 122 String schemaName = deputyManager.getDeputySchemaName(); 123 124 for (DocumentModel deputy : deputies) { 125 if (deputyId.equals((deputy.getProperty(schemaName, "deputy")))) { 126 editableDeputy = deputy; 127 break; 128 } 129 } 130 131 if (editableDeputy != null) { 132 return VIEW_DEPUTIES; 133 } else { 134 return null; 135 } 136 } 137 138 public String deleteDeputy(String deputyId) { 139 deputyManager.removeMandate(currentUser.getName(), deputyId); 140 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, messages.get("message.deputy.deleted"), 141 messages.get("message.deputy.deleted")); 142 FacesContext.getCurrentInstance().addMessage(null, message); 143 144 return VIEW_DEPUTIES; 145 } 146 147 @Factory(value = "userDeputies", scope = EVENT) 148 public List<DocumentModel> getUserDeputies() { 149 return deputyManager.getAvalaibleMandates(currentUser.getName()); 150 } 151 152 @Factory(value = "alternateLogins", scope = EVENT) 153 public List<Principal> getAlternatePrincipals() { 154 List<Principal> result = new ArrayList<Principal>(); 155 List<String> logins = deputyManager.getPossiblesAlternateLogins(currentUser.getName()); 156 157 for (String login : logins) { 158 Principal alternatePrincipal = userManager.getPrincipal(login); 159 if (alternatePrincipal != null) { 160 result.add(alternatePrincipal); 161 } 162 } 163 164 return result; 165 } 166 167 public String loginAsDeputy(String login) throws IOException, ServletException { 168 NuxeoPrincipal nxUser = (NuxeoPrincipal) currentUser; 169 170 if ((!nxUser.isAdministrator()) 171 && (!deputyManager.getPossiblesAlternateLogins(currentUser.getName()).contains(login))) { 172 return null; 173 } 174 175 if (userManager.getPrincipal(login) == null) { 176 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, 177 messages.get("message.deputy.nonExistingUser"), messages.get("message.deputy.nonExistingUser")); 178 FacesContext.getCurrentInstance().addMessage(null, message); 179 return null; 180 } 181 182 reconnectAs(login); 183 184 return null; 185 } 186 187 protected void reconnectAs(String login) throws ServletException, IOException { 188 FacesContext context = FacesContext.getCurrentInstance(); 189 ExternalContext eContext = context.getExternalContext(); 190 Object req = eContext.getRequest(); 191 Object resp = eContext.getResponse(); 192 HttpServletRequest request = null; 193 HttpServletResponse response = null; 194 if (req instanceof HttpServletRequest) { 195 request = (HttpServletRequest) req; 196 } 197 if (resp instanceof HttpServletResponse) { 198 response = (HttpServletResponse) resp; 199 } 200 201 if ((response != null) && (request != null) && !context.getResponseComplete()) { 202 String targetURL = "/" + NXAuthConstants.SWITCH_USER_PAGE; 203 204 request.setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, true); 205 if (login != null) { 206 request.setAttribute(NXAuthConstants.SWITCH_USER_KEY, login); 207 } 208 209 request.getRequestDispatcher(targetURL).forward(request, response); 210 context.responseComplete(); 211 } 212 } 213 214 public String loginAsOriginal() throws ServletException, IOException { 215 reconnectAs(null); 216 return null; 217 } 218 219 public boolean isMandated() { 220 NuxeoPrincipal nxUser = (NuxeoPrincipal) currentUser; 221 if (nxUser == null) { 222 return false; 223 } 224 if (nxUser.getOriginatingUser() != null) { 225 return true; 226 } 227 return false; 228 } 229 230 @Factory(value = "editableDeputy", scope = EVENT) 231 public DocumentModel getEditableDeputy() { 232 return editableDeputy; 233 } 234 235 public String getLoginInformation() { 236 NuxeoPrincipal nxUser = (NuxeoPrincipal) currentUser; 237 if (nxUser == null) { 238 return ""; 239 } 240 241 String originalUser = nxUser.getOriginatingUser(); 242 243 if (originalUser != null) { 244 return nxUser.getName() + " " + messages.get("label.deputed.by") + " " + originalUser; 245 } else { 246 return nxUser.getName(); 247 } 248 } 249 250 public void setAdminLogin(String adminLogin) { 251 this.adminLogin = adminLogin; 252 } 253 254 public String getAdminLogin() { 255 return adminLogin; 256 } 257 258 public String adminLoginAsDeputy() throws IOException, ServletException { 259 260 if (adminLogin == null) { 261 return null; 262 } 263 264 NuxeoPrincipal nxUser = (NuxeoPrincipal) currentUser; 265 if (!nxUser.isAdministrator()) { 266 return null; 267 } 268 269 if (userManager.getPrincipal(adminLogin) == null) { 270 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, 271 messages.get("message.deputy.nonExistingUser"), messages.get("message.deputy.nonExistingUser")); 272 FacesContext.getCurrentInstance().addMessage(null, message); 273 274 return null; 275 } 276 277 reconnectAs(adminLogin); 278 279 return null; 280 } 281 282}