KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > core > server > XincoCoreACEServer


1 /**
2  *Copyright 2004 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoCoreACEServer
23  *
24  * Description: access control entry
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.core.server;
38
39 import java.util.Vector JavaDoc;
40 import java.sql.*;
41
42 import com.bluecubs.xinco.core.*;
43
44 public class XincoCoreACEServer extends XincoCoreACE {
45     
46     //create single ace object for data structures
47
public XincoCoreACEServer(int attrID, XincoDBManager DBM) throws XincoException {
48         
49         try {
50             
51             Statement stmt = DBM.con.createStatement();
52             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_ace WHERE id=" + attrID);
53
54             //throw exception if no result found
55
int RowCount = 0;
56             while (rs.next()) {
57                 RowCount++;
58                 setId(rs.getInt("id"));
59                 setXinco_core_user_id(rs.getInt("xinco_core_user_id"));
60                 setXinco_core_group_id(rs.getInt("xinco_core_group_id"));
61                 setXinco_core_node_id(rs.getInt("xinco_core_node_id"));
62                 setXinco_core_data_id(rs.getInt("xinco_core_data_id"));
63                 setRead_permission(rs.getBoolean("read_permission"));
64                 setWrite_permission(rs.getBoolean("write_permission"));
65                 setExecute_permission(rs.getBoolean("execute_permission"));
66                 setAdmin_permission(rs.getBoolean("admin_permission"));
67             }
68             if (RowCount < 1) {
69                 throw new XincoException();
70             }
71
72             stmt.close();
73             
74         } catch (Exception JavaDoc e) {
75             throw new XincoException();
76         }
77         
78     }
79     
80     //create single ace object for data structures
81
public XincoCoreACEServer(int attrID, int attrUID, int attrGID, int attrNID, int attrDID, boolean attrRP, boolean attrWP, boolean attrEP, boolean attrAP) throws XincoException {
82         
83         setId(attrID);
84         setXinco_core_user_id(attrUID);
85         setXinco_core_group_id(attrGID);
86         setXinco_core_node_id(attrNID);
87         setXinco_core_data_id(attrDID);
88         setRead_permission(attrRP);
89         setWrite_permission(attrWP);
90         setExecute_permission(attrEP);
91         setAdmin_permission(attrAP);
92         
93     }
94     
95     //write to db
96
public int write2DB(XincoDBManager DBM) throws XincoException {
97
98         try {
99             
100             String JavaDoc xcuid = "";
101             String JavaDoc xcgid = "";
102             String JavaDoc xcnid = "";
103             String JavaDoc xcdid = "";
104             
105             int rp = 0;
106             int wp = 0;
107             int xp = 0;
108             int ap = 0;
109             
110             //set values of nullable attributes
111
if (getXinco_core_user_id() == 0) {
112                 xcuid = "NULL";
113             } else {
114                 xcuid = "" + getXinco_core_user_id();
115             }
116             if (getXinco_core_group_id() == 0) {
117                 xcgid = "NULL";
118             } else {
119                 xcgid = "" + getXinco_core_group_id();
120             }
121             if (getXinco_core_node_id() == 0) {
122                 xcnid = "NULL";
123             } else {
124                 xcnid = "" + getXinco_core_node_id();
125             }
126             if (getXinco_core_data_id() == 0) {
127                 xcdid = "NULL";
128             } else {
129                 xcdid = "" + getXinco_core_data_id();
130             }
131             
132             //convert boolean to 0/1
133
if (isRead_permission()) {
134                 rp = 1;
135             }
136             if (isWrite_permission()) {
137                 wp = 1;
138             }
139             if (isExecute_permission()) {
140                 xp = 1;
141             }
142             if (isAdmin_permission()) {
143                 ap = 1;
144             }
145
146             if (getId() > 0) {
147                 Statement stmt = DBM.con.createStatement();
148                 stmt.executeUpdate("UPDATE xinco_core_ace SET xinco_core_user_id=" + xcuid + ", xinco_core_group_id=" + xcgid + ", xinco_core_node_id=" + xcnid + ", xinco_core_data_id=" + xcdid + ", read_permission=" + rp + ", write_permission=" + wp + ", execute_permission=" + xp + ", admin_permission=" + ap + " WHERE id=" + getId());
149                 stmt.close();
150             } else {
151                 setId(DBM.getNewID("xinco_core_ace"));
152
153                 Statement stmt = DBM.con.createStatement();
154                 stmt.executeUpdate("INSERT INTO xinco_core_ace VALUES (" + getId() + ", " + xcuid + ", " + xcgid + ", " + xcnid + ", " + xcdid + ", " + rp + ", " + wp + ", " + xp + ", " + ap + ")");
155                 stmt.close();
156             }
157
158             DBM.con.commit();
159             
160         } catch (Exception JavaDoc e) {
161             try {
162                 DBM.con.rollback();
163             } catch (Exception JavaDoc erollback) {
164             }
165             throw new XincoException();
166         }
167         
168         return getId();
169             
170     }
171     
172     //remove from db
173
public static int removeFromDB(XincoCoreACE attrCACE, XincoDBManager DBM) throws XincoException {
174
175         try {
176             
177             Statement stmt = DBM.con.createStatement();
178             stmt.executeUpdate("DELETE FROM xinco_core_ace WHERE id=" + attrCACE.getId());
179             stmt.close();
180
181             DBM.con.commit();
182             
183         } catch (Exception JavaDoc e) {
184             try {
185                 DBM.con.rollback();
186             } catch (Exception JavaDoc erollback) {
187             }
188             throw new XincoException();
189         }
190         
191         return 0;
192             
193     }
194     
195     //create complete ACL for node or data
196
public static Vector JavaDoc getXincoCoreACL(int attrID, String JavaDoc attrT, XincoDBManager DBM) {
197         
198         Vector JavaDoc core_acl = new Vector JavaDoc();
199         
200         try {
201             Statement stmt = DBM.con.createStatement();
202             ResultSet rs = stmt.executeQuery("SELECT * FROM xinco_core_ace WHERE " + attrT + "=" + attrID + " ORDER BY xinco_core_user_id, xinco_core_group_id, xinco_core_node_id, xinco_core_data_id");
203
204             while (rs.next()) {
205                 core_acl.addElement(new XincoCoreACEServer(rs.getInt("id"), rs.getInt("xinco_core_user_id"), rs.getInt("xinco_core_group_id"), rs.getInt("xinco_core_node_id"), rs.getInt("xinco_core_data_id"), rs.getBoolean("read_permission"), rs.getBoolean("write_permission"), rs.getBoolean("execute_permission"), rs.getBoolean("admin_permission")));
206             }
207
208             stmt.close();
209         } catch (Exception JavaDoc e) {
210             core_acl.removeAllElements();
211         }
212
213         return core_acl;
214     }
215
216     //check access by comparing user / user groups to ACL and return permissions
217
public static XincoCoreACE checkAccess(XincoCoreUser attrU, Vector JavaDoc attrACL) {
218         
219         int i = 0;
220         int j = 0;
221         boolean match_ace = false;
222         XincoCoreACE core_ace = new XincoCoreACE();
223         
224         for (i=0;i<attrACL.size();i++) {
225             //reset match_ace
226
match_ace = false;
227             //check if user is mentioned in ACE
228
if (((XincoCoreACE)attrACL.elementAt(i)).getXinco_core_user_id() == attrU.getId()) { match_ace = true; }
229             //check if group of user is mentioned in ACE
230
if (!match_ace) {
231                 for (j=0;j<attrU.getXinco_core_groups().size();j++) {
232                     if (((XincoCoreACE)attrACL.elementAt(i)).getXinco_core_group_id() == ((XincoCoreGroup)attrU.getXinco_core_groups().elementAt(j)).getId()) {
233                         match_ace = true;
234                         break;
235                     }
236                 }
237             }
238             //add to rights
239
if (match_ace) {
240                 //modify read permission
241
if (!core_ace.isRead_permission()) {
242                     core_ace.setRead_permission(((XincoCoreACE)attrACL.elementAt(i)).isRead_permission());
243                 }
244                 //modify write permission
245
if (!core_ace.isWrite_permission()) {
246                     core_ace.setWrite_permission(((XincoCoreACE)attrACL.elementAt(i)).isWrite_permission());
247                 }
248                 //modify execute permission
249
if (!core_ace.isExecute_permission()) {
250                     core_ace.setExecute_permission(((XincoCoreACE)attrACL.elementAt(i)).isExecute_permission());
251                 }
252                 //modify admin permission
253
if (!core_ace.isAdmin_permission()) {
254                     core_ace.setAdmin_permission(((XincoCoreACE)attrACL.elementAt(i)).isAdmin_permission());
255                 }
256             }
257         }
258         
259         return core_ace;
260     }
261
262 }
263
Popular Tags