Skip to content

Commit

Permalink
SAK-46016 statistics reveals the existence of stealthed tools (sakaip…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjones86 authored Aug 18, 2021
1 parent ba984ee commit f529ff8
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1402,4 +1402,17 @@ public enum SiteTitleValidationStatus {
* @return {@link SiteTitleValidationStatus}
*/
public SiteTitleValidationStatus validateSiteTitle(String orig, String stripped);

/**
* Determine if a stealthed tool is present in the given site.
* NOTE: can't just do "currentSite.getTool(toolID) != null" because getTool() will not
* report on stealthed tools; so we have to do it the long way.
* NOTE: you can technically use this method with non-stealthed tool IDs, but in that case
* it may be easier to just call "currentSite.getTool(toolID) != null"
*
* @param site the current site in question
* @param toolID the ID of the stealthed tool to check for in the current site
* @return true if the stealthed tool is present in the given site; false otherwise
*/
public boolean isStealthedToolPresent(Site site, String toolID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,15 @@ public static SiteTitleValidationStatus validateSiteTitle(String orig, String st

return service.validateSiteTitle(orig, stripped);
}

/**
* {@inheritDoc}
*/
public static boolean isStealthedToolPresent(Site site, String toolID)
{
org.sakaiproject.site.api.SiteService service = getInstance();
if (service == null) { return false; }

return service.isStealthedToolPresent(site, toolID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Set;

import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.SitePage;
import org.sakaiproject.site.api.ToolConfiguration;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -191,7 +190,11 @@ public interface ToolManager
* If the configuration tag is not set or is null, then all users see the tool.
*/
public boolean allowTool(Site site, Placement placement);
}



/**
* Determine if the tool defined by the given tool ID is stealthed globally.
* @param toolID the ID of the tool in question
* @return true if the tool is stealthed globally; false otherwise
*/
public boolean isStealthed(String toolID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,12 @@ public static String getLocalizedToolProperty(String toolId, String key){

return manager.getLocalizedToolProperty(toolId, key);
}


public static boolean isStealthed(String toolID)
{
org.sakaiproject.tool.api.ToolManager manager = getInstance();
if (manager == null) return false;

return manager.isStealthed(toolID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3924,4 +3924,21 @@ public SiteTitleValidationStatus validateSiteTitle(String orig, String stripped)
return SiteTitleValidationStatus.OK;
}
}

/**
* {@inheritDoc}
*/
public boolean isStealthedToolPresent(Site site, String toolID) {
List<SitePage> pages = site.getOrderedPages();
for (SitePage page : pages) {
List<ToolConfiguration> toolConfigs = page.getTools();
for (ToolConfiguration toolConfig : toolConfigs) {
if (toolConfig.getToolId().equals(toolID)) {
return true;
}
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,16 @@ && arrayContains(((Map) session.getAttribute("delegatedaccess.deniedToolsMap")).
return false;
}
}


/**
* {@inheritDoc}
*/
public boolean isStealthed(String toolID) {
Tool tool = getTool(toolID);
Set<Tool> tools = findTools(Collections.emptySet(), null);
return tool == null || !tools.contains(tool);
}

private boolean arrayContains(Object obj, String item){
if(obj != null && obj instanceof String[]){
String[] array = (String[]) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.sakaiproject.site.util.SiteConstants;
import org.sakaiproject.site.util.SiteTypeUtil;
import org.sakaiproject.sitemanage.api.SiteTypeProvider;
import org.sakaiproject.tool.api.ToolManager;
import org.sakaiproject.util.ResourceLoader;

/**
Expand All @@ -47,6 +48,7 @@ public class MenuBuilder
{
// APIs
private static final SiteService SS = (SiteService) ComponentManager.get( SiteService.class );
private static final ToolManager TM = (ToolManager) ComponentManager.get( ToolManager.class );

// sakai.properties
private static final String SAK_PROP_SITE_SETUP_IMPORT_FILE = "site.setup.import.file";
Expand Down Expand Up @@ -222,7 +224,7 @@ public static void buildMenuForSiteInfo( VelocityPortlet portlet, RunData data,
menu.add( buildMenuEntry( rl.getString( "java.edittools" ), "doMenu_edit_site_tools", activeTab.equals( SiteInfoActiveTab.MANAGE_TOOLS ) ) );

// If the page order helper is available, not stealthed and not hidden, show the link
if( SiteAction.notStealthOrHiddenTool( "sakai-site-pageorder-helper" ) )
if( !TM.isStealthed("sakai-site-pageorder-helper" ) )
{
// In particular, need to check site types for showing the tool or not
if( SiteAction.isPageOrderAllowed( siteType, siteProperties.getProperty( SiteConstants.SITE_PROPERTY_OVERRIDE_HIDE_PAGEORDER_SITE_TYPES ) ) )
Expand All @@ -236,7 +238,7 @@ public static void buildMenuForSiteInfo( VelocityPortlet portlet, RunData data,
}

// If the add participant helper is available, not stealthed and not hidden, show the tab
if( allowUpdateSiteMembership && !isMyWorkspace && SiteAction.notStealthOrHiddenTool( SiteAction.getAddUserHelper( site ) ) )
if( allowUpdateSiteMembership && !isMyWorkspace && !TM.isStealthed( SiteAction.getAddUserHelper( site ) ) )
{
// 'Add Participants'
menu.add( buildMenuEntry( rl.getString( "java.addp" ), "doParticipantHelper", activeTab.equals( SiteInfoActiveTab.ADD_PARTICIPANTS ) ) );
Expand Down Expand Up @@ -269,13 +271,13 @@ public static void buildMenuForSiteInfo( VelocityPortlet portlet, RunData data,

if( allowUpdateSite && !isMyWorkspace )
{
if( SiteAction.notStealthOrHiddenTool( "sakai-site-manage-link-helper" ) )
if( !TM.isStealthed( "sakai-site-manage-link-helper" ) )
{
// 'Link to Parent Site'
menu.add( buildMenuEntry( rl.getString( "java.link" ), "doLinkHelper", activeTab.equals( SiteInfoActiveTab.LINK_TO_PARENT_SITE ) ) );
}

if( SiteAction.notStealthOrHiddenTool( "sakai.basiclti.admin.helper" ) )
if( !TM.isStealthed( "sakai.basiclti.admin.helper" ) )
{
// 'External Tools'
menu.add( buildMenuEntry( rl.getString( "java.external" ), "doExternalHelper", activeTab.equals( SiteInfoActiveTab.EXTERNAL_TOOLS ) ) );
Expand Down Expand Up @@ -320,7 +322,7 @@ public static void buildMenuForSiteInfo( VelocityPortlet portlet, RunData data,
}

boolean eventLogEnabled = ServerConfigurationService.getBoolean( SAK_PROP_DISPLAY_USER_AUDIT_LOG, SAK_PROP_DISPLAY_USER_AUDIT_LOG_DEFAULT );
if( SiteAction.notStealthOrHiddenTool( "sakai.useraudit" ) && eventLogEnabled )
if( !TM.isStealthed( "sakai.useraudit" ) && eventLogEnabled )
{
// 'User Audit Log'
menu.add( buildMenuEntry( rl.getString( "java.userAuditEventLog" ), "doUserAuditEventLog", activeTab.equals( SiteInfoActiveTab.USER_AUDIT_LOG ) ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4179,7 +4179,7 @@ private Set getCourseSet(SessionState state) {
*/
private void pageOrderToolTitleIntoContext(Context context, SessionState state, String siteType, boolean newSite, String overrideSitePageOrderSetting) {
// check if this is an existing site and PageOrder is enabled for the site. If so, show tool title
if (!newSite && notStealthOrHiddenTool("sakai-site-pageorder-helper") && isPageOrderAllowed(siteType, overrideSitePageOrderSetting))
if (!newSite && !ToolManager.isStealthed("sakai-site-pageorder-helper") && isPageOrderAllowed(siteType, overrideSitePageOrderSetting))
{
// the actual page titles
context.put(STATE_TOOL_REGISTRATION_TITLE_LIST, state.getAttribute(STATE_TOOL_REGISTRATION_TITLE_LIST));
Expand Down Expand Up @@ -4555,7 +4555,7 @@ public static boolean setHelper(String helperName, String defaultHelperId, Sessi
{
state.setAttribute(stateHelperString, helperId);
}
if (notStealthOrHiddenTool(helperId)) {
if (!ToolManager.isStealthed(helperId)) {
return true;
}
return false;
Expand Down Expand Up @@ -6439,7 +6439,7 @@ private List<MyTool> getGroupedToolList(String groupId, String groupName, String
if (tr != null)
{
String toolId = tr.getId();
if (isSiteTypeInToolCategory(SiteTypeUtil.getTargetSiteType(type), tr) && notStealthOrHiddenTool(toolId) ) // SAK 23808
if (isSiteTypeInToolCategory(SiteTypeUtil.getTargetSiteType(type), tr) && !ToolManager.isStealthed(toolId) ) // SAK 23808
{
newTool = new MyTool();
newTool.title = tr.getTitle();
Expand Down Expand Up @@ -9986,7 +9986,7 @@ private void actionForTemplate(String direction, int index,
Tool tool = toolConf.getTool();
String toolId = StringUtils.trimToEmpty(tool.getId());

if (StringUtils.isNotBlank(toolId) && !notStealthOrHiddenTool(toolId)) {
if (StringUtils.isNotBlank(toolId) && ToolManager.isStealthed(toolId)) {
// Found a stealthed tool, queue for removal
log.debug("found stealthed tool {}", toolId);
rmToolList.add(toolConf);
Expand Down Expand Up @@ -11798,20 +11798,6 @@ private void saveMultipleToolConfiguration(SessionState state, ToolConfiguration
}
}

/**
* Is the tool stealthed or hidden
* @param toolId
* @return
*/
public static boolean notStealthOrHiddenTool(String toolId) {
Tool tool = ToolManager.getTool(toolId);
Set<Tool> tools = ToolManager.findTools(Collections.emptySet(), null);
boolean result = tool != null && tools.contains(tool);
return result;

}


/**
* Is the siteType listed in the tool properties list of categories?
* @param siteType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import lombok.extern.slf4j.Slf4j;
import lombok.Getter;
import lombok.Setter;
import org.sakaiproject.authz.api.SecurityService;

import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.memory.api.MemoryService;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.sitestats.api.StatsManager;
import org.sakaiproject.sitestats.api.event.EventInfo;
Expand Down Expand Up @@ -79,6 +81,7 @@ public class EventRegistryServiceImpl implements EventRegistry, EventRegistrySer
@Setter private ToolManager toolManager;
@Setter private MemoryService memoryService;
@Setter private ServerConfigurationService serverConfigurationService;
@Setter private SecurityService securityService;

// ################################################################
// Spring methods
Expand Down Expand Up @@ -416,9 +419,48 @@ private List<ToolInfo> getMergedEventRegistry() {
cloneRegistry.add(t.clone());
}

// If not admin, remove stealthed tools from clone
if (!securityService.isSuperUser()) {
final Site site = getCurrentSite();
cloneRegistry.removeIf(tool -> isToolStealthed(site, tool.getToolId()));
}

return cloneRegistry;
}


/**
* Utility function to remove stealthed tool from the list:
* 1. If user has stealthed tool(s) in site, show stealthed tool(s) present but not those which aren't
* 2. If user has no stealthed tools in site, do not show any stealthed tools
* @param currentSite the site currently in context
* @param toolID tool registration of the tool in question
* @return true if the tool is stealthed and not present in the current site; false otherwise
*/
private boolean isToolStealthed(Site currentSite, String toolID) {

boolean isStealthed = toolManager.isStealthed(toolID);

// If we have a site and the tool is stealthed, check to see if it's present in the site
if (currentSite != null && isStealthed) {
isStealthed = !siteService.isStealthedToolPresent(currentSite, toolID);
}

return isStealthed;
}

/*
* Utility function to get the current Site.
*/
private Site getCurrentSite() {
Site site = null;
try {
String siteID = toolManager.getCurrentPlacement().getContext();
site = siteService.getSite(siteID);
} catch (Exception ex) {} // ignore

return site;
}

/** Process event registry expired notifications */
public void update(Observable obs, Object obj) {
if(NOTIF_EVENT_REGISTRY_EXPIRED.equals(obj)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -291,6 +292,10 @@ public ToolManager toolManager() {
when(resourcesTool.getId()).thenReturn(StatsManager.RESOURCES_TOOLID);
Set<Tool> tools = new HashSet<>(Arrays.asList(chatTool, resourcesTool));
when(toolManager.findTools(null, null)).thenReturn(tools);
when(toolManager.findTools(Collections.EMPTY_SET, null)).thenReturn(tools);
when(toolManager.findTools(Collections.emptySet(), null)).thenReturn(tools);
when(toolManager.getTool(FakeData.TOOL_CHAT)).thenReturn(chatTool);
when(toolManager.getTool(StatsManager.RESOURCES_TOOLID)).thenReturn(resourcesTool);
return toolManager;
}

Expand Down
1 change: 1 addition & 0 deletions sitestats/sitestats-impl/src/webapp/WEB-INF/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<property name="serverEventIds" ref="org.sakaiproject.sitestats.api.serverevents.List" />
<property name="siteService" ref="org.sakaiproject.site.api.SiteService"/>
<property name="toolManager" ref="org.sakaiproject.tool.api.ToolManager"/>
<property name="securityService" ref="org.sakaiproject.authz.api.SecurityService"/>
</bean>
<bean id="org.sakaiproject.sitestats.api.event.FileEventRegistry"
class="org.sakaiproject.sitestats.impl.event.FileEventRegistry"
Expand Down

0 comments on commit f529ff8

Please sign in to comment.