Source code: com/RuntimeCollective/bboard/bean/PermissibleBoard.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/bboard/bean/PermissibleBoard.java,v 1.13 2003/09/30 15:12:43 joe Exp $
2 * $Revision: 1.13 $
3 * $Date: 2003/09/30 15:12:43 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.bboard.bean;
31
32 import com.RuntimeCollective.bboard.BboardException;
33 import com.RuntimeCollective.permission.bean.Permissible;
34 import com.RuntimeCollective.permission.bean.SimplePermissible;
35 import com.RuntimeCollective.permission.bean.PermissionRule;
36 import com.RuntimeCollective.webapps.RuntimeDataSource;
37 import com.RuntimeCollective.webapps.RuntimeParameters;
38 import com.RuntimeCollective.webapps.bean.User;
39 import com.RuntimeCollective.webapps.bean.Session;
40 import com.RuntimeCollective.webapps.ReturnPathContainer;
41
42 import java.sql.SQLException;
43 import java.util.Iterator;
44 import java.util.Vector;
45 import javax.servlet.http.HttpSession;
46
47 /**
48 * A special BBoard on which users must have special permissions to be able to read
49 * and/or write messages.
50 *
51 * @version $Id: PermissibleBoard.java,v 1.13 2003/09/30 15:12:43 joe Exp $
52 */
53 public class PermissibleBoard extends SimpleBoard implements Permissible {
54
55 // ---Inherited from EntityBean---------------------------
56
57 /** The name of the database table for this bean type. */
58 public static final String DATABASE_TABLE = "bboard_permboard";
59
60 /** Save this bean to the database.
61 * <br>
62 * We assume this bean's PermissionRules have already been saved.
63 */
64 public void save() {
65 try {
66
67 super.save();
68
69 // save the SimplePermissible
70 String spid = null;
71 if (SimplePermissibleId != -1) {
72 RuntimeParameters.getStore().save(SimplePermissible.class.getName(), SimplePermissibleId);
73 spid = ""+SimplePermissibleId;
74 }
75
76 // write Permissible data
77 RuntimeDataSource.save(id, this.DATABASE_TABLE, new String[] { "simple_perm_id" }, new Object[] { spid } );
78
79 } catch (SQLException e) {
80 throw new BboardException("PermissibleBoard "+id+" could not save() : "+e);
81 }
82 }
83
84 /** Delete this bean from the database. */
85 public void delete() {
86
87 try {
88
89 // Delete topics (have to do this here, cos of db integrity constraints)
90 deleteTopics();
91
92 RuntimeDataSource.update("delete from "+this.DATABASE_TABLE+" where id = "+this.id);
93
94 // delete the SimplePermissible
95 if (SimplePermissibleId != -1) {
96 try {
97 RuntimeParameters.getStore().delete(SimplePermissible.class.getName(), SimplePermissibleId);
98 } catch (RuntimeException e) {
99 RuntimeParameters.logDebug(this, "Couldn't delete permissible "+SimplePermissibleId+" for PermissibleBoard "+getId());
100 }
101 }
102
103 super.delete();
104
105 } catch (SQLException e) {
106 throw new BboardException("PermissibleBoard "+id+" could not delete() : "+e);
107 }
108 }
109
110 /** Construct a new blank PermissibleBoard, giving it a new unique ID. */
111 public PermissibleBoard() throws SQLException {
112
113 super();
114
115 com.RuntimeCollective.permission.bean.SimplePermissible simplePermissible = (com.RuntimeCollective.permission.bean.SimplePermissible) RuntimeParameters.getStore().create("com.RuntimeCollective.permission.bean.SimplePermissible");
116 SimplePermissibleId = simplePermissible.getId();
117
118 // we have to save it, otherwise it may get garbage collected...
119 RuntimeParameters.getStore().save("com.RuntimeCollective.permission.bean.SimplePermissible", SimplePermissibleId);
120 }
121
122 /** Get a current PermissibleBoard from the RuntimeDataSource, given an id.
123 * @param id ID of the PermissibleBoard.
124 */
125 public PermissibleBoard(int id) throws SQLException {
126
127 super(id);
128
129 try {
130 // load the core Permissible data
131 int no_fields = 2;
132 Object[] result = RuntimeDataSource.queryRow("select t.id, t.simple_perm_id from "+this.DATABASE_TABLE+" t where t.id = "+id);
133 if (result.length != no_fields)
134 throw new BboardException("PermissibleBoard could not PermissibleBoard("+id+"), : "+result.length+" fields found in "+DATABASE_TABLE+" instead of "+no_fields+".");
135
136 SimplePermissibleId = Integer.parseInt(result[1].toString());
137
138 } catch (SQLException e) {
139 throw new BboardException("PermissibleBoard could not PermissibleBoard("+id+") : "+e);
140 }
141 }
142
143
144 // ---Inherited from PermissionBean---------------------------
145
146 /** Can a user "edit" this Bean. */
147 public boolean canEdit(User user) {
148 try {
149 return getSimplePermissible().canEdit(user);
150 } catch (Exception e) {
151 throw new BboardException("PermissibleBoard could not canEdit(user) : "+e);
152 }
153 }
154
155 /** Can a user "view" this Bean. */
156 public boolean canView(User user) {
157 try {
158 return getSimplePermissible().canView(user);
159 } catch (Exception e) {
160 throw new BboardException("PermissibleBoard could not canView(user) : "+e);
161 }
162 }
163
164 //---Permissible specific methods---------------------
165
166 /**
167 * Set the PermissionRule for a given action on this Bean.
168 * @param action, the action to constrain
169 * @param permissionRule, the rule to use for this action
170 */
171 public void setActionPermissionRule(String action, PermissionRule permissionRule) {
172 try {
173 getSimplePermissible().setActionPermissionRule(action, permissionRule);
174 } catch (Exception e) {
175 throw new BboardException("PermissibleBoard could not setActionPermissionRule : "+e);
176 }
177 }
178
179 /**
180 * Get the PermissionRule for a given action on this Bean.
181 * @param action, the action
182 * @return the permissionRule currently in use for the action
183 */
184 public PermissionRule getActionPermissionRule(String action) {
185 try {
186 return getSimplePermissible().getActionPermissionRule(action);
187 } catch (Exception e) {
188 throw new BboardException("PermissibleBoard could not getActionPermissionRule : "+e);
189 }
190 }
191
192 /**
193 * Can a given User perform a given action on that Bean?
194 * @param action, the action to be performed
195 * @param user, the user who is trying to perform the action
196 * @return a boolean, yes the user can perform the action, or no
197 */
198 public boolean canPerformAction(String action, User user) {
199 try {
200 return getSimplePermissible().canPerformAction(action, user);
201 } catch (Exception e) {
202 throw new BboardException("PermissibleBoard could not canPerformAction : "+e);
203 }
204 }
205
206 /**
207 * Get the path of the page where the session should be sent in order
208 * to (maybe) get authorised to perform the specified action.<p>
209 * On submission of that page, the session should be checked again,
210 * as there may be more than one page to go to.
211 *
212 * @deprecated This method was modified not to refer to Client Tier classes (HttpSession). Use instead:
213 * <code>getAuthorisationPathForAction(action, (User) session.getAttribute(RuntimeParameters.get("logonUserKey")), (Session) session.getAttribute(Session.SESSION_KEY), new HttpSessionReturnPathContainer(session), returnPath)</code>
214 * @param action, the action meant to be performed
215 * @param session, the session who would like to perform the action
216 * @param returnPath, where the session should be sent back after going to that page
217 * @return a String, the local path to go to, or null if the session is accepted
218 */
219 public String getAuthorisationPathForAction(String action, HttpSession session, String returnPath) {
220 try {
221 return getSimplePermissible().getAuthorisationPathForAction(action, session, returnPath);
222 } catch (Exception e) {
223 throw new BboardException("PermissibleBoard could not getAuthorisationPathForAction : "+e);
224 }
225 }
226
227 /**
228 * Get the path of the page where a User should be sent in order
229 * to (maybe) get authorised for an action.
230 * <p>
231 * On submission of that resulting page, the user should be checked again,
232 * as there may be more than one page to go to before he/she is authorised.
233 * @param action, the action
234 * @param user, the user, possibly null if the user hasn't been identified
235 * @param session, the general purpose webapps session that the user is using ATM, possibly null
236 * @param rpContainer, something on which to put the return path, if necessaru
237 * @param returnPath, where the user should be sent back after going to that page
238 * @return a String, the local path to go to, or null if the session is accepted
239 */
240 public String getAuthorisationPathForAction(String action, User user, Session session, ReturnPathContainer rpContainer, String returnPath) {
241 try {
242 return getSimplePermissible().getAuthorisationPathForAction(action, user, session, rpContainer, returnPath);
243 } catch (Exception e) {
244 throw new BboardException("PermissibleBoard could not getAuthorisationPathForAction : "+e);
245 }
246 }
247
248
249 /**
250 * Get an Iterator of the actions constrained until now.
251 * @return an Iterator of Strings
252 */
253 public Iterator getActions() {
254 try {
255 return getSimplePermissible().getActions();
256 } catch (Exception e) {
257 throw new BboardException("PermissibleBoard could not getActions : "+e);
258 }
259 }
260
261
262 //---PermissibleBoard specific methods---------------------
263
264 /** The SimplePermissible */
265 protected int SimplePermissibleId;
266
267 /** Set the SimplePermissible */
268 public void setSimplePermissible(SimplePermissible simplePermissible) {
269 if (simplePermissible != null)
270 this.SimplePermissibleId = simplePermissible.getId();
271 else
272 this.SimplePermissibleId = -1;
273 }
274
275 /** Get the SimplePermissible */
276 public SimplePermissible getSimplePermissible() {
277 if (this.SimplePermissibleId != -1)
278 return (SimplePermissible) RuntimeParameters.getStore().get("com.RuntimeCollective.permission.bean.SimplePermissible", this.SimplePermissibleId);
279 else
280 return null;
281 }
282 }
283
284
285
286
287