001/*
002 * (C) Copyright 2006-2011 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
018 */
019
020package org.nuxeo.ecm.automation.core.operations.users;
021
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.operations.services.directory.AbstractDirectoryOperation;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.platform.usermanager.UserManager;
031
032/**
033 * Fetch a user from {@link UserManager} and return it as a {@link DocumentModel}. Using the DocumentModel rather that a
034 * POJO allow to also fetch custom properties.
035 *
036 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
037 * @since 5.7
038 */
039@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" })
040public class GetNuxeoPrincipal extends AbstractDirectoryOperation {
041
042    public static final String ID = "User.Get";
043
044    @Context
045    protected UserManager umgr;
046
047    @Param(name = "login", required = false)
048    protected String login;
049
050    @Context
051    protected OperationContext ctx;
052
053    @OperationMethod
054    public DocumentModel run() {
055
056        if (login == null || login.isEmpty()) {
057            return umgr.getUserModel(ctx.getPrincipal().getName());
058        } else {
059            validateCanManageDirectories(ctx);
060            return umgr.getUserModel(login);
061        }
062    }
063
064}