use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.
the class ControllerUtils method checkAutoLogin.
/**
* Checks user credentials / automatic login.
*
* @param userSession The UserSession instance associated to the user's session
* @return <code>true</code> if auto login was enabled and the user was sucessfuly
* logged in.
* @throws DatabaseException
*/
protected boolean checkAutoLogin(UserSession userSession) {
String cookieName = SystemGlobals.getValue(ConfigKeys.COOKIE_NAME_DATA);
Cookie cookie = this.getCookieTemplate(cookieName);
Cookie hashCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_USER_HASH));
Cookie autoLoginCookie = this.getCookieTemplate(SystemGlobals.getValue(ConfigKeys.COOKIE_AUTO_LOGIN));
if (hashCookie != null && cookie != null && !cookie.getValue().equals(SystemGlobals.getValue(ConfigKeys.ANONYMOUS_USER_ID)) && autoLoginCookie != null && "1".equals(autoLoginCookie.getValue())) {
String uid = cookie.getValue();
String uidHash = hashCookie.getValue();
// Load the user-specific security hash from the database
try {
UserDAO userDao = DataAccessDriver.getInstance().newUserDAO();
String userHash = userDao.getUserAuthHash(Integer.parseInt(uid));
if (userHash == null || userHash.trim().length() == 0) {
return false;
}
String securityHash = MD5.crypt(userHash);
if (securityHash.equals(uidHash)) {
int userId = Integer.parseInt(uid);
userSession.setUserId(userId);
User user = userDao.selectById(userId);
if (user == null || user.getId() != userId || user.isDeleted()) {
userSession.makeAnonymous();
return false;
}
this.configureUserSession(userSession, user);
return true;
}
} catch (Exception e) {
throw new DatabaseException(e);
}
userSession.makeAnonymous();
}
return false;
}
use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.
the class UserREST method insert.
/**
* Creates a new user.
* Required parameters ara "username", "email" and "password".
*/
public void insert() {
try {
this.authenticate();
String username = this.requiredRequestParameter("username");
String email = this.requiredRequestParameter("email");
String password = this.requiredRequestParameter("password");
if (username.length() > SystemGlobals.getIntValue(ConfigKeys.USERNAME_MAX_LENGTH)) {
throw new APIException(I18n.getMessage("User.usernameTooBig"));
}
if (username.indexOf('<') > -1 || username.indexOf('>') > -1) {
throw new APIException(I18n.getMessage("User.usernameInvalidChars"));
}
UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
if (dao.isUsernameRegistered(username)) {
throw new APIException(I18n.getMessage("UsernameExists"));
}
if (dao.findByEmail(email) != null) {
throw new APIException(I18n.getMessage("User.emailExists", new Object[] { email }));
}
// Ok, time to insert the user
User user = new User();
user.setUsername(username);
user.setEmail(email);
user.setPassword(password);
int userId = dao.addNew(user);
this.setTemplateName(TemplateKeys.API_USER_INSERT);
this.context.put("userId", new Integer(userId));
} catch (Exception e) {
this.setTemplateName(TemplateKeys.API_ERROR);
this.context.put("exception", e);
}
}
use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.
the class GenericPrivateMessageDAO method getPm.
protected PrivateMessage getPm(ResultSet rs, boolean full) throws SQLException {
PrivateMessage pm = new PrivateMessage();
Post p = new Post();
pm.setId(rs.getInt("privmsgs_id"));
pm.setType(rs.getInt("privmsgs_type"));
p.setTime(new Date(rs.getTimestamp("privmsgs_date").getTime()));
p.setSubject(rs.getString("privmsgs_subject"));
SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
pm.setFormatedDate(df.format(p.getTime()));
if (full) {
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
pm.setFromUser(um.selectById(rs.getInt("privmsgs_from_userid")));
pm.setToUser(um.selectById(rs.getInt("privmsgs_to_userid")));
p.setBbCodeEnabled(rs.getInt("privmsgs_enable_bbcode") == 1);
p.setSignatureEnabled(rs.getInt("privmsgs_attach_sig") == 1);
p.setHtmlEnabled(rs.getInt("privmsgs_enable_html") == 1);
p.setSmiliesEnabled(rs.getInt("privmsgs_enable_smilies") == 1);
p.setText(this.getPmText(rs));
}
pm.setPost(p);
return pm;
}
use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.
the class UserAction method insertSave.
public void insertSave() {
UserSession userSession = SessionFacade.getUserSession();
int userId = userSession.getUserId();
if ((!SystemGlobals.getBoolValue(ConfigKeys.REGISTRATION_ENABLED) && !SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) || ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
this.registrationDisabled();
return;
}
User u = new User();
UserDAO dao = DataAccessDriver.getInstance().newUserDAO();
String username = this.request.getParameter("username");
String password = this.request.getParameter("password");
String email = this.request.getParameter("email");
String captchaResponse = this.request.getParameter("captchaResponse");
boolean error = false;
if (username == null || username.trim().equals("") || password == null || password.trim().equals("")) {
this.context.put("error", I18n.getMessage("UsernamePasswordCannotBeNull"));
error = true;
}
if (username != null) {
username = username.trim();
}
if (!error && username.length() > SystemGlobals.getIntValue(ConfigKeys.USERNAME_MAX_LENGTH)) {
this.context.put("error", I18n.getMessage("User.usernameTooBig"));
error = true;
}
if (!error && username.indexOf('<') > -1 || username.indexOf('>') > -1) {
this.context.put("error", I18n.getMessage("User.usernameInvalidChars"));
error = true;
}
if (!error && dao.isUsernameRegistered(username)) {
this.context.put("error", I18n.getMessage("UsernameExists"));
error = true;
}
if (!error && dao.findByEmail(email) != null) {
this.context.put("error", I18n.getMessage("User.emailExists", new String[] { email }));
error = true;
}
if (!error && !userSession.validateCaptchaResponse(captchaResponse)) {
this.context.put("error", I18n.getMessage("CaptchaResponseFails"));
error = true;
}
if (error) {
this.insert(true);
return;
}
u.setUsername(username);
u.setPassword(MD5.crypt(password));
u.setEmail(email);
boolean requiresMailActivation = SystemGlobals.getBoolValue(ConfigKeys.MAIL_USER_EMAIL_AUTH);
if (requiresMailActivation) {
u.setActivationKey(MD5.crypt(username + System.currentTimeMillis()));
}
int newUserId = dao.addNew(u);
if (requiresMailActivation) {
Executor.execute(new EmailSenderTask(new ActivationKeySpammer(u)));
this.setTemplateName(TemplateKeys.USER_INSERT_ACTIVATE_MAIL);
this.context.put("message", I18n.getMessage("User.GoActivateAccountMessage"));
} else if (SecurityRepository.get(userId).canAccess(SecurityConstants.PERM_ADMINISTRATION)) {
JForumExecutionContext.setRedirect(this.request.getContextPath() + "/adminUsers/list" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
} else {
this.logNewRegisteredUserIn(newUserId, u);
}
if (!requiresMailActivation) {
dao.writeUserActive(newUserId);
}
}
use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.
the class UserAction method prepareLostPassword.
public User prepareLostPassword(String username, String email) {
User user = null;
UserDAO um = DataAccessDriver.getInstance().newUserDAO();
if (email != null && !email.trim().equals("")) {
username = um.getUsernameByEmail(email);
}
if (username != null && !username.trim().equals("")) {
List l = um.findByName(username, true);
if (l.size() > 0) {
user = (User) l.get(0);
}
}
if (user == null) {
return null;
}
String hash = MD5.crypt(user.getEmail() + System.currentTimeMillis() + SystemGlobals.getValue(ConfigKeys.USER_HASH_SEQUENCE) + new Random().nextInt(999999));
um.writeLostPasswordHash(user.getEmail(), hash);
user.setActivationKey(hash);
return user;
}
Aggregations