Search in sources :

Example 16 with MCRUserInformation

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"));
}
Also used : HashMap(java.util.HashMap) MCRShibbolethUserInformation(org.mycore.user2.login.MCRShibbolethUserInformation) MCRUserInformation(org.mycore.common.MCRUserInformation) Test(org.junit.Test)

Example 17 with MCRUserInformation

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

Example 18 with MCRUserInformation

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

Example 19 with MCRUserInformation

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;
}
Also used : Response(javax.ws.rs.core.Response) MCRSession(org.mycore.common.MCRSession) MCRAccessException(org.mycore.access.MCRAccessException) SignedJWT(com.nimbusds.jwt.SignedJWT) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRUserInformation(org.mycore.common.MCRUserInformation) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRUserInformation (org.mycore.common.MCRUserInformation)19 MCRSession (org.mycore.common.MCRSession)9 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)6 SignedJWT (com.nimbusds.jwt.SignedJWT)5 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 MCRAccessException (org.mycore.access.MCRAccessException)4 MCRPersistenceException (org.mycore.common.MCRPersistenceException)4 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)4 MCRUser (org.mycore.user2.MCRUser)4 Response (javax.ws.rs.core.Response)3 Document (org.jdom2.Document)3 XMLOutputter (org.jdom2.output.XMLOutputter)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRShibbolethUserInformation (org.mycore.user2.login.MCRShibbolethUserInformation)3 TreeMap (java.util.TreeMap)2 MCRException (org.mycore.common.MCRException)2 BufferedInputStream (java.io.BufferedInputStream)1