001package com.identityworksllc.iiq.common.plugin;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import sailpoint.api.SailPointContext;
006import sailpoint.object.Capability;
007import sailpoint.object.Identity;
008import sailpoint.object.Plugin;
009import sailpoint.rest.plugin.BasePluginResource;
010import sailpoint.tools.GeneralException;
011
012import javax.servlet.http.HttpServletRequest;
013import javax.servlet.http.HttpSession;
014import java.util.ArrayList;
015import java.util.Collection;
016import java.util.List;
017import java.util.Locale;
018import java.util.TimeZone;
019
020/**
021 * A mock plugin resource to use in any situation where an API requires one
022 * but we're not running in a plugin context. This must be associated with a
023 * real plugin (by name).
024 */
025@SuppressWarnings("unused")
026public class MockPluginContext extends BasePluginResource {
027    private static final Log log = LogFactory.getLog(MockPluginContext.class);
028
029    private final SailPointContext context;
030    private final Identity loggedInUser;
031    private final String pluginName;
032
033    /**
034     * Constructor for the mock plugin context
035     *
036     * @param context      The Sailpoint context to use
037     * @param loggedInUser The logged in user to simulate
038     * @param pluginName   The plugin name, which must be real
039     * @throws GeneralException if any failures occur finding the plugin
040     */
041    public MockPluginContext(SailPointContext context, Identity loggedInUser, String pluginName) throws GeneralException {
042        this.context = context;
043        this.loggedInUser = loggedInUser;
044        this.pluginName = pluginName;
045
046        super.setPreAuth(true);
047
048        Plugin plugin = context.getObject(Plugin.class, pluginName);
049        if (plugin == null) {
050            log.warn("The plugin name specified, '" + pluginName + "', is not a valid plugin. The 'getSetting' methods will not work properly.");
051        }
052    }
053
054    /**
055     * Returns the context previously passed to the contructor
056     *
057     * @return The context
058     */
059    @Override
060    public SailPointContext getContext() {
061        return context;
062    }
063
064    @Override
065    protected String[] getCredentials() {
066        return new String[]{getLoggedInUser().getName(), getLoggedInUser().getPassword()};
067    }
068
069    /**
070     * Returns the default system Locale
071     *
072     * @return The default system locale
073     */
074    @Override
075    public Locale getLocale() {
076        return Locale.getDefault();
077    }
078
079    @Override
080    public Identity getLoggedInUser() {
081        return loggedInUser;
082    }
083
084    @Override
085    public List<Capability> getLoggedInUserCapabilities() {
086        return new ArrayList<>(getLoggedInUser().getCapabilityManager().getEffectiveCapabilities());
087    }
088
089    @Override
090    public String getLoggedInUserName() {
091        return getLoggedInUser().getName();
092    }
093
094    @Override
095    public Collection<String> getLoggedInUserRights() {
096        return new ArrayList<>(getLoggedInUser().getCapabilityManager().getEffectiveFlattenedRights());
097    }
098
099    /**
100     * Returns the name of the real plugin we are simulating
101     *
102     * @return The name of the plugin provided in the constructor
103     */
104    @Override
105    public String getPluginName() {
106        return pluginName;
107    }
108
109    /**
110     * Not implemented
111     *
112     * @throws UnsupportedOperationException on any invocation
113     */
114    @Override
115    public HttpServletRequest getRequest() {
116        throw new UnsupportedOperationException();
117    }
118
119    /**
120     * Not implemented
121     *
122     * @throws UnsupportedOperationException on any invocation
123     */
124    @Override
125    public HttpSession getSession() {
126        throw new UnsupportedOperationException();
127    }
128
129    /**
130     * Returns the default system time zone
131     *
132     * @return The default system time zone
133     */
134    @Override
135    public TimeZone getUserTimeZone() {
136        return TimeZone.getDefault();
137    }
138}