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