2 * DemosceneMusic - DefaultUser.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.data;
20 import java.io.UnsupportedEncodingException;
21 import java.util.Collection;
23 import net.pterodactylus.util.crypto.MessageDigestHasher;
24 import net.pterodactylus.util.number.Hex;
25 import net.pterodactylus.util.object.Default;
28 * Default implementation of a user data container.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class DefaultUser extends DefaultBase implements User {
35 * Creates a new user data container.
40 public DefaultUser(String id) {
48 public String getName() {
49 return getValue("name", String.class).get();
56 public User setName(String name) {
57 getValue("name", String.class).set(name);
65 public User setPassword(String password) {
67 getValue("password", String.class).set(Hex.toHex(MessageDigestHasher.getSHA512Hasher().hash(password.getBytes("UTF-8"))));
69 } catch (UnsupportedEncodingException uee1) {
70 throw new RuntimeException("VM does not support UTF-8.", uee1);
78 public User setPasswordHash(String passwordHash) {
79 getValue("password", String.class).set(passwordHash);
87 public boolean verifyPassword(String password) {
89 return getValue("password", String.class).get().equalsIgnoreCase(Hex.toHex(MessageDigestHasher.getSHA512Hasher().hash(password.getBytes("UTF-8"))));
90 } catch (UnsupportedEncodingException uee1) {
91 throw new RuntimeException("VM does not support UTF-8.", uee1);
99 public int getLevel() {
100 return Default.forNull(getValue("level", Integer.class).get(), 0);
107 public User setLevel(int level) {
108 getValue("level", Integer.class).set(level);
116 @SuppressWarnings("unchecked")
117 public Collection<String> getOpenIds() {
118 return getValue("openIds", Collection.class).get();
125 public User setOpenIds(Collection<String> openIds) {
126 getValue("openIds", Collection.class).set(openIds);