Search in sources :

Example 1 with MCRUserAttributeMapper

use of org.mycore.user2.MCRUserAttributeMapper 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 MCRUserAttributeMapper

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

the class MCRShibbolethLoginServlet method doGetPost.

public void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest req = job.getRequest();
    HttpServletResponse res = job.getResponse();
    String msg = null;
    String uid = (String) req.getAttribute("uid");
    String userId = uid != null ? uid : req.getRemoteUser();
    if (userId != null) {
        final String realmId = userId.contains("@") ? userId.substring(userId.indexOf("@") + 1) : null;
        if (realmId != null && MCRRealmFactory.getRealm(realmId) != null) {
            userId = realmId != null ? userId.replace("@" + realmId, "") : userId;
            final Map<String, Object> attributes = new HashMap<>();
            final MCRUserAttributeMapper attributeMapper = MCRRealmFactory.getAttributeMapper(realmId);
            for (final String key : attributeMapper.getAttributeNames()) {
                final Object value = req.getAttribute(key);
                if (value != null) {
                    LOGGER.info("received {}:{}", key, value);
                    attributes.put(key, value);
                }
            }
            MCRUserInformation userinfo;
            MCRUser user = MCRUserManager.getUser(userId, realmId);
            if (user != null) {
                LOGGER.debug("login existing user \"{}\"", user.getUserID());
                attributeMapper.mapAttributes(user, attributes);
                user.setLastLogin();
                MCRUserManager.updateUser(user);
                userinfo = user;
            } else {
                userinfo = new MCRShibbolethUserInformation(userId, realmId, attributes);
            }
            MCRSessionMgr.getCurrentSession().setUserInformation(userinfo);
            // MCR-1154
            req.changeSessionId();
            res.sendRedirect(res.encodeRedirectURL(req.getParameter("url")));
            return;
        } else {
            msg = "Login from realm \"" + realmId + "\" is not allowed.";
        }
    } else {
        msg = "Principal could not be received from IDP.";
    }
    job.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED, msg);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) MCRUser(org.mycore.user2.MCRUser) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRUserAttributeMapper(org.mycore.user2.MCRUserAttributeMapper) MCRUserInformation(org.mycore.common.MCRUserInformation)

Aggregations

HashMap (java.util.HashMap)2 MCRUserInformation (org.mycore.common.MCRUserInformation)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Document (org.jdom2.Document)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 Test (org.junit.Test)1 MCRUser (org.mycore.user2.MCRUser)1 MCRUserAttributeMapper (org.mycore.user2.MCRUserAttributeMapper)1 MCRShibbolethUserInformation (org.mycore.user2.login.MCRShibbolethUserInformation)1