2016-04-08 13:12:31 +02:00
|
|
|
package net.frozenorb.apiv3.models;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
2016-02-12 02:40:06 +01:00
|
|
|
import lombok.Getter;
|
2016-04-27 23:58:00 +02:00
|
|
|
import lombok.Setter;
|
2016-03-22 00:58:08 +01:00
|
|
|
import net.frozenorb.apiv3.APIv3;
|
2016-04-28 22:57:44 +02:00
|
|
|
import net.frozenorb.apiv3.serialization.ExcludeFromReplies;
|
2016-04-30 20:03:34 +02:00
|
|
|
import net.frozenorb.apiv3.utils.TimeUtils;
|
2016-04-27 02:46:34 +02:00
|
|
|
import org.bson.Document;
|
2016-04-27 23:58:00 +02:00
|
|
|
import org.mindrot.jbcrypt.BCrypt;
|
2016-03-21 23:28:17 +01:00
|
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
|
|
import org.mongodb.morphia.annotations.Id;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
import java.util.*;
|
2016-02-23 13:14:42 +01:00
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Entity(value = "users", noClassnameStored = true)
|
|
|
|
public final class User {
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-03-22 00:58:08 +01:00
|
|
|
@Getter @Id private UUID id;
|
2016-02-23 13:14:42 +01:00
|
|
|
@Getter private String lastName;
|
2016-03-22 00:58:08 +01:00
|
|
|
@Getter @ExcludeFromReplies private Map<String, Date> aliases;
|
|
|
|
@Getter @ExcludeFromReplies private String otpCode;
|
2016-04-27 23:58:00 +02:00
|
|
|
@Getter @ExcludeFromReplies @Setter private String emailToken;
|
|
|
|
@Getter @ExcludeFromReplies @Setter private Date emailTokenSet;
|
2016-03-22 00:58:08 +01:00
|
|
|
@Getter @ExcludeFromReplies private String password;
|
2016-04-27 23:58:00 +02:00
|
|
|
@Getter @Setter private String email;
|
2016-02-23 13:14:42 +01:00
|
|
|
@Getter private int phoneNumber;
|
|
|
|
@Getter private String lastSeenOn;
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date lastSeenAt;
|
|
|
|
@Getter private Date firstSeen;
|
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public static User byId(String id) {
|
|
|
|
try {
|
|
|
|
return byId(UUID.fromString(id));
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new IllegalArgumentException("Invalid UUID string " + id, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:58:08 +01:00
|
|
|
public static User byId(UUID id) {
|
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("id").equal(id).get();
|
|
|
|
}
|
|
|
|
|
2016-04-23 03:04:15 +02:00
|
|
|
// TODO: FIND ALL USAGES OF THIS AND
|
|
|
|
// SEE HOW MANY OF THEM (EX NON GET REQUESTS)
|
|
|
|
// WE CAN DROP
|
2016-04-08 13:12:31 +02:00
|
|
|
public static User byIdOrName(String idOrName) {
|
|
|
|
if (idOrName.length() == 36) {
|
|
|
|
return byId(UUID.fromString(idOrName));
|
|
|
|
} else {
|
|
|
|
return byName(idOrName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
public static User byName(String name) {
|
2016-04-17 21:23:02 +02:00
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("lastName").equalIgnoreCase(name).get();
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:58:00 +02:00
|
|
|
public static User byEmailToken(String name) {
|
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("emailToken").equal(name).get();
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public User() {} // For Morphia
|
|
|
|
|
|
|
|
public User(UUID id, String lastName) {
|
|
|
|
this.id = id;
|
|
|
|
this.lastName = lastName;
|
|
|
|
this.aliases = new HashMap<>();
|
|
|
|
this.otpCode = null;
|
|
|
|
this.password = null;
|
|
|
|
this.email = null;
|
2016-03-22 00:58:08 +01:00
|
|
|
this.phoneNumber = -1;
|
|
|
|
this.lastSeenOn = null;
|
2016-03-21 23:28:17 +01:00
|
|
|
this.lastSeenAt = new Date();
|
|
|
|
this.firstSeen = new Date();
|
|
|
|
|
|
|
|
aliases.put(lastName, new Date());
|
2016-02-12 02:40:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 21:23:02 +02:00
|
|
|
public boolean hasPermissionAnywhere(String permission) {
|
|
|
|
return hasPermissionScoped(permission, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermissionScoped(String permission, ServerGroup scope) {
|
|
|
|
// TODO: BLAH FIX THIS IF WE DONT REMOVE THEN IDK WHAT TO SAY
|
|
|
|
// Also this is 1 > 0 because 'return true;' means all usages of this
|
|
|
|
// get marked as a warning until we change it.
|
|
|
|
return 1 > 0;
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public List<Grant> getGrants() {
|
|
|
|
return APIv3.getDatastore().createQuery(Grant.class).field("target").equal(id).asList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<IPLogEntry> getIPLog() {
|
|
|
|
return APIv3.getDatastore().createQuery(IPLogEntry.class).field("user").equal(id).asList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Punishment> getPunishments() {
|
|
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("target").equal(id).asList();
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:58:00 +02:00
|
|
|
public List<Punishment> getPunishments(Collection<Punishment.PunishmentType> types) {
|
|
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("target").equal(id).field("type").in(types).asList();
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public UserMetaEntry getMeta(ServerGroup group) {
|
|
|
|
return APIv3.getDatastore().createQuery(UserMetaEntry.class).field("user").equal(id).field("serverGroup").equalIgnoreCase(group.getId()).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveMeta(ServerGroup group, Document data) {
|
|
|
|
UserMetaEntry entry = getMeta(group);
|
|
|
|
|
|
|
|
if (entry == null) {
|
|
|
|
APIv3.getDatastore().save(new UserMetaEntry(this, group, data));
|
|
|
|
} else {
|
|
|
|
entry.setData(data);
|
|
|
|
APIv3.getDatastore().save(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:58:00 +02:00
|
|
|
public void setPassword(char[] unencrypted) {
|
|
|
|
this.password = BCrypt.hashpw(new String(unencrypted), BCrypt.gensalt());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean checkPassword(char[] unencrypted) {
|
|
|
|
return BCrypt.checkpw(new String(unencrypted), password);
|
|
|
|
}
|
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
public Rank getHighestRank(ServerGroup serverGroup) {
|
|
|
|
Rank highest = null;
|
|
|
|
|
|
|
|
for (Grant grant : getGrants()) {
|
|
|
|
if (!grant.isActive()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverGroup != null && !grant.appliesOn(serverGroup)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rank rank = Rank.byId(grant.getRank());
|
|
|
|
|
|
|
|
if (highest == null || rank.getWeight() > highest.getWeight()) {
|
|
|
|
highest = rank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highest != null) {
|
|
|
|
return highest;
|
|
|
|
} else {
|
|
|
|
return Rank.byId("default");
|
|
|
|
}
|
2016-04-28 22:57:44 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
public Rank getHighestRankAnywhere() {
|
|
|
|
return getHighestRank(null);
|
|
|
|
}
|
2016-04-28 22:57:44 +02:00
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
public Map<String, Object> getLoginInfo(Server server) {
|
|
|
|
String accessDenialReason = null;
|
2016-04-28 22:57:44 +02:00
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
for (Punishment punishment : getPunishments(ImmutableSet.of(
|
|
|
|
Punishment.PunishmentType.BLACKLIST,
|
|
|
|
Punishment.PunishmentType.BAN
|
|
|
|
))) {
|
|
|
|
if (!punishment.isActive()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-28 22:57:44 +02:00
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
|
|
|
|
accessDenialReason = "Your account has been blacklisted from the MineHQ Network. \n\nThis type of punishment cannot be appealed.";
|
|
|
|
} else {
|
|
|
|
accessDenialReason = "Your account has been suspended from the MineHQ Network. \n\n";
|
2016-04-28 22:57:44 +02:00
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
if (punishment.getExpiresAt() != null) {
|
|
|
|
accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(punishment.getExpiresAt(), new Date()));
|
|
|
|
} else {
|
|
|
|
accessDenialReason += "Appeal at MineHQ.com/appeal";
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 22:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
ServerGroup actorGroup = server.resolveGroup();
|
|
|
|
Rank rank = getHighestRank(actorGroup);
|
|
|
|
Map<String, Boolean> rankPermissions = actorGroup.calculatePermissions(rank);
|
|
|
|
|
|
|
|
return ImmutableMap.of(
|
|
|
|
"user", this,
|
|
|
|
"access", ImmutableMap.of(
|
|
|
|
"allowed", accessDenialReason == null,
|
|
|
|
"reason", accessDenialReason == null ? "Public server" : accessDenialReason
|
|
|
|
),
|
|
|
|
"rank", rank,
|
|
|
|
"permissions", rankPermissions
|
|
|
|
);
|
2016-04-28 22:57:44 +02:00
|
|
|
}
|
|
|
|
|
2016-02-12 02:40:06 +01:00
|
|
|
}
|