LOGGER.entering(CLASS_NAME, METHOD);
if (Strings.isNullOrEmpty(value)) {
return null;
}
DatabaseConnectionPool connectionPool = null;
Connection lookupConn = null;
boolean isReadOnly = false;
try {
connectionPool = connectorSession.getConnector().getJdbcDatabase()
.getConnectionPool();
lookupConn = connectionPool.getConnection();
isReadOnly = lookupConn.isReadOnly();
lookupConn.setReadOnly(true);
// Find the user.
PreparedStatement pstmt = lookupConn.prepareStatement("select * from "
+ userTableName + " where " + field + " = ?",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
long userId = -1L;
String notesName;
String gsaName;
try {
pstmt.setString(1, value.toLowerCase());
rs = pstmt.executeQuery();
if (!rs.next()) {
return null;
}
userId = rs.getLong("userid");
notesName = rs.getString("notesname");
gsaName = rs.getString("gsaname");
} finally {
Util.close(rs);
Util.close(pstmt);
}
User user = new User(userId, notesName, gsaName);
// User is authenticated and should be a member of "-default-" group.
user.addGroup("-default-");
// Find user groups and nested groups
pstmt = lookupConn.prepareStatement(
Util.buildString(
"select groupname from ", groupTableName,
" where groupid in ",
"(select groupid from ",userGroupsTableName,
" where userid = ?)"),
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
pstmt.setLong(1, userId);
rs = pstmt.executeQuery();
while (rs.next()) {
user.addGroup(rs.getString(1));
}
} finally {
Util.close(rs);
Util.close(pstmt);
}
// Find user's parent and great grand-parent groups
// This query queries for user's groups. From user's groups, it looks up
// all parent ids. From parent ids, it looks up for group names.
pstmt = lookupConn.prepareStatement(
Util.buildString(
"select groupname from ", groupTableName,
" where groupid in ",
"(select parentgroupid from ", groupChildrenTableName,
" where childgroupid in ",
"(select groupid from ", userGroupsTableName,
" where userid = ?)", ")"),
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
pstmt.setLong(1, userId);
rs = pstmt.executeQuery();
while (rs.next()) {
user.addGroup(rs.getString(1));
}
} finally {
Util.close(rs);
Util.close(pstmt);
}
// Find their roles.
pstmt = lookupConn.prepareStatement("select replicaid, rolename from "
+ roleTableName + " where roleid in (select roleid from "
+ userRolesTableName + " where userid = ?)",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
pstmt.setLong(1, userId);
rs = pstmt.executeQuery();
while (rs.next()) {
user.addRole(rs.getString(1), rs.getString(2));
}
} finally {
Util.close(rs);
Util.close(pstmt);
}
// Find any roles they acquire because of direct group membership and
// via parent groups.
pstmt = lookupConn.prepareStatement("select replicaid, rolename from "
+ roleTableName + " where roleid in (select roleid from "
+ groupRolesTableName + " where groupid in "
+ "(select groupid from " + userGroupsTableName
+ " where userid = ? union select parentgroupid from "
+ groupChildrenTableName
+ " where childgroupid in (select groupid from "
+ userGroupsTableName + " where userid = ?)))",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try {
pstmt.setLong(1, userId);
pstmt.setLong(2, userId);
rs = pstmt.executeQuery();
while (rs.next()) {
user.addRole(rs.getString(1), rs.getString(2));
}
} finally {
Util.close(rs);
Util.close(pstmt);
}
return user;
} catch (Exception e) {
LOGGER.logp(Level.SEVERE, CLASS_NAME, METHOD,
"Failed to find user record for: " + field
+ " = " + value, e);
return null;
} finally {
try {
lookupConn.setReadOnly(isReadOnly);
connectionPool.releaseConnection(lookupConn);
} catch (SQLException e) {
LOGGER.logp(Level.WARNING, CLASS_NAME, METHOD,
"Failure releasing connection", e);
}
LOGGER.exiting(CLASS_NAME, METHOD);