use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.
the class MCRUserTransformerTest method testBuildMCRUser.
/**
* Test method for {@link org.mycore.user2.utils.MCRUserTransformer#buildMCRUser(org.jdom2.Element)}.
* @throws IOException
* @throws JAXBException
*/
@Test
public final void testBuildMCRUser() throws IOException, JAXBException {
Element input = MCRURIResolver.instance().resolve("resource:test-user.xml");
MCRUser mcrUser = MCRUserTransformer.buildMCRUser(input);
Document output = MCRUserTransformer.buildExportableXML(mcrUser);
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(input, System.out);
System.out.println();
xout.output(output, System.out);
assertTrue("Input element is not the same as outputElement", MCRXMLHelper.deepEqual(input, output.getRootElement()));
}
use of org.mycore.user2.MCRUser 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);
}
}
}
Aggregations