All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.internet2.middleware.grouper.grouperUi.beans.ui.CustomUiContainer Maven / Gradle / Ivy

There is a newer version: 5.13.5
Show newest version
/**
 * @author mchyzer
 * $Id$
 */
package edu.internet2.middleware.grouper.grouperUi.beans.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;

import edu.internet2.middleware.grouper.Group;
import edu.internet2.middleware.grouper.GrouperSession;
import edu.internet2.middleware.grouper.Member;
import edu.internet2.middleware.grouper.MemberFinder;
import edu.internet2.middleware.grouper.MembershipFinder;
import edu.internet2.middleware.grouper.app.customUi.CustomUiConfiguration;
import edu.internet2.middleware.grouper.app.gsh.GrouperGroovyInput;
import edu.internet2.middleware.grouper.app.gsh.GrouperGroovysh;
import edu.internet2.middleware.grouper.app.gsh.GrouperGroovysh.GrouperGroovyResult;
import edu.internet2.middleware.grouper.cfg.text.GrouperTextContainer;
import edu.internet2.middleware.grouper.exception.GrouperSessionException;
import edu.internet2.middleware.grouper.misc.GrouperSessionHandler;
import edu.internet2.middleware.grouper.privs.PrivilegeHelper;
import edu.internet2.middleware.grouper.ui.GrouperUiFilter;
import edu.internet2.middleware.grouper.ui.customUi.CustomUiEngine;
import edu.internet2.middleware.grouper.ui.customUi.CustomUiTextType;
import edu.internet2.middleware.grouper.ui.customUi.CustomUiUserQueryDisplayBean;
import edu.internet2.middleware.grouper.util.GrouperUtil;
import edu.internet2.middleware.grouperClient.collections.MultiKey;
import edu.internet2.middleware.grouperClient.util.ExpirableCache;
import edu.internet2.middleware.subject.Subject;


/**
 *
 */
public class CustomUiContainer {
  
  public String getUrlParamsForForm() {
    
    StringBuilder result = new StringBuilder();
    
    boolean first = true;
    for (String key : this.customUiEngine.getUrlParameters().keySet()) {
      String value = GrouperUtil.defaultString(this.customUiEngine.getUrlParameters().get(key));
      if (!first) {
        result.append("&");
      }
      result.append(GrouperUtil.escapeUrlEncode(key)).append("=").append(GrouperUtil.escapeUrlEncode(value));
      
    }
    return result.toString();
  }
  
  /**
   * custom ui config user is currently viewing/editing 
   */
  private GuiCustomUiConfiguration guiCustomUiConfiguration;
  
  private List guiCustomUiConfigurations = new ArrayList();
  
  /**
   * 
   * @return the value for combobox
   */
  public String getUserComboboxValue() {

    final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();

    final Member loggedInMember = MemberFinder.findBySubject(GrouperSession.staticGrouperSession(), loggedInSubject, true);

    if (!StringUtils.equals(loggedInMember.getId(), this.member.getId())) {
      
      return this.member.getSubjectSourceId() + "||" + this.member.getSubjectId();
      
    }
    return "";
  }
  
  /**
   * member to operate on
   */
  private Member member;
  

  
  /**
   * @return the member
   */
  public Member getMember() {
    return this.member;
  }

  
  /**
   * @param member the member to set
   */
  public void setMember(Member member) {
    this.member = member;
  }

  private boolean calculatedDisplayBeans = false;
  
  /**
   * 
   * @return the set of display beans
   */
  public Set getCustomUiUserQueryDisplayBeans() {
    if (!this.calculatedDisplayBeans) {
      Map substituteMap = overrideMap();

      this.customUiEngine.generateUserQueryDisplayBeans(substituteMap);
    }
    return this.customUiEngine.getCustomUiUserQueryDisplayBeans();
  }
  
  public String getLogCustomUiEngine() {
    if (LOG.isDebugEnabled()) {
      LOG.debug(GrouperUtil.mapToString(this.customUiEngine.getDebugMap()));
    }
    return "";
  }
  
  /** logger */
  private static final Log LOG = GrouperUtil.getLog(CustomUiContainer.class);

  /**
   * map from text type to text
   */
  private Map textTypeToText = new HashMap() {

    /**
     * @see java.util.HashMap#get(java.lang.Object)
     */
    @Override
    public String get(Object key) {

      CustomUiTextType customUiTextType = key instanceof CustomUiTextType ? (CustomUiTextType)key : CustomUiTextType.valueOfIgnoreCase((String)key, true);
      
      Map substituteMap = overrideMap();
      
      return CustomUiContainer.this.customUiEngine.findBestText(customUiTextType, substituteMap);
    }
    
  };
  
  /**
   * has computed logic
   */
  private boolean hasComputedLogic = false;

  
  /**
   * has computed logic
   * @return the hasComputedLogic
   */
  public boolean isHasComputedLogic() {
    return this.hasComputedLogic;
  }


  
  /**
   * has computed logic
   * @param hasComputedLogic1 the hasComputedLogic to set
   */
  public void setHasComputedLogic(boolean hasComputedLogic1) {
    this.hasComputedLogic = hasComputedLogic1;
  }


  /**
   * @return the textTypeToText
   */
  public Map getTextTypeToText() {
    return this.textTypeToText;
  }

  /**
   * if can change variables, null if not calculated
   */
  private Boolean canAssignVariables;
  
  /**
   * 
   * @return if can change variables
   */
  public boolean isCanAssignVariables() {
    if (this.canAssignVariables == null) {

      String groupUuidOrName = this.getCustomUiEngine().getCustomUiConfig().getGroupCanAssignVariables();
      
      Subject subjectLoggedIn = GrouperUiFilter.retrieveSubjectLoggedIn();
      if (!StringUtils.isBlank(groupUuidOrName)) {
        this.canAssignVariables = MembershipFinder.hasMemberCacheNoCheckSecurity(groupUuidOrName, subjectLoggedIn);
      } else {
        this.canAssignVariables = false;
      }
    }
    return this.canAssignVariables;
  }
  
  /**
   * custom ui engine
   */
  private CustomUiEngine customUiEngine = null;
  
  /**
   * custom ui engine
   * @return the customUiEngine
   */
  public CustomUiEngine getCustomUiEngine() {
    return this.customUiEngine;
  }

  /**
   * custom ui engine
   * @param customUiEngine1 the customUiEngine to set
   */
  public void setCustomUiEngine(CustomUiEngine customUiEngine1) {
    this.customUiEngine = customUiEngine1;
  }

  /**
   * attribute names for screen
   * @return attributes
   */
  public Set getAttributeNames() {
    Map userQueryVariables = this.customUiEngine.userQueryVariables();
    if (userQueryVariables == null) {
      return null;
    }
    return userQueryVariables.keySet();
  }
  
  
  /**
   * 
   */
  public CustomUiContainer() {
  }

  /**
   * 
   * @return true if should add/remove member
   */
  public boolean isManageMembership() {
    
    String manageMembership = (String)this.getTextTypeToText().get(CustomUiTextType.manageMembership.name());
    return GrouperUtil.booleanValue(manageMembership, true);

  }
  
  /**
   * if should show enroll button
   * @return true if should show
   */
  public boolean isEnrollButtonShow() {
    String show = (String)this.getTextTypeToText().get(CustomUiTextType.enrollButtonShow.name());
    if (!StringUtils.isBlank(show)) {
      return GrouperUtil.booleanValue(show);
    }
    
    final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();

    // else if can optin
    final Group group = GrouperRequestContainer.retrieveFromRequestOrCreate().getGroupContainer().getGuiGroup().getGroup();
    
    boolean isMember = (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
      
      public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
        return group.hasMember(loggedInSubject);
      }
    });  
    
    return !isMember && group.canHavePrivilege(loggedInSubject, "optins", false);
  }
  
  /**
   * 
   * @return the text
   */
  public String getEnrollButtonText() {
    String buttonText = (String)this.getTextTypeToText().get(CustomUiTextType.enrollButtonText.name());
    if (!StringUtils.isBlank(buttonText)) {
      return buttonText;
    }
    return TextContainer.retrieveFromRequest().getText().get("guiCustomUiGroupDefaultEnrollButtonText");
  }
  
  /**
   * if show user environment
   */
  private Boolean canSeeUserEnvironment;
  
  
  /**
   * @return the showUserEnvironment
   */
  public boolean isCanSeeUserEnvironment() {
    if (this.canSeeUserEnvironment == null) {
      
      String groupUuidOrName = this.getCustomUiEngine().getCustomUiConfig().getGroupCanSeeUserEnvironment();
      
      Subject subjectLoggedIn = GrouperUiFilter.retrieveSubjectLoggedIn();
      
      if (!StringUtils.isBlank(groupUuidOrName)) {
        this.canSeeUserEnvironment = MembershipFinder.hasMemberCacheNoCheckSecurity(groupUuidOrName, subjectLoggedIn);
      } else {
        this.canSeeUserEnvironment = false;
      }
      
    }
    return this.canSeeUserEnvironment;
  }
  
  /**
   * are we on an enroll or unenroll
   */
  private boolean enroll;
  
  /**
   * @return the enroll
   */
  public boolean isEnroll() {
    return this.enroll;
  }

  /**
   * @param enroll1 the enroll to set
   */
  public void setEnroll(boolean enroll1) {
    this.enroll = enroll1;
  }


  /**
   * @return the showScreenState
   */
  public boolean isCanSeeScreenState() {
    if (this.canSeeScreenState == null) {
      
      String groupUuidOrName = this.getCustomUiEngine().getCustomUiConfig().getGroupCanSeeScreenState();
      
      Subject subjectLoggedIn = GrouperUiFilter.retrieveSubjectLoggedIn();
      
      if (!StringUtils.isBlank(groupUuidOrName)) {
        this.canSeeScreenState = MembershipFinder.hasMemberCacheNoCheckSecurity(groupUuidOrName, subjectLoggedIn);
      } else {
        this.canSeeScreenState = false;
      }
    }
    return this.canSeeScreenState;
  }

  /**
   * if show screen state
   */
  private Boolean canSeeScreenState;
  
  /**
   * cache if manager
   */
  private Boolean manager = null;

  /**
   * cache of privs for custom ui
   */
  private static ExpirableCache subjectSourceSubjectIdGroupNameFieldNameCache = new ExpirableCache(2);
  
  /**
   * if manager
   * @return true if should show
   */
  public boolean isManager() {
    
    if (this.manager == null && this.customUiEngine != null) {
      
      boolean turnOffManager = GrouperUtil.booleanValue(this.customUiEngine.getUrlParameters().get("cu_grouperTurnOffManager"), false) ;
      if (turnOffManager) {
        this.manager = false;
      }
      
      final Map userQueryVariables = this.customUiEngine.userQueryVariables();
      if (userQueryVariables != null) {
        Boolean overrideOff = (Boolean)userQueryVariables.get("cu_grouperTurnOffManager");
        if (overrideOff != null && overrideOff) {
          this.manager = false;
        }
      }
    }      

    final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
    
    if (this.manager == null) {
      
      String groupUuidOrName = this.getCustomUiEngine().getCustomUiConfig().getGroupOfManagers();
      
      if (!StringUtils.isBlank(groupUuidOrName)) {
        Subject subjectLoggedIn = GrouperUiFilter.retrieveSubjectLoggedIn();
        
        this.manager = MembershipFinder.hasMemberCacheNoCheckSecurity(groupUuidOrName, subjectLoggedIn);
        
      }
    }
    
    if (this.manager == null) {
      // else if can optin
      final Group group = GrouperRequestContainer.retrieveFromRequestOrCreate().getGroupContainer().getGuiGroup().getGroup();
  
      MultiKey readerKey = new MultiKey(loggedInSubject.getSourceId(), loggedInSubject.getId(), group.getName(), "readers");
      MultiKey updaterKey = new MultiKey(loggedInSubject.getSourceId(), loggedInSubject.getId(), group.getName(), "updaters");
      Boolean readerResult = null;
      Boolean updaterResult = null;
    
      if (this.manager == null) {
        readerResult = subjectSourceSubjectIdGroupNameFieldNameCache.get(readerKey);
        updaterResult = subjectSourceSubjectIdGroupNameFieldNameCache.get(updaterKey);
        if (readerResult != null && updaterResult != null) {
          this.manager = readerResult && updaterResult;
        }
      }
    
      if (this.manager == null) {
        readerResult = group.canHavePrivilege(loggedInSubject, "readers", false);
        updaterResult = group.canHavePrivilege(loggedInSubject, "updaters", false);
    
        this.manager = readerResult && updaterResult;
        
        subjectSourceSubjectIdGroupNameFieldNameCache.put(readerKey, readerResult);
        subjectSourceSubjectIdGroupNameFieldNameCache.put(updaterKey, updaterResult);
      }
    }
    return this.manager;
  }


  /**
   * if should show unenroll button
   * @return true if should show
   */
  public boolean isUnenrollButtonShow() {
    String show = (String)this.getTextTypeToText().get(CustomUiTextType.unenrollButtonShow.name());
    if (!StringUtils.isBlank(show)) {
      return GrouperUtil.booleanValue(show);
    }
    
    final Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();

    // else if can optin
    final Group group = GrouperRequestContainer.retrieveFromRequestOrCreate().getGroupContainer().getGuiGroup().getGroup();
    
    
    boolean isMember = (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
      
      public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
        return group.hasMember(loggedInSubject);
      }
    });  

    return isMember && group.canHavePrivilege(loggedInSubject, "optouts", false);
  }
  
  /**
   * 
   * @return the text
   */
  public String getUnenrollButtonText() {
    String buttonText = (String)this.getTextTypeToText().get(CustomUiTextType.unenrollButtonText.name());
    if (!StringUtils.isBlank(buttonText)) {
      return buttonText;
    }
    return TextContainer.retrieveFromRequest().getText().get("guiCustomUiGroupDefaultUnenrollButtonText");
  }

  /**
   */
  public void resetCache() {
    this.canAssignVariables = null;
    this.canSeeScreenState = null;
    this.canSeeUserEnvironment = null;
    this.manager = null;
  }

  private boolean leaveGroupButtonPressed = false;

  
  
  
  public boolean isLeaveGroupButtonPressed() {
    return leaveGroupButtonPressed;
  }


  
  public void setLeaveGroupButtonPressed(boolean leaveGroupButtonPressed) {
    this.leaveGroupButtonPressed = leaveGroupButtonPressed;
  }

  private boolean joinGroupButtonPressed = false;
  

  
  public boolean isJoinGroupButtonPressed() {
    return joinGroupButtonPressed;
  }


  
  public void setJoinGroupButtonPressed(boolean joinGroupButtonPressed) {
    this.joinGroupButtonPressed = joinGroupButtonPressed;
  }

  public String gshRunScript(Group group, Subject subject, Subject subjectLoggedIn, String scriptPart) {
    final StringBuilder script = new StringBuilder();
    
    Map variables = new TreeMap();
    
    variables.putAll(GrouperUtil.nonNull(this.overrideMap()));
    variables.putAll(GrouperUtil.nonNull(this.customUiEngine.userQueryVariables()));

    GrouperGroovyInput grouperGroovyInput = new GrouperGroovyInput();

    for (String key: new TreeSet(variables.keySet())) {
      
      Object value = variables.get(key);
      grouperGroovyInput.assignInputValueObject(key, value);
      if (value instanceof Number) {
        script.append("Number " + key + " = (Number)grouperGroovyRuntime.retrieveInputValueObject(\"" + key + "\");\n");
      } else if (value instanceof Boolean) {
        script.append("boolean " + key + " = grouperGroovyRuntime.retrieveInputValueBoolean(\"" + key + "\");\n");
      } else if (value instanceof String) {
        script.append("String " + key + " = grouperGroovyRuntime.retrieveInputValueString(\"" + key + "\");\n");
      } else if (value == null) {
        script.append("Object " + key + " = null;\n");
      }
      
    }
   return (String) GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
      
      @Override
      public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
        
        grouperGroovyInput.assignInputValueObject("subject", subject);
        script.append("Subject subject = (Subject)grouperGroovyRuntime.retrieveInputValueObject(\"subject\");\n");
        grouperGroovyInput.assignInputValueObject("subjectLoggedIn", subjectLoggedIn);
        script.append("Subject subjectLoggedIn = (Subject)grouperGroovyRuntime.retrieveInputValueObject(\"subjectLoggedIn\");\n");
        grouperGroovyInput.assignInputValueObject("group", group);
        script.append("Group group = (Group)grouperGroovyRuntime.retrieveInputValueObject(\"group\");\n");
        script.append("GrouperSession grouperSession = grouperGroovyRuntime.getGrouperSession();\n");
        grouperGroovyInput.assignScriptPrependHeaders(GrouperUtil.whitespaceCountNewLines(script.toString()));
        script.append(scriptPart);
        grouperGroovyInput.assignScript(script.toString());
        GrouperGroovyResult grouperGroovyResult = new GrouperGroovyResult();
        GrouperGroovysh.runScript(grouperGroovyInput, grouperGroovyResult);
        if (grouperGroovyResult.getException() != null) {
          throw grouperGroovyResult.getException();
        }
        if (grouperGroovyResult.getResultCode() != null && grouperGroovyResult.getResultCode() != 0) {
          throw new RuntimeException(grouperGroovyResult.fullOutput());
        }
        return grouperGroovyResult.fullOutput();
      }
    });
  }
  
  /**
   * @return map
   */
  public Map overrideMap() {
    
    Map substituteMap = new LinkedHashMap();
    substituteMap.put("grouperRequestContainer", GrouperRequestContainer.retrieveFromRequestOrCreate());
    substituteMap.put("request", GrouperUiFilter.retrieveHttpServletRequest());
    substituteMap.put("textContainer", GrouperTextContainer.retrieveFromRequest());
    substituteMap.put("cu_joinGroupButtonPressed", false);
    substituteMap.put("cu_leaveGroupButtonPressed", false);
    if (this.joinGroupButtonPressed) {
      substituteMap.put("cu_joinGroupButtonPressed", true);
    } else if (this.leaveGroupButtonPressed) {
      substituteMap.put("cu_leaveGroupButtonPressed", true);
    }
    return substituteMap;
  }
  
  /**
   * @return true if can view custom ui under misc
   */
  public boolean isCanViewCustomUiMisc() {
    
    Subject loggedInSubject = GrouperUiFilter.retrieveSubjectLoggedIn();
    if (PrivilegeHelper.isWheelOrRoot(loggedInSubject)) {
      return true;
    }
    return false;
  }


  
  public List getGuiCustomUiConfigurations() {
    return guiCustomUiConfigurations;
  }


  
  public void setGuiCustomUiConfigurations(List guiCustomUiConfigurations) {
    this.guiCustomUiConfigurations = guiCustomUiConfigurations;
  }
  
  public List getAllCustomUiTypes() {
    return Arrays.asList(new CustomUiConfiguration());
  }


  
  public GuiCustomUiConfiguration getGuiCustomUiConfiguration() {
    return guiCustomUiConfiguration;
  }


  
  public void setGuiCustomUiConfiguration(GuiCustomUiConfiguration guiCustomUiConfiguration) {
    this.guiCustomUiConfiguration = guiCustomUiConfiguration;
  }
  
  /**
   * current grouped config index we are looping through
   */
  private int index;

  
  public int getIndex() {
    return index;
  }

  public void setIndex(int index) {
    this.index = index;
  }
  
  private String currentConfigSuffix;

  private boolean managerAction;
  
  public String getCurrentConfigSuffix() {
    return currentConfigSuffix;
  }

  
  public void setCurrentConfigSuffix(String currentConfigSuffix) {
    this.currentConfigSuffix = currentConfigSuffix;
  }

  

  
  public boolean isManagerAction() {
    return managerAction;
  }


  public void setManagerAction(boolean theManagerAction) {
    this.managerAction = theManagerAction;
    
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy