src/org/sonews/storage/impl/DrupalDatabase.java
changeset 101 d54786065fa3
parent 73 1feed5fbf147
child 102 d843b4fee5dc
     1.1 --- a/src/org/sonews/storage/impl/DrupalDatabase.java	Wed Oct 19 17:23:53 2011 +0200
     1.2 +++ b/src/org/sonews/storage/impl/DrupalDatabase.java	Wed Oct 19 21:40:51 2011 +0200
     1.3 @@ -28,6 +28,7 @@
     1.4  import java.util.logging.Level;
     1.5  import java.util.logging.Logger;
     1.6  import org.sonews.config.Config;
     1.7 +import org.sonews.daemon.Connections;
     1.8  import org.sonews.feed.Subscription;
     1.9  import org.sonews.storage.Article;
    1.10  import org.sonews.storage.ArticleHead;
    1.11 @@ -387,9 +388,41 @@
    1.12  		return Collections.emptyList();
    1.13  	}
    1.14  
    1.15 +	/**
    1.16 +	 * Checks username and password.
    1.17 +	 * @param username
    1.18 +	 * @param password
    1.19 +	 * @return true if credentials are valid | false otherwise
    1.20 +	 * @throws StorageBackendException it there is any error during authentication process 
    1.21 +	 * (but should not be thrown if only bad thing is wrong username or password)
    1.22 +	 */
    1.23 +	@Override
    1.24 +	public boolean authenticateUser(String username, char[] password) throws StorageBackendException {
    1.25 +		PreparedStatement ps = null;
    1.26 +		ResultSet rs = null;
    1.27 +		try {
    1.28 +			ps = conn.prepareStatement("SELECT nntp_login(?, ?)");
    1.29 +			ps.setString(1, username);
    1.30 +			ps.setString(2, String.copyValueOf(password));
    1.31 +			rs = ps.executeQuery();
    1.32 +			rs.next();
    1.33 +			return rs.getInt(1) == 1;
    1.34 +		} catch (Exception e) {
    1.35 +			throw new StorageBackendException(e);
    1.36 +		} finally {
    1.37 +			close(null, ps, rs);
    1.38 +		}
    1.39 +	}
    1.40 +
    1.41  	@Override
    1.42  	public void addArticle(Article art) throws StorageBackendException {
    1.43 -		log.log(Level.SEVERE, "TODO: addArticle {0}", new Object[]{art});
    1.44 +		if (art.getAuthenticatedUser() == null) {
    1.45 +			log.log(Level.SEVERE, "User was not authenticated, so his article was rejected.");
    1.46 +			throw new StorageBackendException("User must be authenticated to post articles");
    1.47 +		} else {
    1.48 +
    1.49 +			log.log(Level.INFO, "User ''{0}'' has posted an article", art.getAuthenticatedUser());
    1.50 +		}
    1.51  	}
    1.52  
    1.53  	@Override