KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/auth/ChannelListPermission.java,v 1.5 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.5 $
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.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 /**
50  * This class is used in MVNForumPermissionImpl to imnplement channel-specific permission
51  * NOTE: This class is NOT thread-safe
52  */

53 class ChannelListPermission {
54
55     private static Log log = LogFactory.getLog(ChannelListPermission.class);
56
57     ArrayList JavaDoc channelList = new ArrayList JavaDoc();
58
59     boolean allChannelsPermission = false;
60
61     boolean bypassPrivateChannel = false;
62
63     public ChannelListPermission() {
64     }
65
66     void setAllChannelsPermission(boolean permission) {
67         allChannelsPermission = permission;
68     }
69
70     void setChannelPermission(int channelID, boolean permission) {
71         // always remove channelID
72
Iterator JavaDoc iter = channelList.iterator();
73         while (iter.hasNext()) {
74             int currentChannelID = ((Integer JavaDoc) iter.next()).intValue();
75             if (currentChannelID == channelID) {
76                 iter.remove();
77             }
78         } //while
79

80         // now add to the list if the permission = false
81
if (permission) {
82             // add permission
83
channelList.add(new Integer JavaDoc(channelID));
84         }
85     }
86
87     boolean hasPermission(int channelID) {
88
89         for (int i = 0; i < channelList.size(); i++) {
90             int currentChannelID = ((Integer JavaDoc)channelList.get(i)).intValue();
91             if (currentChannelID == channelID) {
92                 return true;
93             }
94         }
95
96         // have permission on all channels, then we check if this is a Private Channel
97
if (allChannelsPermission) {
98             if (bypassPrivateChannel) {
99                 return true;
100             }
101
102             // now we assume (channelBean.getChannelType() == ChannelBean.CHANNEL_TYPE_DEFAULT)
103
return true;
104             /*
105             try {
106                 ChannelBean channelBean = ChannelCache.getInstance().getBean(channelID);
107                 if (channelBean.getChannelType() == ChannelBean.CHANNEL_TYPE_DEFAULT) {
108                     return true;
109                 }
110             } catch (Exception ex) {
111                 log.error("Cannot get the ChannelBean in ChannelListPermission", ex);
112             }*/

113         }
114
115         // if not found, then we return false (no permission on the channel)
116
return false;
117     }
118
119     boolean hasPermssionInAnyChannels() {
120
121         // now check if have permission on any channels by checking the channelList size
122
if (channelList.size() > 0) {
123             // channelList size > 0 means there is permission on at least one channel
124
return true;
125         }
126
127         // have permission on all channels, then we check if this is a Private Channel
128
if (allChannelsPermission) {
129             if (bypassPrivateChannel) {
130                 return true;
131             }
132
133             // now we assume (channelBean.getChannelType() == ChannelBean.CHANNEL_TYPE_DEFAULT)
134
return true;
135             /*
136             try {
137                 Collection channelBeans = ChannelCache.getInstance().getBeans();
138                 for (Iterator iter = channelBeans.iterator(); iter.hasNext(); ) {
139                     ChannelBean channelBean = (ChannelBean)iter.next();
140                     if (channelBean.getChannelType() == ChannelBean.CHANNEL_TYPE_DEFAULT) {
141                         return true;
142                     }
143                 }
144             } catch (Exception ex) {
145                 log.error("Cannot get Channel Beans in ChannelListPermission", ex);
146             }*/

147         }
148
149         // if not found, then we return false (no permission on any channels)
150
return false;
151     }
152
153     public boolean isBypassPrivateChannel() {
154         return bypassPrivateChannel;
155     }
156
157     public void setBypassPrivateChannel(boolean ignorePrivateOption) {
158         this.bypassPrivateChannel = ignorePrivateOption;
159     }
160 }
161
Popular Tags