Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Base64;
- import java.util.Scanner;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import com.mysql.cj.jdbc.MysqlDataSource;
- import encryption.Encrypt;
- import main.MainS;
- import main.Vars;
- import userInterface.UInterface;
- public class ManageUsers {
- private int userIdIndex;
- private String userQuery=null;
- private String passQuery=null;
- private int rootQuery;
- private static int usersAccess = 0;
- private static int usersExpiry;
- private static int userIsRoot = 0;
- private static int usersTime;
- private static int usersCooldown;
- private static String usersUid, getPass;
- public ManageUsers(String usersUid, String getPass) {
- this.usersUid = usersUid;
- this.getPass = getPass;
- }
- public ManageUsers() {
- //Not used
- }
- public void setUserDetails(String uu, String pass, int time, int uc, int ua, int ue, int uir) {
- usersUid = uu;
- getPass = pass;
- usersTime = time;
- usersCooldown = uc;
- usersAccess = ua;
- usersExpiry = ue;
- userIsRoot = uir;
- }
- public void userManager() {
- MysqlDataSource db = new MysqlDataSource();
- Scanner scanner = new Scanner(System.in);
- db.setPort(Vars.DB_PORT);
- db.setDatabaseName(Vars.DB_NAME);
- db.setUser(Vars.DB_USER);
- db.setPassword(Vars.DB_PASS);
- db.setServerName(Vars.SERVER_IP);
- try (Connection conn = db.getConnection()) {
- try (PreparedStatement stmt = conn.prepareStatement(
- "SELECT * FROM users WHERE usersUid = ?"
- )) {
- stmt.setString(1, usersUid);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- userQuery = rs.getString("usersUid");
- }
- }
- if (userQuery != null) {
- try(PreparedStatement passStmt = conn.prepareStatement(
- "SELECT * FROM users WHERE usersPwd = ?"
- )) {
- passStmt.setString(1, getPass);
- ResultSet rsk = passStmt.executeQuery();
- while(rsk.next()) {
- passQuery = rsk.getString("usersPwd");
- }
- try {
- if(passQuery.equalsIgnoreCase(getPass)) {
- //Log the user in and provide access to the Userinterface, once the key was found.
- System.out.println("Username and password do match, going on.");
- UInterface uInterface = new UInterface();
- uInterface.userGui();
- }
- } catch(NullPointerException npe) {
- System.out.println("Wrong password.");
- System.exit(1);
- }
- }
- }
- else {
- System.out.println("User does not exists, ask an admin to create one for you!");
- System.exit(1);
- }
- } catch(SQLException sqle) {
- sqle.printStackTrace();
- System.exit(0);
- }
- }
- public boolean getUserPerms(String userToTest) {
- MysqlDataSource db = new MysqlDataSource();
- db.setPort(Vars.DB_PORT);
- db.setDatabaseName(Vars.DB_NAME);
- db.setUser(Vars.DB_USER);
- db.setPassword(Vars.DB_PASS);
- db.setServerName(Vars.SERVER_IP);
- try (Connection conn = db.getConnection()) {
- System.out.println("Checking permission for: "+usersUid);
- try (PreparedStatement stmt = conn.prepareStatement(
- "SELECT * FROM users WHERE usersUid = ?"
- )) {
- stmt.setString(1, usersUid);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- userQuery = rs.getString("usersUid");
- }
- }
- if(userQuery != null) {
- /*User permission system not working properly, returns rather only 1 or 0 all the time*/
- try (PreparedStatement s = conn.prepareStatement(
- "SELECT * FROM users WHERE userIsRoot = ?"
- )) {
- s.setInt(1, 0); //value 1, as every admin has 1 in his table, default users get 0
- ResultSet r = s.executeQuery();
- while (r.next()) {
- rootQuery = r.getInt("userIsRoot");
- }
- System.out.println("Permission: "+rootQuery);
- }
- }
- if(rootQuery == 1) {
- return true;
- } else {
- return false;
- }
- } catch(SQLException sqle) {
- System.out.println("Could not validate permissions");
- System.exit(0);
- }
- return false;
- }
- public boolean createNewUser() {
- MysqlDataSource db = new MysqlDataSource();
- db.setPort(Vars.DB_PORT);
- db.setDatabaseName(Vars.DB_NAME);
- db.setUser(Vars.DB_USER);
- db.setPassword(Vars.DB_PASS);
- db.setServerName(Vars.SERVER_IP);
- try (Connection conn = db.getConnection()) {
- try (PreparedStatement stmt = conn.prepareStatement(
- "SELECT * FROM users WHERE usersUid = ?"
- )) {
- stmt.setString(1, usersUid);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- userIdIndex = rs.getInt(1) + 1;
- userQuery = rs.getString("usersUid");
- }
- }
- if(userQuery != usersUid) {
- try (PreparedStatement stm = conn.prepareStatement("INSERT INTO `users`(usersId,usersUid,usersPwd,usersTime,usersCooldown,usersAccess,usersExpiry,userIsRoot) VALUES"
- + "(?, ?, ?, ?, ?, ?, ?, ?)")) {
- stm.setObject(1, userIdIndex);
- stm.setString(2, usersUid);
- stm.setString(3, getPass);
- stm.setObject(4, usersTime);
- stm.setObject(5, usersCooldown);
- stm.setObject(6, usersAccess);
- stm.setObject(7, usersExpiry);
- stm.setObject(8, userIsRoot);
- stm.executeUpdate();
- } catch(SQLException s) {
- System.out.println("Could not create user, check parameters.");
- }
- System.out.println("User was created");
- return true;
- }
- } catch(SQLException sqle) {
- System.out.println("User exists already!");
- System.exit(0);
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment