Search in sources :

Example 1 with MCRUser

use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.

the class MCRUserAttributeMapperTest method testUserUpdate.

@Test
public void testUserUpdate() 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());
    assertTrue(user.isUserInRole("editor"));
    Map<String, String> extraAttribs = new HashMap<>();
    extraAttribs.put("attrib1", "test123");
    extraAttribs.put("attrib2", "test321");
    user.setAttributes(extraAttribs);
    MCRUserManager.createUser(user);
    startNewTransaction();
    attributes = new HashMap<>();
    attributes.put("eduPersonPrincipalName", mcrUser.getUserName() + "@" + realmId);
    attributes.put("displayName", mcrUser.getRealName());
    attributes.put("mail", "new@mycore.de");
    attributes.put("eduPersonAffiliation", "admin");
    MCRUser storedUser = MCRUserManager.getUser(user.getUserName(), realmId);
    MCRUserAttributeMapper attributeMapper = MCRRealmFactory.getAttributeMapper(realmId);
    boolean changed = attributeMapper.mapAttributes(storedUser, attributes);
    assertTrue("should changed", changed);
    assertNotEquals(user.getEMailAddress(), storedUser.getEMailAddress());
    Document exportableXML = MCRUserTransformer.buildExportableXML(storedUser);
    new XMLOutputter(Format.getPrettyFormat()).output(exportableXML, System.out);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) HashMap(java.util.HashMap) MCRShibbolethUserInformation(org.mycore.user2.login.MCRShibbolethUserInformation) Document(org.jdom2.Document) MCRUserInformation(org.mycore.common.MCRUserInformation) Test(org.junit.Test)

Example 2 with MCRUser

use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.

the class MCRLoginServlet method presentLoginForm.

protected void presentLoginForm(MCRServletJob job) throws IOException, TransformerException, SAXException, JAXBException {
    HttpServletRequest req = job.getRequest();
    HttpServletResponse res = job.getResponse();
    if (LOCAL_LOGIN_SECURE_ONLY && !req.isSecure()) {
        res.sendError(HttpServletResponse.SC_FORBIDDEN, getErrorI18N("component.user2.login", "httpsOnly"));
        return;
    }
    String returnURL = getReturnURL(req);
    String formAction = req.getRequestURI();
    MCRLogin loginForm = new MCRLogin(MCRSessionMgr.getCurrentSession().getUserInformation(), returnURL, formAction);
    String uid = getProperty(req, "uid");
    String pwd = getProperty(req, "pwd");
    if (uid != null) {
        MCRUser user = MCRUserManager.login(uid, pwd);
        if (user == null) {
            res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            loginForm.setLoginFailed(true);
        } else {
            // user logged in
            // MCR-1154
            req.changeSessionId();
            LOGGER.info("user {} logged in successfully.", uid);
            res.sendRedirect(res.encodeRedirectURL(getReturnURL(req)));
            return;
        }
    }
    addFormFields(loginForm, job.getRequest().getParameter(REALM_URL_PARAMETER));
    getLayoutService().doLayout(req, res, new MCRJAXBContent<>(JAXBContext.newInstance(MCRLogin.class), loginForm));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRUser(org.mycore.user2.MCRUser) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 3 with MCRUser

use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.

the class MCRUserTransformer method getDocument.

private static Document getDocument(MCRUser user) {
    MCRJAXBContent<MCRUser> content = new MCRJAXBContent<>(JAXB_CONTEXT, user);
    try {
        Document userXML = content.asXML();
        sortAttributes(userXML);
        return userXML;
    } catch (SAXParseException | JDOMException | IOException e) {
        throw new MCRException("Exception while transforming MCRUser " + user.getUserID() + " to JDOM document.", e);
    }
}
Also used : MCRJAXBContent(org.mycore.common.content.MCRJAXBContent) MCRException(org.mycore.common.MCRException) SAXParseException(org.xml.sax.SAXParseException) MCRUser(org.mycore.user2.MCRUser) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException)

Example 4 with MCRUser

use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.

the class MCRPersistTransientUserEventHandler method handleObjectCreated.

/**
 * Persists {@link MCRTransientUser} if an {@link MCRObject} was created.
 *
 * @see org.mycore.common.events.MCREventHandlerBase#handleObjectCreated(org.mycore.common.events.MCREvent, org.mycore.datamodel.metadata.MCRObject)
 */
@Override
protected void handleObjectCreated(MCREvent evt, MCRObject obj) {
    MCRUser currentUser = MCRUserManager.getCurrentUser();
    if (!MCRUserManager.isInvalidUser(currentUser) && MCRUserManager.getUser(currentUser.getUserID()) == null) {
        LOGGER.info("create new user \"{}\"", currentUser.getUserID());
        MCRUserManager.createUser((MCRTransientUser) currentUser);
    }
}
Also used : MCRUser(org.mycore.user2.MCRUser)

Example 5 with MCRUser

use of org.mycore.user2.MCRUser in project mycore by MyCoRe-Org.

the class MCRCASServlet method doGetPost.

public void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest req = job.getRequest();
    HttpServletResponse res = job.getResponse();
    String ticket = req.getParameter("ticket");
    if ((ticket == null) || (ticket.trim().length() == 0)) {
        res.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    // Validate ticket at CAS server
    Cas20ProxyTicketValidator sv = new Cas20ProxyTicketValidator(serverURL);
    sv.setAcceptAnyProxy(true);
    Assertion a = sv.validate(ticket, clientURL);
    AttributePrincipal principal = a.getPrincipal();
    // Get user name logged in
    String userName = principal.getName();
    LOGGER.info("Login {}", userName);
    MCRUser user;
    boolean userExists = MCRUserManager.exists(userName, realmID);
    if (userExists)
        user = MCRUserManager.getUser(userName, realmID);
    else
        user = new MCRUser(userName, realmID);
    // Get user properties from LDAP server
    boolean userChanged = MCRLDAPClient.instance().updateUserProperties(user);
    if (userChanged && userExists) {
        MCRUserManager.updateUser(user);
    }
    // Store login user in session and redirect browser to target url
    MCRSessionMgr.getCurrentSession().setUserInformation(user);
    // MCR-1154
    req.changeSessionId();
    MCRLoginServlet.redirect(res);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MCRUser(org.mycore.user2.MCRUser) Assertion(org.jasig.cas.client.validation.Assertion) HttpServletResponse(javax.servlet.http.HttpServletResponse) Cas20ProxyTicketValidator(org.jasig.cas.client.validation.Cas20ProxyTicketValidator) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal)

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