Source Code Cross Referenced for DatabaseUsers.java in  » Web-Framework » rife-1.6.1 » com » uwyn » rife » authentication » credentialsmanagers » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » rife 1.6.1 » com.uwyn.rife.authentication.credentialsmanagers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003:         * Steven Grimm <koreth[remove] at midwinter dot com>
004:         * Distributed under the terms of either:
005:         * - the common development and distribution license (CDDL), v1.0; or
006:         * - the GNU Lesser General Public License, v2.1 or later
007:         * $Id: DatabaseUsers.java 3634 2007-01-08 21:42:24Z gbevin $
008:         */
009:        package com.uwyn.rife.authentication.credentialsmanagers;
010:
011:        import com.uwyn.rife.authentication.credentialsmanagers.exceptions.*;
012:        import com.uwyn.rife.database.*;
013:        import com.uwyn.rife.database.queries.*;
014:
015:        import com.uwyn.rife.authentication.Credentials;
016:        import com.uwyn.rife.authentication.CredentialsManager;
017:        import com.uwyn.rife.authentication.PasswordEncrypting;
018:        import com.uwyn.rife.authentication.credentials.RoleUserCredentials;
019:        import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
020:        import com.uwyn.rife.database.exceptions.DatabaseException;
021:        import com.uwyn.rife.tools.InnerClassException;
022:        import com.uwyn.rife.tools.StringEncryptor;
023:        import java.security.NoSuchAlgorithmException;
024:        import java.sql.ResultSet;
025:        import java.sql.SQLException;
026:
027:        public abstract class DatabaseUsers extends DbQueryManager implements 
028:                CredentialsManager, RoleUsersManager, PasswordEncrypting {
029:            protected StringEncryptor mPasswordEncryptor = null;
030:
031:            protected DatabaseUsers(Datasource datasource) {
032:                super (datasource);
033:            }
034:
035:            public abstract boolean install()
036:                    throws CredentialsManagerException;
037:
038:            public abstract boolean remove() throws CredentialsManagerException;
039:
040:            public StringEncryptor getPasswordEncryptor() {
041:                return mPasswordEncryptor;
042:            }
043:
044:            public void setPasswordEncryptor(StringEncryptor passwordEncryptor) {
045:                mPasswordEncryptor = passwordEncryptor;
046:            }
047:
048:            protected boolean _install(final CreateSequence createSequenceRole,
049:                    final CreateTable createTableRole,
050:                    final CreateTable createTableUser,
051:                    final CreateTable createTableRoleLink)
052:                    throws CredentialsManagerException {
053:                assert createSequenceRole != null;
054:                assert createTableRole != null;
055:                assert createTableUser != null;
056:                assert createTableRoleLink != null;
057:
058:                try {
059:                    executeUpdate(createSequenceRole);
060:                    executeUpdate(createTableRole);
061:                    executeUpdate(createTableUser);
062:                    executeUpdate(createTableRoleLink);
063:                } catch (DatabaseException e) {
064:                    throw new InstallCredentialsErrorException(e);
065:                }
066:
067:                return true;
068:            }
069:
070:            protected boolean _remove(final DropSequence dropSequenceRole,
071:                    final DropTable dropTableRole,
072:                    final DropTable dropTableUser,
073:                    final DropTable dropTableRoleLink)
074:                    throws CredentialsManagerException {
075:                assert dropSequenceRole != null;
076:                assert dropTableRole != null;
077:                assert dropTableUser != null;
078:                assert dropTableRoleLink != null;
079:
080:                try {
081:                    executeUpdate(dropTableRoleLink);
082:                    executeUpdate(dropTableUser);
083:                    executeUpdate(dropTableRole);
084:                    executeUpdate(dropSequenceRole);
085:                } catch (DatabaseException e) {
086:                    throw new RemoveCredentialsErrorException(e);
087:                }
088:
089:                return true;
090:            }
091:
092:            protected long _verifyCredentials(Select verifyCredentialsNoRole,
093:                    Select verifyCredentialsRole, Credentials credentials)
094:                    throws CredentialsManagerException {
095:                assert verifyCredentialsNoRole != null;
096:                assert verifyCredentialsRole != null;
097:
098:                if (null == credentials) {
099:                    return -1;
100:                }
101:
102:                long result = -1;
103:
104:                RoleUserCredentials role_user = null;
105:                if (credentials instanceof  RoleUserCredentials) {
106:                    role_user = (RoleUserCredentials) credentials;
107:                } else {
108:                    throw new UnsupportedCredentialsTypeException(credentials);
109:                }
110:
111:                try {
112:                    Select query = null;
113:                    // check if the role should be verified too and get the appropriate query
114:                    if (null == role_user.getRole()) {
115:                        query = verifyCredentialsNoRole;
116:                    } else {
117:                        query = verifyCredentialsRole;
118:                    }
119:
120:                    ProcessVerifyCredentials process_verify_credentials = new ProcessVerifyCredentials();
121:
122:                    if (executeFetchFirst(
123:                            query,
124:                            process_verify_credentials,
125:                            new DbPreparedStatementHandler<RoleUserCredentials>(
126:                                    role_user) {
127:                                public void setParameters(
128:                                        DbPreparedStatement statement) {
129:                                    statement.setString("login", mData
130:                                            .getLogin());
131:
132:                                    // handle automatic password encoding
133:                                    if (null == mPasswordEncryptor) {
134:                                        statement.setString("passwd", mData
135:                                                .getPassword());
136:                                    } else {
137:                                        try {
138:                                            statement
139:                                                    .setString(
140:                                                            "passwd",
141:                                                            mPasswordEncryptor
142:                                                                    .encrypt(mData
143:                                                                            .getPassword()));
144:                                        } catch (NoSuchAlgorithmException e) {
145:                                            throw new DatabaseException(e);
146:                                        }
147:                                    }
148:
149:                                    // set the role if that's required
150:                                    if (mData.getRole() != null) {
151:                                        statement.setString("role", mData
152:                                                .getRole());
153:                                    }
154:                                }
155:                            })) {
156:                        result = process_verify_credentials.getUserId();
157:                    }
158:                } catch (DatabaseException e) {
159:                    throw new VerifyCredentialsErrorException(credentials, e);
160:                }
161:
162:                return result;
163:            }
164:
165:            protected void _addRole(final SequenceValue getRoleId,
166:                    final Insert addRole, final String role)
167:                    throws CredentialsManagerException {
168:                assert getRoleId != null;
169:                assert addRole != null;
170:
171:                if (null == role || 0 == role.length()) {
172:                    throw new AddRoleErrorException(role);
173:                }
174:
175:                try {
176:                    try {
177:                        inTransaction(new DbTransactionUserWithoutResult() {
178:                            public void useTransactionWithoutResult()
179:                                    throws InnerClassException {
180:                                // get the id of the new role
181:                                final int role_id = executeGetFirstInt(getRoleId);
182:                                if (-1 == role_id) {
183:                                    throwException(new AddRoleErrorException(
184:                                            role));
185:                                }
186:
187:                                // store the new role with the obtained id
188:                                if (0 == executeUpdate(addRole,
189:                                        new DbPreparedStatementHandler() {
190:                                            public void setParameters(
191:                                                    DbPreparedStatement statement) {
192:                                                statement.setInt("roleId",
193:                                                        role_id).setString(
194:                                                        "name", role);
195:                                            }
196:                                        })) {
197:                                    throwException(new AddRoleErrorException(
198:                                            role));
199:                                }
200:                            }
201:                        });
202:                    } catch (InnerClassException e) {
203:                        throw (CredentialsManagerException) e.getCause();
204:                    }
205:                } catch (DatabaseException e) {
206:                    throw new AddRoleErrorException(role, e);
207:                }
208:            }
209:
210:            protected boolean _containsRole(Select containsRole,
211:                    final String role) throws CredentialsManagerException {
212:                assert containsRole != null;
213:
214:                if (null == role || 0 == role.length()) {
215:                    return false;
216:                }
217:
218:                boolean result = false;
219:
220:                try {
221:                    result = executeHasResultRows(containsRole,
222:                            new DbPreparedStatementHandler() {
223:                                public void setParameters(
224:                                        DbPreparedStatement statement) {
225:                                    statement.setString("name", role);
226:                                }
227:                            });
228:                } catch (DatabaseException e) {
229:                    throw new ContainsRoleErrorException(role, e);
230:                }
231:
232:                return result;
233:            }
234:
235:            protected long _countRoles(Select countRoles)
236:                    throws CredentialsManagerException {
237:                assert countRoles != null;
238:
239:                try {
240:                    return executeGetFirstLong(countRoles);
241:                } catch (DatabaseException e) {
242:                    throw new CountRolesErrorException(e);
243:                }
244:            }
245:
246:            protected boolean _listRoles(Select listRolesQuery,
247:                    ListRoles processor) throws CredentialsManagerException {
248:                assert listRolesQuery != null;
249:
250:                if (null == processor) {
251:                    return false;
252:                }
253:
254:                boolean result = false;
255:
256:                try {
257:                    result = executeFetchAll(listRolesQuery,
258:                            new ListDatabaseRoles(processor));
259:                } catch (DatabaseException e) {
260:                    throw new ListRolesErrorException(e);
261:                }
262:
263:                return result;
264:            }
265:
266:            protected void _addUser(final Insert addUserWithId,
267:                    final Select getFreeUserId, final Select getRoleId,
268:                    final Insert addRoleLink, final String login,
269:                    final RoleUserAttributes attributes)
270:                    throws CredentialsManagerException {
271:                assert addUserWithId != null;
272:                assert getFreeUserId != null;
273:                assert getRoleId != null;
274:                assert addRoleLink != null;
275:
276:                if (null == login || 0 == login.length() || null == attributes) {
277:                    throw new AddUserErrorException(login, attributes);
278:                }
279:
280:                try {
281:                    try {
282:                        inTransaction(new DbTransactionUserWithoutResult() {
283:                            public void useTransactionWithoutResult()
284:                                    throws InnerClassException {
285:                                // ensure that the password is encoded if an encoder has been set
286:                                String password = null;
287:                                if (null == mPasswordEncryptor
288:                                        || attributes.getPassword().startsWith(
289:                                                mPasswordEncryptor.toString())) {
290:                                    password = attributes.getPassword();
291:                                } else {
292:                                    try {
293:                                        password = mPasswordEncryptor
294:                                                .encrypt(attributes
295:                                                        .getPassword());
296:                                    } catch (NoSuchAlgorithmException e) {
297:                                        throwException(new AddUserErrorException(
298:                                                login, attributes, e));
299:                                    }
300:                                }
301:
302:                                synchronized (getFreeUserId) {
303:                                    final String adapted_password = password;
304:
305:                                    // get a new user id if it has not been provided
306:                                    if (attributes.getUserId() < 0) {
307:                                        attributes
308:                                                .setUserId(executeGetFirstLong(getFreeUserId));
309:                                    }
310:
311:                                    if (0 == executeUpdate(addUserWithId,
312:                                            new DbPreparedStatementHandler() {
313:                                                public void setParameters(
314:                                                        DbPreparedStatement statement) {
315:                                                    statement
316:                                                            .setLong(
317:                                                                    "userId",
318:                                                                    attributes
319:                                                                            .getUserId())
320:                                                            .setString("login",
321:                                                                    login)
322:                                                            .setString(
323:                                                                    "passwd",
324:                                                                    adapted_password);
325:                                                }
326:                                            })) {
327:                                        throwException(new AddUserErrorException(
328:                                                login, attributes));
329:                                    }
330:                                }
331:
332:                                // assign the correct roles to the user
333:                                if (attributes.getRoles() != null) {
334:
335:                                    // insert the role link
336:                                    if (0 == executeUpdate(addRoleLink,
337:                                            new DbPreparedStatementHandler() {
338:                                                public int performUpdate(
339:                                                        DbPreparedStatement statement) {
340:                                                    for (String role : attributes
341:                                                            .getRoles()) {
342:                                                        // obtain the role id
343:                                                        int roleid = executeGetFirstInt(
344:                                                                getRoleId,
345:                                                                new DbPreparedStatementHandler<String>(
346:                                                                        role) {
347:                                                                    public void setParameters(
348:                                                                            DbPreparedStatement statement) {
349:                                                                        statement
350:                                                                                .setString(
351:                                                                                        "name",
352:                                                                                        mData);
353:                                                                    }
354:                                                                });
355:
356:                                                        if (-1 == roleid) {
357:                                                            throw new UnknownRoleErrorException(
358:                                                                    role,
359:                                                                    login,
360:                                                                    attributes);
361:                                                        }
362:
363:                                                        statement
364:                                                                .setLong(
365:                                                                        "userId",
366:                                                                        attributes
367:                                                                                .getUserId())
368:                                                                .setInt(
369:                                                                        "roleId",
370:                                                                        roleid);
371:
372:                                                        if (0 == statement
373:                                                                .executeUpdate()) {
374:                                                            return 0;
375:                                                        }
376:
377:                                                        statement
378:                                                                .clearParameters();
379:                                                    }
380:
381:                                                    return 1;
382:                                                }
383:                                            })) {
384:                                        throwException(new AddUserErrorException(
385:                                                login, attributes));
386:                                    }
387:                                }
388:                            }
389:                        });
390:                    } catch (InnerClassException e) {
391:                        throw (CredentialsManagerException) e.getCause();
392:                    }
393:                } catch (DatabaseException e) {
394:                    throw new AddUserErrorException(login, attributes, e);
395:                }
396:            }
397:
398:            protected RoleUserAttributes _getAttributes(Select getAttributes,
399:                    Select getUserRoles, final String login)
400:                    throws CredentialsManagerException {
401:                assert getAttributes != null;
402:                assert getUserRoles != null;
403:
404:                if (null == login || 0 == login.length()) {
405:                    return null;
406:                }
407:
408:                RoleUserAttributes attributes = null;
409:
410:                try {
411:                    attributes = executeFetchFirstBean(getAttributes,
412:                            RoleUserAttributes.class,
413:                            new DbPreparedStatementHandler() {
414:                                public void setParameters(
415:                                        DbPreparedStatement statement) {
416:                                    statement.setString("login", login);
417:                                }
418:                            });
419:
420:                    if (attributes != null) {
421:                        final long userid = attributes.getUserId();
422:
423:                        RoleFetcher fetcher = new RoleFetcher(attributes);
424:                        executeFetchAll(getUserRoles, fetcher,
425:                                new DbPreparedStatementHandler() {
426:                                    public void setParameters(
427:                                            DbPreparedStatement statement) {
428:                                        statement.setLong("userId", userid);
429:                                    }
430:                                });
431:                    }
432:                } catch (DatabaseException e) {
433:                    throw new GetAttributesErrorException(login, e);
434:                }
435:
436:                return attributes;
437:            }
438:
439:            protected class RoleFetcher extends DbRowProcessor {
440:                private RoleUserAttributes mAttributes = null;
441:
442:                public RoleFetcher(RoleUserAttributes attributes) {
443:                    mAttributes = attributes;
444:                }
445:
446:                public boolean processRow(ResultSet resultSet)
447:                        throws SQLException {
448:                    mAttributes.addRole(resultSet.getString("name"));
449:
450:                    return true;
451:                }
452:            }
453:
454:            protected boolean _containsUser(Select containsUser,
455:                    final String login) throws CredentialsManagerException {
456:                assert containsUser != null;
457:
458:                if (null == login || 0 == login.length()) {
459:                    return false;
460:                }
461:
462:                boolean result = false;
463:
464:                try {
465:                    result = executeHasResultRows(containsUser,
466:                            new DbPreparedStatementHandler() {
467:                                public void setParameters(
468:                                        DbPreparedStatement statement) {
469:                                    statement.setString("login", login);
470:                                }
471:                            });
472:                } catch (DatabaseException e) {
473:                    throw new ContainsUserErrorException(login, e);
474:                }
475:
476:                return result;
477:            }
478:
479:            protected long _countUsers(Select countUsers)
480:                    throws CredentialsManagerException {
481:                assert countUsers != null;
482:
483:                long result = -1;
484:
485:                try {
486:                    result = executeGetFirstLong(countUsers);
487:                } catch (DatabaseException e) {
488:                    throw new CountUsersErrorException(e);
489:                }
490:
491:                return result;
492:            }
493:
494:            protected String _getLogin(Select getLogin, final long userId)
495:                    throws CredentialsManagerException {
496:                assert getLogin != null;
497:
498:                if (userId < 0) {
499:                    return null;
500:                }
501:
502:                String result = null;
503:
504:                try {
505:                    result = executeGetFirstString(getLogin,
506:                            new DbPreparedStatementHandler() {
507:                                public void setParameters(
508:                                        DbPreparedStatement statement) {
509:                                    statement.setLong("userId", userId);
510:                                }
511:                            });
512:                } catch (DatabaseException e) {
513:                    throw new GetLoginErrorException(e, userId);
514:                }
515:
516:                return result;
517:            }
518:
519:            protected long _getUserId(Select getUserId, final String login)
520:                    throws CredentialsManagerException {
521:                assert getUserId != null;
522:
523:                if (null == login || 0 == login.length()) {
524:                    return -1;
525:                }
526:
527:                long result = -1;
528:
529:                try {
530:                    result = executeGetFirstLong(getUserId,
531:                            new DbPreparedStatementHandler() {
532:                                public void setParameters(
533:                                        DbPreparedStatement statement) {
534:                                    statement.setString("login", login);
535:                                }
536:                            });
537:                } catch (DatabaseException e) {
538:                    throw new GetUserIdErrorException(e, login);
539:                }
540:
541:                return result;
542:            }
543:
544:            protected boolean _listUsers(Select listUsersQuery,
545:                    ListUsers processor) throws CredentialsManagerException {
546:                assert listUsersQuery != null;
547:
548:                if (null == processor) {
549:                    return false;
550:                }
551:
552:                boolean result = false;
553:
554:                try {
555:                    ListDatabaseUsers row_processor = new ListDatabaseUsers(
556:                            processor);
557:                    result = executeFetchAll(listUsersQuery, row_processor);
558:                } catch (DatabaseException e) {
559:                    throw new ListUsersErrorException(e);
560:                }
561:
562:                return result;
563:            }
564:
565:            protected boolean _listUsers(Select listUsersRangedQuery,
566:                    ListUsers processor, final int limit, final int offset)
567:                    throws CredentialsManagerException {
568:                assert listUsersRangedQuery != null;
569:
570:                if (null == processor || limit <= 0) {
571:                    return false;
572:                }
573:
574:                boolean result = false;
575:
576:                try {
577:                    ListDatabaseUsers row_processor = new ListDatabaseUsers(
578:                            processor);
579:                    result = executeFetchAll(listUsersRangedQuery,
580:                            row_processor, new DbPreparedStatementHandler() {
581:                                public void setParameters(
582:                                        DbPreparedStatement statement) {
583:                                    statement.setInt("limit", limit).setInt(
584:                                            "offset", offset);
585:                                }
586:                            });
587:                } catch (DatabaseException e) {
588:                    throw new ListUsersErrorException(e);
589:                }
590:
591:                return result;
592:            }
593:
594:            protected boolean _isUserInRole(Select isUserInRole,
595:                    final long userId, final String role)
596:                    throws CredentialsManagerException {
597:                assert isUserInRole != null;
598:
599:                if (userId < 0 || null == role || 0 == role.length()) {
600:                    return false;
601:                }
602:
603:                boolean result = false;
604:
605:                try {
606:                    result = executeHasResultRows(isUserInRole,
607:                            new DbPreparedStatementHandler() {
608:                                public void setParameters(
609:                                        DbPreparedStatement statement) {
610:                                    statement.setLong("userId", userId)
611:                                            .setString("role", role);
612:                                }
613:                            });
614:                } catch (DatabaseException e) {
615:                    throw new IsUserInRoleErrorException(userId, role, e);
616:                }
617:
618:                return result;
619:            }
620:
621:            protected boolean _listUsersInRole(Select listUsersInRole,
622:                    ListUsers processor, final String role)
623:                    throws CredentialsManagerException {
624:                assert listUsersInRole != null;
625:
626:                if (null == processor) {
627:                    return false;
628:                }
629:
630:                if (null == role || 0 == role.length()) {
631:                    return false;
632:                }
633:
634:                boolean result = false;
635:
636:                try {
637:                    ListDatabaseUsers row_processor = new ListDatabaseUsers(
638:                            processor);
639:                    result = executeFetchAll(listUsersInRole, row_processor,
640:                            new DbPreparedStatementHandler() {
641:                                public void setParameters(
642:                                        DbPreparedStatement statement) {
643:                                    statement.setString("role", role);
644:                                }
645:                            });
646:                } catch (DatabaseException e) {
647:                    throw new ListUsersErrorException(e);
648:                }
649:
650:                return result;
651:            }
652:
653:            protected boolean _updateUser(final Update updateUser,
654:                    final Delete removeRoleLinksByUser, final Select getRoleId,
655:                    final Insert addRoleLink, final String login,
656:                    final RoleUserAttributes attributes)
657:                    throws CredentialsManagerException {
658:                assert updateUser != null;
659:                assert removeRoleLinksByUser != null;
660:                assert getRoleId != null;
661:                assert addRoleLink != null;
662:
663:                if (null == login || 0 == login.length() || null == attributes) {
664:                    throw new UpdateUserErrorException(login, attributes);
665:                }
666:
667:                boolean result = false;
668:                try {
669:                    try {
670:                        result = ((Boolean) inTransaction(new DbTransactionUser() {
671:                            public Boolean useTransaction()
672:                                    throws InnerClassException {
673:                                // obtain the user id
674:                                try {
675:                                    final long userid = getUserId(login);
676:
677:                                    if (userid < 0) {
678:                                        throwException(new UpdateUserErrorException(
679:                                                login, attributes));
680:                                    }
681:
682:                                    // only handle the password if it has been provided
683:                                    if (attributes.getPassword() != null) {
684:                                        // ensure that the password is encoded if an encoder has been set
685:                                        String password = null;
686:                                        if (null == mPasswordEncryptor
687:                                                || attributes
688:                                                        .getPassword()
689:                                                        .startsWith(
690:                                                                mPasswordEncryptor
691:                                                                        .toString())) {
692:                                            password = attributes.getPassword();
693:                                        } else {
694:                                            try {
695:                                                password = mPasswordEncryptor
696:                                                        .encrypt(attributes
697:                                                                .getPassword());
698:                                            } catch (NoSuchAlgorithmException e) {
699:                                                throwException(new UpdateUserErrorException(
700:                                                        login, attributes, e));
701:                                            }
702:                                        }
703:
704:                                        // update the user password
705:                                        final String adapted_password = password;
706:                                        if (0 == executeUpdate(
707:                                                updateUser,
708:                                                new DbPreparedStatementHandler() {
709:                                                    public void setParameters(
710:                                                            DbPreparedStatement statement) {
711:                                                        statement
712:                                                                .setString(
713:                                                                        "passwd",
714:                                                                        adapted_password)
715:                                                                .setString(
716:                                                                        "login",
717:                                                                        login);
718:                                                    }
719:                                                })) {
720:                                            // no update was performed
721:                                            return false;
722:                                        }
723:                                    }
724:
725:                                    // remove the previous roles
726:                                    executeUpdate(removeRoleLinksByUser,
727:                                            new DbPreparedStatementHandler() {
728:                                                public void setParameters(
729:                                                        DbPreparedStatement statement) {
730:                                                    statement.setLong("userId",
731:                                                            userid);
732:                                                }
733:                                            });
734:
735:                                    // assign the correct roles to the user
736:                                    if (attributes.getRoles() != null) {
737:                                        if (0 == executeUpdate(
738:                                                addRoleLink,
739:                                                new DbPreparedStatementHandler() {
740:                                                    public int performUpdate(
741:                                                            DbPreparedStatement statement) {
742:                                                        for (String role : attributes
743:                                                                .getRoles()) {
744:                                                            // obtain the role id
745:                                                            int roleid = executeGetFirstInt(
746:                                                                    getRoleId,
747:                                                                    new DbPreparedStatementHandler<String>(
748:                                                                            role) {
749:                                                                        public void setParameters(
750:                                                                                DbPreparedStatement statement) {
751:                                                                            statement
752:                                                                                    .setString(
753:                                                                                            "name",
754:                                                                                            mData);
755:                                                                        }
756:                                                                    });
757:
758:                                                            if (-1 == roleid) {
759:                                                                throwException(new UnknownRoleErrorException(
760:                                                                        role,
761:                                                                        login,
762:                                                                        attributes));
763:                                                            }
764:
765:                                                            statement
766:                                                                    .setLong(
767:                                                                            "userId",
768:                                                                            userid)
769:                                                                    .setInt(
770:                                                                            "roleId",
771:                                                                            roleid);
772:
773:                                                            if (0 == statement
774:                                                                    .executeUpdate()) {
775:                                                                return 0;
776:                                                            }
777:
778:                                                            statement
779:                                                                    .clearParameters();
780:                                                        }
781:
782:                                                        return 1;
783:                                                    }
784:                                                })) {
785:                                            throwException(new UpdateUserErrorException(
786:                                                    login, attributes));
787:                                        }
788:                                    }
789:                                } catch (CredentialsManagerException e) {
790:                                    throwException(new UpdateUserErrorException(
791:                                            login, attributes, e));
792:                                }
793:
794:                                // update was successful
795:                                return true;
796:                            }
797:                        })).booleanValue();
798:                    } catch (InnerClassException e) {
799:                        throw (CredentialsManagerException) e.getCause();
800:                    }
801:                } catch (DatabaseException e) {
802:                    throw new UpdateUserErrorException(login, attributes, e);
803:                }
804:
805:                return result;
806:            }
807:
808:            protected boolean _removeUser(Delete removeUserByLogin,
809:                    final String login) throws CredentialsManagerException {
810:                assert removeUserByLogin != null;
811:
812:                if (null == login || 0 == login.length()) {
813:                    return false;
814:                }
815:
816:                boolean result = false;
817:
818:                try {
819:                    if (0 != executeUpdate(removeUserByLogin,
820:                            new DbPreparedStatementHandler() {
821:                                public void setParameters(
822:                                        DbPreparedStatement statement) {
823:                                    statement.setString("login", login);
824:                                }
825:                            })) {
826:                        result = true;
827:                    }
828:                } catch (DatabaseException e) {
829:                    throw new RemoveUserErrorException(login, e);
830:                }
831:
832:                return result;
833:            }
834:
835:            protected boolean _removeUser(Delete removeUserByUserId,
836:                    final long userId) throws CredentialsManagerException {
837:                assert removeUserByUserId != null;
838:
839:                if (userId < 0) {
840:                    return false;
841:                }
842:
843:                boolean result = false;
844:
845:                try {
846:                    if (0 != executeUpdate(removeUserByUserId,
847:                            new DbPreparedStatementHandler() {
848:                                public void setParameters(
849:                                        DbPreparedStatement statement) {
850:                                    statement.setLong("userId", userId);
851:                                }
852:                            })) {
853:                        result = true;
854:                    }
855:                } catch (DatabaseException e) {
856:                    throw new RemoveUserErrorException(userId, e);
857:                }
858:
859:                return result;
860:            }
861:
862:            protected boolean _removeRole(Delete removeRole, final String name)
863:                    throws CredentialsManagerException {
864:                assert removeRole != null;
865:
866:                if (null == name || 0 == name.length()) {
867:                    return false;
868:                }
869:
870:                boolean result = false;
871:
872:                try {
873:                    if (0 != executeUpdate(removeRole,
874:                            new DbPreparedStatementHandler() {
875:                                public void setParameters(
876:                                        DbPreparedStatement statement) {
877:                                    statement.setString("role", name);
878:                                }
879:                            })) {
880:                        result = true;
881:                    }
882:                } catch (DatabaseException e) {
883:                    throw new RemoveRoleErrorException(name, e);
884:                }
885:
886:                return result;
887:            }
888:
889:            protected void _clearUsers(Delete clearUsers)
890:                    throws CredentialsManagerException {
891:                assert clearUsers != null;
892:
893:                try {
894:                    executeUpdate(clearUsers);
895:                } catch (DatabaseException e) {
896:                    throw new ClearUsersErrorException(e);
897:                }
898:            }
899:
900:            protected boolean _listUserRoles(Select listUserRolesQuery,
901:                    final String login, final ListRoles processor)
902:                    throws CredentialsManagerException {
903:                assert listUserRolesQuery != null;
904:
905:                if (null == processor) {
906:                    return false;
907:                }
908:
909:                boolean result = false;
910:
911:                try {
912:                    result = executeFetchAll(listUserRolesQuery,
913:                            new ListDatabaseRoles(processor),
914:                            new DbPreparedStatementHandler() {
915:                                public void setParameters(
916:                                        DbPreparedStatement statement) {
917:                                    statement.setString("login", login);
918:                                }
919:                            });
920:                } catch (DatabaseException e) {
921:                    throw new ListRolesErrorException(e);
922:                }
923:
924:                return result;
925:            }
926:
927:            protected class ProcessVerifyCredentials extends DbRowProcessor {
928:                private long mUserId = -1;
929:
930:                public boolean processRow(ResultSet resultSet)
931:                        throws SQLException {
932:                    assert resultSet != null;
933:
934:                    mUserId = resultSet.getLong(1);
935:
936:                    return true;
937:                }
938:
939:                public long getUserId() {
940:                    return mUserId;
941:                }
942:            }
943:
944:            protected static class ListDatabaseRoles extends DbRowProcessor {
945:                private ListRoles mListRoles = null;
946:
947:                public ListDatabaseRoles(ListRoles listRoles) {
948:                    mListRoles = listRoles;
949:                }
950:
951:                public boolean processRow(ResultSet resultSet)
952:                        throws SQLException {
953:                    return mListRoles.foundRole(resultSet.getString("name"));
954:                }
955:            }
956:
957:            protected static class ListDatabaseUsers extends DbRowProcessor {
958:                private ListUsers mListUsers = null;
959:
960:                public ListDatabaseUsers(ListUsers listUsers) {
961:                    mListUsers = listUsers;
962:                }
963:
964:                public boolean processRow(ResultSet resultSet)
965:                        throws SQLException {
966:                    return mListUsers.foundUser(resultSet.getLong("userId"),
967:                            resultSet.getString("login"), resultSet
968:                                    .getString("passwd"));
969:                }
970:            }
971:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.