Skip to content

Commit

Permalink
Merge pull request sakaiproject#154 from cschauer/SAK-24185
Browse files Browse the repository at this point in the history
SAK-24185 Allow custom role mapping per placement
  • Loading branch information
ottenhoff committed Mar 6, 2015
2 parents e86885b + d305faf commit 630f8b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ protected void processRoster(HttpServletRequest request, HttpServletResponse res
return;
}

String roleMapProp = pitch.getProperty("rolemap");
String releaseName = pitch.getProperty(LTIService.LTI_SENDNAME);
String releaseEmail = pitch.getProperty(LTIService.LTI_SENDEMAILADDR);
String assignment = pitch.getProperty("assignment");
Expand All @@ -656,13 +657,17 @@ protected void processRoster(HttpServletRequest request, HttpServletResponse res
try {
List<Map<String,Object>> lm = new ArrayList<Map<String,Object>>();
Set<Member> members = site.getMembers();
Map<String, String> roleMap = SakaiBLTIUtil.convertRoleMapPropToMap(roleMapProp);
for (Member member : members ) {
Map<String,Object> mm = new TreeMap<String,Object>();
Role role = member.getRole();
String ims_user_id = member.getUserId();
mm.put("/user_id",ims_user_id);
String ims_role = "Learner";
if ( maintainRole != null && maintainRole.equals(role.getId())) ims_role = "Instructor";
if ( roleMap.containsKey(role.getId()) ) {
ims_role = roleMap.get(role.getId());
}
// This is incorrect according to
// http://developers.imsglobal.org/ext_membership.html. It
// should be roles. If we can determine that nobody is using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static void addSiteInfo(Properties props, Properties lti2subst, Site site
setProperty(props, BasicLTIConstants.LAUNCH_PRESENTATION_RETURN_URL, returnUrl);
}

public static void addRoleInfo(Properties props, Properties lti2subst, String context)
public static void addRoleInfo(Properties props, Properties lti2subst, String context, String roleMapProp)
{
String theRole = "Learner";
if ( SecurityService.isSuperUser() )
Expand All @@ -314,6 +314,7 @@ else if ( SiteService.allowUpdateSite(context) )

String realmId = SiteService.siteReference(context);
User user = null;
Map<String, String> roleMap = convertRoleMapPropToMap(roleMapProp);
try {
user = UserDirectoryService.getCurrentUser();
if ( user != null ) {
Expand All @@ -323,6 +324,10 @@ else if ( SiteService.allowUpdateSite(context) )
if ( realm != null ) role = realm.getUserRole(user.getId());
if ( role != null ) roleId = role.getId();
if ( roleId != null && roleId.length() > 0 ) setProperty(props, "ext_sakai_role", roleId);
if ( roleMap.containsKey(roleId) ) {
setProperty(props, BasicLTIConstants.ROLES, roleMap.get(roleId));
setProperty(lti2subst, "Membership.role", roleMap.get(roleId));
}
}
} catch (GroupNotDefinedException e) {
dPrint("SiteParticipantHelper.getExternalRealmId: site realm not found"+e.getMessage());
Expand Down Expand Up @@ -367,7 +372,10 @@ public static boolean sakaiInfo(Properties props, String context, String placeme

// Add the generic information
addGlobalData(site, props, null, rb);
addRoleInfo(props, null, context);
ToolConfiguration placement = SiteService.findTool(placementId);
Properties config = placement.getConfig();
String roleMapProp = toNull(getCorrectProperty(config, "rolemap", placement));
addRoleInfo(props, null, context, roleMapProp);
addSiteInfo(props, null, site);

// Add Placement Information
Expand Down Expand Up @@ -653,7 +661,7 @@ public static String[] postLaunchHTML(Map<String, Object> content, Map<String,Ob
}
addGlobalData(site, ltiProps, lti2subst, rb);
addSiteInfo(ltiProps, lti2subst, site);
addRoleInfo(ltiProps, lti2subst, context);
addRoleInfo(ltiProps, lti2subst, context, (String)tool.get("rolemap"));

if ( deploy != null ) {
setProperty(lti2subst,"ToolConsumerProfile.url", getOurServerUrl() +
Expand Down Expand Up @@ -1550,5 +1558,22 @@ public static void popAdvisor() {
SecurityService.popAdvisor();
}


/**
* Converts a string from a comma-separated list of role maps to a Map<String, String>.
* Each role mapping in the string should be of the form <sakairole>:<ltirole>.
*/
public static Map<String, String> convertRoleMapPropToMap(String roleMapProp) {
Map<String, String> roleMap = new HashMap<String, String>();
if (roleMapProp == null) return roleMap;

String[] roleMapPairs = roleMapProp.split(",");
for (String s : roleMapPairs) {
String[] roleMapPair = s.split(":");
if (roleMapPair.length != 2) {
throw new IllegalArgumentException("Malformed rolemap property. Value must be a comma-separated list of values of the form <sakairole>:<ltirole>");
}
roleMap.put(roleMapPair[0], roleMapPair[1]);
}
return roleMap;
}
}

0 comments on commit 630f8b4

Please sign in to comment.