KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > proxy > ForumProxy


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.proxy;
26
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.nemesis.forum.Authorization;
32 import org.nemesis.forum.Forum;
33 import org.nemesis.forum.ForumPermissions;
34 import org.nemesis.forum.ForumThread;
35 import org.nemesis.forum.Group;
36 import org.nemesis.forum.Message;
37 import org.nemesis.forum.MessageFilter;
38 import org.nemesis.forum.User;
39 import org.nemesis.forum.config.Constants;
40 import org.nemesis.forum.exception.ForumAlreadyExistsException;
41 import org.nemesis.forum.exception.ForumMessageNotFoundException;
42 import org.nemesis.forum.exception.ForumThreadNotFoundException;
43 import org.nemesis.forum.exception.UnauthorizedException;
44
45 /**
46  * A protection proxy for Forums. A proxy has a set of permissions that are
47  * specified at creation time of the proxy. Subsequently, those permissions
48  * are use to restrict access to protected Forum methods. If a user does
49  * not have the right to execute a particular method, and UnauthorizedException
50  * is thrown.
51  *
52  * @see Forum
53  * @see ForumPermissions
54  * @see UnauthorizedException
55  */

56 public class ForumProxy implements Forum {
57
58     private Forum forum;
59     private Authorization authorization;
60     private ForumPermissions permissions;
61
62     /**
63      * Creates a new ForumProxy object.
64      *
65      * @param forum the forum to protect by proxy
66      * @param authorization the user's authorization token
67      * @param permissions the permissions to use with this proxy.
68      */

69     public ForumProxy(Forum forum, Authorization authorization, ForumPermissions permissions) {
70         this.forum = forum;
71         this.authorization = authorization;
72         this.permissions = permissions;
73     }
74
75     //** Methods from interface below**//
76

77     //:AJOUT:
78
public int getModerationType(){
79         return forum.getModerationType();
80     }
81     
82     public void setModerationType(int type) throws UnauthorizedException{
83         if (permissions.isSystemOrForumAdmin()) {
84             forum.setModerationType(type);
85         } else {
86             throw new UnauthorizedException();
87         }
88     }
89                 
90                 
91
92     public int getID() {
93         return forum.getID();
94     }
95
96     public String JavaDoc getName() {
97         return forum.getName();
98     }
99
100     public void setName(String JavaDoc name) throws UnauthorizedException, ForumAlreadyExistsException {
101         if (permissions.isSystemOrForumAdmin()) {
102             forum.setName(name);
103         } else {
104             throw new UnauthorizedException();
105         }
106     }
107
108     public String JavaDoc getDescription() {
109         return forum.getDescription();
110     }
111
112     public void setDescription(String JavaDoc description) throws UnauthorizedException {
113         if (permissions.isSystemOrForumAdmin()) {
114             forum.setDescription(description);
115         } else {
116             throw new UnauthorizedException();
117         }
118     }
119
120     public Date JavaDoc getCreationDate() {
121         return forum.getCreationDate();
122     }
123
124     public void setCreationDate(Date JavaDoc creationDate) throws UnauthorizedException {
125         if (permissions.isSystemOrForumAdmin()) {
126             forum.setCreationDate(creationDate);
127         } else {
128             throw new UnauthorizedException();
129         }
130     }
131
132     public Date JavaDoc getModifiedDate() {
133         return forum.getModifiedDate();
134     }
135
136     public void setModifiedDate(Date JavaDoc modifiedDate) throws UnauthorizedException {
137         if (permissions.isSystemOrForumAdmin()) {
138             forum.setModifiedDate(modifiedDate);
139         } else {
140             throw new UnauthorizedException();
141         }
142     }
143
144     public String JavaDoc getProperty(String JavaDoc name) {
145         return forum.getProperty(name);
146     }
147
148     public void setProperty(String JavaDoc name, String JavaDoc value) throws UnauthorizedException {
149         if (permissions.isSystemOrForumAdmin()) {
150             forum.setProperty(name, value);
151         } else {
152             throw new UnauthorizedException();
153         }
154     }
155
156     public Enumeration JavaDoc propertyNames() {
157         return forum.propertyNames();
158     }
159
160     public ForumThread createThread(Message rootMessage) throws UnauthorizedException {
161         if (permissions.get(Constants.CREATE_THREAD)) {
162             ForumThread thread = forum.createThread(rootMessage);
163             return new ForumThreadProxy(thread, authorization, permissions);
164         } else {
165             throw new UnauthorizedException();
166         }
167     }
168
169     public Message createMessage(User user) throws UnauthorizedException {
170         if (permissions.get(Constants.CREATE_MESSAGE) || permissions.get(Constants.CREATE_THREAD)) {
171             //The user must be anonymous or the actual user in order to post as
172
//that user. Otherwise, throw an exception.
173
if (user.hasPermission(Constants.USER_ADMIN) || user.isAnonymous()) {
174                 Message message = forum.createMessage(user);
175                 return new MessageProxy(message, authorization, permissions);
176             } else {
177                 throw new UnauthorizedException();
178             }
179
180         } else {
181             throw new UnauthorizedException();
182         }
183     }
184     
185     public Message getMessage(int messageID) throws ForumMessageNotFoundException {
186             Message m = forum.getMessage(messageID);
187             //Apply protection proxy and return.
188
return new MessageProxy(m, authorization, permissions);
189     }
190
191     public void deleteThread(ForumThread thread) throws UnauthorizedException {
192         if (permissions.isSystemOrForumAdmin() || permissions.get(Constants.MODERATOR)) {
193             forum.deleteThread(thread);
194         } else {
195             throw new UnauthorizedException();
196         }
197     }
198
199     public void moveThread(ForumThread thread, Forum newForum) throws UnauthorizedException, IllegalArgumentException JavaDoc {
200         //If the user is an amdin of both forums
201
if (permissions.isSystemOrForumAdmin()
202             && (newForum.hasPermission(Constants.SYSTEM_ADMIN) || newForum.hasPermission(Constants.FORUM_ADMIN))) {
203             forum.moveThread(thread, newForum);
204         } else {
205             throw new UnauthorizedException();
206         }
207     }
208
209     public void addThread(ForumThread thread) throws UnauthorizedException {
210         if (permissions.get(Constants.CREATE_THREAD)) {
211             forum.addThread(thread);
212         } else {
213             throw new UnauthorizedException();
214         }
215     }
216
217     public ForumThread getThread(int threadID) throws ForumThreadNotFoundException {
218         ForumThread thread = forum.getThread(threadID);
219         //Apply protection proxy and return.
220
return new ForumThreadProxy(thread, authorization, permissions);
221     }
222
223     public Iterator JavaDoc threads() {
224         Iterator JavaDoc iterator = forum.threads();
225         return new ForumThreadIteratorProxy(iterator, authorization, permissions);
226     }
227
228     public Iterator JavaDoc threads(int startIndex, int numResults) {
229         Iterator JavaDoc iterator = forum.threads(startIndex, numResults);
230         return new ForumThreadIteratorProxy(iterator, authorization, permissions);
231     }
232     
233     public Iterator JavaDoc threads(boolean approved) {
234         Iterator JavaDoc iterator = forum.threads(approved);
235         return new ForumThreadIteratorProxy(iterator, authorization, permissions);
236     }
237
238     public Iterator JavaDoc threads(boolean approved,int startIndex, int numResults) {
239         Iterator JavaDoc iterator = forum.threads(approved,startIndex, numResults);
240         return new ForumThreadIteratorProxy(iterator, authorization, permissions);
241     }
242
243     public int getThreadCount() {
244         return forum.getThreadCount();
245     }
246
247     public int getMessageCount() {
248         return forum.getMessageCount();
249     }
250     
251     public int getMessageCount(boolean approved) {
252             return forum.getMessageCount(approved);
253     }
254     
255     public int getThreadCount(boolean approved) {
256             return forum.getThreadCount(approved);
257     }
258
259
260
261
262
263     public void addUserPermission(User user, int permissionType) throws UnauthorizedException {
264         //Don't let someone become a System Admin through this method.
265
//The ForumPermissions class probably needs to be changed.
266
if (permissionType == Constants.SYSTEM_ADMIN) {
267             throw new UnauthorizedException();
268         }
269         if (permissions.isSystemOrForumAdmin()) {
270             forum.addUserPermission(user, permissionType);
271         } else {
272             throw new UnauthorizedException();
273         }
274     }
275
276     public void removeUserPermission(User user, int permissionType) throws UnauthorizedException {
277         if (permissions.isSystemOrForumAdmin()) {
278             forum.removeUserPermission(user, permissionType);
279         } else {
280             throw new UnauthorizedException();
281         }
282     }
283
284     public int[] usersWithPermission(int permissionType) throws UnauthorizedException {
285         if (permissions.isSystemOrForumAdmin()) {
286             return forum.usersWithPermission(permissionType);
287         } else {
288             throw new UnauthorizedException();
289         }
290     }
291
292     public void addGroupPermission(Group group, int permissionType) throws UnauthorizedException {
293         //Don't let someone become a System Admin through this method.
294
//The ForumPermissions class probably needs to be changed.
295
if (permissionType == Constants.SYSTEM_ADMIN) {
296             throw new UnauthorizedException();
297         }
298         if (permissions.isSystemOrForumAdmin()) {
299             forum.addGroupPermission(group, permissionType);
300         } else {
301             throw new UnauthorizedException();
302         }
303     }
304
305     public void removeGroupPermission(Group group, int permissionType) throws UnauthorizedException {
306         if (permissions.isSystemOrForumAdmin()) {
307             forum.removeGroupPermission(group, permissionType);
308         } else {
309             throw new UnauthorizedException();
310         }
311     }
312
313     public int[] groupsWithPermission(int permissionType) throws UnauthorizedException {
314         if (permissions.isSystemOrForumAdmin()) {
315             return forum.groupsWithPermission(permissionType);
316         } else {
317             throw new UnauthorizedException();
318         }
319     }
320
321     public MessageFilter[] getForumMessageFilters() throws UnauthorizedException {
322         if (permissions.isSystemOrForumAdmin()) {
323             return forum.getForumMessageFilters();
324         } else {
325             throw new UnauthorizedException();
326         }
327     }
328
329     public void addForumMessageFilter(MessageFilter filter) throws UnauthorizedException {
330         if (permissions.isSystemOrForumAdmin()) {
331             forum.addForumMessageFilter(filter);
332         } else {
333             throw new UnauthorizedException();
334         }
335     }
336
337     public void addForumMessageFilter(MessageFilter filter, int index) throws UnauthorizedException {
338         if (permissions.isSystemOrForumAdmin()) {
339             forum.addForumMessageFilter(filter, index);
340         } else {
341             throw new UnauthorizedException();
342         }
343     }
344
345     public void removeForumMessageFilter(int index) throws UnauthorizedException {
346         if (permissions.isSystemOrForumAdmin()) {
347             forum.removeForumMessageFilter(index);
348         } else {
349             throw new UnauthorizedException();
350         }
351     }
352
353     public Message applyFilters(Message message) {
354         return forum.applyFilters(message);
355     }
356
357     public ForumPermissions getPermissions(Authorization authorization) {
358         return forum.getPermissions(authorization);
359     }
360
361     public boolean hasPermission(int type) {
362         return permissions.get(type);
363     }
364
365     public String JavaDoc toString() {
366         return forum.toString();
367     }
368 }
369
Popular Tags