KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > auth > ForumListPermission


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/ForumListPermission.java,v 1.8 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.auth;
42
43 import java.util.*;
44
45 import com.mvnforum.db.ForumBean;
46 import com.mvnforum.db.ForumCache;
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 /**
51  * This class is used in MVNForumPermissionImpl to imnplement forum-specific permission
52  * NOTE: This class is NOT thread-safe
53  */

54 class ForumListPermission {
55
56     private static Log log = LogFactory.getLog(ForumListPermission.class);
57
58     ArrayList forumList = new ArrayList();
59
60     boolean allForumsPermission = false;
61
62     boolean bypassPrivateForum = false;
63
64     public ForumListPermission() {
65     }
66
67     void setAllForumsPermission(boolean permission) {
68         allForumsPermission = permission;
69     }
70
71     void setForumPermission(int forumID, boolean permission) {
72         // always remove forumid
73
Iterator iter = forumList.iterator();
74         while (iter.hasNext()) {
75             int currentForumID = ((Integer JavaDoc) iter.next()).intValue();
76             if (currentForumID == forumID) {
77                 iter.remove();
78             }
79         } //while
80

81         // now add to the list if the permission = false
82
if (permission) {
83             // add permission
84
forumList.add(new Integer JavaDoc(forumID));
85         }
86     }
87
88     boolean hasPermission(int forumID) {
89
90         for (int i = 0; i < forumList.size(); i++) {
91             int currentForumID = ((Integer JavaDoc)forumList.get(i)).intValue();
92             if (currentForumID == forumID) {
93                 return true;
94             }
95         }
96
97         // have permission on all forums, then we check if this is a Private Forum
98
if (allForumsPermission) {
99             if (bypassPrivateForum) {
100                 return true;
101             }
102
103             try {
104                 ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
105                 if (forumBean.getForumType() == ForumBean.FORUM_TYPE_DEFAULT) {
106                     return true;
107                 }
108             } catch (Exception JavaDoc ex) {
109                 log.error("Cannot get the ForumBean in ForumListPermission", ex);
110             }
111         }
112
113         // if not found, then we return false (no permission on the forum)
114
return false;
115     }
116
117     boolean hasPermssionInAnyForums() {
118
119         // now check if have permission on any forums by checking the forumList size
120
if (forumList.size() > 0) {
121             // forumList size > 0 means there is permission on at least one forum
122
return true;
123         }
124
125         // have permission on all forums, then we check if this is a Private Forum
126
if (allForumsPermission) {
127             if (bypassPrivateForum) {
128                 return true;
129             }
130
131             try {
132                 Collection forumBeans = ForumCache.getInstance().getBeans();
133                 for (Iterator iter = forumBeans.iterator(); iter.hasNext(); ) {
134                     ForumBean forumBean = (ForumBean)iter.next();
135                     if (forumBean.getForumType() == ForumBean.FORUM_TYPE_DEFAULT) {
136                         return true;
137                     }
138                 }
139             } catch (Exception JavaDoc ex) {
140                 log.error("Cannot get ForumBeans in ForumListPermission", ex);
141             }
142         }
143
144         // if not found, then we return false (no permission on any forums)
145
return false;
146     }
147
148     public boolean isBypassPrivateForum() {
149         return bypassPrivateForum;
150     }
151
152     public void setBypassPrivateForum(boolean ignorePrivateOption) {
153         this.bypassPrivateForum = ignorePrivateOption;
154     }
155 }
156
Popular Tags