Search in sources :

Example 6 with MCRUser

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

the class MCRLDAPClient method main.

public static void main(String[] args) throws Exception {
    String userName = args[0];
    String realmID = args[1];
    MCRUser user = MCRUserManager.getUser(userName, realmID);
    if (user == null) {
        user = new MCRUser(userName, realmID);
    }
    LOGGER.info("\n{}", new XMLOutputter(Format.getPrettyFormat()).outputString(MCRUserTransformer.buildExportableSafeXML(user)));
    MCRLDAPClient.instance().updateUserProperties(user);
    LOGGER.info("\n{}", new XMLOutputter(Format.getPrettyFormat()).outputString(MCRUserTransformer.buildExportableSafeXML(user)));
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRUser(org.mycore.user2.MCRUser)

Example 7 with MCRUser

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

the class MCRLoginServlet method addCurrentUserInfo.

static void addCurrentUserInfo(MCRLogin login) {
    MCRUserInformation userInfo = MCRSessionMgr.getCurrentSession().getUserInformation();
    String realmId = (userInfo instanceof MCRUser) ? ((MCRUser) userInfo).getRealm().getLabel() : userInfo.getUserAttribute(MCRRealm.USER_INFORMATION_ATTR);
    if (realmId == null) {
        realmId = MCRRealmFactory.getLocalRealm().getLabel();
    }
    login.setRealm(realmId);
}
Also used : MCRUser(org.mycore.user2.MCRUser) MCRUserInformation(org.mycore.common.MCRUserInformation)

Example 8 with MCRUser

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

the class MCRLoginServlet method addCurrentUserInfo.

static void addCurrentUserInfo(Element rootElement) {
    MCRUserInformation userInfo = MCRSessionMgr.getCurrentSession().getUserInformation();
    rootElement.setAttribute("user", userInfo.getUserID());
    String realmId = (userInfo instanceof MCRUser) ? ((MCRUser) userInfo).getRealm().getLabel() : userInfo.getUserAttribute(MCRRealm.USER_INFORMATION_ATTR);
    if (realmId == null) {
        realmId = MCRRealmFactory.getLocalRealm().getLabel();
    }
    rootElement.setAttribute(REALM_URL_PARAMETER, realmId);
    rootElement.setAttribute("guest", String.valueOf(currentUserIsGuest()));
}
Also used : MCRUser(org.mycore.user2.MCRUser) MCRUserInformation(org.mycore.common.MCRUserInformation)

Example 9 with MCRUser

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

Example 10 with MCRUser

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

the class MCRUserAttributeMapperTest method testUserCreate.

@Test
public void testUserCreate() 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();
    MCRUser storedUser = MCRUserManager.getUser(user.getUserName(), realmId);
    assertEquals(mcrUser.getEMailAddress(), storedUser.getEMailAddress());
    assertEquals(extraAttribs.get("attrib1"), storedUser.getAttributes().get("attrib1"));
    assertEquals(extraAttribs.get("attrib2"), storedUser.getAttributes().get("attrib2"));
    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)

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