/* * Copyright (c) 2010 StockPlay development team * All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package com.kapti.data; import com.kapti.exceptions.InvocationException; import com.kapti.exceptions.StockPlayException; import java.io.IOException; import java.io.Serializable; import java.security.NoSuchAlgorithmException; import java.util.Date; import java.security.MessageDigest; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; /** * \brief Basisobject voor gebruiker-gerelateerde data * * Deze klasse wordt gebruikt om gebruiker-gerelateerde data te verpakken * in een object dat dan verder intern gebruikt wordt binnen de backend. Het * biedt ook de nodige functionaliteit om zichzelf terug te converteren naar * een object dat over XML-RPC verstuurd kan worden, of om net zichzelf te * construeren of aan te passen aan de hand van dergelijke data. */ public class User implements Serializable { // // Member data // public static enum Fields { ID, NICKNAME, PASSWORD, EMAIL, LASTNAME, FIRSTNAME, REGDATE, ROLE, POINTS, STARTAMOUNT, CASH, RRN } public static Map<Fields, Class> Types = new HashMap<Fields, Class>() { { put(Fields.ID, Integer.class); put(Fields.NICKNAME, String.class); put(Fields.PASSWORD, String.class); put(Fields.EMAIL, String.class); put(Fields.LASTNAME, String.class); put(Fields.FIRSTNAME, String.class); put(Fields.REGDATE, Date.class); put(Fields.ROLE, Integer.class); put(Fields.POINTS, Integer.class); put(Fields.STARTAMOUNT, Double.class); put(Fields.CASH, Double.class); put(Fields.RRN, String.class); } }; private int id = -1; private String nickname = ""; private String password = ""; private String email = ""; private String lastname = ""; private String firstname = ""; private Date regdate = null; private Role role = Role.USER; private int points = 0; private double startamount = 0; private double cash = 0; private long rrn = 0; // // Construction // // public User(int id, String nickname, String email, String lastname, String firstname, Date regdate) { // this.id = id; // this.nickname = nickname; // this.email = email; // this.lastname = lastname; // this.firstname = firstname; // this.regdate = regdate; // } public User() { } public User(int id) { this.id = id; } // // Methods // private void setId(int id) { this.id = id; } public void setEncryptedPassword(String password) { this.password = password; } public void setPassword(String password) { this.password = encryptPassword(password); } public String getPassword() { return password; } private static String byteArrayToHexString(byte[] b) { StringBuffer sb = new StringBuffer(b.length * 2); for (int i = 0; i < b.length; i++) { int v = b[i] & 0xff; if (v < 16) { sb.append('0'); } sb.append(Integer.toHexString(v)); } return sb.toString().toUpperCase(); } private String encryptPassword(String password) { try { //we halen de salt op Properties properties = new Properties(); properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("backend.properties")); String salt = properties.getProperty("salt"); MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.reset(); digest.update(salt.getBytes()); return byteArrayToHexString(digest.digest(password.getBytes())); } catch (IOException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (NoSuchAlgorithmException ex) { Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex); return null; } } public boolean checkPassword(String password) { return this.password.equals(encryptPassword(password)); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getFirstname() { return firstname; } public Role getRole() { return role; } public void setRole(int role) { this.role = Role.fromId(role); } public void setRole(Role role) { this.role = role; } public String getLastname() { return lastname; } public String getNickname() { return nickname; } public double getCash() { return cash; } public int getId() { return id; } public int getPoints() { return points; } public Date getRegdate() { return regdate; } public double getStartamount() { return startamount; } public void setCash(double cash) { this.cash = cash; } public void setPoints(int points) { this.points = points; } public void setStartamount(double startamount) { this.startamount = startamount; } public long getRijksregisternummer() { return rrn; } public void setRijksregisternummer(long rrn) { this.rrn = rrn; } public void setFirstname(String firstname) { this.firstname = firstname; } public void setLastname(String lastname) { this.lastname = lastname; } public void setNickname(String nickname) { this.nickname = nickname; } public void setRegdate(Date regdate) { this.regdate = regdate; } /** * Geeft het User-object in een generiek HashMap-object terug, zodat het geserialiseerd kan worden voor XML-RPC * @param iFields De velden die moeten worden opgenomen in de struct (Opmerking: een paswoord kan niet worden opgevraagd!) * @return * */ public HashMap<String, Object> toStruct(Fields... iFields) { HashMap<String, Object> oStruct = new HashMap<String, Object>(); for (Fields tField : iFields) { switch (tField) { case ID: oStruct.put(tField.name(), getId()); break; case NICKNAME: oStruct.put(tField.name(), getNickname()); break; case EMAIL: oStruct.put(tField.name(), getEmail()); break; case LASTNAME: if (getLastname() != null) { oStruct.put(tField.name(), getLastname()); } break; case FIRSTNAME: if (getFirstname() != null) { oStruct.put(tField.name(), getFirstname()); } break; case REGDATE: oStruct.put(tField.name(), getRegdate()); break; case ROLE: oStruct.put(tField.name(), getRole().getId()); break; case POINTS: oStruct.put(tField.name(), getPoints()); break; case STARTAMOUNT: oStruct.put(tField.name(), getStartamount()); break; case CASH: oStruct.put(tField.name(), getCash()); break; case RRN: oStruct.put(tField.name(), Long.toString(getRijksregisternummer())); break; } } return oStruct; } public void applyStruct(HashMap<String, Object> iStruct) throws StockPlayException { for (String tKey : iStruct.keySet()) { Object tValue = iStruct.get(tKey); Fields tField = null; try { tField = Fields.valueOf(tKey.toUpperCase()); } catch (IllegalArgumentException e) { throw new InvocationException(InvocationException.Type.KEY_DOES_NOT_EXIST, "requested key '" + tKey + "' does not exist"); } if (!Types.get(tField).isInstance(iStruct.get(tKey))) { throw new InvocationException(InvocationException.Type.BAD_REQUEST, "provided key '" + tKey + "' requires a " + Types.get(tField) + " instead of an " + iStruct.get(tKey).getClass()); } switch (tField) { case FIRSTNAME: setFirstname((String) tValue); break; case LASTNAME: setLastname((String) tValue); break; case NICKNAME: setNickname((String) tValue); break; case PASSWORD: setPassword((String) tValue); break; case EMAIL: setEmail((String) tValue); break; case ROLE: setRole((Integer) tValue); break; case RRN: if (tValue instanceof String) { setRijksregisternummer(Long.parseLong((String) tValue)); } else { setRijksregisternummer(((Integer) tValue).longValue()); } break; case STARTAMOUNT: case POINTS: case CASH: case REGDATE: default: throw new InvocationException(InvocationException.Type.READ_ONLY_KEY, "requested key '" + tKey + "' cannot be modified"); } } } public static User fromStruct(HashMap<String, Object> iStruct) throws StockPlayException { // Create case mapping HashMap<Fields, String> tStructMap = new HashMap<Fields, String>(); for (String tKey : iStruct.keySet()) { Fields tField = null; try { tField = Fields.valueOf(tKey.toUpperCase()); } catch (IllegalArgumentException e) { throw new InvocationException(InvocationException.Type.KEY_DOES_NOT_EXIST, "requested key '" + tKey + "' does not exist"); } if (!Types.get(tField).isInstance(iStruct.get(tKey))) { throw new InvocationException(InvocationException.Type.BAD_REQUEST, "provided key '" + tKey + "' requires a " + Types.get(tField) + " instead of an " + iStruct.get(tKey).getClass()); } tStructMap.put(tField, tKey); } // Check needed keys // if (tStructMap.containsKey(Fields.ID)){// && tStructMap.containsKey(Fields.NICKNAME) && tStructMap.containsKey(Fields.EMAIL) && tStructMap.containsKey(Fields.FIRSTNAME) && tStructMap.containsKey(Fields.LASTNAME) && tStructMap.containsKey(Fields.REGDATE)) { User tUser = new User(); return tUser; // } else { // throw new ServiceException(ServiceException.Type.NOT_ENOUGH_INFORMATION); // } } public enum Role { USER(0), ADMIN(1), SCRAPER(2), TRANSACTIONMANAGER(3), POINTSMANAGER(4), AI(5); int id; private Role(int id) { this.id = id; } public static Role fromId(int id) { for (Role r : Role.values()) { if (r.id == id) { return r; } } return null; } public int getId() { return id; } } }