001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.acl;
034:
035: import java.util.StringTokenizer;
036:
037: import java.sql.Connection;
038: import java.sql.SQLException;
039: import java.sql.Statement;
040: import java.sql.PreparedStatement;
041: import java.sql.CallableStatement;
042: import java.sql.ResultSet;
043:
044: import com.knowgate.debug.DebugFile;
045: import com.knowgate.jdc.JDCConnection;
046: import com.knowgate.dataobjs.DB;
047: import com.knowgate.dataobjs.DBPersist;
048:
049: import com.knowgate.misc.Gadgets;
050: import com.knowgate.dataobjs.DBSubset;
051:
052: /**
053: * <p>Security Role Groups</p>
054: * @author Sergio Montoro Ten
055: * @version 3.0
056: */
057: public final class ACLGroup extends DBPersist {
058:
059: /**
060: * Default constructor.
061: */
062: public ACLGroup() {
063: super (DB.k_acl_groups, "ACLGroup");
064: }
065:
066: /**
067: * Construct object and set gu_acl_group field.
068: * Object is not fully loaded from database.
069: * @param sGroupUId Group Unique Identifier
070: */
071: public ACLGroup(String sGroupUId) {
072: super (DB.k_acl_groups, "ACLGroup");
073: put(DB.gu_acl_group, sGroupUId);
074: }
075:
076: // ----------------------------------------------------------
077:
078: public boolean store(JDCConnection oConn) throws SQLException {
079:
080: if (!AllVals.containsKey(DB.gu_acl_group)) {
081: put(DB.gu_acl_group, Gadgets.generateUUID());
082: }
083:
084: return super .store(oConn);
085: } // store
086:
087: // ----------------------------------------------------------
088:
089: /**
090: * <p>Add User to Group.</p>
091: * <p>Insert new register at k_x_group_user table.</p>
092: * @param oConn Database Connection
093: * @param sIdUser User Unique Identifier.
094: * @throws SQLException May throw a primary key constraint violation is user already belongs to group.
095: */
096: public int addACLUser(JDCConnection oConn, String sIdUser)
097: throws SQLException {
098: Statement oStmt;
099: int iRetVal;
100:
101: if (DebugFile.trace) {
102: DebugFile.writeln("Begin ACLGroup.addACLUser(Connection], "
103: + sIdUser + ")");
104: DebugFile.incIdent();
105: }
106:
107: oStmt = oConn.createStatement();
108:
109: if (DebugFile.trace)
110: DebugFile.writeln("Statement.executeUpdate(INSERT INTO "
111: + DB.k_x_group_user + "(" + DB.gu_acl_group + ","
112: + DB.gu_user + ") VALUES('"
113: + getStringNull(DB.gu_acl_group, "null") + "','"
114: + sIdUser + "')");
115:
116: iRetVal = oStmt.executeUpdate("INSERT INTO "
117: + DB.k_x_group_user + "(" + DB.gu_acl_group + ","
118: + DB.gu_user + ") VALUES('"
119: + getString(DB.gu_acl_group) + "','" + sIdUser + "')");
120: oStmt.close();
121:
122: if (DebugFile.trace) {
123: DebugFile.decIdent();
124: DebugFile.writeln("End ACLGroup.addACLUser() : "
125: + String.valueOf(iRetVal));
126: }
127:
128: return iRetVal;
129: } // addACLUser
130:
131: // ----------------------------------------------------------
132:
133: /**
134: * <p>Add Users to Group.</p>
135: * <p>Insert new registers at k_x_group_user table.</p>
136: * @param oConn Database Connection
137: * @param sUserList A string of comma delimited User GUIDs that must be added to this ACLGroup.
138: * @throws SQLException May throw a primary key constraint violation is user already belongs to group.
139: */
140: public int addACLUsers(JDCConnection oConn, String sUserList)
141: throws SQLException {
142:
143: if (DebugFile.trace) {
144: DebugFile
145: .writeln("Begin ACLGroup.addACLUsers(Connection], "
146: + sUserList + ")");
147: DebugFile.incIdent();
148: }
149:
150: Statement oStmt;
151: int iRetVal = 0;
152: StringTokenizer oStrTok = new StringTokenizer(sUserList, ",");
153: String sIdUser;
154:
155: oStmt = oConn.createStatement();
156:
157: while (oStrTok.hasMoreElements()) {
158: sIdUser = oStrTok.nextToken();
159:
160: if (DebugFile.trace)
161: DebugFile
162: .writeln("Statement.executeUpdate(INSERT INTO "
163: + DB.k_x_group_user
164: + "("
165: + DB.gu_acl_group
166: + ","
167: + DB.gu_user
168: + ") VALUES('"
169: + getStringNull(DB.gu_acl_group, "null")
170: + "','" + sIdUser + "')");
171:
172: iRetVal += oStmt.executeUpdate("INSERT INTO "
173: + DB.k_x_group_user + "(" + DB.gu_acl_group + ","
174: + DB.gu_user + ") VALUES('"
175: + getString(DB.gu_acl_group) + "','" + sIdUser
176: + "')");
177: oStmt.close();
178: } // wend
179:
180: if (DebugFile.trace) {
181: DebugFile.decIdent();
182: DebugFile.writeln("End ACLGroup.addACLUsers() : "
183: + String.valueOf(iRetVal));
184: }
185:
186: return iRetVal;
187: } // addACLUser
188:
189: // ----------------------------------------------------------
190:
191: /**
192: * <p>Remove User from Group.</p>
193: * <p>remove register from k_x_group_user table.</p>
194: * @param oConn Database Connection
195: * @param sIdUser User Unique Identifier.
196: * @throws SQLException
197: */
198:
199: public int removeACLUser(JDCConnection oConn, String sIdUser)
200: throws SQLException {
201:
202: if (DebugFile.trace) {
203: DebugFile
204: .writeln("Begin ACLGroup.removeACLUser(Connection], "
205: + sIdUser + ")");
206: DebugFile.incIdent();
207: }
208:
209: int iRetVal;
210: Statement oStmt = oConn.createStatement();
211:
212: if (DebugFile.trace)
213: DebugFile.writeln("Statement.executeUpdate(DELETE FROM "
214: + DB.k_x_group_user + " WHERE " + DB.gu_user + "='"
215: + sIdUser + "' AND " + DB.gu_acl_group + "='"
216: + getStringNull(DB.gu_acl_group, "null") + "'");
217:
218: iRetVal = oStmt.executeUpdate("DELETE FROM "
219: + DB.k_x_group_user + " WHERE " + DB.gu_user + "='"
220: + sIdUser + "' AND " + DB.gu_acl_group + "='"
221: + getString(DB.gu_acl_group) + "'");
222: oStmt.close();
223:
224: if (DebugFile.trace) {
225: DebugFile.decIdent();
226: DebugFile.writeln("End ACLGroup.removeACLUser() : "
227: + String.valueOf(iRetVal));
228: }
229:
230: return iRetVal;
231: } // removeACLUser
232:
233: // ----------------------------------------------------------
234:
235: /**
236: * <p>Remove all users from this group.</p>
237: * <p>Delete registers from k_x_group_user</p>
238: * @param oConn Database connection
239: * @throws SQLException
240: */
241: public int clearACLUsers(JDCConnection oConn) throws SQLException {
242:
243: if (DebugFile.trace) {
244: DebugFile
245: .writeln("Begin ACLGroup.clearACLUsers([Connection])");
246: DebugFile.incIdent();
247: }
248:
249: int iRetVal;
250:
251: Statement oStmt = oConn.createStatement();
252:
253: if (DebugFile.trace)
254: DebugFile.writeln("DELETE FROM " + DB.k_x_group_user
255: + " WHERE " + DB.gu_acl_group + "='"
256: + getStringNull(DB.gu_acl_group, "null") + "'");
257:
258: iRetVal = oStmt.executeUpdate("DELETE FROM "
259: + DB.k_x_group_user + " WHERE " + DB.gu_acl_group
260: + "='" + getString(DB.gu_acl_group) + "'");
261:
262: oStmt.close();
263:
264: if (DebugFile.trace) {
265: DebugFile.decIdent();
266: DebugFile.writeln("End ACLGroup.clearACLUsers() : "
267: + String.valueOf(iRetVal));
268: }
269:
270: return iRetVal;
271: } // clearACLUsers
272:
273: /**
274: * Get users that belong to this group
275: * @param oConn JDCConnection
276: * @return DBSubset with all the columns from k_users table
277: * @throws SQLException
278: * @since 3.0
279: */
280: public DBSubset getACLUsers(JDCConnection oConn)
281: throws SQLException {
282: Object aGroup[] = { get(DB.gu_acl_group) };
283: DBSubset oUsers = new DBSubset(DB.k_x_group_user + " x,"
284: + DB.k_users + " u", "u." + DB.gu_user + ",u."
285: + DB.dt_created + ",u." + DB.id_domain + ",u."
286: + DB.tx_nickname + ",u." + DB.tx_pwd + ",u."
287: + DB.tx_pwd_sign + ",u." + DB.bo_change_pwd + ",u."
288: + DB.bo_searchable + ",u." + DB.bo_active + ",u."
289: + DB.len_quota + ",u." + DB.max_quota + ",u."
290: + DB.tp_account + ",u." + DB.id_account + ",u."
291: + DB.dt_last_update + ",u." + DB.dt_last_visit + ",u."
292: + DB.dt_cancel + ",u." + DB.tx_main_email + ",u."
293: + DB.tx_alt_email + ",u." + DB.nm_user + ",u."
294: + DB.tx_surname1 + ",u." + DB.tx_surname2 + ",u."
295: + DB.tx_challenge + ",u." + DB.tx_reply + ",u."
296: + DB.dt_pwd_expires + ",u." + DB.gu_category + ",u."
297: + DB.gu_workarea + ",u." + DB.nm_company + ",u."
298: + DB.de_title + ",u." + DB.id_gender + ",u."
299: + DB.dt_birth + ",u." + DB.ny_age + ",u."
300: + DB.marital_status + ",u." + DB.tx_education + ",u."
301: + DB.icq_id + ",u." + DB.sn_passport + ",u."
302: + DB.tp_passport + ",u." + DB.tx_comments, "x."
303: + DB.gu_acl_group + "=? AND " + "x." + DB.gu_user
304: + "=u." + DB.k_users, 10);
305:
306: oUsers.load(oConn, aGroup);
307: return oUsers;
308: } // getACLUsers
309:
310: // **********************************************************
311: // Metodos Estaticos
312:
313: /**
314: * <p>Get Group Unique Id. from its name.</p>
315: * <p>This method executes a SQL query with a ResultSet</p>
316: * @param oConn Connection Database Connection
317: * @param iDomainId int Domain Identifier to with Group belongs
318: * @param sGroupNm String Group Name
319: * @return Group Unique Identifier or <b>null</b> if no group with such name was found at given domain.
320: * @throws SQLException
321: * @since 3.0
322: */
323:
324: public static String getIdFromName(Connection oConn, int iDomainId,
325: String sGroupNm) throws SQLException {
326: String sRetVal;
327: PreparedStatement oStmt;
328: ResultSet oRSet;
329: oStmt = oConn
330: .prepareStatement("SELECT " + DB.gu_acl_group
331: + " FROM " + DB.k_acl_groups + " WHERE "
332: + DB.id_domain + "=? AND " + DB.nm_acl_group
333: + "=?", ResultSet.TYPE_FORWARD_ONLY,
334: ResultSet.CONCUR_READ_ONLY);
335: oStmt.setInt(1, iDomainId);
336: oStmt.setString(2, sGroupNm);
337: oRSet = oStmt.executeQuery();
338: if (oRSet.next())
339: sRetVal = oRSet.getString(1);
340: else
341: sRetVal = null;
342: oRSet.close();
343: oStmt.close();
344: return sRetVal;
345: }
346:
347: /**
348: * <p>Get Group Unique Id. from its name.</p>
349: * <p>This method calls k_sp_get_group_id stored procedure.</p>
350: * @param oConn JDCConnection
351: * @param iDomainId int Domain Identifier to with Group belongs
352: * @param sGroupNm Group Name
353: * @return Group String Unique Identifier or <b>null</b> if no group with such name was found at given domain.
354: * @throws SQLException
355: */
356:
357: public static String getIdFromName(JDCConnection oConn,
358: int iDomainId, String sGroupNm) throws SQLException {
359: String sRetVal;
360:
361: switch (oConn.getDataBaseProduct()) {
362:
363: case JDCConnection.DBMS_MYSQL:
364: case JDCConnection.DBMS_MSSQL:
365: case JDCConnection.DBMS_ORACLE:
366: sRetVal = DBPersist.getUIdFromName(oConn, new Integer(
367: iDomainId), sGroupNm, "k_sp_get_group_id");
368: break;
369: default:
370: sRetVal = getIdFromName((Connection) oConn, iDomainId,
371: sGroupNm);
372: } // end switch
373:
374: return sRetVal;
375: } // getIdFromName
376:
377: // ----------------------------------------------------------
378:
379: /**
380: * <p>Delete Group</p>
381: * <p>Call k_sp_del_group stored procedure</p>
382: * @param oConn Database Connection
383: * @param sGroupGUID Group Unique Identifier
384: * @throws SQLException
385: */
386: public static boolean delete(JDCConnection oConn, String sGroupGUID)
387: throws SQLException {
388: boolean bRetVal;
389:
390: if (DebugFile.trace) {
391: DebugFile.writeln("Begin ACLGroup.delete([Connection], "
392: + sGroupGUID + ")");
393: DebugFile.incIdent();
394: }
395:
396: switch (oConn.getDataBaseProduct()) {
397: case JDCConnection.DBMS_POSTGRESQL:
398: Statement oStmt = oConn.createStatement();
399: if (DebugFile.trace)
400: DebugFile
401: .writeln("Statement.executeQuery(SELECT k_sp_del_group ('"
402: + sGroupGUID + "'))");
403: ResultSet oRSet = oStmt
404: .executeQuery("SELECT k_sp_del_group ('"
405: + sGroupGUID + "')");
406: oRSet.close();
407: oStmt.close();
408: bRetVal = true;
409: break;
410: default:
411: if (DebugFile.trace)
412: DebugFile
413: .writeln("Connection.prepareCall({ call k_sp_del_group ('"
414: + sGroupGUID + "') })");
415: CallableStatement oCall = oConn
416: .prepareCall("{ call k_sp_del_group ('"
417: + sGroupGUID + "') }");
418: bRetVal = oCall.execute();
419: oCall.close();
420: }
421:
422: if (DebugFile.trace) {
423: DebugFile.decIdent();
424: DebugFile.writeln("End ACLGroup.delete() : "
425: + String.valueOf(bRetVal));
426: }
427:
428: return bRetVal;
429: }
430:
431: // **********************************************************
432: // Public Constants
433:
434: public static final short ClassId = 3;
435:
436: } // ACLGroup
|