1 package org.apache.turbine.om.security.peer;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import com.workingdogs.village.Record;
58 import java.util.ArrayList;
59 import java.util.List;
60 import java.util.Map;
61 import org.apache.torque.TorqueException;
62 import org.apache.torque.om.BaseObject;
63 import org.apache.torque.om.Persistent;
64 import org.apache.torque.util.BasePeer;
65 import org.apache.torque.util.Criteria;
66 import org.apache.turbine.om.security.Group;
67 import org.apache.turbine.om.security.Role;
68 import org.apache.turbine.om.security.TurbineRole;
69 import org.apache.turbine.om.security.User;
70 import org.apache.turbine.services.security.TurbineSecurity;
71 import org.apache.turbine.services.security.db.DBSecurityService;
72 import org.apache.turbine.util.ObjectUtils;
73 import org.apache.turbine.util.db.map.TurbineMapBuilder;
74 import org.apache.turbine.util.security.DataBackendException;
75 import org.apache.turbine.util.security.RoleSet;
76
77
78 /***
79 * This class handles all the database access for the ROLE table.
80 * This table contains all the roles that a given member can play.
81 *
82 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
83 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
84 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
85 * @version $Id: RolePeer.java,v 1.3 2002/07/11 07:34:30 mpoeschl Exp $
86 */
87 public class RolePeer extends BasePeer
88 {
89 private static final TurbineMapBuilder mapBuilder =
90 (TurbineMapBuilder) getMapBuilder("org.apache.turbine.util.db.map.TurbineMapBuilder");
91
92 /*** The table name for this peer. */
93 private static final String TABLE_NAME = mapBuilder.getTableRole();
94
95 /*** The column name for the role id field. */
96 public static final String ROLE_ID = mapBuilder.getRole_RoleId();
97
98 /*** The column name for the name field. */
99 public static final String NAME = mapBuilder.getRole_Name();
100
101 /*** The column name for the ObjectData field */
102 public static final String OBJECTDATA = mapBuilder.getRole_ObjectData();
103
104 /*** The Oracle sequence name for this peer. */
105 private static final String SEQUENCE_NAME = mapBuilder.getSequenceRole();
106
107 /***
108 * Retrieves/assembles a RoleSet based on the Criteria passed in
109 */
110 public static RoleSet retrieveSet(Criteria criteria) throws Exception
111 {
112 List results = RolePeer.doSelect(criteria);
113 RoleSet rs = new RoleSet();
114 for (int i = 0; i < results.size(); i++)
115 {
116 rs.add((Role) results.get(i));
117 }
118 return rs;
119 }
120
121 /***
122 * Retrieves a set of Roles that an User was assigned in a Group
123 *
124 * @param user An user.
125 * @param group A group
126 * @return A Set of Roles of this User in the Group
127 * @exception Exception, a generic exception.
128 */
129 public static RoleSet retrieveSet( User user, Group group )
130 throws Exception
131 {
132 Criteria criteria = new Criteria();
133
134 /*
135 * Peer specific methods should absolutely NOT be part
136 * of any of the generic interfaces in the security system.
137 * this is not good.
138 *
139 * UserPeer up = TurbineSecurity.getUserPeerInstance();
140 */
141
142 UserPeer up = ((DBSecurityService)TurbineSecurity.getService())
143 .getUserPeerInstance();
144
145 criteria.add(up.getFullColumnName(UserPeer.USERNAME),
146 user.getUserName());
147 criteria.add(UserGroupRolePeer.GROUP_ID,
148 ((Persistent)group).getPrimaryKey());
149
150 criteria.addJoin(up.getFullColumnName(UserPeer.USER_ID),
151 UserGroupRolePeer.USER_ID);
152 criteria.addJoin(UserGroupRolePeer.ROLE_ID, RolePeer.ROLE_ID);
153 return retrieveSet(criteria);
154 }
155
156 /***
157 * Issues a select based on a criteria.
158 *
159 * @param Criteria object containing data that is used to create
160 * the SELECT statement.
161 * @return Vector containing Role objects.
162 * @exception Exception, a generic exception.
163 */
164 public static List doSelect(Criteria criteria)
165 throws TorqueException
166 {
167 try
168 {
169 criteria.addSelectColumn(ROLE_ID)
170 .addSelectColumn(NAME)
171 .addSelectColumn(OBJECTDATA);
172
173 if (criteria.getOrderByColumns() == null ||
174 criteria.getOrderByColumns().size() == 0)
175 {
176 criteria.addAscendingOrderByColumn(NAME);
177 }
178
179 // Place any checks here to intercept criteria which require
180 // custom SQL. For example:
181 // if ( criteria.containsKey("SomeTable.SomeColumn") )
182 // {
183 // String whereSql = "SomeTable.SomeColumn IN (Select ...";
184 // criteria.add("SomeTable.SomeColumn",
185 // whereSQL, criteria.CUSTOM);
186 // }
187
188 // BasePeer returns a Vector of Value (Village) arrays. The
189 // array order follows the order columns were placed in the
190 // Select clause.
191 List rows = BasePeer.doSelect(criteria);
192 List results = new ArrayList();
193
194 // Populate the object(s).
195 for (int i = 0; i < rows.size(); i++ )
196 {
197 //Role obj = new Role();
198 Role obj = new TurbineRole();
199 Record row = (Record) rows.get(i);
200 ((TurbineRole) obj).setPrimaryKey(row.getValue(1).asInt());
201 ((TurbineRole) obj).setName(row.getValue(2).asString());
202 byte[] objectData = (byte[]) row.getValue(3).asBytes();
203 Map temp = (Map) ObjectUtils.deserialize(objectData);
204 if (temp != null)
205 {
206 ((TurbineRole) obj).setAttributes(temp);
207 }
208 results.add(obj);
209 }
210
211 return results;
212 }
213 catch (Exception ex)
214 {
215 throw new TorqueException (ex);
216 }
217 }
218
219 /***
220 * Builds a criteria object based upon an Role object
221 */
222 public static Criteria buildCriteria( Role role )
223 {
224 Criteria criteria = new Criteria();
225 if ( !((BaseObject)role).isNew() )
226 {
227 criteria.add(ROLE_ID, ((BaseObject)role).getPrimaryKey());
228 }
229 criteria.add(NAME, role.getName());
230 // causing the removal and updating of roles to
231 // crap out because of the generated SQL.
232 //criteria.add(OBJECTDATA, role.getAttributes());
233 return criteria;
234 }
235
236 /***
237 * Issues an update based on a criteria.
238 *
239 * @param Criteria object containing data that is used to create
240 * the UPDATE statement.
241 * @exception Exception, a generic exception.
242 */
243 public static void doUpdate(Criteria criteria)
244 throws TorqueException
245 {
246 Criteria selectCriteria = new Criteria(2);
247 selectCriteria.put( ROLE_ID, criteria.remove(ROLE_ID) );
248 BasePeer.doUpdate( selectCriteria, criteria );
249 }
250
251 /***
252 * Checks if a Role is defined in the system. The name
253 * is used as query criteria.
254 *
255 * @param permission The Role to be checked.
256 * @return <code>true</code> if given Role exists in the system.
257 * @throws DataBackendException when more than one Role with
258 * the same name exists.
259 * @throws Exception, a generic exception.
260 */
261 public static boolean checkExists( Role role )
262 throws DataBackendException, Exception
263 {
264 Criteria criteria = new Criteria();
265 criteria.addSelectColumn(ROLE_ID);
266 criteria.add(NAME, role.getName());
267 List results = BasePeer.doSelect(criteria);
268 if(results.size() > 1)
269 {
270 throw new DataBackendException("Multiple roles named '" +
271 role.getName() + "' exist!");
272 }
273 return (results.size() == 1);
274 }
275
276 /***
277 * Get the name of this table.
278 *
279 * @return A String with the name of the table.
280 */
281 public static String getTableName()
282 {
283 return TABLE_NAME;
284 }
285
286 /***
287 * Returns the full name of a column.
288 *
289 * @return A String with the full name of the column.
290 */
291 public static String getColumnName (String name)
292 {
293 StringBuffer sb = new StringBuffer();
294 sb.append (TABLE_NAME);
295 sb.append (".");
296 sb.append (name);
297 return sb.toString();
298 }
299 }
This page was automatically generated by Maven