001package com.identityworksllc.iiq.common.auth;
002
003import sailpoint.api.SailPointContext;
004import sailpoint.object.Capability;
005import sailpoint.object.Identity;
006import sailpoint.plugin.PluginContext;
007import sailpoint.rest.plugin.BasePluginResource;
008import sailpoint.tools.GeneralException;
009import sailpoint.tools.Util;
010import sailpoint.web.UserContext;
011
012import java.util.ArrayList;
013import java.util.Collection;
014import java.util.List;
015
016/**
017 * Creates a fake plugin context for use outside of a plugin.
018 */
019public class DummyPluginResource extends BasePluginResource implements UserContext, PluginContext {
020    /**
021     * The context
022     */
023    private final SailPointContext context;
024
025    /**
026     * The Identity to use as the logged in user for this fake context
027     */
028    private final Identity loggedInUser;
029
030    /**
031     * The plugin name
032     */
033    private final String pluginName;
034
035    /**
036     * Constructs a new DummyPluginResource with the given auth data
037     *
038     * @param context The SailPointContext to return from {@link BasePluginResource#getContext()}
039     * @param loggedInUser The Identity to return from various getLoggedIn... methods
040     * @param pluginName The name of the plugin
041     */
042    public DummyPluginResource(SailPointContext context, Identity loggedInUser, String pluginName) {
043        this.context = context;
044        this.loggedInUser = loggedInUser;
045        this.pluginName = pluginName;
046        this.uriInfo = new DummyUriInfo();
047    }
048
049    /**
050     * @see BasePluginResource#getContext()
051     */
052    @Override
053    public SailPointContext getContext() {
054        return context;
055    }
056
057    /**
058     * @see BasePluginResource#getLoggedInUser()
059     */
060    @Override
061    public Identity getLoggedInUser() throws GeneralException {
062        return loggedInUser;
063    }
064
065    /**
066     * @see BasePluginResource#getLoggedInUserCapabilities()
067     */
068    @Override
069    public List<Capability> getLoggedInUserCapabilities() {
070        List<Capability> capabilities = new ArrayList<>();
071        try {
072            capabilities.addAll(getLoggedInUser().getCapabilityManager().getEffectiveCapabilities());
073        } catch(GeneralException e) {
074            /* Ignore this */
075        }
076        return capabilities;
077    }
078
079    /**
080     * @see BasePluginResource#getLoggedInUserName()
081     */
082    @Override
083    public String getLoggedInUserName() throws GeneralException {
084        return getLoggedInUser().getName();
085    }
086
087    /**
088     * @see BasePluginResource#getLoggedInUserRights()
089     */
090    @Override
091    public Collection<String> getLoggedInUserRights() {
092        List<String> rights = new ArrayList<>();
093        try {
094            rights.addAll(getLoggedInUser().getCapabilityManager().getEffectiveFlattenedRights());
095        } catch(GeneralException e) {
096            /* Ignore this */
097        }
098        return rights;
099    }
100
101    /**
102     * @see BasePluginResource#getPluginName()
103     */
104    @Override
105    public String getPluginName() {
106        return pluginName;
107    }
108
109    /**
110     * @see BasePluginResource#getSettingBool(String)
111     */
112    @Override
113    public boolean getSettingBool(String settingName) {
114        if (Util.isNotNullOrEmpty(getPluginName())) {
115            return super.getSettingBool(settingName);
116        } else {
117            return false;
118        }
119    }
120
121    /**
122     * @see BasePluginResource#getSettingInt(String)
123     */
124    @Override
125    public int getSettingInt(String settingName) {
126        if (Util.isNotNullOrEmpty(getPluginName())) {
127            return super.getSettingInt(settingName);
128        } else {
129            return 0;
130        }
131    }
132
133    /**
134     * @see BasePluginResource#getSettingString(String)
135     */
136    @Override
137    public String getSettingString(String settingName) {
138        if (Util.isNotNullOrEmpty(getPluginName())) {
139            return super.getSettingString(settingName);
140        } else {
141            return null;
142        }
143    }
144}