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.util.Date;
028: import java.util.Enumeration;
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.Group;
036: import org.nemesis.forum.Message;
037: import org.nemesis.forum.MessageFilter;
038: import org.nemesis.forum.User;
039: import org.nemesis.forum.config.Constants;
040: import org.nemesis.forum.exception.ForumAlreadyExistsException;
041: import org.nemesis.forum.exception.ForumMessageNotFoundException;
042: import org.nemesis.forum.exception.ForumThreadNotFoundException;
043: import org.nemesis.forum.exception.UnauthorizedException;
044:
045: /**
046: * A protection proxy for Forums. A proxy has a set of permissions that are
047: * specified at creation time of the proxy. Subsequently, those permissions
048: * are use to restrict access to protected Forum methods. If a user does
049: * not have the right to execute a particular method, and UnauthorizedException
050: * is thrown.
051: *
052: * @see Forum
053: * @see ForumPermissions
054: * @see UnauthorizedException
055: */
056: public class ForumProxy implements Forum {
057:
058: private Forum forum;
059: private Authorization authorization;
060: private ForumPermissions permissions;
061:
062: /**
063: * Creates a new ForumProxy object.
064: *
065: * @param forum the forum to protect by proxy
066: * @param authorization the user's authorization token
067: * @param permissions the permissions to use with this proxy.
068: */
069: public ForumProxy(Forum forum, Authorization authorization,
070: ForumPermissions permissions) {
071: this .forum = forum;
072: this .authorization = authorization;
073: this .permissions = permissions;
074: }
075:
076: //** Methods from interface below**//
077:
078: //:AJOUT:
079: public int getModerationType() {
080: return forum.getModerationType();
081: }
082:
083: public void setModerationType(int type)
084: throws UnauthorizedException {
085: if (permissions.isSystemOrForumAdmin()) {
086: forum.setModerationType(type);
087: } else {
088: throw new UnauthorizedException();
089: }
090: }
091:
092: public int getID() {
093: return forum.getID();
094: }
095:
096: public String getName() {
097: return forum.getName();
098: }
099:
100: public void setName(String name) throws UnauthorizedException,
101: ForumAlreadyExistsException {
102: if (permissions.isSystemOrForumAdmin()) {
103: forum.setName(name);
104: } else {
105: throw new UnauthorizedException();
106: }
107: }
108:
109: public String getDescription() {
110: return forum.getDescription();
111: }
112:
113: public void setDescription(String description)
114: throws UnauthorizedException {
115: if (permissions.isSystemOrForumAdmin()) {
116: forum.setDescription(description);
117: } else {
118: throw new UnauthorizedException();
119: }
120: }
121:
122: public Date getCreationDate() {
123: return forum.getCreationDate();
124: }
125:
126: public void setCreationDate(Date creationDate)
127: throws UnauthorizedException {
128: if (permissions.isSystemOrForumAdmin()) {
129: forum.setCreationDate(creationDate);
130: } else {
131: throw new UnauthorizedException();
132: }
133: }
134:
135: public Date getModifiedDate() {
136: return forum.getModifiedDate();
137: }
138:
139: public void setModifiedDate(Date modifiedDate)
140: throws UnauthorizedException {
141: if (permissions.isSystemOrForumAdmin()) {
142: forum.setModifiedDate(modifiedDate);
143: } else {
144: throw new UnauthorizedException();
145: }
146: }
147:
148: public String getProperty(String name) {
149: return forum.getProperty(name);
150: }
151:
152: public void setProperty(String name, String value)
153: throws UnauthorizedException {
154: if (permissions.isSystemOrForumAdmin()) {
155: forum.setProperty(name, value);
156: } else {
157: throw new UnauthorizedException();
158: }
159: }
160:
161: public Enumeration propertyNames() {
162: return forum.propertyNames();
163: }
164:
165: public ForumThread createThread(Message rootMessage)
166: throws UnauthorizedException {
167: if (permissions.get(Constants.CREATE_THREAD)) {
168: ForumThread thread = forum.createThread(rootMessage);
169: return new ForumThreadProxy(thread, authorization,
170: permissions);
171: } else {
172: throw new UnauthorizedException();
173: }
174: }
175:
176: public Message createMessage(User user)
177: throws UnauthorizedException {
178: if (permissions.get(Constants.CREATE_MESSAGE)
179: || permissions.get(Constants.CREATE_THREAD)) {
180: //The user must be anonymous or the actual user in order to post as
181: //that user. Otherwise, throw an exception.
182: if (user.hasPermission(Constants.USER_ADMIN)
183: || user.isAnonymous()) {
184: Message message = forum.createMessage(user);
185: return new MessageProxy(message, authorization,
186: permissions);
187: } else {
188: throw new UnauthorizedException();
189: }
190:
191: } else {
192: throw new UnauthorizedException();
193: }
194: }
195:
196: public Message getMessage(int messageID)
197: throws ForumMessageNotFoundException {
198: Message m = forum.getMessage(messageID);
199: //Apply protection proxy and return.
200: return new MessageProxy(m, authorization, permissions);
201: }
202:
203: public void deleteThread(ForumThread thread)
204: throws UnauthorizedException {
205: if (permissions.isSystemOrForumAdmin()
206: || permissions.get(Constants.MODERATOR)) {
207: forum.deleteThread(thread);
208: } else {
209: throw new UnauthorizedException();
210: }
211: }
212:
213: public void moveThread(ForumThread thread, Forum newForum)
214: throws UnauthorizedException, IllegalArgumentException {
215: //If the user is an amdin of both forums
216: if (permissions.isSystemOrForumAdmin()
217: && (newForum.hasPermission(Constants.SYSTEM_ADMIN) || newForum
218: .hasPermission(Constants.FORUM_ADMIN))) {
219: forum.moveThread(thread, newForum);
220: } else {
221: throw new UnauthorizedException();
222: }
223: }
224:
225: public void addThread(ForumThread thread)
226: throws UnauthorizedException {
227: if (permissions.get(Constants.CREATE_THREAD)) {
228: forum.addThread(thread);
229: } else {
230: throw new UnauthorizedException();
231: }
232: }
233:
234: public ForumThread getThread(int threadID)
235: throws ForumThreadNotFoundException {
236: ForumThread thread = forum.getThread(threadID);
237: //Apply protection proxy and return.
238: return new ForumThreadProxy(thread, authorization, permissions);
239: }
240:
241: public Iterator threads() {
242: Iterator iterator = forum.threads();
243: return new ForumThreadIteratorProxy(iterator, authorization,
244: permissions);
245: }
246:
247: public Iterator threads(int startIndex, int numResults) {
248: Iterator iterator = forum.threads(startIndex, numResults);
249: return new ForumThreadIteratorProxy(iterator, authorization,
250: permissions);
251: }
252:
253: public Iterator threads(boolean approved) {
254: Iterator iterator = forum.threads(approved);
255: return new ForumThreadIteratorProxy(iterator, authorization,
256: permissions);
257: }
258:
259: public Iterator threads(boolean approved, int startIndex,
260: int numResults) {
261: Iterator iterator = forum.threads(approved, startIndex,
262: numResults);
263: return new ForumThreadIteratorProxy(iterator, authorization,
264: permissions);
265: }
266:
267: public int getThreadCount() {
268: return forum.getThreadCount();
269: }
270:
271: public int getMessageCount() {
272: return forum.getMessageCount();
273: }
274:
275: public int getMessageCount(boolean approved) {
276: return forum.getMessageCount(approved);
277: }
278:
279: public int getThreadCount(boolean approved) {
280: return forum.getThreadCount(approved);
281: }
282:
283: public void addUserPermission(User user, int permissionType)
284: throws UnauthorizedException {
285: //Don't let someone become a System Admin through this method.
286: //The ForumPermissions class probably needs to be changed.
287: if (permissionType == Constants.SYSTEM_ADMIN) {
288: throw new UnauthorizedException();
289: }
290: if (permissions.isSystemOrForumAdmin()) {
291: forum.addUserPermission(user, permissionType);
292: } else {
293: throw new UnauthorizedException();
294: }
295: }
296:
297: public void removeUserPermission(User user, int permissionType)
298: throws UnauthorizedException {
299: if (permissions.isSystemOrForumAdmin()) {
300: forum.removeUserPermission(user, permissionType);
301: } else {
302: throw new UnauthorizedException();
303: }
304: }
305:
306: public int[] usersWithPermission(int permissionType)
307: throws UnauthorizedException {
308: if (permissions.isSystemOrForumAdmin()) {
309: return forum.usersWithPermission(permissionType);
310: } else {
311: throw new UnauthorizedException();
312: }
313: }
314:
315: public void addGroupPermission(Group group, int permissionType)
316: throws UnauthorizedException {
317: //Don't let someone become a System Admin through this method.
318: //The ForumPermissions class probably needs to be changed.
319: if (permissionType == Constants.SYSTEM_ADMIN) {
320: throw new UnauthorizedException();
321: }
322: if (permissions.isSystemOrForumAdmin()) {
323: forum.addGroupPermission(group, permissionType);
324: } else {
325: throw new UnauthorizedException();
326: }
327: }
328:
329: public void removeGroupPermission(Group group, int permissionType)
330: throws UnauthorizedException {
331: if (permissions.isSystemOrForumAdmin()) {
332: forum.removeGroupPermission(group, permissionType);
333: } else {
334: throw new UnauthorizedException();
335: }
336: }
337:
338: public int[] groupsWithPermission(int permissionType)
339: throws UnauthorizedException {
340: if (permissions.isSystemOrForumAdmin()) {
341: return forum.groupsWithPermission(permissionType);
342: } else {
343: throw new UnauthorizedException();
344: }
345: }
346:
347: public MessageFilter[] getForumMessageFilters()
348: throws UnauthorizedException {
349: if (permissions.isSystemOrForumAdmin()) {
350: return forum.getForumMessageFilters();
351: } else {
352: throw new UnauthorizedException();
353: }
354: }
355:
356: public void addForumMessageFilter(MessageFilter filter)
357: throws UnauthorizedException {
358: if (permissions.isSystemOrForumAdmin()) {
359: forum.addForumMessageFilter(filter);
360: } else {
361: throw new UnauthorizedException();
362: }
363: }
364:
365: public void addForumMessageFilter(MessageFilter filter, int index)
366: throws UnauthorizedException {
367: if (permissions.isSystemOrForumAdmin()) {
368: forum.addForumMessageFilter(filter, index);
369: } else {
370: throw new UnauthorizedException();
371: }
372: }
373:
374: public void removeForumMessageFilter(int index)
375: throws UnauthorizedException {
376: if (permissions.isSystemOrForumAdmin()) {
377: forum.removeForumMessageFilter(index);
378: } else {
379: throw new UnauthorizedException();
380: }
381: }
382:
383: public Message applyFilters(Message message) {
384: return forum.applyFilters(message);
385: }
386:
387: public ForumPermissions getPermissions(Authorization authorization) {
388: return forum.getPermissions(authorization);
389: }
390:
391: public boolean hasPermission(int type) {
392: return permissions.get(type);
393: }
394:
395: public String toString() {
396: return forum.toString();
397: }
398: }
|