# HG changeset patch # User František Kučera # Date 1320009303 -3600 # Node ID a059aecd17945a9dc0565cb0e810f7937fcd65f7 # Parent ca54040b44091b16277488d9cb6b7287948738e7 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. diff -r ca54040b4409 -r a059aecd1794 src/org/sonews/acl/User.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/sonews/acl/User.java Sun Oct 30 22:15:03 2011 +0100 @@ -0,0 +1,75 @@ +/* + * SONEWS News Server + * see AUTHORS for the list of contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.sonews.acl; + +/** + * Represents users (clients) accessing our service through NNTP protocol. + * + * This class can be extended by your plugin + * to describe additional information, that was gained during login process. + * + * When User object is created, default authentication status is false. + * + * @author František Kučera (frantovo.cz) + */ +public class User { + + private String userName; + private boolean authenticated = false; + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + /** + * In some configurations users don't have to use their password – + * they can just tell us their name and we will trust them – + * in this case User object will exist end user name will be filled, but this method will return false. + * + * @return true if user was succesfully authenticated (has provided correct password). + */ + public boolean isAuthenticated() { + return authenticated; + } + + /** + * This method is to be called from AUTHINFO PASS Command implementation. + * + * @param authenticated true if user has provided right password in AUTHINFO PASS password. + * @see #isAuthenticated() + */ + public void setAuthenticated(boolean authenticated) { + this.authenticated = authenticated; + } + + public User() { + } + + public User(String userName) { + this.userName = userName; + } + + public User(String userName, boolean authenticated) { + this.userName = userName; + this.authenticated = authenticated; + } +} diff -r ca54040b4409 -r a059aecd1794 src/org/sonews/daemon/NNTPConnection.java --- a/src/org/sonews/daemon/NNTPConnection.java Sun Oct 30 22:13:32 2011 +0100 +++ b/src/org/sonews/daemon/NNTPConnection.java Sun Oct 30 22:15:03 2011 +0100 @@ -31,6 +31,7 @@ import java.util.Timer; import java.util.TimerTask; import java.util.logging.Level; +import org.sonews.acl.User; import org.sonews.daemon.command.Command; import org.sonews.storage.Article; import org.sonews.storage.Group; @@ -62,9 +63,7 @@ private int readLock = 0; private final Object readLockGate = new Object(); private SelectionKey writeSelKey = null; - - private String username; - private boolean userAuthenticated = false; + private User user; public NNTPConnection(final SocketChannel channel) throws IOException { @@ -148,7 +147,7 @@ // will be received and a timeout can be triggered. this.channel.socket().shutdownInput(); } catch (IOException ex) { - Log.get().warning("Exception in NNTPConnection.shutdownInput(): " + ex); + Log.get().log(Level.WARNING, "Exception in NNTPConnection.shutdownInput(): {0}", ex); } } @@ -162,9 +161,9 @@ channel.close(); } catch (SocketException ex) { // Socket was already disconnected - Log.get().info("NNTPConnection.shutdownOutput(): " + ex); + Log.get().log(Level.INFO, "NNTPConnection.shutdownOutput(): {0}", ex); } catch (Exception ex) { - Log.get().warning("NNTPConnection.shutdownOutput(): " + ex); + Log.get().log(Level.WARNING, "NNTPConnection.shutdownOutput(): {0}", ex); } } }, 3000); @@ -227,7 +226,7 @@ raw = Arrays.copyOf(raw, raw.length - 1); } - Log.get().fine("<< " + line); + Log.get().log(Level.FINE, "<< {0}", line); if (command == null) { command = parseCommandLine(line); @@ -344,7 +343,7 @@ CharSequence debugLine) throws IOException { if (!charset.canEncode()) { - Log.get().severe("FATAL: Charset " + charset + " cannot encode!"); + Log.get().log(Level.SEVERE, "FATAL: Charset {0} cannot encode!", charset); return; } @@ -362,14 +361,14 @@ ChannelWriter.getInstance().getSelector().wakeup(); } catch (Exception ex) // CancelledKeyException and ChannelCloseException { - Log.get().warning("NNTPConnection.writeToChannel(): " + ex); + Log.get().log(Level.WARNING, "NNTPConnection.writeToChannel(): {0}", ex); return; } // Update last activity timestamp this.lastActivity = System.currentTimeMillis(); if (debugLine != null) { - Log.get().fine(">> " + debugLine); + Log.get().log(Level.FINE, ">> {0}", debugLine); } } @@ -392,34 +391,17 @@ } /** - * @return Current username. - * But user may not have been authenticated yet. - * You must check {@link #isUserAuthenticated()} + * @return Currently logged user (but you should check {@link User#isAuthenticated()}, if user is athenticated, or we just trust him) */ - public String getUsername() { - return username; + public User getUser() { + return user; } /** * This method is to be called from AUTHINFO USER Command implementation. * @param username username from AUTHINFO USER username. */ - public void setUsername(String username) { - this.username = username; - } - - /** - * @return true if current user (see {@link #getUsername()}) has been succesfully authenticated. - */ - public boolean isUserAuthenticated() { - return userAuthenticated; - } - - /** - * This method is to be called from AUTHINFO PASS Command implementation. - * @param userAuthenticated true if user has provided right password in AUTHINFO PASS password. - */ - public void setUserAuthenticated(boolean userAuthenticated) { - this.userAuthenticated = userAuthenticated; + public void setUser(User user) { + this.user = user; } } diff -r ca54040b4409 -r a059aecd1794 src/org/sonews/daemon/command/PostCommand.java --- a/src/org/sonews/daemon/command/PostCommand.java Sun Oct 30 22:13:32 2011 +0100 +++ b/src/org/sonews/daemon/command/PostCommand.java Sun Oct 30 22:15:03 2011 +0100 @@ -210,8 +210,8 @@ private void postArticle(NNTPConnection conn, Article article) throws IOException { - if (conn.isUserAuthenticated()) { - article.setAuthenticatedUser(conn.getUsername()); + if (conn.getUser() != null && conn.getUser().isAuthenticated()) { + article.setAuthenticatedUser(conn.getUser().getUserName()); } if (article.getHeader(Headers.CONTROL)[0].length() > 0) {