use of org.mycore.common.MCRUserInformation in project mycore by MyCoRe-Org.
the class MCRUserAttributeMapperTest method testUserInformationMapping.
@Test
public void testUserInformationMapping() throws Exception {
Map<String, Object> attributes = new HashMap<>();
attributes.put("eduPersonPrincipalName", mcrUser.getUserName() + "@" + realmId);
attributes.put("displayName", mcrUser.getRealName());
attributes.put("mail", mcrUser.getEMailAddress());
attributes.put("eduPersonAffiliation", roles);
MCRUserInformation userInfo = new MCRShibbolethUserInformation(mcrUser.getUserName(), realmId, attributes);
MCRTransientUser user = new MCRTransientUser(userInfo);
assertEquals(mcrUser.getUserName(), user.getUserName());
assertEquals(mcrUser.getRealName(), user.getRealName());
assertEquals(mcrUser.getEMailAddress(), user.getEMailAddress());
assertTrue(user.isUserInRole("editor"));
}
use of org.mycore.common.MCRUserInformation in project mycore by MyCoRe-Org.
the class MCRContainerLoginFormServlet method think.
/* (non-Javadoc)
* @see org.mycore.frontend.servlets.MCRServlet#think(org.mycore.frontend.servlets.MCRServletJob)
*/
@Override
protected void think(MCRServletJob job) throws Exception {
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
MCRUserInformation userInformation = mcrSession.getUserInformation();
MCRLogin login = new MCRLogin(userInformation, getFormAction());
login.getForm().getInput().addAll(Arrays.asList(getUserNameField(), getPasswordField()));
job.getRequest().setAttribute(LOGIN_ATTR, new MCRJAXBContent<>(MCRLogin.getContext(), login));
}
use of org.mycore.common.MCRUserInformation in project mycore by MyCoRe-Org.
the class MCRPIJobRegistrationService method runAsJobUser.
public void runAsJobUser(PIRunnable task) throws MCRPersistentIdentifierException {
final boolean jobUserPresent = isJobUserPresent();
final String jobUser = getJobUser();
MCRSession session = null;
MCRUserInformation savedUserInformation = null;
session = MCRSessionMgr.getCurrentSession();
if (jobUserPresent) {
savedUserInformation = session.getUserInformation();
MCRUser user = MCRUserManager.getUser(jobUser);
/* workaround https://mycore.atlassian.net/browse/MCR-1400*/
session.setUserInformation(MCRSystemUserInformation.getGuestInstance());
session.setUserInformation(user);
LOGGER.info("Continue as User {}", jobUser);
}
boolean transactionActive = !session.isTransactionActive();
try {
if (transactionActive) {
session.beginTransaction();
}
task.run();
} finally {
if (transactionActive && session.isTransactionActive()) {
session.commitTransaction();
}
if (jobUserPresent) {
LOGGER.info("Continue as previous User {}", savedUserInformation.getUserID());
/* workaround https://mycore.atlassian.net/browse/MCR-1400*/
session.setUserInformation(MCRSystemUserInformation.getGuestInstance());
session.setUserInformation(savedUserInformation);
}
}
}
use of org.mycore.common.MCRUserInformation in project mycore by MyCoRe-Org.
the class MCRRestAPIUploadHelper method deleteDerivate.
/**
* deletes a whole derivate
* @param info - the Jersey UriInfo object
* @param request - the HTTPServletRequest object
* @param pathParamMcrObjID - the MyCoRe Object ID
* @param pathParamMcrDerID - the MyCoRe Derivate ID
* @return a Jersey Response Object
* @throws MCRRestAPIException
*/
public static Response deleteDerivate(UriInfo info, HttpServletRequest request, String pathParamMcrObjID, String pathParamMcrDerID) throws MCRRestAPIException {
Response response = Response.status(Status.INTERNAL_SERVER_ERROR).build();
SignedJWT signedJWT = MCRJSONWebTokenUtil.retrieveAuthenticationToken(request);
String base64Signature = request.getHeader("X-MyCoRe-RestAPI-Signature");
if (base64Signature == null) {
// ToDo error handling
}
try (MCRJPATransactionWrapper mtw = new MCRJPATransactionWrapper()) {
// MCRSession session = MCRServlet.getSession(request);
MCRSession session = MCRSessionMgr.getCurrentSession();
MCRUserInformation currentUser = session.getUserInformation();
session.setUserInformation(MCRUserManager.getUser(MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(signedJWT)));
MCRObjectID objID = MCRObjectID.getInstance(pathParamMcrObjID);
MCRObjectID derID = MCRObjectID.getInstance(pathParamMcrDerID);
// MCRAccessManager.checkPermission() uses CACHE, which seems to be dirty from other calls????
MCRAccessManager.invalidPermissionCache(derID.toString(), PERMISSION_DELETE);
if (MCRAccessManager.checkPermission(derID.toString(), PERMISSION_DELETE)) {
try {
MCRMetadataManager.deleteMCRDerivate(derID);
} catch (MCRPersistenceException pe) {
// dir does not exist - do nothing
} catch (MCRAccessException e) {
LOGGER.error(e);
}
}
session.setUserInformation(currentUser);
response = Response.created(info.getBaseUriBuilder().path("v1/objects/" + objID + "/derivates").build()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, "Bearer " + MCRJSONWebTokenUtil.createJWTAuthorizationHeader(signedJWT)).build();
}
return response;
}
Aggregations