001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: generic.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.authentication.credentialsmanagers.databasedrivers;
009:
010: import com.uwyn.rife.database.queries.*;
011:
012: import com.uwyn.rife.authentication.Credentials;
013: import com.uwyn.rife.authentication.credentialsmanagers.DatabaseUsers;
014: import com.uwyn.rife.authentication.credentialsmanagers.ListRoles;
015: import com.uwyn.rife.authentication.credentialsmanagers.ListUsers;
016: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
017: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateLoginException;
018: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateRoleException;
019: import com.uwyn.rife.authentication.credentialsmanagers.exceptions.DuplicateUserIdException;
020: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
021: import com.uwyn.rife.config.RifeConfig;
022: import com.uwyn.rife.database.Datasource;
023:
024: public class generic extends DatabaseUsers {
025: protected CreateSequence mCreateSequenceRole = null;
026: protected CreateTable mCreateTableRole = null;
027: protected CreateTable mCreateTableUser = null;
028: protected CreateTable mCreateTableRoleLink = null;
029: protected Select mVerifyCredentialsNoRole = null;
030: protected Select mVerifyCredentialsRole = null;
031: protected Select mGetRoleId = null;
032: protected SequenceValue mGetNewRoleId = null;
033: protected Insert mAddRole = null;
034: protected Select mContainsRole = null;
035: protected Select mCountRoles = null;
036: protected Select mListRoles = null;
037: protected Insert mAddUserWithId = null;
038: protected Select mGetFreeUserId = null;
039: protected Insert mAddRoleLink = null;
040: protected Select mGetAttributes = null;
041: protected Select mGetUserRoles = null;
042: protected Select mContainsUser = null;
043: protected Select mCountUsers = null;
044: protected Select mGetLogin = null;
045: protected Select mGetUserId = null;
046: protected Select mListUsers = null;
047: protected Select mListUsersRanged = null;
048: protected Select mIsUserInRole = null;
049: protected Select mListUsersInRole = null;
050: protected Update mUpdateUser = null;
051: protected Delete mRemoveRoleLinksByUserId = null;
052: protected Delete mRemoveUserByLogin = null;
053: protected Delete mRemoveUserByUserId = null;
054: protected Delete mRemoveRole = null;
055: protected Delete mClearUsers = null;
056: protected Select mListUserRoles = null;
057: protected DropSequence mDropSequenceRole = null;
058: protected DropTable mDropTableRole = null;
059: protected DropTable mDropTableUser = null;
060: protected DropTable mDropTableRoleLink = null;
061:
062: public generic(Datasource datasource) {
063: super (datasource);
064:
065: mCreateSequenceRole = new CreateSequence(getDatasource())
066: .name(RifeConfig.Authentication.getSequenceRole());
067:
068: mCreateTableRole = new CreateTable(getDatasource()).table(
069: RifeConfig.Authentication.getTableRole()).column(
070: "roleId", int.class, CreateTable.NOTNULL).column(
071: "name", String.class,
072: RifeConfig.Authentication.getRoleNameMaximumLength(),
073: CreateTable.NOTNULL).primaryKey(
074: RifeConfig.Authentication.getTableRole().toUpperCase()
075: + "_PK", "roleId").unique(
076: RifeConfig.Authentication.getTableRole().toUpperCase()
077: + "_NAME_UQ", "name");
078:
079: mCreateTableUser = new CreateTable(getDatasource()).table(
080: RifeConfig.Authentication.getTableUser()).column(
081: "userId", long.class, CreateTable.NOTNULL).column(
082: "login", String.class,
083: RifeConfig.Authentication.getLoginMaximumLength(),
084: CreateTable.NOTNULL).column("passwd", String.class,
085: RifeConfig.Authentication.getPasswordMaximumLength(),
086: CreateTable.NOTNULL).primaryKey(
087: RifeConfig.Authentication.getTableUser().toUpperCase()
088: + "_PK", "userId").unique(
089: RifeConfig.Authentication.getTableUser().toUpperCase()
090: + "_LOGIN_UQ", "login");
091:
092: mCreateTableRoleLink = new CreateTable(getDatasource()).table(
093: RifeConfig.Authentication.getTableRoleLink()).column(
094: "userId", long.class, CreateTable.NOTNULL).column(
095: "roleId", int.class, CreateTable.NOTNULL).primaryKey(
096: RifeConfig.Authentication.getTableRoleLink()
097: .toUpperCase()
098: + "_PK", new String[] { "userId", "roleId" })
099: .foreignKey(
100: RifeConfig.Authentication.getTableRoleLink()
101: .toUpperCase()
102: + "_USERID_FK",
103: mCreateTableUser.getTable(), "userId",
104: "userId", null, CreateTable.CASCADE)
105: .foreignKey(
106: RifeConfig.Authentication.getTableRoleLink()
107: .toUpperCase()
108: + "_ROLEID_FK",
109: mCreateTableRole.getTable(), "roleId",
110: "roleId", null, CreateTable.CASCADE);
111:
112: mVerifyCredentialsNoRole = new Select(getDatasource()).from(
113: mCreateTableUser.getTable()).field("userId")
114: .whereParameter("login", "=").whereParameterAnd(
115: "passwd", "=");
116:
117: mVerifyCredentialsRole = new Select(getDatasource()).from(
118: mCreateTableUser.getTable()).join(
119: mCreateTableRoleLink.getTable()).join(
120: mCreateTableRole.getTable()).field(
121: mCreateTableUser.getTable() + ".userId")
122: .whereParameter("login", "=").whereParameterAnd(
123: "passwd", "=").whereAnd(
124: mCreateTableUser.getTable() + ".userId = "
125: + mCreateTableRoleLink.getTable()
126: + ".userId").whereParameterAnd("name",
127: "role", "=").whereAnd(
128: mCreateTableRole.getTable() + ".roleId = "
129: + mCreateTableRoleLink.getTable()
130: + ".roleId");
131:
132: mGetRoleId = new Select(getDatasource()).from(
133: mCreateTableRole.getTable()).field("roleId")
134: .whereParameter("name", "=");
135:
136: mGetNewRoleId = new SequenceValue(getDatasource()).name(
137: mCreateSequenceRole.getName()).next();
138:
139: mAddRole = new Insert(getDatasource()).into(
140: mCreateTableRole.getTable()).fieldParameter("roleId")
141: .fieldParameter("name");
142:
143: mContainsRole = new Select(getDatasource()).from(
144: mCreateTableRole.getTable())
145: .whereParameter("name", "=");
146:
147: mCountRoles = new Select(getDatasource()).field("count(*)")
148: .from(mCreateTableRole.getTable());
149:
150: mListRoles = new Select(getDatasource()).from(
151: mCreateTableRole.getTable()).orderBy("name");
152:
153: mAddUserWithId = new Insert(getDatasource()).into(
154: mCreateTableUser.getTable()).fieldParameter("userId")
155: .fieldParameter("login").fieldParameter("passwd");
156:
157: mGetFreeUserId = new Select(getDatasource()).field(
158: "COALESCE(MAX(userId)+1, 0) as freeUserId").from(
159: mCreateTableUser.getTable());
160:
161: mAddRoleLink = new Insert(getDatasource()).into(
162: mCreateTableRoleLink.getTable()).fieldParameter(
163: "userId").fieldParameter("roleId");
164:
165: mGetAttributes = new Select(getDatasource()).field("userId")
166: .field("passwd AS password").from(
167: mCreateTableUser.getTable()).whereParameter(
168: "login", "=");
169:
170: mGetUserRoles = new Select(getDatasource()).field("name").from(
171: mCreateTableRoleLink.getTable()).join(
172: mCreateTableRole.getTable()).whereParameter("userId",
173: "=").whereAnd(
174: mCreateTableRoleLink.getTable() + ".roleId = "
175: + mCreateTableRole.getTable() + ".roleId")
176: .orderBy("name");
177:
178: mContainsUser = new Select(getDatasource()).from(
179: mCreateTableUser.getTable()).whereParameter("login",
180: "=");
181:
182: mCountUsers = new Select(getDatasource()).field("count(*)")
183: .from(mCreateTableUser.getTable());
184:
185: mGetLogin = new Select(getDatasource()).field("login").from(
186: mCreateTableUser.getTable()).whereParameter("userId",
187: "=");
188:
189: mGetUserId = new Select(getDatasource()).field("userId").from(
190: mCreateTableUser.getTable()).whereParameter("login",
191: "=");
192:
193: mListUsers = new Select(getDatasource()).from(
194: mCreateTableUser.getTable()).orderBy("login");
195:
196: mListUsersRanged = new Select(getDatasource()).from(
197: mCreateTableUser.getTable()).orderBy("login")
198: .limitParameter("limit").offsetParameter("offset");
199:
200: mIsUserInRole = new Select(getDatasource()).from(
201: mCreateTableRoleLink.getTable()).join(
202: mCreateTableRole.getTable()).field(
203: mCreateTableRoleLink.getTable() + ".userId")
204: .whereParameter(
205: mCreateTableRoleLink.getTable() + ".userId",
206: "=").whereParameterAnd("name", "role", "=")
207: .whereAnd(
208: mCreateTableRole.getTable() + ".roleId = "
209: + mCreateTableRoleLink.getTable()
210: + ".roleId");
211:
212: mListUsersInRole = new Select(getDatasource()).field(
213: mCreateTableUser.getTable() + ".userId").field("login")
214: .field("passwd").from(mCreateTableUser.getTable())
215: .join(mCreateTableRoleLink.getTable()).join(
216: mCreateTableRole.getTable()).where(
217: mCreateTableUser.getTable() + ".userId = "
218: + mCreateTableRoleLink.getTable()
219: + ".userId").whereAnd(
220: mCreateTableRoleLink.getTable() + ".roleId = "
221: + mCreateTableRole.getTable()
222: + ".roleId").whereParameterAnd(
223: mCreateTableRole.getTable() + ".name", "role",
224: "=").orderBy("login");
225:
226: mUpdateUser = new Update(getDatasource()).table(
227: mCreateTableUser.getTable()).fieldParameter("passwd")
228: .whereParameter("login", "=");
229:
230: mRemoveRoleLinksByUserId = new Delete(getDatasource()).from(
231: mCreateTableRoleLink.getTable()).whereParameter(
232: "userId", "=");
233:
234: mRemoveUserByLogin = new Delete(getDatasource()).from(
235: mCreateTableUser.getTable()).whereParameter("login",
236: "=");
237:
238: mRemoveUserByUserId = new Delete(getDatasource()).from(
239: mCreateTableUser.getTable()).whereParameter("userId",
240: "=");
241:
242: mRemoveRole = new Delete(getDatasource()).from(
243: mCreateTableRole.getTable()).whereParameter("name",
244: "role", "=");
245:
246: mClearUsers = new Delete(getDatasource()).from(mCreateTableUser
247: .getTable());
248:
249: mListUserRoles = new Select(getDatasource()).from(
250: mCreateTableRole.getTable()).join(
251: mCreateTableRoleLink.getTable()).join(
252: mCreateTableUser.getTable()).field(
253: mCreateTableRole.getTable() + ".name").where(
254: mCreateTableRole.getTable() + ".roleId = "
255: + mCreateTableRoleLink.getTable() + ".roleId")
256: .whereParameterAnd(
257: mCreateTableUser.getTable() + ".login", "=")
258: .whereAnd(
259: mCreateTableUser.getTable() + ".userId = "
260: + mCreateTableRoleLink.getTable()
261: + ".userId");
262:
263: mDropSequenceRole = new DropSequence(getDatasource())
264: .name(mCreateSequenceRole.getName());
265:
266: mDropTableRole = new DropTable(getDatasource())
267: .table(mCreateTableRole.getTable());
268:
269: mDropTableUser = new DropTable(getDatasource())
270: .table(mCreateTableUser.getTable());
271:
272: mDropTableRoleLink = new DropTable(getDatasource())
273: .table(mCreateTableRoleLink.getTable());
274: }
275:
276: public boolean install() throws CredentialsManagerException {
277: return _install(mCreateSequenceRole, mCreateTableRole,
278: mCreateTableUser, mCreateTableRoleLink);
279: }
280:
281: public boolean remove() throws CredentialsManagerException {
282: return _remove(mDropSequenceRole, mDropTableRole,
283: mDropTableUser, mDropTableRoleLink);
284: }
285:
286: public long verifyCredentials(Credentials credentials)
287: throws CredentialsManagerException {
288: return _verifyCredentials(mVerifyCredentialsNoRole,
289: mVerifyCredentialsRole, credentials);
290: }
291:
292: public DatabaseUsers addRole(String role)
293: throws CredentialsManagerException {
294: try {
295: _addRole(mGetNewRoleId, mAddRole, role);
296: } catch (CredentialsManagerException e) {
297: if (null != e.getCause() && null != e.getCause().getCause()) {
298: String message = e.getCause().getCause().getMessage()
299: .toUpperCase();
300: if (-1 != message.indexOf(mCreateTableRole
301: .getUniqueConstraints().get(0).getName())) {
302: throw new DuplicateRoleException(role);
303: }
304: }
305:
306: throw e;
307: }
308:
309: return this ;
310: }
311:
312: public boolean containsRole(String role)
313: throws CredentialsManagerException {
314: return _containsRole(mContainsRole, role);
315: }
316:
317: public long countRoles() throws CredentialsManagerException {
318: return _countRoles(mCountRoles);
319: }
320:
321: public boolean listRoles(ListRoles processor)
322: throws CredentialsManagerException {
323: return _listRoles(mListRoles, processor);
324: }
325:
326: public DatabaseUsers addUser(String login,
327: RoleUserAttributes attributes)
328: throws CredentialsManagerException {
329: try {
330: _addUser(mAddUserWithId, mGetFreeUserId, mGetRoleId,
331: mAddRoleLink, login, attributes);
332: } catch (CredentialsManagerException e) {
333: if (null != e.getCause() && null != e.getCause().getCause()) {
334: String message = e.getCause().getCause().getMessage()
335: .toUpperCase();
336: if (-1 != message.indexOf(mCreateTableUser
337: .getPrimaryKeys().get(0).getName())) {
338: throw new DuplicateUserIdException(attributes
339: .getUserId());
340: }
341: if (-1 != message.indexOf(mCreateTableUser
342: .getUniqueConstraints().get(0).getName())) {
343: throw new DuplicateLoginException(login);
344: }
345: }
346:
347: throw e;
348: }
349:
350: return this ;
351: }
352:
353: public RoleUserAttributes getAttributes(String login)
354: throws CredentialsManagerException {
355: return _getAttributes(mGetAttributes, mGetUserRoles, login);
356: }
357:
358: public boolean containsUser(String login)
359: throws CredentialsManagerException {
360: return _containsUser(mContainsUser, login);
361: }
362:
363: public long countUsers() throws CredentialsManagerException {
364: return _countUsers(mCountUsers);
365: }
366:
367: public String getLogin(long userId)
368: throws CredentialsManagerException {
369: return _getLogin(mGetLogin, userId);
370: }
371:
372: public long getUserId(String login)
373: throws CredentialsManagerException {
374: return _getUserId(mGetUserId, login);
375: }
376:
377: public boolean listUsers(ListUsers processor)
378: throws CredentialsManagerException {
379: return _listUsers(mListUsers, processor);
380: }
381:
382: public boolean listUsers(ListUsers processor, int limit, int offset)
383: throws CredentialsManagerException {
384: return _listUsers(mListUsersRanged, processor, limit, offset);
385: }
386:
387: public boolean isUserInRole(long userId, String role)
388: throws CredentialsManagerException {
389: return _isUserInRole(mIsUserInRole, userId, role);
390: }
391:
392: public boolean listUsersInRole(ListUsers processor, String role)
393: throws CredentialsManagerException {
394: return _listUsersInRole(mListUsersInRole, processor, role);
395: }
396:
397: public boolean updateUser(String login,
398: RoleUserAttributes attributes)
399: throws CredentialsManagerException {
400: return _updateUser(mUpdateUser, mRemoveRoleLinksByUserId,
401: mGetRoleId, mAddRoleLink, login, attributes);
402: }
403:
404: public boolean removeUser(String login)
405: throws CredentialsManagerException {
406: return _removeUser(mRemoveUserByLogin, login);
407: }
408:
409: public boolean removeUser(long userId)
410: throws CredentialsManagerException {
411: return _removeUser(mRemoveUserByUserId, userId);
412: }
413:
414: public boolean removeRole(String name)
415: throws CredentialsManagerException {
416: return _removeRole(mRemoveRole, name);
417: }
418:
419: public void clearUsers() throws CredentialsManagerException {
420: _clearUsers(mClearUsers);
421: }
422:
423: public boolean listUserRoles(String login, ListRoles processor)
424: throws CredentialsManagerException {
425: return _listUserRoles(mListUserRoles, login, processor);
426: }
427: }
|