001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.login;
013
014import org.nuxeo.ecm.automation.OperationContext;
015import org.nuxeo.ecm.automation.OperationException;
016import org.nuxeo.ecm.automation.core.Constants;
017import org.nuxeo.ecm.automation.core.annotations.Context;
018import org.nuxeo.ecm.automation.core.annotations.Operation;
019import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
020import org.nuxeo.ecm.core.api.DocumentModel;
021
022/**
023 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
024 */
025@Operation(id = Logout.ID, category = Constants.CAT_USERS_GROUPS, label = "Logout", description = "Perform a logout. This should be used only after using the Login As operation to restore original login. This is a void operations - the input will be returned back as the output.")
026public class Logout {
027
028    public static final String ID = "Auth.Logout";
029
030    @Context
031    protected OperationContext ctx;
032
033    @OperationMethod
034    public void run() throws OperationException {
035        ctx.getLoginStack().pop();
036    }
037
038    @OperationMethod
039    public DocumentModel run(DocumentModel doc) throws OperationException {
040        run();
041        // refetch the input document if any using the new session
042        // otherwise using document methods that are delegating the call to the
043        // session that created the document will call the old session.
044        return ctx.getCoreSession().getDocument(doc.getRef());
045    }
046
047}