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