Ověřování uživatelů: uživatele budeme mít jako objekt, ne jen jako String/boolean – aby šel rozšiřovat o případné další informace.
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 30 Oct 2011 22:15:03 +0100
changeset 113a059aecd1794
parent 112 ca54040b4409
child 114 311502c83ff8
Ověřování uživatelů: uživatele budeme mít jako objekt, ne jen jako String/boolean – aby šel rozšiřovat o případné další informace.
src/org/sonews/acl/User.java
src/org/sonews/daemon/NNTPConnection.java
src/org/sonews/daemon/command/PostCommand.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/acl/User.java	Sun Oct 30 22:15:03 2011 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package org.sonews.acl;
    1.22 +
    1.23 +/**
    1.24 + * Represents users (clients) accessing our service through NNTP protocol.
    1.25 + * 
    1.26 + * This class can be extended by your plugin 
    1.27 + * to describe additional information, that was gained during login process.
    1.28 + * 
    1.29 + * When User object is created, default authentication status is false.
    1.30 + * 
    1.31 + * @author František Kučera (frantovo.cz)
    1.32 + */
    1.33 +public class User {
    1.34 +
    1.35 +	private String userName;
    1.36 +	private boolean authenticated = false;
    1.37 +
    1.38 +	public String getUserName() {
    1.39 +		return userName;
    1.40 +	}
    1.41 +
    1.42 +	public void setUserName(String userName) {
    1.43 +		this.userName = userName;
    1.44 +	}
    1.45 +
    1.46 +	/**
    1.47 +	 * In some configurations users don't have to use their password – 
    1.48 +	 * they can just tell us their name and we will trust them – 
    1.49 +	 * in this case User object will exist end user name will be filled, but this method will return false.
    1.50 +	 * 
    1.51 +	 * @return true if user was succesfully authenticated (has provided correct password).
    1.52 +	 */
    1.53 +	public boolean isAuthenticated() {
    1.54 +		return authenticated;
    1.55 +	}
    1.56 +
    1.57 +	/**
    1.58 +	 * This method is to be called from AUTHINFO PASS Command implementation.
    1.59 +	 * 
    1.60 +	 * @param authenticated true if user has provided right password in AUTHINFO PASS password.
    1.61 +	 * @see #isAuthenticated() 
    1.62 +	 */
    1.63 +	public void setAuthenticated(boolean authenticated) {
    1.64 +		this.authenticated = authenticated;
    1.65 +	}
    1.66 +
    1.67 +	public User() {
    1.68 +	}
    1.69 +
    1.70 +	public User(String userName) {
    1.71 +		this.userName = userName;
    1.72 +	}
    1.73 +
    1.74 +	public User(String userName, boolean authenticated) {
    1.75 +		this.userName = userName;
    1.76 +		this.authenticated = authenticated;
    1.77 +	}
    1.78 +}
     2.1 --- a/src/org/sonews/daemon/NNTPConnection.java	Sun Oct 30 22:13:32 2011 +0100
     2.2 +++ b/src/org/sonews/daemon/NNTPConnection.java	Sun Oct 30 22:15:03 2011 +0100
     2.3 @@ -31,6 +31,7 @@
     2.4  import java.util.Timer;
     2.5  import java.util.TimerTask;
     2.6  import java.util.logging.Level;
     2.7 +import org.sonews.acl.User;
     2.8  import org.sonews.daemon.command.Command;
     2.9  import org.sonews.storage.Article;
    2.10  import org.sonews.storage.Group;
    2.11 @@ -62,9 +63,7 @@
    2.12  	private int readLock = 0;
    2.13  	private final Object readLockGate = new Object();
    2.14  	private SelectionKey writeSelKey = null;
    2.15 -	
    2.16 -	private String username;
    2.17 -	private boolean userAuthenticated = false;
    2.18 +	private User user;
    2.19  
    2.20  	public NNTPConnection(final SocketChannel channel)
    2.21  			throws IOException {
    2.22 @@ -148,7 +147,7 @@
    2.23  			// will be received and a timeout can be triggered.
    2.24  			this.channel.socket().shutdownInput();
    2.25  		} catch (IOException ex) {
    2.26 -			Log.get().warning("Exception in NNTPConnection.shutdownInput(): " + ex);
    2.27 +			Log.get().log(Level.WARNING, "Exception in NNTPConnection.shutdownInput(): {0}", ex);
    2.28  		}
    2.29  	}
    2.30  
    2.31 @@ -162,9 +161,9 @@
    2.32  					channel.close();
    2.33  				} catch (SocketException ex) {
    2.34  					// Socket was already disconnected
    2.35 -					Log.get().info("NNTPConnection.shutdownOutput(): " + ex);
    2.36 +					Log.get().log(Level.INFO, "NNTPConnection.shutdownOutput(): {0}", ex);
    2.37  				} catch (Exception ex) {
    2.38 -					Log.get().warning("NNTPConnection.shutdownOutput(): " + ex);
    2.39 +					Log.get().log(Level.WARNING, "NNTPConnection.shutdownOutput(): {0}", ex);
    2.40  				}
    2.41  			}
    2.42  		}, 3000);
    2.43 @@ -227,7 +226,7 @@
    2.44  			raw = Arrays.copyOf(raw, raw.length - 1);
    2.45  		}
    2.46  
    2.47 -		Log.get().fine("<< " + line);
    2.48 +		Log.get().log(Level.FINE, "<< {0}", line);
    2.49  
    2.50  		if (command == null) {
    2.51  			command = parseCommandLine(line);
    2.52 @@ -344,7 +343,7 @@
    2.53  			CharSequence debugLine)
    2.54  			throws IOException {
    2.55  		if (!charset.canEncode()) {
    2.56 -			Log.get().severe("FATAL: Charset " + charset + " cannot encode!");
    2.57 +			Log.get().log(Level.SEVERE, "FATAL: Charset {0} cannot encode!", charset);
    2.58  			return;
    2.59  		}
    2.60  
    2.61 @@ -362,14 +361,14 @@
    2.62  			ChannelWriter.getInstance().getSelector().wakeup();
    2.63  		} catch (Exception ex) // CancelledKeyException and ChannelCloseException
    2.64  		{
    2.65 -			Log.get().warning("NNTPConnection.writeToChannel(): " + ex);
    2.66 +			Log.get().log(Level.WARNING, "NNTPConnection.writeToChannel(): {0}", ex);
    2.67  			return;
    2.68  		}
    2.69  
    2.70  		// Update last activity timestamp
    2.71  		this.lastActivity = System.currentTimeMillis();
    2.72  		if (debugLine != null) {
    2.73 -			Log.get().fine(">> " + debugLine);
    2.74 +			Log.get().log(Level.FINE, ">> {0}", debugLine);
    2.75  		}
    2.76  	}
    2.77  
    2.78 @@ -392,34 +391,17 @@
    2.79  	}
    2.80  
    2.81  	/**
    2.82 -	 * @return Current username. 
    2.83 -	 * But user may not have been authenticated yet.
    2.84 -	 * You must check {@link #isUserAuthenticated()}
    2.85 +	 * @return Currently logged user (but you should check {@link User#isAuthenticated()}, if user is athenticated, or we just trust him)
    2.86  	 */
    2.87 -	public String getUsername() {
    2.88 -		return username;
    2.89 +	public User getUser() {
    2.90 +		return user;
    2.91  	}
    2.92  
    2.93  	/**
    2.94  	 * This method is to be called from AUTHINFO USER Command implementation.
    2.95  	 * @param username username from AUTHINFO USER username.
    2.96  	 */
    2.97 -	public void setUsername(String username) {
    2.98 -		this.username = username;
    2.99 -	}
   2.100 -
   2.101 -	/**
   2.102 -	 * @return true if current user (see {@link #getUsername()}) has been succesfully authenticated.
   2.103 -	 */
   2.104 -	public boolean isUserAuthenticated() {
   2.105 -		return userAuthenticated;
   2.106 -	}
   2.107 -
   2.108 -	/**
   2.109 -	 * This method is to be called from AUTHINFO PASS Command implementation.
   2.110 -	 * @param userAuthenticated true if user has provided right password in AUTHINFO PASS password.
   2.111 -	 */
   2.112 -	public void setUserAuthenticated(boolean userAuthenticated) {
   2.113 -		this.userAuthenticated = userAuthenticated;
   2.114 +	public void setUser(User user) {
   2.115 +		this.user = user;
   2.116  	}
   2.117  }
     3.1 --- a/src/org/sonews/daemon/command/PostCommand.java	Sun Oct 30 22:13:32 2011 +0100
     3.2 +++ b/src/org/sonews/daemon/command/PostCommand.java	Sun Oct 30 22:15:03 2011 +0100
     3.3 @@ -210,8 +210,8 @@
     3.4  
     3.5  	private void postArticle(NNTPConnection conn, Article article)
     3.6  			throws IOException {
     3.7 -		if (conn.isUserAuthenticated()) {
     3.8 -			article.setAuthenticatedUser(conn.getUsername());
     3.9 +		if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
    3.10 +			article.setAuthenticatedUser(conn.getUser().getUserName());
    3.11  		}
    3.12  		
    3.13  		if (article.getHeader(Headers.CONTROL)[0].length() > 0) {