Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-50970 When you impersonate a instructor and use Enter Student View, when you exit, you should return to the instructor sessions instead of admin session #13287

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public interface UsageSessionService
String EVENT_ROLEVIEW_EXIT = ROLEVIEW_PREFIX + ".exit";
String EVENT_ROLEVIEW_START = ROLEVIEW_PREFIX + ".start";

public static final String SAKAI_SESSION_USER_ID = "sakai.session.user.id";
public static final String SAKAI_SESSION_USER_EID = "sakai.session.user.eid";

/**
* Establish a usage session associated with the current request or thread.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ public void impersonateUser(String userId) throws SakaiException {
User mockUser = userDirectoryService().getUser(userId);
String mockUserId = mockUser.getId();
String mockUserEid = mockUser.getEid();
String realUserId = currentSession.getUserId();
String realUserEid = currentSession.getUserEid();

eventTrackingService().post(eventTrackingService().newEvent(UsageSessionService.EVENT_ROLEVIEW_BECOME, userDirectoryService().userReference(mockUserId), false));
log.info("Entering into RoleView mode, real user [{}] is impersonating mock user [{}]", currentSession.getUserEid(), mockUserEid);
Expand All @@ -516,6 +518,9 @@ public void impersonateUser(String userId) throws SakaiException {
// logout - clear, but do not invalidate, preserving the current session
currentSession.clearExcept(saveAttributes);
// login - set the user id and eid into session, and refresh this user's authz information
currentSession.setAttribute(UsageSessionService.SAKAI_SESSION_USER_ID, realUserId);
currentSession.setAttribute(UsageSessionService.SAKAI_SESSION_USER_EID, realUserEid);

currentSession.setUserId(mockUserId);
currentSession.setUserEid(mockUserEid);
authzGroupService().refreshUser(mockUserId);
Expand All @@ -534,32 +539,30 @@ public void impersonateUser(String userId) throws SakaiException {
public void restoreUser() throws SakaiException {
Session currentSession = sessionManager().getCurrentSession();
if (currentSession != null) {
UsageSession usageSession = (UsageSession) currentSession.getAttribute(USAGE_SESSION_KEY);
if (usageSession != null) {
String realUserId = usageSession.getUserId();
String realUserEid = usageSession.getUserEid();

if (StringUtils.isAnyBlank(realUserId, realUserEid)) {
log.error("Can not restore session from roleview mode, missing the real user information, session is likely corrupt");
String realUserId = (String) currentSession.getAttribute(UsageSessionService.SAKAI_SESSION_USER_ID);
String realUserEid = (String) currentSession.getAttribute(UsageSessionService.SAKAI_SESSION_USER_EID);
if (StringUtils.isAnyBlank(realUserId, realUserEid)) {
UsageSession usageSession = (UsageSession) currentSession.getAttribute(USAGE_SESSION_KEY);
if (usageSession != null) {
realUserId = usageSession.getUserId();
realUserEid = usageSession.getUserEid();
} else {
log.error("Can not restore session from roleview mode, missing the original session, session is likely corrupt");
currentSession.invalidate();
throw new SakaiException("Can not restore session from roleview mode, missing the real user information");
throw new SakaiException("Can not restore session from roleview mode, missing the original session");
}
}

// Restore the original user session
List<String> saveAttributes = List.of(
UsageSessionService.USAGE_SESSION_KEY,
UsageSessionService.SAKAI_CSRF_SESSION_ATTRIBUTE);
currentSession.clearExcept(saveAttributes);
// Restore the original user session
List<String> saveAttributes = List.of(
UsageSessionService.USAGE_SESSION_KEY,
UsageSessionService.SAKAI_CSRF_SESSION_ATTRIBUTE);
currentSession.clearExcept(saveAttributes);

currentSession.setUserId(realUserId);
currentSession.setUserEid(realUserEid);
authzGroupService().refreshUser(realUserId);
log.info("Exiting from roleview mode, restored real user [{}] for session [{}]", realUserEid, currentSession.getId());
} else {
log.error("Can not restore session from roleview mode, missing the original session, session is likely corrupt");
currentSession.invalidate();
throw new SakaiException("Can not restore session from roleview mode, missing the original session");
}
currentSession.setUserId(realUserId);
currentSession.setUserEid(realUserEid);
authzGroupService().refreshUser(realUserId);
log.info("Exiting from roleview mode, restored real user [{}] for session [{}]", realUserEid, currentSession.getId());
} else {
log.warn("Restore from roleview for user, but a session does not exist for this request, skipping");
}
Expand Down
Loading