Code cleaning and optimization
This commit is contained in:
@ -1,33 +1,34 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
|
||||
public class User {
|
||||
private String username;
|
||||
private InetSocketAddress socketAddress;
|
||||
private UserType userType;
|
||||
|
||||
public User(InetSocketAddress socketAddress, String username, UserType userType) {
|
||||
this.socketAddress = socketAddress;
|
||||
this.username = username;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return socketAddress;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public UserType getUserType() {
|
||||
return userType;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Internationalization.getPhrase("user-tostring", username, socketAddress, userType);
|
||||
}
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import es.mesacarlos.webconsole.config.UserType;
|
||||
import es.mesacarlos.webconsole.util.Internationalization;
|
||||
|
||||
public class ConnectedUser {
|
||||
private String username;
|
||||
private InetSocketAddress socketAddress;
|
||||
private UserType userType;
|
||||
|
||||
public ConnectedUser(InetSocketAddress socketAddress, String username, UserType userType) {
|
||||
this.socketAddress = socketAddress;
|
||||
this.username = username;
|
||||
this.userType = userType;
|
||||
}
|
||||
|
||||
public InetSocketAddress getSocketAddress() {
|
||||
return socketAddress;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public UserType getUserType() {
|
||||
return userType;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Internationalization.getPhrase("user-tostring", username, socketAddress, userType);
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LoginManager {
|
||||
private ArrayList<User> loggedInUsers = new ArrayList<User>();
|
||||
private ArrayList<ConnectedUser> loggedInUsers = new ArrayList<ConnectedUser>();
|
||||
private static LoginManager instance;
|
||||
|
||||
private LoginManager() {}
|
||||
@ -19,7 +19,7 @@ public class LoginManager {
|
||||
* Logs user in
|
||||
* @param user User to login
|
||||
*/
|
||||
public void logIn(User user) {
|
||||
public void logIn(ConnectedUser user) {
|
||||
loggedInUsers.add(user);
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ public class LoginManager {
|
||||
* @param address User to logout
|
||||
*/
|
||||
public void logOut(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
for(ConnectedUser user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
loggedInUsers.remove(user);
|
||||
}
|
||||
@ -38,8 +38,8 @@ public class LoginManager {
|
||||
* @param address socket of the user
|
||||
* @return User object, null if no user logged in from that address
|
||||
*/
|
||||
public User getUser(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
public ConnectedUser getUser(InetSocketAddress address) {
|
||||
for(ConnectedUser user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
return user;
|
||||
return null;
|
||||
@ -51,7 +51,7 @@ public class LoginManager {
|
||||
* @return true if user is logged in, false otherwise
|
||||
*/
|
||||
public boolean isLoggedIn(InetSocketAddress address) {
|
||||
for(User user : loggedInUsers)
|
||||
for(ConnectedUser user : loggedInUsers)
|
||||
if(user.getSocketAddress().equals(address))
|
||||
return true;
|
||||
return false;
|
||||
@ -61,7 +61,7 @@ public class LoginManager {
|
||||
* Retrieve the full logged-in user list
|
||||
* @return list of logged in users
|
||||
*/
|
||||
public ArrayList<User> getLoggedInUsers() {
|
||||
public ArrayList<ConnectedUser> getLoggedInUsers() {
|
||||
return loggedInUsers;
|
||||
}
|
||||
|
||||
|
@ -1,52 +0,0 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
import es.mesacarlos.webconsole.config.ConfigManager;
|
||||
import es.mesacarlos.webconsole.config.UserData;
|
||||
|
||||
public class PasswordManager {
|
||||
|
||||
/**
|
||||
* Get the user type of a given password
|
||||
* @param password Password to check
|
||||
* @return ADMIN if password correspond to a admin user, VIEWER if viewer or UNKNOWN if invalid password
|
||||
*/
|
||||
public static UserType isValidUser(String password) {
|
||||
//Check if is an admin
|
||||
String username = getAdminUsernameFromPassword(password);
|
||||
if(username != null)
|
||||
return UserType.ADMIN;
|
||||
|
||||
//Check if is a viewer
|
||||
username = getViewerUsernameFromPassword(password);
|
||||
if(username != null)
|
||||
return UserType.VIEWER;
|
||||
|
||||
//He is nothing
|
||||
return UserType.UNKNOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided password corresponds to any admin
|
||||
* @param password Provided password
|
||||
* @return Name of the user if password corresponds to a valid admin, null if is a viewer or an invalid password
|
||||
*/
|
||||
public static String getAdminUsernameFromPassword(String password) {
|
||||
for(UserData ud : ConfigManager.getInstance().getAdmins())
|
||||
if(ud.getPassword().equals(password))
|
||||
return ud.getUsername();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided password corresponds to any viewer
|
||||
* @param password Provided password
|
||||
* @return Name of the user if password corresponds to a valid viewer, null if is a admin or invalid password
|
||||
*/
|
||||
public static String getViewerUsernameFromPassword(String password) {
|
||||
for(UserData ud : ConfigManager.getInstance().getViewers())
|
||||
if(ud.getPassword().equals(password))
|
||||
return ud.getUsername();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package es.mesacarlos.webconsole.auth;
|
||||
|
||||
public enum UserType {
|
||||
ADMIN,
|
||||
VIEWER,
|
||||
UNKNOWN
|
||||
}
|
Reference in New Issue
Block a user