Search in sources :

Example 11 with MCRUser

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()));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlElement(javax.xml.bind.annotation.XmlElement) Element(org.jdom2.Element) MCRUser(org.mycore.user2.MCRUser) Document(org.jdom2.Document) Test(org.junit.Test)

Example 12 with MCRUser

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);
        }
    }
}
Also used : MCRSession(org.mycore.common.MCRSession) MCRUser(org.mycore.user2.MCRUser) MCRUserInformation(org.mycore.common.MCRUserInformation)

Aggregations

MCRUser (org.mycore.user2.MCRUser)10 MCRUserInformation (org.mycore.common.MCRUserInformation)6 Document (org.jdom2.Document)4 XMLOutputter (org.jdom2.output.XMLOutputter)4 HashMap (java.util.HashMap)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.junit.Test)3 MCRShibbolethUserInformation (org.mycore.user2.login.MCRShibbolethUserInformation)2 IOException (java.io.IOException)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1 AttributePrincipal (org.jasig.cas.client.authentication.AttributePrincipal)1 Assertion (org.jasig.cas.client.validation.Assertion)1 Cas20ProxyTicketValidator (org.jasig.cas.client.validation.Cas20ProxyTicketValidator)1 Element (org.jdom2.Element)1 JDOMException (org.jdom2.JDOMException)1 MCRException (org.mycore.common.MCRException)1 MCRSession (org.mycore.common.MCRSession)1 MCRJAXBContent (org.mycore.common.content.MCRJAXBContent)1