001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.services.dbobj;
066:
067: import com.jcorporate.expresso.core.db.DBException;
068: import com.jcorporate.expresso.core.dbobj.DBField;
069: import com.jcorporate.expresso.core.dbobj.DBObject;
070:
071: /**
072: * storage for read/write permissions for a group, concerning a given row in the database
073: * this object and its table are a complement to the RowPermissions object and table.
074: * RowGroupPerms holds group permissions.
075: * <p/>
076: * this object and its table should be manipulated only through RowSecuredDBObject
077: *
078: * @author larry hamel
079: * @see com.jcorporate.expresso.core.dbobj.RowSecuredDBObject
080: * @see com.jcorporate.expresso.services.dbobj.RowPermissions
081: */
082: public class RowGroupPerms extends DBObject /* ironically, we do not subclass SecuredDBObject because anybody must be able to write their own permissions */{
083:
084: public static final String GROUP_PERMISSIONS_TABLE_NAME = "ROW_GRP_PERMS";
085: /**
086: * field name for name of table
087: */
088: public static final String TABLE_NAME = "TARGET_TABLE";
089:
090: /**
091: * field name for primary key of row
092: */
093: public static final String ROW_KEY = "ROW_KEY";
094:
095: /**
096: * field name for the group about which "group privilege settings" (read/write) apply
097: */
098: public static final String GROUP = "PERM_GROUP";
099:
100: /**
101: * field name for bits that make up permissions; perms stored in this table, ROW_GRP_PERMS, have bits for owner and other
102: * which are unused. Conversely, perms stored in table ROW_PERMISSIONS have bits for group permissions that are unused.
103: */
104: public static final String PERMISSIONS = "PERMISSIONS";
105:
106: /**
107: * default constructor
108: */
109: public RowGroupPerms() throws DBException {
110: }
111:
112: /**
113: * convenience constructor which sets table, key
114: * @param table tablename
115: * @param rowKey pipe-delimited concatenation of all necessary values for keys
116: */
117: public RowGroupPerms(String table, String rowKey)
118: throws DBException {
119: // test for key length being too large
120: if (rowKey == null) {
121: throw new DBException("null row key");
122: }
123: if (table == null) {
124: throw new DBException("null table name");
125: }
126:
127: if ((rowKey.length() + table.length()) > RowPermissions.sMaxKeyLen) {
128: throw new DBException(
129: "Cannot create row permissions for table: "
130: + this .getJDBCMetaData().getTargetTable()
131: + " row: "
132: + rowKey
133: + " because table name + row's ID (PK) exceeds maximum of "
134: + RowPermissions.sMaxKeyLen);
135: }
136:
137: setField(TABLE_NAME, table);
138: setField(ROW_KEY, rowKey);
139: }
140:
141: /**
142: * convenience constructor which sets table, key, group
143: * @param table tablename
144: * @param rowKey pipe-delimited concatenation of all necessary values for keys
145: * @param grp name of group
146: */
147: public RowGroupPerms(String table, String rowKey, String grp)
148: throws DBException {
149: // test for key length being too large
150: if (rowKey == null) {
151: throw new DBException("null row key");
152: }
153: if (table == null) {
154: throw new DBException("null table name");
155: }
156:
157: if (grp == null) {
158: throw new DBException("null grp name");
159: }
160:
161: if ((rowKey.length() + table.length() + grp.length()) > RowPermissions.sMaxKeyLen) {
162: throw new DBException(
163: "Cannot create row permissions for table: "
164: + this .getJDBCMetaData().getTargetTable()
165: + " row: "
166: + rowKey
167: + " because table name + row's ID (PK) exceeds maximum of "
168: + RowPermissions.sMaxKeyLen);
169: }
170:
171: setField(TABLE_NAME, table);
172: setField(ROW_KEY, rowKey);
173: setField(GROUP, grp);
174: }
175:
176: /**
177: * convenience copy constructor
178: * @param model an object from which all attributes will be copies (table, group, perms)
179: */
180: public RowGroupPerms(RowGroupPerms model) throws DBException {
181: this (model.table(), model.getKey());
182: group(model.group());
183: permissions(model.permissions());
184: }
185:
186: /**
187: * @return true if this group can administrate
188: */
189: public boolean canGroupAdministrate() throws DBException {
190: return (RowPermissions.GROUP_PERMISSION_MASK & permissions()) == RowPermissions.GROUP_PERMISSION_MASK;
191: }
192:
193: /**
194: * @return true if this group can read
195: */
196: public boolean canGroupRead() throws DBException {
197: return (RowPermissions.GROUP_READ_MASK & permissions()) == RowPermissions.GROUP_READ_MASK;
198: }
199:
200: /**
201: * @return true if this group can write
202: */
203: public boolean canGroupWrite() throws DBException {
204: return (RowPermissions.GROUP_WRITE_MASK & permissions()) == RowPermissions.GROUP_WRITE_MASK;
205: }
206:
207: /**
208: * override in subclesses, and be sure to call this as first line of override
209: *
210: * @throws DBException upon error
211: */
212: protected synchronized void setupFields() throws DBException {
213: setTargetTable(GROUP_PERMISSIONS_TABLE_NAME);
214: setDescription("RowGroupPermissons");
215: addField(TABLE_NAME, DBField.VARCHAR_TYPE,
216: RowPermissions.MAX_TABLE_NAME_LENGTH, false,
217: "Targettablename");
218: /**
219: * @todo should ROW_KEY be longvarchar? is that indexable on all databases?
220: */
221: addField(ROW_KEY, RowPermissions.sKeyType,
222: RowPermissions.sMaxKeyLen, false, "Rowkey");
223: addField(GROUP, DBField.CHAR_TYPE,
224: UserGroup.GROUP_NAME_MAX_LEN, false, "Group");
225: addField(PERMISSIONS, DBField.INT_TYPE, 0, true,
226: "Permissionbits");
227:
228: addKey(TABLE_NAME);
229: addKey(ROW_KEY);
230: addKey(GROUP);
231:
232: addIndex("tablerow", TABLE_NAME + "," + ROW_KEY, false);
233: }
234:
235: /**
236: * get group name
237: */
238: public String group() throws DBException {
239: return this .getField(GROUP);
240: }
241:
242: /**
243: * Set group;
244: * will throw run-time exception if the entire key for this row,
245: * which includes tablename, row key, and group name,
246: * is longer than permitted maximum for the host database.
247: *
248: * @param group the group string?
249: * @throws DBException upon error
250: */
251: public void group(String group) throws DBException {
252: if (group == null) {
253: throw new DBException("null group name");
254: }
255:
256: String tablename = getField(TABLE_NAME);
257: String rowKey = getField(ROW_KEY);
258: if (tablename.length() + rowKey.length() + group.length() > RowPermissions.sMaxKeyLen) {
259: throw new DBException(
260: "Cannot create group row permissions for table: "
261: + tablename
262: + ", row: "
263: + rowKey
264: + ", group: "
265: + group
266: + " because table name + rowKey + group exceeds maximum of "
267: + RowPermissions.sMaxKeyLen);
268: }
269: setField(GROUP, group);
270: }
271:
272: /**
273: * set group group is "Everybody"
274: * just sets fields--does not save; caller must call update()
275: *
276: * @throws DBException upon error
277: */
278: public void setDefaultPermissions() throws DBException {
279: permissions(RowPermissions.DEFAULT_PERMISSIONS);
280: group(RowPermissions.DEFAULT_PERMISSION_GROUP);
281: }
282:
283: /**
284: * set permissions
285: *
286: * @param perm Permissions code
287: * @throws DBException upon error
288: */
289: public void permissions(int perm) throws DBException {
290: setField(PERMISSIONS, perm);
291: }
292:
293: /**
294: * set permissions; not protected by test
295: *
296: * @return integer permission code
297: * @throws DBException upon 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: * accessor for table name
309: *
310: * @return table name
311: */
312: public String table() throws DBException {
313: return getField(TABLE_NAME);
314: }
315:
316: /**
317: * set the target table
318: */
319: public void table(String targetTable) throws DBException {
320: setField(TABLE_NAME, targetTable);
321: }
322:
323: /**
324: * @return row key
325: */
326: public String key() throws DBException {
327: return getField(ROW_KEY);
328: }
329:
330: /**
331: * set the row key
332: */
333: public void key(String key) throws DBException {
334: setField(ROW_KEY, key);
335: }
336: }
|