KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > ForumFactoryProxy


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum;
108
109 import com.Yasna.forum.util.ClientIP;
110
111 import java.util.*;
112 //JDK1.1// import com.sun.java.util.collections.*;
113

114 /**
115  * A protection proxy for ForumFactory. It ensures that only authorized users
116  * are allowed to access restricted methods.
117  */

118 public class ForumFactoryProxy extends ForumFactory {
119
120     private ForumFactory factory;
121     private Authorization authorization;
122     private ForumPermissions permissions;
123
124     public ForumFactoryProxy(ForumFactory factory, Authorization authorization,
125             ForumPermissions permissions)
126     {
127         this.factory = factory;
128         this.authorization = authorization;
129         this.permissions = permissions;
130     }
131
132     public Forum createForum(String JavaDoc name, String JavaDoc description,
133                              boolean moderated, int forumGroupID,boolean article)
134             throws UnauthorizedException, ForumAlreadyExistsException
135     {
136         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
137             Forum newForum = factory.createForum(name, description,
138                                                  moderated, forumGroupID,article);
139             return new ForumProxy(newForum, authorization, permissions);
140         }
141         else {
142             throw new UnauthorizedException();
143         }
144     }
145
146     public void deleteForum(Forum forum) throws UnauthorizedException {
147         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
148             factory.deleteForum(forum);
149         }
150         else {
151             throw new UnauthorizedException();
152         }
153     }
154
155     public void deleteCategory(Category category) throws UnauthorizedException {
156         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
157             factory.deleteCategory(category);
158         }
159         else {
160             throw new UnauthorizedException();
161         }
162     }
163
164     public Forum getForum(int forumID) throws ForumNotFoundException,
165             UnauthorizedException
166     {
167         Forum forum = factory.getForum(forumID);
168         ForumPermissions forumPermissions = forum.getPermissions(authorization);
169         //Create a new permissions object with the combination of the
170
//permissions of this object and tempPermissions.
171
ForumPermissions newPermissions =
172                 new ForumPermissions(permissions, forumPermissions);
173         //Check and see if the user has READ permissions. If not, throw an
174
//an UnauthorizedException.
175
if (!(
176             newPermissions.get(ForumPermissions.READ) ||
177             newPermissions.get(ForumPermissions.FORUM_ADMIN) ||
178             newPermissions.get(ForumPermissions.SYSTEM_ADMIN)
179             ))
180         {
181             throw new UnauthorizedException();
182         }
183         return new ForumProxy(forum, authorization, newPermissions);
184     }
185
186     public Forum getForum(String JavaDoc name) throws ForumNotFoundException,
187             UnauthorizedException
188     {
189         Forum forum = factory.getForum(name);
190         ForumPermissions forumPermissions = forum.getPermissions(authorization);
191         //Create a new permissions object with the combination of the
192
//permissions of this object and tempPermissions.
193
ForumPermissions newPermissions =
194                 new ForumPermissions(permissions, forumPermissions);
195         //Check and see if the user has READ permissions. If not, throw an
196
//an UnauthorizedException.
197
if (!(
198             newPermissions.get(ForumPermissions.READ) ||
199             newPermissions.get(ForumPermissions.FORUM_ADMIN) ||
200             newPermissions.get(ForumPermissions.SYSTEM_ADMIN)
201             ))
202         {
203             throw new UnauthorizedException();
204         }
205         return new ForumProxy(forum, authorization, newPermissions);
206     }
207
208     public int getForumCount() {
209         return factory.getForumCount();
210     }
211
212     public Iterator categories() {
213         return new CategoryIteratorProxy(factory.categories(), authorization, permissions);
214     }
215
216     public Category getCategory(int categoryID) throws CategoryNotFoundException,
217         UnauthorizedException
218     {
219         Category category = factory.getCategory(categoryID);
220         //category permissions can be implemented here
221
return new CategoryProxy(category, authorization, permissions);
222     }
223
224     public Category getCategory(String JavaDoc name) throws CategoryNotFoundException,
225         UnauthorizedException
226     {
227         Category category = factory.getCategory(name);
228         //category permissions can be implemented here
229
return new CategoryProxy(category, authorization, permissions);
230     }
231
232     public Category createCategory(String JavaDoc name, String JavaDoc description)
233             throws UnauthorizedException, CategoryAlreadyExistsException
234     {
235         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
236             Category newCategory = factory.createCategory(name, description);
237             return new CategoryProxy(newCategory, authorization, permissions);
238         }
239         else {
240             throw new UnauthorizedException();
241         }
242     }
243
244     public Iterator forums() {
245         return new ForumIteratorProxy(factory.forums(), authorization, permissions,false);
246     }
247     public Iterator forumsWithArticlesForums(){
248         return new ForumIteratorProxy(factory.forums(), authorization, permissions,true);
249     }
250     public Iterator forumsModeration() {
251         return new ForumModeratorIteratorProxy(factory.forums(), authorization, permissions);
252     }
253
254     public ProfileManager getProfileManager() {
255         ProfileManager profileManager = factory.getProfileManager();
256         return new ProfileManagerProxy(profileManager, authorization, permissions);
257     }
258
259     public SearchIndexer getSearchIndexer() throws UnauthorizedException {
260         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
261             return factory.getSearchIndexer();
262         }
263         else {
264             throw new UnauthorizedException();
265         }
266     }
267
268     public int [] usersWithPermission(int permissionType)
269             throws UnauthorizedException
270     {
271         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
272             return factory.usersWithPermission(permissionType);
273         }
274         else {
275             throw new UnauthorizedException();
276         }
277     }
278
279     public int[] groupsWithPermission(int permissionType)
280             throws UnauthorizedException
281     {
282         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
283             return factory.groupsWithPermission(permissionType);
284         }
285         else {
286             throw new UnauthorizedException();
287         }
288     }
289
290     public ForumPermissions getPermissions(Authorization authorization) {
291         return factory.getPermissions(authorization);
292     }
293
294     public boolean hasPermission(int type) {
295         return permissions.get(type);
296     }
297
298     /**
299      * Returns the forum factory class that the proxy wraps. In some cases,
300      * this is necessary so that an outside class can get at methods that only
301      * a particular forum factory sublclass contains. Because this is
302      * potentially a dangerours operation, access to the underlying class is
303      * restricted to those with SYSTEM_ADMIN permissions.
304      *
305      * @throws UnauthorizedException if does not have ADMIN permissions.
306      */

307     public ForumFactory getUnderlyingForumFactory()
308             throws UnauthorizedException
309     {
310         if (permissions.get(ForumPermissions.SYSTEM_ADMIN)) {
311             return factory;
312         }
313         else {
314             throw new UnauthorizedException();
315         }
316     }
317     public Query createQuery(){
318         return factory.createQuery();
319     }
320     public void BlackListIP(ClientIP cip,boolean add) throws UnauthorizedException{
321         if (permissions.get(ForumPermissions.SYSTEM_ADMIN) || permissions.get(ForumPermissions.FORUM_ADMIN) || permissions.get(ForumPermissions.MODERATOR)) {
322             factory.BlackListIP(cip,add);
323         }
324         else {
325             throw new UnauthorizedException();
326         }
327     }
328     public boolean isBlackListed(ClientIP cip){
329         return factory.isBlackListed(cip);
330     }
331     public ForumThread getArticleThread(String JavaDoc pageKey,Forum forumID) throws ForumThreadNotFoundException,UnauthorizedException{
332         return new ForumThreadProxy(factory.getArticleThread(pageKey,forumID),authorization,permissions);
333     }
334     public Iterator getThreadTypeIterator(){
335         return factory.getThreadTypeIterator();
336     }
337     public ThreadType getThreadType(int typeid){
338         return factory.getThreadType(typeid);
339     }
340     public Iterator getSessionList(){
341         return factory.getSessionList();
342     }
343     public int getYesterdayUserCount(){
344         return factory.getYesterdayUserCount();
345     }
346
347 }
348
Popular Tags