Search in sources :

Example 16 with UserDAO

use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.

the class UserCommon method saveUser.

/**
	 * Updates the user information
	 * 
	 * @param userId int The user id we are saving
     * @return List
	 */
public static List saveUser(int userId) {
    List errors = new ArrayList();
    UserDAO um = DataAccessDriver.getInstance().newUserDAO();
    User u = um.selectById(userId);
    RequestContext request = JForumExecutionContext.getRequest();
    boolean isAdmin = SessionFacade.getUserSession().isAdmin();
    if (isAdmin) {
        String username = request.getParameter("username");
        if (username != null) {
            u.setUsername(username.trim());
        }
        if (request.getParameter("rank_special") != null) {
            u.setRankId(request.getIntParameter("rank_special"));
        }
    }
    SafeHtml safeHtml = new SafeHtml();
    u.setId(userId);
    u.setIcq(safeHtml.makeSafe(request.getParameter("icq")));
    u.setAim(safeHtml.makeSafe(request.getParameter("aim")));
    u.setMsnm(safeHtml.makeSafe(request.getParameter("msn")));
    u.setYim(safeHtml.makeSafe(request.getParameter("yim")));
    u.setFrom(safeHtml.makeSafe(request.getParameter("location")));
    u.setOccupation(safeHtml.makeSafe(request.getParameter("occupation")));
    u.setInterests(safeHtml.makeSafe(request.getParameter("interests")));
    u.setBiography(safeHtml.makeSafe(request.getParameter("biography")));
    u.setSignature(safeHtml.makeSafe(request.getParameter("signature")));
    u.setViewEmailEnabled(request.getParameter("viewemail").equals("1"));
    u.setViewOnlineEnabled(request.getParameter("hideonline").equals("0"));
    u.setNotifyPrivateMessagesEnabled(request.getParameter("notifypm").equals("1"));
    u.setNotifyOnMessagesEnabled(request.getParameter("notifyreply").equals("1"));
    u.setAttachSignatureEnabled(request.getParameter("attachsig").equals("1"));
    u.setHtmlEnabled(request.getParameter("allowhtml").equals("1"));
    u.setLang(request.getParameter("language"));
    u.setBbCodeEnabled("1".equals(request.getParameter("allowbbcode")));
    u.setSmiliesEnabled("1".equals(request.getParameter("allowsmilies")));
    u.setNotifyAlways("1".equals(request.getParameter("notify_always")));
    u.setNotifyText("1".equals(request.getParameter("notify_text")));
    String website = safeHtml.makeSafe(request.getParameter("website"));
    if (!StringUtils.isEmpty(website) && !website.toLowerCase().startsWith("http://")) {
        website = "http://" + website;
    }
    u.setWebSite(website);
    String currentPassword = request.getParameter("current_password");
    boolean isCurrentPasswordEmpty = currentPassword == null || "".equals(currentPassword.trim());
    if (isAdmin || !isCurrentPasswordEmpty) {
        if (!isCurrentPasswordEmpty) {
            currentPassword = MD5.crypt(currentPassword);
        }
        if (isAdmin || u.getPassword().equals(currentPassword)) {
            u.setEmail(safeHtml.makeSafe(request.getParameter("email")));
            String newPassword = request.getParameter("new_password");
            if (newPassword != null && newPassword.length() > 0) {
                u.setPassword(MD5.crypt(newPassword));
            }
        } else {
            errors.add(I18n.getMessage("User.currentPasswordInvalid"));
        }
    }
    if (request.getParameter("avatardel") != null) {
        File avatarFile = new File(u.getAvatar());
        File fileToDelete = new File(SystemGlobals.getApplicationPath() + "/images/avatar/" + avatarFile.getName());
        if (fileToDelete.exists()) {
            fileToDelete.delete();
        }
        u.setAvatar(null);
    }
    if (request.getObjectParameter("avatar") != null) {
        try {
            UserCommon.handleAvatar(u);
        } catch (Exception e) {
            UserCommon.logger.warn("Problems while uploading the avatar: " + e);
            errors.add(I18n.getMessage("User.avatarUploadError"));
        }
    } else if (SystemGlobals.getBoolValue(ConfigKeys.AVATAR_ALLOW_EXTERNAL_URL)) {
        String avatarUrl = request.getParameter("avatarUrl");
        if (!StringUtils.isEmpty(avatarUrl)) {
            if (avatarUrl.toLowerCase().startsWith("http://")) {
                try {
                    Image image = ImageIO.read(new URL(avatarUrl));
                    if (image != null) {
                        if (image.getWidth(null) > SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_WIDTH) || image.getHeight(null) > SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT)) {
                            errors.add(I18n.getMessage("User.avatarTooBig"));
                        } else {
                            u.setAvatar(avatarUrl);
                        }
                    }
                } catch (Exception e) {
                    errors.add(I18n.getMessage("User.avatarUploadError"));
                }
            } else {
                errors.add(I18n.getMessage("User.avatarUrlShouldHaveHttp"));
            }
        }
    }
    if (errors.size() == 0) {
        um.update(u);
        if (SessionFacade.getUserSession().getUserId() == userId) {
            SessionFacade.getUserSession().setLang(u.getLang());
        }
    }
    return errors;
}
Also used : User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) SafeHtml(net.jforum.util.SafeHtml) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RequestContext(net.jforum.context.RequestContext) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) File(java.io.File) URL(java.net.URL)

Example 17 with UserDAO

use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.

the class PostAction method listByUser.

public void listByUser() {
    PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
    UserDAO um = DataAccessDriver.getInstance().newUserDAO();
    TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
    User u = um.selectById(this.request.getIntParameter("user_id"));
    if (u.getId() == 0) {
        this.context.put("message", I18n.getMessage("User.notFound"));
        this.setTemplateName(TemplateKeys.USER_NOT_FOUND);
        return;
    }
    int count = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
    int start = ViewCommon.getStartPage();
    int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
    List posts = pm.selectByUserByLimit(u.getId(), start, postsPerPage);
    int totalMessages = pm.countUserPosts(u.getId());
    // get list of forums
    Map topics = new HashMap();
    Map forums = new HashMap();
    for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
        Post p = (Post) iter.next();
        if (!topics.containsKey(new Integer(p.getTopicId()))) {
            Topic t = TopicRepository.getTopic(new Topic(p.getTopicId()));
            if (t == null) {
                t = tm.selectRaw(p.getTopicId());
            }
            this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(t.getForumId())));
            this.context.put("am", new AttachmentCommon(this.request, t.getForumId()));
            topics.put(new Integer(t.getId()), t);
        }
        if (!forums.containsKey(new Integer(p.getForumId()))) {
            Forum f = ForumRepository.getForum(p.getForumId());
            if (f == null) {
                // Ok, probably the user does not have permission to see this forum
                iter.remove();
                totalMessages--;
                continue;
            }
            forums.put(new Integer(f.getId()), f);
        }
        PostCommon.preparePostForDisplay(p);
    }
    this.setTemplateName(TemplateKeys.POSTS_USER_POSTS_LIST);
    this.context.put("canDownloadAttachments", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
    this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
    this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
    this.context.put("posts", posts);
    this.context.put("topics", topics);
    this.context.put("forums", forums);
    this.context.put("u", u);
    this.context.put("pageTitle", I18n.getMessage("PostShow.userPosts") + " " + u.getUsername());
    this.context.put("karmaMin", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MIN_POINTS)));
    this.context.put("karmaMax", new Integer(SystemGlobals.getValue(ConfigKeys.KARMA_MAX_POINTS)));
    ViewCommon.contextToPagination(start, totalMessages, count);
}
Also used : User(net.jforum.entities.User) HashMap(java.util.HashMap) Post(net.jforum.entities.Post) TopicDAO(net.jforum.dao.TopicDAO) Forum(net.jforum.entities.Forum) PostDAO(net.jforum.dao.PostDAO) UserDAO(net.jforum.dao.UserDAO) Iterator(java.util.Iterator) List(java.util.List) Topic(net.jforum.entities.Topic) Map(java.util.Map) HashMap(java.util.HashMap) AttachmentCommon(net.jforum.view.forum.common.AttachmentCommon)

Example 18 with UserDAO

use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.

the class UserAction method profile.

public void profile() {
    DataAccessDriver da = DataAccessDriver.getInstance();
    UserDAO udao = da.newUserDAO();
    User u = udao.selectById(this.request.getIntParameter("user_id"));
    if (u.getId() == 0) {
        this.userNotFound();
    } else {
        this.setTemplateName(TemplateKeys.USER_PROFILE);
        this.context.put("karmaEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED));
        this.context.put("rank", new RankingRepository());
        this.context.put("u", u);
        this.context.put("avatarAllowExternalUrl", SystemGlobals.getBoolValue(ConfigKeys.AVATAR_ALLOW_EXTERNAL_URL));
        int loggedId = SessionFacade.getUserSession().getUserId();
        int count = 0;
        List bookmarks = da.newBookmarkDAO().selectByUser(u.getId());
        for (Iterator iter = bookmarks.iterator(); iter.hasNext(); ) {
            Bookmark b = (Bookmark) iter.next();
            if (b.isPublicVisible() || loggedId == u.getId()) {
                count++;
            }
        }
        this.context.put("pageTitle", I18n.getMessage("UserProfile.allAbout") + " " + u.getUsername());
        this.context.put("nbookmarks", new Integer(count));
        this.context.put("ntopics", new Integer(da.newTopicDAO().countUserTopics(u.getId())));
        this.context.put("nposts", new Integer(da.newPostDAO().countUserPosts(u.getId())));
    }
}
Also used : User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO) Bookmark(net.jforum.entities.Bookmark) DataAccessDriver(net.jforum.dao.DataAccessDriver) RankingRepository(net.jforum.repository.RankingRepository) Iterator(java.util.Iterator) List(java.util.List)

Example 19 with UserDAO

use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.

the class UserAction method activateAccount.

public void activateAccount() {
    String hash = this.request.getParameter("hash");
    int userId = (new Integer(this.request.getParameter("user_id"))).intValue();
    UserDAO um = DataAccessDriver.getInstance().newUserDAO();
    User u = um.selectById(userId);
    boolean isValid = um.validateActivationKeyHash(userId, hash);
    if (isValid) {
        // Activate the account
        um.writeUserActive(userId);
        this.logNewRegisteredUserIn(userId, u);
    } else {
        this.setTemplateName(TemplateKeys.USER_INVALID_ACTIVATION);
        this.context.put("message", I18n.getMessage("User.invalidActivationKey", new Object[] { this.request.getContextPath() + "/user/activateManual" + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) }));
    }
}
Also used : User(net.jforum.entities.User) UserDAO(net.jforum.dao.UserDAO)

Example 20 with UserDAO

use of net.jforum.dao.UserDAO in project jforum2 by rafaelsteil.

the class PostAction method quote.

public void quote() {
    PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
    Post p = pm.selectById(this.request.getIntParameter("post_id"));
    if (p.getId() == 0) {
        this.postNotFound();
        return;
    }
    if (p.isModerationNeeded()) {
        this.notModeratedYet();
        return;
    }
    if (!this.anonymousPost(p.getForumId())) {
        return;
    }
    Topic topic = TopicRepository.getTopic(new Topic(p.getTopicId()));
    if (topic == null) {
        topic = DataAccessDriver.getInstance().newTopicDAO().selectRaw(p.getTopicId());
    }
    if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
        return;
    }
    if (topic.getStatus() == Topic.STATUS_LOCKED) {
        this.topicLocked();
        return;
    }
    this.setTemplateName(TemplateKeys.POSTS_QUOTE);
    this.context.put("forum", ForumRepository.getForum(p.getForumId()));
    this.context.put("action", "insertSave");
    this.context.put("post", p);
    UserDAO um = DataAccessDriver.getInstance().newUserDAO();
    User u = um.selectById(p.getUserId());
    int userId = SessionFacade.getUserSession().getUserId();
    this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(topic.getForumId())));
    QuotaLimit ql = new AttachmentCommon(this.request, topic.getForumId()).getQuotaLimit(userId);
    this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
    this.context.put("moderationLoggingEnabled", SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED));
    this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
    this.context.put("isNewPost", true);
    this.context.put("topic", topic);
    this.context.put("quote", "true");
    this.context.put("quoteUser", u.getUsername());
    this.context.put("setType", false);
    this.context.put("htmlAllowed", SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, Integer.toString(topic.getForumId())));
    this.context.put("start", this.request.getParameter("start"));
    this.context.put("user", DataAccessDriver.getInstance().newUserDAO().selectById(userId));
    this.context.put("pageTitle", I18n.getMessage("PostForm.reply") + " " + topic.getTitle());
    this.context.put("smilies", SmiliesRepository.getSmilies());
    boolean needCaptcha = SystemGlobals.getBoolValue(ConfigKeys.CAPTCHA_POSTS);
    if (needCaptcha) {
        SessionFacade.getUserSession().createNewCaptcha();
    }
    this.context.put("needCaptcha", needCaptcha);
}
Also used : User(net.jforum.entities.User) PostDAO(net.jforum.dao.PostDAO) UserDAO(net.jforum.dao.UserDAO) Post(net.jforum.entities.Post) Topic(net.jforum.entities.Topic) QuotaLimit(net.jforum.entities.QuotaLimit) AttachmentCommon(net.jforum.view.forum.common.AttachmentCommon)

Aggregations

UserDAO (net.jforum.dao.UserDAO)26 User (net.jforum.entities.User)18 List (java.util.List)12 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)6 Topic (net.jforum.entities.Topic)6 Post (net.jforum.entities.Post)5 HashMap (java.util.HashMap)4 PostDAO (net.jforum.dao.PostDAO)4 AttachmentCommon (net.jforum.view.forum.common.AttachmentCommon)4 Map (java.util.Map)3 DataAccessDriver (net.jforum.dao.DataAccessDriver)3 Forum (net.jforum.entities.Forum)3 UserSession (net.jforum.entities.UserSession)3 Date (java.util.Date)2 TopicDAO (net.jforum.dao.TopicDAO)2 Group (net.jforum.entities.Group)2 PrivateMessage (net.jforum.entities.PrivateMessage)2 QuotaLimit (net.jforum.entities.QuotaLimit)2 APIException (net.jforum.exceptions.APIException)2