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 *     Nuxeo
011 */
012
013package org.nuxeo.ecm.automation.core.operations.users;
014
015import org.nuxeo.ecm.automation.OperationContext;
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.automation.core.annotations.Param;
021import org.nuxeo.ecm.automation.core.operations.services.directory.AbstractDirectoryOperation;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.platform.usermanager.UserManager;
024
025/**
026 * Fetch a user from {@link UserManager} and return it as a {@link DocumentModel}. Using the DocumentModel rather that a
027 * POJO allow to also fetch custom properties.
028 *
029 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
030 * @since 5.7
031 */
032@Operation(id = GetNuxeoPrincipal.ID, category = Constants.CAT_USERS_GROUPS, label = "Get Nuxeo Principal", description = "Retrieve Nuxeo principal and export it as a DocumentModel, if login parameter is not set the Operation will return informations about the current user, otherwise Directory Administration rights are required.", aliases = { "NuxeoPrincipal.Get" })
033public class GetNuxeoPrincipal extends AbstractDirectoryOperation {
034
035    public static final String ID = "User.Get";
036
037    @Context
038    protected UserManager umgr;
039
040    @Param(name = "login", required = false)
041    protected String login;
042
043    @Context
044    protected OperationContext ctx;
045
046    @OperationMethod
047    public DocumentModel run() {
048
049        if (login == null || login.isEmpty()) {
050            return umgr.getUserModel(ctx.getPrincipal().getName());
051        } else {
052            validateCanManageDirectories(ctx);
053            return umgr.getUserModel(login);
054        }
055    }
056
057}