use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.
the class JDBCUserDatabase method rename.
/**
* @see org.apache.wiki.auth.user.UserDatabase#rename(String, String)
*/
public void rename(String loginName, String newName) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException {
// Get the existing user; if not found, throws NoSuchPrincipalException
UserProfile profile = findByLoginName(loginName);
// Get user with the proposed name; if found, it's a collision
try {
UserProfile otherProfile = findByLoginName(newName);
if (otherProfile != null) {
throw new DuplicateUserException("security.error.cannot.rename", newName);
}
} catch (NoSuchPrincipalException e) {
// Good! That means it's safe to save using the new name
}
Connection conn = null;
try {
// Open the database connection
conn = m_ds.getConnection();
if (m_supportsCommits) {
conn.setAutoCommit(false);
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date modDate = new Date(ts.getTime());
// Change the login ID for the user record
PreparedStatement ps = conn.prepareStatement(m_renameProfile);
ps.setString(1, newName);
ps.setTimestamp(2, ts);
ps.setString(3, loginName);
ps.execute();
ps.close();
// Change the login ID for the role records
ps = conn.prepareStatement(m_renameRoles);
ps.setString(1, newName);
ps.setString(2, loginName);
ps.execute();
ps.close();
// Set the profile name and mod time
profile.setLoginName(newName);
profile.setLastModified(modDate);
// Commit and close connection
if (m_supportsCommits) {
conn.commit();
}
} catch (SQLException e) {
throw new WikiSecurityException(e.getMessage(), e);
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
}
use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.
the class JDBCUserDatabase method findByPreparedStatement.
/**
* Private method that returns the first {@link UserProfile} matching a
* named column's value. This method will also set the UID if it has not yet been set.
* @param sql the SQL statement that should be prepared; it must have one parameter
* to set (either a String or a Long)
* @param index the value to match
* @return the resolved UserProfile
* @throws SQLException
*/
private UserProfile findByPreparedStatement(String sql, Object index) throws NoSuchPrincipalException {
UserProfile profile = null;
boolean found = false;
boolean unique = true;
Connection conn = null;
try {
// Open the database connection
conn = m_ds.getConnection();
if (m_supportsCommits) {
conn.setAutoCommit(false);
}
PreparedStatement ps = conn.prepareStatement(sql);
// Set the parameter to search by
if (index instanceof String) {
ps.setString(1, (String) index);
} else if (index instanceof Long) {
ps.setLong(1, ((Long) index).longValue());
} else {
throw new IllegalArgumentException("Index type not recognized!");
}
// Go and get the record!
ResultSet rs = ps.executeQuery();
while (rs.next()) {
if (profile != null) {
unique = false;
break;
}
profile = newProfile();
// Fetch the basic user attributes
profile.setUid(rs.getString(m_uid));
if (profile.getUid() == null) {
profile.setUid(generateUid(this));
}
profile.setCreated(rs.getTimestamp(m_created));
profile.setEmail(rs.getString(m_email));
profile.setFullname(rs.getString(m_fullName));
profile.setLastModified(rs.getTimestamp(m_modified));
Date lockExpiry = rs.getDate(m_lockExpiry);
profile.setLockExpiry(rs.wasNull() ? null : lockExpiry);
profile.setLoginName(rs.getString(m_loginName));
profile.setPassword(rs.getString(m_password));
// Fetch the user attributes
String rawAttributes = rs.getString(m_attributes);
if (rawAttributes != null) {
try {
Map<String, ? extends Serializable> attributes = Serializer.deserializeFromBase64(rawAttributes);
profile.getAttributes().putAll(attributes);
} catch (IOException e) {
log.error("Could not parse user profile attributes!", e);
}
}
found = true;
}
ps.close();
} catch (SQLException e) {
throw new NoSuchPrincipalException(e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
if (!found) {
throw new NoSuchPrincipalException("Could not find profile in database!");
}
if (!unique) {
throw new NoSuchPrincipalException("More than one profile in database!");
}
return profile;
}
use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.
the class XMLUserDatabase method rename.
/**
* @see org.apache.wiki.auth.user.UserDatabase#rename(String, String)
*/
public synchronized void rename(String loginName, String newName) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException {
if (c_dom == null) {
log.fatal("Could not rename profile '" + loginName + "'; database does not exist");
throw new IllegalStateException("FATAL: database does not exist");
}
checkForRefresh();
// Get the existing user; if not found, throws NoSuchPrincipalException
UserProfile profile = findByLoginName(loginName);
// Get user with the proposed name; if found, it's a collision
try {
UserProfile otherProfile = findByLoginName(newName);
if (otherProfile != null) {
throw new DuplicateUserException("security.error.cannot.rename", newName);
}
} catch (NoSuchPrincipalException e) {
// Good! That means it's safe to save using the new name
}
// Find the user with the old login id attribute, and change it
NodeList users = c_dom.getElementsByTagName(USER_TAG);
for (int i = 0; i < users.getLength(); i++) {
Element user = (Element) users.item(i);
if (user.getAttribute(LOGIN_NAME).equals(loginName)) {
DateFormat c_format = new SimpleDateFormat(DATE_FORMAT);
Date modDate = new Date(System.currentTimeMillis());
setAttribute(user, LOGIN_NAME, newName);
setAttribute(user, LAST_MODIFIED, c_format.format(modDate));
profile.setLoginName(newName);
profile.setLastModified(modDate);
break;
}
}
// Commit to disk
saveDOM();
}
use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.
the class UserDatabaseLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
*
* {@inheritDoc}
*/
public boolean login() throws LoginException {
UserDatabaseCallback ucb = new UserDatabaseCallback();
NameCallback ncb = new NameCallback("User name");
PasswordCallback pcb = new PasswordCallback("Password", false);
Callback[] callbacks = new Callback[] { ucb, ncb, pcb };
try {
m_handler.handle(callbacks);
UserDatabase db = ucb.getUserDatabase();
String username = ncb.getName();
String password = new String(pcb.getPassword());
// Look up the user and compare the password hash
if (db == null) {
throw new FailedLoginException("No user database: check the callback handler code!");
}
UserProfile profile = db.findByLoginName(username);
String storedPassword = profile.getPassword();
if (storedPassword != null && db.validatePassword(username, password)) {
if (log.isDebugEnabled()) {
log.debug("Logged in user database user " + username);
}
// If login succeeds, commit these principals/roles
m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
return true;
}
throw new FailedLoginException("The username or password is incorrect.");
} catch (IOException e) {
String message = "IO exception; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (UnsupportedCallbackException e) {
String message = "Unable to handle callback; disallowing login.";
log.error(message, e);
throw new LoginException(message);
} catch (NoSuchPrincipalException e) {
throw new FailedLoginException("The username or password is incorrect.");
}
}
use of org.apache.wiki.auth.NoSuchPrincipalException in project jspwiki by apache.
the class Installer method adminExists.
/**
* Returns <code>true</code> if the administrative user had
* been created previously.
* @return the result
*/
public boolean adminExists() {
// See if the admin user exists already
UserManager userMgr = m_engine.getUserManager();
UserDatabase userDb = userMgr.getUserDatabase();
try {
userDb.findByLoginName(ADMIN_ID);
return true;
} catch (NoSuchPrincipalException e) {
return false;
}
}
Aggregations