KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > RowPermissions


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.DBField;
69 import com.jcorporate.expresso.core.dbobj.DBObject;
70
71 /**
72  * storage for read/write permissions concerning a given row in the database
73  * this table stores user & "other" permissions
74  * <p/>
75  * this object and its table should be manipulated only through RowSecuredDBObject
76  *
77  * @author larry hamel
78  * @see com.jcorporate.expresso.core.dbobj.RowSecuredDBObject
79  * @see com.jcorporate.expresso.services.dbobj.RowGroupPerms
80  */

81 public class RowPermissions extends DBObject /* ironically, we do not subclass SecuredDBObject because everyone must be able to write their own permissions */ {
82
83     public static final String JavaDoc PERMISSIONS_TABLE_NAME = "ROW_PERMISSIONS";
84     public static final String JavaDoc PERMISSIONS_TABLE_DESCRIP = "RowPermissions";
85     /**
86      * field name for name of table
87      */

88     public static final String JavaDoc TABLE_NAME = "TARGET_TABLE";
89
90     /**
91      * field name for primary key of row
92      */

93     public static final String JavaDoc ROW_KEY = "ROW_KEY";
94
95     /**
96      * field name for owner
97      */

98     public static final String JavaDoc OWNER_ID = "OWNER_ID";
99
100
101     /**
102      * field name for bits that make up permissions; perms stored in this table, ROW_PERMISSIONS, have bits for
103      * group permissions that are unused. Conversely, perms stored in table ROW_GRP_PERMS, have bits for owner and other
104      * which are unused.
105      */

106     public static final String JavaDoc PERMISSIONS = "PERMISSIONS";
107
108     /**
109      * max length of a primary key of this table, which is
110      * made up of the target table name + target row PK
111      * Most databases have a system limit.
112      * InterBase probably has one of the lowest limits, 200 bytes for the index,
113      * which is a combination of all indexed fields,
114      * documented at http://bdn.borland.com/article/0,1410,25245,00.html
115      * <p/>
116      * increase this length if you need to, and if your database allows a larger
117      * number of bytes in indices.
118      */

119     public static final int MAX_KEY_LENGTH = 150;
120     /**
121      * Maximum length of name of table; all tables must have names that fit.
122      * Most databases have a system limit.
123      */

124     public static final int MAX_TABLE_NAME_LENGTH = 20;
125
126     protected static int sMaxKeyLen = MAX_KEY_LENGTH; // can be overridden
127
protected static String JavaDoc sKeyType = DBField.VARCHAR_TYPE; // can be overridden
128

129     /**
130      * bitmasks for permissions, stored as Java int
131      */

132     public static final int OWNER_WRITE_MASK = 256;
133     public static final int OWNER_READ_MASK = 128;
134     public static final int OWNER_PERMISSION_MASK = 64;
135     public static final int GROUP_READ_MASK = 32;
136     public static final int GROUP_WRITE_MASK = 16;
137     public static final int GROUP_PERMISSION_MASK = 8;
138     public static final int OTHERS_READ_MASK = 4;
139     public static final int OTHERS_WRITE_MASK = 2;
140     public static final int OTHERS_PERMISSION_MASK = 1;
141
142     public static final int OTHERS_READ_AND_GROUP_WRITES_PERMISSIONS =
143             OWNER_READ_MASK
144             + OWNER_WRITE_MASK
145             + OWNER_PERMISSION_MASK
146             + GROUP_READ_MASK
147             + GROUP_WRITE_MASK
148             + GROUP_PERMISSION_MASK
149             + OTHERS_READ_MASK;
150
151     public static final int DEFAULT_PERMISSIONS = OTHERS_READ_AND_GROUP_WRITES_PERMISSIONS;
152
153     public static final int WIDE_OPEN_PERMISSIONS =
154             OWNER_READ_MASK
155             + OWNER_WRITE_MASK
156             + OWNER_PERMISSION_MASK
157             + GROUP_READ_MASK
158             + GROUP_WRITE_MASK
159             + GROUP_PERMISSION_MASK
160             + OTHERS_READ_MASK
161             + OTHERS_WRITE_MASK
162             + OTHERS_PERMISSION_MASK;
163
164     public static final int OWNER_ONLY_PERMISSIONS =
165             OWNER_READ_MASK
166             + OWNER_WRITE_MASK
167             + OWNER_PERMISSION_MASK;
168
169     public static final int OWNER_AND_GROUP_WRITE_PERMISSIONS =
170             OWNER_READ_MASK
171             + OWNER_WRITE_MASK
172             + OWNER_PERMISSION_MASK
173             + GROUP_READ_MASK
174             + GROUP_WRITE_MASK
175             + GROUP_PERMISSION_MASK;
176
177     public static final int GROUP_READ_ONLY_PERMISSIONS =
178             OWNER_READ_MASK
179             + OWNER_WRITE_MASK
180             + OWNER_PERMISSION_MASK
181             + GROUP_READ_MASK;
182
183     /**
184      * group can write, but others cannot even read
185      */

186     public static final int GROUP_ONLY_READWRITE_PERMISSIONS =
187             OWNER_READ_MASK
188             + OWNER_WRITE_MASK
189             + OWNER_PERMISSION_MASK
190             + GROUP_WRITE_MASK
191             + GROUP_READ_MASK;
192
193
194     /**
195      * if no group has been named, here's a default name (10 char max)
196      * which is created by UserGroup.populateDefaultValues()
197      */

198     public static final String JavaDoc DEFAULT_PERMISSION_GROUP = UserGroup.ALL_USERS_GROUP;
199
200     public RowPermissions() throws DBException {
201     }
202
203     public RowPermissions(String JavaDoc table, String JavaDoc rowKey)
204             throws DBException {
205         if (rowKey == null) {
206             throw new DBException("null row key");
207         }
208         if (table == null) {
209             throw new DBException("null table name");
210         }
211
212         // test for key length being too large
213
if ((rowKey.length() + table.length()) > getMaxKeyLen()) {
214             throw new DBException("Cannot create row permissions for table: "
215                     + this.getJDBCMetaData().getTargetTable() + " row: " + rowKey
216                     + " because table name + row's ID (PK) exceeds maximum of " + getMaxKeyLen());
217         }
218
219         setField(TABLE_NAME, table);
220         setField(ROW_KEY, rowKey);
221     }
222
223     /**
224      * always returns true
225      *
226      * @return true if the own can administrate the row
227      * @throws DBException upon error
228      */

229     public boolean canOwnerAdministrate() throws DBException {
230         return true;
231     }
232
233     public boolean canOwnerRead() throws DBException {
234         return (OWNER_READ_MASK & this.permissions()) == OWNER_READ_MASK;
235     }
236
237     public boolean canOwnerWrite() throws DBException {
238         return (OWNER_WRITE_MASK & permissions()) == OWNER_WRITE_MASK;
239     }
240
241     public boolean canOthersAdministrate() throws DBException {
242         return (OTHERS_PERMISSION_MASK & permissions()) == OTHERS_PERMISSION_MASK;
243     }
244
245     public boolean canOthersRead() throws DBException {
246         return (OTHERS_READ_MASK & permissions()) == OTHERS_READ_MASK;
247     }
248
249     public boolean canOthersWrite() throws DBException {
250         return (OTHERS_WRITE_MASK & permissions()) == OTHERS_WRITE_MASK;
251     }
252
253
254     /**
255      * override in subclesses, and be sure to call this as first line of override
256      *
257      * @throws DBException upon error
258      */

259     protected synchronized void setupFields() throws DBException {
260         setTargetTable(PERMISSIONS_TABLE_NAME);
261         setDescription(PERMISSIONS_TABLE_DESCRIP);
262         addField(TABLE_NAME, DBField.VARCHAR_TYPE, MAX_TABLE_NAME_LENGTH, false, "Targettablename");
263         /**
264          * @todo should ROW_KEY be longvarchar? is that indexable on all databases?
265          * [Answer: LongVarChar is NOT indexable on several databases -MR]
266          */

267         addField(ROW_KEY, sKeyType, getMaxKeyLen(), false, "Rowkey");
268         addField(OWNER_ID, DBField.INT_TYPE, 0, true, "Owner");
269         addField(PERMISSIONS, DBField.INT_TYPE, 0, true, "Permissionbits");
270         addKey(TABLE_NAME);
271         addKey(ROW_KEY);
272     }
273
274     public int owner() throws DBException {
275         return this.getFieldInt(OWNER_ID);
276     }
277
278     public void owner(int theOwner) throws DBException {
279         setField(OWNER_ID, theOwner);
280     }
281
282     /**
283      * set permissions
284      *
285      * @param perm the permissions to set
286      * @throws DBException upon DataObject error
287      */

288     public void permissions(int perm) throws DBException {
289         setField(PERMISSIONS, perm);
290     }
291
292     /**
293      * get permissions, a bit-field of privilege bits; see constants in this class
294      * for mask definitions
295      *
296      * @return the permission code
297      * @throws DBException upon DataObject error
298      */

299     public int permissions() throws DBException {
300         // protect against empty perm.
301
if (getField(PERMISSIONS).length() == 0) {
302             return 0;
303         }
304         return getFieldInt(PERMISSIONS);
305     }
306
307     /**
308      * indicates that this object is new--no permissions have been set
309      *
310      * @return true if the field is Fresh.
311      * @throws DBException upon DataObject error
312      */

313     public boolean isFresh() throws DBException {
314         return getField(PERMISSIONS).length() == 0;
315     }
316
317     /**
318      * @return the maximum length permitted for a primary key, if the table is to be acommodated by RowPermissions
319      */

320     public static int getMaxKeyLen() {
321         return sMaxKeyLen;
322     }
323
324 }
325
Popular Tags