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);
}
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));
}
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);
}
}
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);
}
}
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);
}
Aggregations