use of org.mycore.user2.login.MCRShibbolethUserInformation 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);
}
use of org.mycore.user2.login.MCRShibbolethUserInformation 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);
}
use of org.mycore.user2.login.MCRShibbolethUserInformation 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);
}
use of org.mycore.user2.login.MCRShibbolethUserInformation 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"));
}
Aggregations