練功房推薦書單

  • 猛虎出柙雙劍合璧版:最新 OCA / OCP Java SE 7 Programmer 專業認證
  • Java SE 8 技術手冊
  • SCJP 6.0認證教戰手冊(第二版)Oracle Certified Professional Java Programmer(附光碟)
  • 麥克風的股市求生手冊
Display the name of JForum's user as his real name instead of his username(login ID)  XML
Forum Index » JForum中文社群 JForum Chinese Users Community
Author Message
andowson

六段學員
[Avatar]

Joined: 2007-01-02 22:20:40
Messages: 680
Location: 台北
Offline

If we do an SSO integration with user's username as his social security number or identification number from another webapp, and it is not good to show this information to public. We need to display the name of JForum's user as his real name instead of his username(login ID).

After finishing JForum SSO with user's first name and last name saved.
Here is a guideline for doing this:

1.We need to get user's real name as first_name and last_name from the jfoum_users table. Modify net.jforum.dao.generic.GenericUserDAO.java:
protected void fillUserFromResultSet(User u, ResultSet rs) throws SQLException
{
		...
		u.setFirstName(rs.getString("first_name"));
		u.setLastName(rs.getString("last_name"));
}

protected List processSelectAll(ResultSet rs) throws SQLException
{
			...
			u.setFirstName(rs.getString("first_name"));
			u.setLastName(rs.getString("last_name"));
			...
}

public User getLastUserInfo()
{
			...
			u.setUsername(rs.getString("username"));
			u.setId(rs.getInt("user_id"));
			u.setFirstName(rs.getString("first_name"));
			u.setLastName(rs.getString("last_name"));

			return u;
			...
}

2.Modify WEB-INF/config/database/generic/generic_queries.sql, add first_name, last_name to the original SQL statement. Here are some queries needing modification:
UserModel.selectAll = SELECT user_email, user_id, user_posts, user_regdate, username, deleted, user_karma, user_from, \
	user_website, user_viewemail, first_name, last_name FROM jforum_users ORDER BY user_id

UserModel.selectAllByLimit = SELECT user_email, user_id, user_posts, user_regdate, username, deleted, user_karma, user_from, user_website, user_viewemail, first_name, last_name \
	FROM jforum_users ORDER BY username LIMIT ?, ?

UserModel.selectAllByGroup = SELECT user_email, u.user_id, user_posts, user_regdate, username, deleted, user_karma, user_from, \
	user_website, user_viewemail, first_name, last_name \
	FROM jforum_users u, jforum_user_groups ug \
	WHERE u.user_id = ug.user_id \
	AND ug.group_id = ? \
	ORDER BY user_id LIMIT ?, ?

UserModel.findByName = SELECT user_id, username, user_email, deleted, first_name, last_name FROM jforum_users WHERE UPPER(username) LIKE UPPER(?) 

UserModel.update = UPDATE jforum_users SET user_aim = ?, \
	user_avatar = ?,\
	gender = ?, \
	themes_id = ?,\
	user_allow_pm = ?, \
	user_allowavatar = ?, \
	user_allowbbcode = ?, \
	user_allowhtml = ?, \
	user_allowsmilies = ?, \
	user_email = ?, \
	user_from = ?, \
	user_icq = ?, \
	user_interests = ?, \
	user_occ = ?, \
	user_sig = ?, \
	user_website = ?, \
	user_yim = ?, \
	user_msnm = ?, \
	user_password = ?, \
	user_viewemail = ?, \
	user_viewonline = ?, \
	user_notify = ?, \
	user_attachsig = ?, \
	username = ?, \
	user_lang = ?, \
	user_notify_pm = ?, \
	user_biography = ?, \
	user_lastvisit = ?, \
	user_notify_always = ?, \
	user_notify_text = ?, \
	rank_id = ?, \
	first_name = ?, \
	last_name = ? \
	WHERE user_id = ?

UserModel.lastUserRegistered=SELECT user_id, username, first_name, last_name FROM jforum_users ORDER BY user_regdate DESC LIMIT 1

ForumModel.lastPostInfo = SELECT post_time, p.topic_id, t.topic_replies, post_id, u.user_id, username, first_name, last_name \
	FROM jforum_posts p, jforum_users u, jforum_topics t , jforum_forums f \
	WHERE t.forum_id = f.forum_id \
	AND t.topic_id = p.topic_id \
	AND f.forum_last_post_id = t.topic_last_post_id \
	AND t.topic_last_post_id = p.post_id \
	AND p.forum_id = ? \
	AND p.user_id = u.user_id \
    AND p.need_moderate = 0

TopicModel.topicPosters = SELECT user_id, username, user_karma, user_avatar, user_allowavatar, user_regdate, user_posts, user_icq, \
	user_from, user_email, rank_id, user_sig, user_attachsig, user_viewemail, user_msnm, user_yim, user_website, user_sig, user_aim, first_name, last_name \
	FROM jforum_users \
	WHERE user_id IN (:ids:)

TopicModel.getUserInformation = SELECT user_id, username, first_name, last_name FROM jforum_users WHERE user_id IN (#ID#)

 
SearchModel.getPostsDataForLucene = SELECT p.post_id, p.forum_id, p.topic_id, p.user_id, u.username, p.enable_bbcode, p.enable_smilies, p.post_time, pt.post_subject, pt.post_text, t.topic_title, u.first_name, u.last_name \
	FROM jforum_posts p, jforum_posts_text pt, jforum_users u, jforum_topics t \
	WHERE p.post_id IN (:posts:) \
	AND p.post_id = pt.post_id  \
	AND p.topic_id = t.topic_id \
	AND p.user_id = u.user_Id

PrivateMessageModel.baseListing = SELECT pm.privmsgs_type, pm.privmsgs_id, pm.privmsgs_date, pm.privmsgs_subject, u.user_id, u.username, u.first_name, u.last_name \
	FROM jforum_privmsgs pm, jforum_users u \
	#FILTER# \
	ORDER BY pm.privmsgs_date DESC

ModerationModel.topicsByForum = SELECT p.post_id, t.topic_id, t.topic_title, t.topic_replies, p.user_id, enable_bbcode, p.attach, \
	enable_html, enable_smilies, pt.post_subject, pt.post_text, username, first_name, last_name \
	FROM jforum_posts p, jforum_posts_text pt, jforum_users u, jforum_topics t \
	WHERE p.post_id = pt.post_id \
	AND p.topic_id = t.topic_id \
	AND t.forum_id = ? \
	AND p.user_id = u.user_id \
	AND p.need_moderate = 1 \
	ORDER BY t.topic_id, post_time ASC

ModerationLog.selectAll = SELECT l.*, u.username, u2.username AS poster_username, u2.first_name AS poster_first_name, u2.last_name AS poster_last_name FROM jforum_moderation_log l \
	LEFT JOIN jforum_users u2 ON u2.user_id = l.post_user_id \
	LEFT JOIN jforum_users u ON l.user_id = u.user_id \
	ORDER BY log_id DESC \
	LIMIT ?, ?

3.JForum provide a method getName() in net.jforum.entities.User.java, we can modify it as follows to change the order of first name and last name for some languages like zh_CN and zh_TW:
	public String getName()
	{
		String defaultLang = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT);
		if ((this.lang != null && this.lang.startsWith("zh")) || defaultLang.startsWith("zh")) {
			return this.lastName + this.firstName;
		} else {
		    return this.firstName + " " + this.lastName;
		}
	}

4.Modify net.jforum.entities.UserSession.java, add a new attribute "name" and modify some method to store its value:
private String name;
public String getName()
public String setName(String name)

public UserSession(UserSession us)
{
                ...
		this.name = us.getName();
                ...
}

public void dataToUser(User user)
{
                ...
		this.setName(user.getName());
                ...
}

5.Modify net.jforum.dao.generic.GenericUserSessionDAO.java
public UserSession selectById(UserSession us, Connection conn)
	public UserSession selectById(UserSession us, Connection conn)
	{
                        ...
			if (rs.next()) {
				returnUs.setSessionTime(rs.getLong("session_time"));
				returnUs.setStartTime(rs.getTimestamp("session_start"));
				found = true;
			}
			rs.close();
			rs = null;
			p.close();			
			p = null;
			
			p = conn.prepareStatement(SystemGlobals.getSql("UserModel.selectById"));
			p.setInt(1, us.getUserId());
			rs = p.executeQuery();
			if (rs.next()) {
				String defaultLang = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT);
				String lang = returnUs.getLang();
				if ((lang != null && lang.startsWith("zh")) || defaultLang.startsWith("zh")) {				
				    returnUs.setName(rs.getString("last_name")+rs.getString("first_name"));
				} else {
					returnUs.setName(rs.getString("first_name") + " " + rs.getString("last_name"));
				}
			}
			return (found ? returnUs : null);
		}
                ...
	}

6.Modify net.jforum.entities.LastPostInfo.java
private String name;
public String getName()
public String setName(String name)

7.Modify net.jforum.repository.ForumRepository.java
public static synchronized void updateForumStats(Topic t, User u, Post p)
{
                        ...
			lpi.setUsername(u.getUsername());
			lpi.setName(u.getName());
			forum.setLastPostInfo(lpi);
                        ...
}

8.Modify net.jforum.dao.generic.GenericForumDAO.java
private LastPostInfo getLastPostInfo(int forumId, boolean tryFix)
{
                                ...
			if (rs.next()) {
                                ...
				String defaultLang = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT);
				if (defaultLang.startsWith("zh")) {
				    lpi.setName(rs.getString("last_name")+ rs.getString("first_name"));
				} else {
					lpi.setName(rs.getString("first_name")+ " " + rs.getString("last_name"));								
				}
                                ...
}

9.Modify net.jforum.view.forum.UserAction.java
public void edit()
{
                        ...
			this.context.put("u", u);
			this.context.put("action", "editSave");
			this.context.put("pageTitle", I18n.getMessage("UserProfile.profileFor") + " " + u.getName());
                        ...
}

public void profile()
{
                        ...
			this.context.put("pageTitle", I18n.getMessage("UserProfile.allAbout")+" "+u.getName());
                        ...
}

10.Modify net.jforum.dao.generic.GenericTopicDAO.java
public List fillTopicsData(PreparedStatement p)
{
                               ...
				while (rs.next()) {
					User u = new User();
					u.setId(rs.getInt("user_id"));
					u.setUsername(rs.getString("username"));
					u.setFirstName(rs.getString("first_name"));
					u.setLastName(rs.getString("last_name"));
					users.put(new Integer(rs.getInt("user_id")), u);
				}
                                 ...
				for (Iterator iter = l.iterator(); iter.hasNext();) {
					Topic t = (Topic) iter.next();					
					t.setPostedBy((User)users.get(new Integer(t.getPostedBy().getId())));
					t.setLastPostBy((User)users.get(new Integer(t.getLastPostBy().getId())));
				}
                                ...
}

public Map topicPosters(int topicId)
{
                                ...
				u.setSignature(rs.getString("user_sig"));
				u.setFirstName(rs.getString("first_name"));
				u.setLastName(rs.getString("last_name"));

				m.put(new Integer(u.getId()), u);
                                ...
}

11.Modify net.jforum.dao.generic.GenericPrivateMessageDAO.java
public List selectFromInbox(User user)
{
                               ...
				fromUser.setFirstName(rs.getString("first_name"));
				fromUser.setLastName(rs.getString("last_name"));
                               ...
}

public List selectFromSent(User user)
{
                                ...
				toUser.setFirstName(rs.getString("first_name"));
				toUser.setLastName(rs.getString("last_name"));
                                ...
}

12.Modify net.jforum.view.forum.PrivateMessageAction.java
public void sendTo()
{
                        ...	
			this.context.put("toUsername", recipient.getName());
			this.context.put("pageTitle", I18n.getMessage("PrivateMessage.title") 
				+ " " + I18n.getMessage("PrivateMessage.to") 
				+ " " + recipient.getName());
                        ...
}

public void quote()
{
                ...
		this.context.put("quote", "true");
		this.context.put("quoteUser", pm.getFromUser().getNickname());
		this.context.put("post", pm.getPost());
                ...
}

13.Modify net.jforum.entities.Post.java
private String authorName;
public String getAuthorName()
public void setAuthorName(String authorName)
public Post(Post p) {
...
this.setAuthorName(p.getAuthorName());
}

14.Modify net.jforum.dao.generic.GenericLuceneDAO.java
public List getPostsData(int[] postIds)
{
                                                ...
			while (rs.next()) {
				Post post = this.makePost(rs);
				post.setPostUsername(rs.getString("username"));
				String lang = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT);
				if (lang.startsWith("zh")) {
				    post.setAuthorName(rs.getString("last_name") + rs.getString("first_name"));
				} else {
				    post.setAuthorName(rs.getString("first_name") + " " + rs.getString("last_name"));
				}
				
				l.add(post);
			}
                                                ...
}

15.Modify net.jforum.view.forum.PostAction.java
public void quote()
{
                                ...
		this.context.put("quoteUser", u.getName());
                                ...
}

public void listByUser()
{
		...
		this.context.put("pageTitle", I18n.getMessage("PostShow.userPosts") + " " + u.getName());
		...
}

16.Modify net.jforum.view.forum.RecentTopicsAction.java
public void showTopicsByUser() 
{
		...
		this.context.put("pageTitle", I18n.getMessage("ForumListing.userTopics") + " " + u.getName());
		...
}

17. Modify net.jforum.view.forum.HottestTopicsAction.java
public void showTopicsByUser() 
{
                ...
		this.context.put("u", u);
		this.context.put("pageTitle", I18n.getMessage("ForumListing.userTopics") + " " + u.getNickname());
                ...

}

18. Modify net.jforum.dao.generic.GenericModerationDAO.java
	protected Post getPost(ResultSet rs) throws SQLException
	{
		Post p = new Post();
		
		p.setPostUsername(rs.getString("username"));
                                String lang = SystemGlobals.getValue(ConfigKeys.I18N_DEFAULT);
		if (lang.startsWith("zh")) {
		    p.setAuthorName(rs.getString("last_name") + rs.getString("first_name"));
		} else {
		    p.setAuthorName(rs.getString("first_name") + " " + rs.getString("last_name"));
		}
		p.setId(rs.getInt("post_id"));
		p.setUserId(rs.getInt("user_id"));
		p.setBbCodeEnabled(rs.getInt("enable_bbcode") == 1);
		p.setHtmlEnabled(rs.getInt("enable_html") == 1);
		p.setSmiliesEnabled(rs.getInt("enable_smilies") == 1);
		p.setSubject(rs.getString("post_subject"));
		p.setText(this.getPostTextFromResultSet(rs));
		
		return p;
	}

19.Modify net.jforum.view.forum.BookmarkAction.java
	private void addUser()
	{
		User u = DataAccessDriver.getInstance().newUserDAO().selectById(
				this.request.getIntParameter("relation_id"));
		String title = u.getName();
                ...
        }
 
	public void list()
	{
		...
		this.context.put("pageTitle", I18n.getMessage("Bookmarks.for")+" "+u.getName());				
	}

20.Modify net.jforum.view.forum.ForumAction.java
public void list()
{
...
if (onlineUsersList.size() == 0) {
UserSession us = new UserSession();

us.setUserId(aid);
us.setUsername(I18n.getMessage("Guest"));
us.setFirstName(I18n.getMessage("Guest"));

onlineUsersList.add(us);
}
...
}
21.Replace ".username" with ".name" in the following files under templates/default/ diectory:
forum_list.htm
forum_show.htm
recent_thread.htm
hottest_thread.htm
new_message.htm
user_list.htm
user_posts_show.htm
user_topics_show.htm
post_show_user_inc.htm
topic_review.htm
pm_list.htm
pm_finduser.htm
pm_read_message.htm
pm_review_message.htm
bookmark_list.htm

22.Replace ${topic.postedBy.username?html} with ${u.name?html} and replace ${topic.lastPostBy.username?html? with ${u.name?html} in the following files under templates/default/ diectory:
user_topics_show.htm
new_message.htm

23.Replace ".postUsername" with ".authorName" in the following files under templates/default/ diectory:
search_result.htm
forum_show.htm

This message was edited 36 times. Last update was at 2010-04-22 10:05:04

收進你的MyShare個人書籤 plurk twitter 分享

分享經驗 累積智慧
[WWW] [MSN]
 
Forum Index » JForum中文社群 JForum Chinese Users Community
Go to: