001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.proxy;
026:
027: import java.sql.SQLException;
028: import java.util.Date;
029: import java.util.Iterator;
030:
031: import org.nemesis.forum.Authorization;
032: import org.nemesis.forum.Forum;
033: import org.nemesis.forum.ForumPermissions;
034: import org.nemesis.forum.ForumThread;
035: import org.nemesis.forum.Message;
036: import org.nemesis.forum.TreeWalker;
037: import org.nemesis.forum.impl.DbForumThread;
038: import org.nemesis.forum.config.Constants;
039: import org.nemesis.forum.exception.ForumMessageNotFoundException;
040: import org.nemesis.forum.exception.UnauthorizedException;
041:
042: /**
043: * Protection proxy for ForumThread objects. It restricts access to protected
044: * methods by throwing UnauthorizedExceptions if the user does not have
045: * permission to access the class.
046: */
047: public class ForumThreadProxy implements ForumThread {
048:
049: private ForumThread thread;
050: private Authorization authorization;
051: private ForumPermissions permissions;
052:
053: /**
054: * Creates a new proxy.
055: */
056: public ForumThreadProxy(ForumThread thread,
057: Authorization authorization, ForumPermissions permissions) {
058: this .thread = thread;
059: this .authorization = authorization;
060: this .permissions = permissions;
061: }
062:
063: public int getID() {
064: return thread.getID();
065: }
066:
067: public String getName() {
068: return thread.getName();
069: }
070:
071: public boolean isApproved() {
072: return thread.isApproved();
073: }
074:
075: public void setApproved(boolean approved)
076: throws UnauthorizedException {
077: if (permissions.isSystemOrForumAdmin()
078: || permissions.get(Constants.MODERATOR)) {
079: thread.setApproved(approved);
080: } else {
081: throw new UnauthorizedException();
082: }
083: }
084:
085: public Date getCreationDate() {
086: return thread.getCreationDate();
087: }
088:
089: public void setCreationDate(Date creationDate)
090: throws UnauthorizedException {
091: if (permissions.isSystemOrForumAdmin()) {
092: thread.setCreationDate(creationDate);
093: } else
094: throw new UnauthorizedException();
095: }
096:
097: public Date getModifiedDate() {
098: return thread.getModifiedDate();
099: }
100:
101: public void setModifiedDate(Date modifiedDate)
102: throws UnauthorizedException {
103: if (permissions.isSystemOrForumAdmin()) {
104: thread.setModifiedDate(modifiedDate);
105: } else
106: throw new UnauthorizedException();
107: }
108:
109: public Forum getForum() {
110: Forum forum = thread.getForum();
111: return new ForumProxy(forum, authorization, permissions);
112: }
113:
114: public int getMessageCount() {
115: return thread.getMessageCount();
116: }
117:
118: public int getMessageCount(boolean approved) {
119: return thread.getMessageCount(approved);
120: }
121:
122: public Message getRootMessage() {
123: Message message = thread.getRootMessage();
124: return new MessageProxy(message, authorization, permissions);
125: }
126:
127: public void addMessage(Message parentMessage, Message newMessage) {
128: thread.addMessage(parentMessage, newMessage);
129: }
130:
131: public void deleteMessage(Message message)
132: throws UnauthorizedException {
133: if (permissions.isSystemOrForumAdmin()
134: || permissions.get(Constants.MODERATOR)) {
135: thread.deleteMessage(message);
136: } else {
137: throw new UnauthorizedException();
138: }
139: }
140:
141: public void moveMessage(Message message, ForumThread newThread,
142: Message parentMessage) throws UnauthorizedException {
143: //If the user is an amdin of both forums
144: if (permissions.isSystemOrForumAdmin()
145: && (newThread.hasPermission(Constants.SYSTEM_ADMIN) || newThread
146: .hasPermission(Constants.FORUM_ADMIN))) {
147: thread.moveMessage(message, newThread, parentMessage);
148: } else {
149: throw new UnauthorizedException();
150: }
151: }
152:
153: public Message getMessage(int messageID)
154: throws ForumMessageNotFoundException {
155: Message message = thread.getMessage(messageID);
156: //Apply the protection proxy and return message.
157: return new MessageProxy(message, authorization, permissions);
158: }
159:
160: public TreeWalker treeWalker() {
161: return new TreeWalkerProxy(thread.treeWalker(), authorization,
162: permissions);
163: }
164:
165: public TreeWalker treeWalker(boolean approved) {
166: return new TreeWalkerProxy(thread.treeWalker(approved),
167: authorization, permissions);
168: }
169:
170: public Iterator messages() {
171: Iterator iterator = thread.messages();
172: return new MessageIteratorProxy(iterator, authorization,
173: permissions);
174: }
175:
176: public Iterator messages(int startIndex, int numResults) {
177: Iterator iterator = thread.messages(startIndex, numResults);
178: return new MessageIteratorProxy(iterator, authorization,
179: permissions);
180: }
181:
182: public Iterator messages(boolean approved) {
183: Iterator iterator = thread.messages(approved);
184: return new MessageIteratorProxy(iterator, authorization,
185: permissions);
186: }
187:
188: public Iterator messages(boolean approved, int startIndex,
189: int numResults) {
190: Iterator iterator = thread.messages(approved, startIndex,
191: numResults);
192: return new MessageIteratorProxy(iterator, authorization,
193: permissions);
194: }
195:
196: public boolean hasPermission(int type) {
197: return permissions.get(type);
198: }
199:
200: public String toString() {
201: return thread.toString();
202: }
203:
204: /**
205: * Small violation of our pluggable backend architecture so that database
206: * insertions can be made more efficiently and transactional. The fact
207: * that this violation is needed probably means that the proxy architecture
208: * needs to be adjusted a bit.
209: */
210: public void insertIntoDb(java.sql.Connection con)
211: throws SQLException {
212: ((DbForumThread) thread).insertIntoDb(con);
213: }
214: }
|