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 your code is not running in a plugin context. This must be associated with a
023 * real plugin (by name).
024 *
025 * In particular, {@link com.identityworksllc.iiq.common.ThingAccessUtils} requires a
026 * plugin context, which this can provide.
027 *
028 * @deprecated Use {@link com.identityworksllc.iiq.common.auth.DummyPluginResource}
029 */
030@Deprecated
031public class MockPluginContext extends BasePluginResource {
032    private static final Log log = LogFactory.getLog(MockPluginContext.class);
033
034    private final SailPointContext context;
035    private final Identity loggedInUser;
036    private final String pluginName;
037
038    /**
039     * Constructor for the mock plugin context
040     *
041     * @param context      The Sailpoint context to use
042     * @param loggedInUser The logged in user to simulate
043     * @param pluginName   The plugin name, which must be real
044     * @throws GeneralException if any failures occur finding the plugin
045     */
046    public MockPluginContext(SailPointContext context, Identity loggedInUser, String pluginName) throws GeneralException {
047        this.context = context;
048        this.loggedInUser = loggedInUser;
049        this.pluginName = pluginName;
050
051        super.setPreAuth(true);
052
053        Plugin plugin = context.getObject(Plugin.class, pluginName);
054        if (plugin == null) {
055            log.warn("The plugin name specified, '" + pluginName + "', is not a valid plugin. The 'getSetting' methods will not work properly.");
056        }
057    }
058
059    /**
060     * Returns the context previously passed to the contructor
061     *
062     * @return The context
063     */
064    @Override
065    public SailPointContext getContext() {
066        return context;
067    }
068
069    @Override
070    protected String[] getCredentials() {
071        return new String[]{getLoggedInUser().getName(), getLoggedInUser().getPassword()};
072    }
073
074    /**
075     * Returns the default system Locale
076     *
077     * @return The default system locale
078     */
079    @Override
080    public Locale getLocale() {
081        return Locale.getDefault();
082    }
083
084    @Override
085    public Identity getLoggedInUser() {
086        return loggedInUser;
087    }
088
089    @Override
090    public List<Capability> getLoggedInUserCapabilities() {
091        return new ArrayList<>(getLoggedInUser().getCapabilityManager().getEffectiveCapabilities());
092    }
093
094    @Override
095    public String getLoggedInUserName() {
096        return getLoggedInUser().getName();
097    }
098
099    @Override
100    public Collection<String> getLoggedInUserRights() {
101        return new ArrayList<>(getLoggedInUser().getCapabilityManager().getEffectiveFlattenedRights());
102    }
103
104    /**
105     * Returns the name of the real plugin we are simulating
106     *
107     * @return The name of the plugin provided in the constructor
108     */
109    @Override
110    public String getPluginName() {
111        return pluginName;
112    }
113
114    /**
115     * Not implemented
116     *
117     * @throws UnsupportedOperationException on any invocation
118     */
119    @Override
120    public HttpServletRequest getRequest() {
121        throw new UnsupportedOperationException();
122    }
123
124    /**
125     * Not implemented
126     *
127     * @throws UnsupportedOperationException on any invocation
128     */
129    @Override
130    public HttpSession getSession() {
131        throw new UnsupportedOperationException();
132    }
133
134    /**
135     * Returns the default system time zone
136     *
137     * @return The default system time zone
138     */
139    @Override
140    public TimeZone getUserTimeZone() {
141        return TimeZone.getDefault();
142    }
143}