Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xenon.security;
- import java.security.GeneralSecurityException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import xenon.sql.SQLEngine;
- /**
- * Represents a persons account.
- *
- * @author Colby
- */
- public class Account {
- public Account(SQLEngine sql, String attemptUsername, String attemptPassword) throws SQLException, GeneralSecurityException {
- username = attemptUsername;
- password = attemptPassword;
- rights = Rights.Administrator;
- if (true) {
- return;
- }
- Connection dedi = null;
- try {
- dedi = sql.getDedicatedConnection();
- PreparedStatement stmt = getCredStatement(dedi);
- stmt.setString(1, attemptUsername);
- ResultSet set = stmt.executeQuery();
- if (set.next()) {
- String actualHash = set.getString("password");
- String actualSalt = set.getString("salt");
- String attemptHash = MD5.getHash(attemptPassword, actualSalt);
- if (!actualHash.equals(attemptHash)) {
- throw new GeneralSecurityException("User: " + attemptUsername
- + " Invalid password: " + attemptPassword);
- }
- rights = rightsForName(nameForId(translateVBIDToServer(set.getInt("usergroupid"))));
- username = attemptUsername;
- password = attemptHash;
- } else {
- throw new GeneralSecurityException("Invalid username: "
- + attemptUsername);
- }
- } finally {
- if (dedi != null) {
- dedi.close();
- }
- }
- }
- @Override
- public boolean equals(Object o) {
- if (o instanceof Account) {
- Account other = (Account) o;
- return other.username.equals(username);
- }
- return false;
- }
- public String getPassword() {
- return password;
- }
- public Rights getRights() {
- return rights;
- }
- public String getUsername() {
- return username;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setRights(Rights rights) {
- this.rights = rights;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- /**
- * Translates the vbulliten usergroup id into a number usable by the server
- *
- * @param VBID
- * The number to convert
- * @return The converted number
- */
- public static byte translateVBIDToServer(int VBID) {
- switch (VBID) {
- case 2:
- return 0;// Member
- case 39:
- return 0;// Platinum
- case 42:
- return 0;// Gold
- case 10:
- return 1;// Mod
- case 9:
- return 2;// Admin
- case 12:
- return 2;// Developer
- case 6:
- return 2;// Executive
- default:
- return 0;// Undefined
- }
- }
- /**
- * Translates the specified id into the name of the Rights that holds that
- * id
- *
- * @param id
- * The id to get the parents name of
- * @return The name of the id's parent
- * @throws IllegalArgumentException
- * If there is no Rights associated with the specified id
- */
- public static String nameForId(int id) {
- for (Rights r : Rights.values()) {
- if (r.getId() == id) {
- return r.toString();
- }
- }
- return Rights.Player.toString();
- }
- /**
- * Translates the specified name into its parent Rights
- *
- * @param name
- * The name to get the parent
- * @return The parent Rights with the specified name
- * @see Rights.valueOf(String)
- * @throws IllegalArgumentException
- * If there is no parent Rights with the specified name
- */
- public static Rights rightsForName(String name) {
- Rights result = Rights.valueOf(name);
- return result != null ? result : Rights.Player;
- }
- public enum Rights {
- Player(0), Moderator(1), Administrator(2);
- Rights(int id) {
- this.id = id;
- }
- public int getId() {
- return id;
- }
- private int id;
- }
- @SuppressWarnings("unused")
- private static PreparedStatement getCredStatement(Connection con) {
- if (credCheck != null) {
- return credCheck;
- }
- try {
- return con.prepareStatement("SELECT * FROM user WHERE username=?");
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- private String username;
- private String password;
- private Rights rights;
- private static PreparedStatement credCheck;
- }
Add Comment
Please, Sign In to add comment