Guest User

ManageUsers

a guest
Apr 18th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Base64;
  10. import java.util.Scanner;
  11.  
  12. import javax.crypto.SecretKey;
  13. import javax.crypto.spec.SecretKeySpec;
  14.  
  15. import com.mysql.cj.jdbc.MysqlDataSource;
  16. import encryption.Encrypt;
  17. import main.MainS;
  18. import main.Vars;
  19. import userInterface.UInterface;
  20.  
  21. public class ManageUsers {
  22.  
  23. private int userIdIndex;
  24. private String userQuery=null;
  25. private String passQuery=null;
  26. private int rootQuery;
  27. private static int usersAccess = 0;
  28. private static int usersExpiry;
  29. private static int userIsRoot = 0;
  30. private static int usersTime;
  31. private static int usersCooldown;
  32. private static String usersUid, getPass;
  33.  
  34.  
  35. public ManageUsers(String usersUid, String getPass) {
  36.  
  37. this.usersUid = usersUid;
  38. this.getPass = getPass;
  39. }
  40.  
  41. public ManageUsers() {
  42. //Not used
  43. }
  44.  
  45. public void setUserDetails(String uu, String pass, int time, int uc, int ua, int ue, int uir) {
  46.  
  47. usersUid = uu;
  48. getPass = pass;
  49. usersTime = time;
  50. usersCooldown = uc;
  51. usersAccess = ua;
  52. usersExpiry = ue;
  53. userIsRoot = uir;
  54.  
  55. }
  56.  
  57. public void userManager() {
  58.  
  59. MysqlDataSource db = new MysqlDataSource();
  60. Scanner scanner = new Scanner(System.in);
  61.  
  62. db.setPort(Vars.DB_PORT);
  63. db.setDatabaseName(Vars.DB_NAME);
  64. db.setUser(Vars.DB_USER);
  65. db.setPassword(Vars.DB_PASS);
  66. db.setServerName(Vars.SERVER_IP);
  67.  
  68. try (Connection conn = db.getConnection()) {
  69.  
  70. try (PreparedStatement stmt = conn.prepareStatement(
  71. "SELECT * FROM users WHERE usersUid = ?"
  72. )) {
  73. stmt.setString(1, usersUid);
  74. ResultSet rs = stmt.executeQuery();
  75.  
  76. while (rs.next()) {
  77.  
  78. userQuery = rs.getString("usersUid");
  79.  
  80. }
  81. }
  82.  
  83.  
  84. if (userQuery != null) {
  85.  
  86. try(PreparedStatement passStmt = conn.prepareStatement(
  87. "SELECT * FROM users WHERE usersPwd = ?"
  88. )) {
  89.  
  90. passStmt.setString(1, getPass);
  91. ResultSet rsk = passStmt.executeQuery();
  92.  
  93. while(rsk.next()) {
  94.  
  95. passQuery = rsk.getString("usersPwd");
  96.  
  97. }
  98. try {
  99. if(passQuery.equalsIgnoreCase(getPass)) {
  100. //Log the user in and provide access to the Userinterface, once the key was found.
  101. System.out.println("Username and password do match, going on.");
  102. UInterface uInterface = new UInterface();
  103. uInterface.userGui();
  104. }
  105. } catch(NullPointerException npe) {
  106. System.out.println("Wrong password.");
  107. System.exit(1);
  108. }
  109.  
  110. }
  111.  
  112. }
  113. else {
  114.  
  115. System.out.println("User does not exists, ask an admin to create one for you!");
  116. System.exit(1);
  117.  
  118. }
  119. } catch(SQLException sqle) {
  120. sqle.printStackTrace();
  121. System.exit(0);
  122. }
  123.  
  124.  
  125.  
  126. }
  127.  
  128. public boolean getUserPerms(String userToTest) {
  129.  
  130. MysqlDataSource db = new MysqlDataSource();
  131.  
  132. db.setPort(Vars.DB_PORT);
  133. db.setDatabaseName(Vars.DB_NAME);
  134. db.setUser(Vars.DB_USER);
  135. db.setPassword(Vars.DB_PASS);
  136. db.setServerName(Vars.SERVER_IP);
  137.  
  138. try (Connection conn = db.getConnection()) {
  139.  
  140. System.out.println("Checking permission for: "+usersUid);
  141.  
  142. try (PreparedStatement stmt = conn.prepareStatement(
  143. "SELECT * FROM users WHERE usersUid = ?"
  144. )) {
  145. stmt.setString(1, usersUid);
  146. ResultSet rs = stmt.executeQuery();
  147.  
  148. while (rs.next()) {
  149. userQuery = rs.getString("usersUid");
  150. }
  151.  
  152. }
  153. if(userQuery != null) {
  154.  
  155. /*User permission system not working properly, returns rather only 1 or 0 all the time*/
  156.  
  157. try (PreparedStatement s = conn.prepareStatement(
  158. "SELECT * FROM users WHERE userIsRoot = ?"
  159. )) {
  160. s.setInt(1, 0); //value 1, as every admin has 1 in his table, default users get 0
  161. ResultSet r = s.executeQuery();
  162.  
  163. while (r.next()) {
  164. rootQuery = r.getInt("userIsRoot");
  165.  
  166. }
  167. System.out.println("Permission: "+rootQuery);
  168. }
  169. }
  170.  
  171. if(rootQuery == 1) {
  172.  
  173. return true;
  174.  
  175. } else {
  176. return false;
  177. }
  178.  
  179.  
  180. } catch(SQLException sqle) {
  181. System.out.println("Could not validate permissions");
  182. System.exit(0);
  183. }
  184.  
  185. return false;
  186. }
  187.  
  188. public boolean createNewUser() {
  189.  
  190. MysqlDataSource db = new MysqlDataSource();
  191.  
  192. db.setPort(Vars.DB_PORT);
  193. db.setDatabaseName(Vars.DB_NAME);
  194. db.setUser(Vars.DB_USER);
  195. db.setPassword(Vars.DB_PASS);
  196. db.setServerName(Vars.SERVER_IP);
  197.  
  198. try (Connection conn = db.getConnection()) {
  199. try (PreparedStatement stmt = conn.prepareStatement(
  200. "SELECT * FROM users WHERE usersUid = ?"
  201. )) {
  202.  
  203. stmt.setString(1, usersUid);
  204. ResultSet rs = stmt.executeQuery();
  205.  
  206. while (rs.next()) {
  207.  
  208. userIdIndex = rs.getInt(1) + 1;
  209. userQuery = rs.getString("usersUid");
  210.  
  211. }
  212. }
  213.  
  214. if(userQuery != usersUid) {
  215. try (PreparedStatement stm = conn.prepareStatement("INSERT INTO `users`(usersId,usersUid,usersPwd,usersTime,usersCooldown,usersAccess,usersExpiry,userIsRoot) VALUES"
  216. + "(?, ?, ?, ?, ?, ?, ?, ?)")) {
  217. stm.setObject(1, userIdIndex);
  218. stm.setString(2, usersUid);
  219. stm.setString(3, getPass);
  220. stm.setObject(4, usersTime);
  221. stm.setObject(5, usersCooldown);
  222. stm.setObject(6, usersAccess);
  223. stm.setObject(7, usersExpiry);
  224. stm.setObject(8, userIsRoot);
  225. stm.executeUpdate();
  226.  
  227. } catch(SQLException s) {
  228. System.out.println("Could not create user, check parameters.");
  229. }
  230. System.out.println("User was created");
  231. return true;
  232. }
  233.  
  234. } catch(SQLException sqle) {
  235. System.out.println("User exists already!");
  236. System.exit(0);
  237. }
  238.  
  239. return false;
  240. }
  241.  
  242.  
  243. }
  244.  
Add Comment
Please, Sign In to add comment