New view-only mode for client

This commit is contained in:
Carlos
2020-06-22 19:24:46 +02:00
parent d8c71045d2
commit 209244bd22
7 changed files with 128 additions and 66 deletions

View File

@ -2,11 +2,6 @@ package es.mesacarlos.webconsole.auth;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Map;
import org.bukkit.Bukkit;
import es.mesacarlos.webconsole.WebConsole;
public class LoginManager {
private ArrayList<User> loggedInUsers = new ArrayList<User>();
@ -38,6 +33,11 @@ public class LoginManager {
loggedInUsers.remove(user);
}
/**
* Get user object by socket
* @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)
if(user.getSocketAddress().equals(address))
@ -65,52 +65,4 @@ public class LoginManager {
return loggedInUsers;
}
/**
* 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 String isValidAdminPassword(String password) {
WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole");
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false);
for(Map.Entry<String, Object> entry : passwords.entrySet()) {
String pwd = (String)entry.getValue();
if(pwd.equals(password))
return entry.getKey();
}
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 String isValidViewerPassword(String password) {
WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole");
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false);
for(Map.Entry<String, Object> entry : passwords.entrySet()) {
String pwd = (String)entry.getValue();
if(pwd.equals(password))
return entry.getKey();
}
return null;
}
public UserType isValidUser(String password) {
//Check if is an admin
String username = isValidAdminPassword(password);
if(username != null)
return UserType.ADMIN;
//Check if is a viewer
username = isValidViewerPassword(password);
if(username != null)
return UserType.VIEWER;
//He is nothing
return UserType.UNKNOWN;
}
}

View File

@ -0,0 +1,65 @@
package es.mesacarlos.webconsole.auth;
import java.util.Map;
import org.bukkit.Bukkit;
import es.mesacarlos.webconsole.WebConsole;
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 = isValidAdminPassword(password);
if(username != null)
return UserType.ADMIN;
//Check if is a viewer
username = isValidViewerPassword(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 isValidAdminPassword(String password) {
WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole");
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("admin").getValues(false);
for(Map.Entry<String, Object> entry : passwords.entrySet()) {
String pwd = (String)entry.getValue();
if(pwd.equals(password))
return entry.getKey();
}
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 isValidViewerPassword(String password) {
WebConsole plugin = (WebConsole)Bukkit.getPluginManager().getPlugin("WebConsole");
Map<String, Object> passwords = plugin.getConfig().getConfigurationSection("passwords").getConfigurationSection("viewer").getValues(false);
for(Map.Entry<String, Object> entry : passwords.entrySet()) {
String pwd = (String)entry.getValue();
if(pwd.equals(password))
return entry.getKey();
}
return null;
}
}