APIv3/src/main/java/net/frozenorb/apiv3/model/User.java

417 lines
14 KiB
Java
Raw Normal View History

2016-06-16 16:29:33 +02:00
package net.frozenorb.apiv3.model;
2016-02-12 02:40:06 +01:00
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2016-05-14 05:23:46 +02:00
import com.google.common.base.Charsets;
2016-04-30 20:03:34 +02:00
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
2016-05-14 05:23:46 +02:00
import com.google.common.hash.Hashing;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.result.UpdateResult;
2016-06-16 06:08:44 +02:00
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
2016-06-16 06:08:44 +02:00
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
2016-05-14 05:23:46 +02:00
import lombok.AllArgsConstructor;
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-06-16 06:08:44 +02:00
import net.frozenorb.apiv3.serialization.gson.ExcludeFromReplies;
2016-06-17 07:12:32 +02:00
import net.frozenorb.apiv3.serialization.jackson.UuidJsonDeserializer;
import net.frozenorb.apiv3.serialization.jackson.UuidJsonSerializer;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
2016-06-16 16:29:33 +02:00
import net.frozenorb.apiv3.util.MojangUtils;
import net.frozenorb.apiv3.util.PermissionUtils;
import net.frozenorb.apiv3.util.SyncUtils;
2016-06-17 07:12:32 +02:00
import net.frozenorb.apiv3.util.UuidUtils;
2016-04-27 02:46:34 +02:00
import org.bson.Document;
2016-02-12 02:40:06 +01:00
2016-06-16 18:25:22 +02:00
import java.time.Instant;
2016-04-27 02:46:34 +02:00
import java.util.*;
2016-02-23 13:14:42 +01:00
2016-06-16 06:08:44 +02:00
@Entity
2016-05-14 05:23:46 +02:00
@AllArgsConstructor
public final class User {
private static final MongoCollection<User> usersCollection = APIv3.getDatabase().getCollection("users", User.class);
2016-02-12 02:40:06 +01:00
2016-06-17 07:12:32 +02:00
@Getter @Id @JsonSerialize(using=UuidJsonSerializer.class) @JsonDeserialize(using=UuidJsonDeserializer.class) private UUID id;
@Getter private String lastUsername;
2016-06-16 18:25:22 +02:00
@Getter @ExcludeFromReplies private Map<String, Instant> aliases = new HashMap<>();
@Getter @ExcludeFromReplies @Setter private String totpSecret;
@Getter @ExcludeFromReplies @Setter private String emailToken;
2016-06-16 18:25:22 +02:00
@Getter @ExcludeFromReplies @Setter private Instant emailTokenSetAt;
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-05-05 22:00:32 +02:00
@Getter private String phoneNumber;
2016-02-23 13:14:42 +01:00
@Getter private String lastSeenOn;
2016-06-16 18:25:22 +02:00
@Getter private Instant lastSeenAt;
@Getter private Instant firstSeenAt;
@Getter private boolean online;
2016-03-21 23:28:17 +01:00
public static List<User> findAllSync() {
2016-06-16 06:08:44 +02:00
return SyncUtils.blockMulti(usersCollection.find().sort(new Document("lastSeenAt", -1)));
}
public static User findByIdSync(String id) {
UUID uuid;
2016-04-27 02:46:34 +02:00
try {
uuid = UUID.fromString(id);
2016-06-17 07:12:32 +02:00
} catch (NullPointerException | IllegalArgumentException ex) {
2016-05-01 06:34:02 +02:00
return null;
2016-04-27 02:46:34 +02:00
}
return findByIdSync(uuid);
2016-04-27 02:46:34 +02:00
}
public static User findByIdSync(UUID id) {
2016-06-17 07:12:32 +02:00
if (UuidUtils.isAcceptableUuid(id)) {
return SyncUtils.blockOne(usersCollection.find(new Document("_id", id)));
2016-05-14 05:36:17 +02:00
} else {
return null;
}
2016-03-22 00:58:08 +01:00
}
public static User findByEmailTokenSync(String emailToken) {
return SyncUtils.blockOne(usersCollection.find(new Document("emailToken", emailToken)));
2016-04-08 13:12:31 +02:00
}
public static User findByLastUsernameSync(String lastUsername) {
return SyncUtils.blockOne(usersCollection.find(new Document("lastUsername", lastUsername)));
2016-04-27 23:58:00 +02:00
}
public static void findAll(SingleResultCallback<List<User>> callback) {
2016-06-16 06:08:44 +02:00
usersCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
}
public static void findById(String id, SingleResultCallback<User> callback) {
try {
UUID uuid = UUID.fromString(id);
findById(uuid, callback);
} catch (IllegalArgumentException ex) { // from UUID parsing
callback.onResult(null, ex);
}
}
public static void findById(UUID id, SingleResultCallback<User> callback) {
2016-06-17 07:12:32 +02:00
if (UuidUtils.isAcceptableUuid(id)) {
usersCollection.find(new Document("_id", id)).first(callback);
} else {
callback.onResult(null, null);
}
}
public static void findByIdGrouped(Iterable<UUID> search, SingleResultCallback<Map<UUID, User>> callback) {
usersCollection.find(new Document("_id", new Document("$in", search))).into(new ArrayList<>(), (users, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
Map<UUID, User> result = new HashMap<>();
for (UUID user : search) {
result.put(user, null);
}
for (User user : users) {
result.put(user.getId(), user);
}
callback.onResult(result, null);
}
});
}
public static void findByEmailToken(String emailToken, SingleResultCallback<User> callback) {
usersCollection.find(new Document("emailToken", emailToken)).first(callback);
}
public static void findByLastUsername(String lastUsername, SingleResultCallback<User> callback) {
usersCollection.find(new Document("lastUsername", lastUsername)).first(callback);
}
2016-03-21 23:28:17 +01:00
public User() {} // For Morphia
2016-06-17 07:12:32 +02:00
// TODO: THIS IS CURRENTLY BLOCKING. MAYBE FOR THE HEARTBEAT WE CAN DO SOMETHING
// TO MAKE IT NOT SO BLOCKING
2016-05-01 02:08:58 +02:00
public User(UUID id, String lastUsername) {
2016-03-21 23:28:17 +01:00
this.id = id;
2016-05-09 05:18:55 +02:00
this.lastUsername = ""; // Intentional, so updateUsername actually does something.
2016-03-21 23:28:17 +01:00
this.aliases = new HashMap<>();
2016-05-03 01:34:30 +02:00
this.totpSecret = null;
2016-03-21 23:28:17 +01:00
this.password = null;
this.email = null;
2016-05-05 22:00:32 +02:00
this.phoneNumber = null;
2016-03-22 00:58:08 +01:00
this.lastSeenOn = null;
2016-06-16 18:25:22 +02:00
this.lastSeenAt = Instant.now();
this.firstSeenAt = Instant.now();
2016-03-21 23:28:17 +01:00
2016-06-17 07:12:32 +02:00
updateUsername(lastUsername);
2016-02-12 02:40:06 +01:00
}
2016-05-01 02:08:58 +02:00
public boolean hasPermissionAnywhere(String permission) {
2016-06-17 07:12:32 +02:00
Map<String, Boolean> globalPermissions = getGlobalPermissions();
return globalPermissions.containsKey(permission) && globalPermissions.get(permission);
}
// TODO: ASYNC
public Map<String, Boolean> getGlobalPermissions() {
Map<String, Boolean> globalPermissions = PermissionUtils.getDefaultPermissions(getHighestRankAnywhere());
2016-05-05 22:00:32 +02:00
for (Map.Entry<ServerGroup, Rank> serverGroupEntry : getHighestRanks().entrySet()) {
ServerGroup serverGroup = serverGroupEntry.getKey();
Rank rank = serverGroupEntry.getValue();
globalPermissions = PermissionUtils.mergePermissions(
globalPermissions,
serverGroup.calculatePermissions(rank)
);
}
2016-06-17 07:12:32 +02:00
return ImmutableMap.copyOf(globalPermissions);
2016-04-17 21:23:02 +02:00
}
// TODO: Clean
public boolean seenOnServer(Server server) {
if (online && server.getId().equals(this.lastSeenOn)) {
return false;
}
2016-05-01 02:08:58 +02:00
this.lastSeenOn = server.getId();
if (!online) {
2016-06-16 18:25:22 +02:00
this.lastSeenAt = Instant.now();
}
this.online = true;
return true;
}
public void leftServer() {
2016-06-16 18:25:22 +02:00
this.lastSeenAt = Instant.now();
this.online = false;
2016-05-09 05:18:55 +02:00
}
2016-06-17 07:12:32 +02:00
public void updateUsername(String newUsername) {
if (!newUsername.equals(lastUsername)) {
this.lastUsername = newUsername;
2016-05-09 05:18:55 +02:00
2016-06-17 07:12:32 +02:00
User withNewUsername;
2016-06-16 06:08:44 +02:00
2016-06-17 07:12:32 +02:00
while ((withNewUsername = User.findByLastUsernameSync(newUsername)) != null) {
BlockingCallback<String> callback = new BlockingCallback<>();
MojangUtils.getName(withNewUsername.getId(), callback);
withNewUsername.updateUsername(callback.get());
2016-06-16 06:08:44 +02:00
}
2016-06-17 07:12:32 +02:00
}
this.aliases.put(newUsername, Instant.now());
2016-05-01 02:08:58 +02:00
}
2016-05-14 05:23:46 +02:00
public void setPassword(String input) {
this.password = Hashing
.sha256()
.hashString(input + "$" + id.toString(), Charsets.UTF_8)
.toString();
2016-04-27 23:58:00 +02:00
}
2016-05-14 05:23:46 +02:00
public boolean checkPassword(String input) {
String hashed = Hashing
.sha256()
.hashString(input + "$" + id.toString(), Charsets.UTF_8)
.toString();
2016-06-16 16:29:33 +02:00
return password != null && hashed.equals(password);
2016-04-27 23:58:00 +02:00
}
public Rank getHighestRankAnywhere() {
return getHighestRankScoped(null);
2016-05-05 22:00:32 +02:00
}
public Rank getHighestRankScoped(ServerGroup serverGroup) {
return getHighestRankScoped(serverGroup, Grant.findByUserSync(this));
}
// TODO: Clean
// This is only used to help batch requests to mongo
public Rank getHighestRankScoped(ServerGroup serverGroup, Iterable<Grant> grants) {
2016-05-09 05:18:55 +02:00
Rank highest = null;
2016-04-30 20:03:34 +02:00
for (Grant grant : grants) {
2016-05-01 02:08:58 +02:00
if (!grant.isActive() || (serverGroup != null && !grant.appliesOn(serverGroup))) {
2016-04-30 20:03:34 +02:00
continue;
}
Rank rank = Rank.findById(grant.getRank());
2016-04-30 20:03:34 +02:00
if (highest == null || rank.getWeight() > highest.getWeight()) {
highest = rank;
}
}
if (highest != null) {
return highest;
} else {
return Rank.findById("default");
2016-04-30 20:03:34 +02:00
}
2016-04-28 22:57:44 +02:00
}
// TODO: Clean
2016-05-05 22:00:32 +02:00
public Map<ServerGroup, Rank> getHighestRanks() {
Map<ServerGroup, Rank> highestRanks = new HashMap<>();
Rank defaultRank = Rank.findById("default");
List<Grant> userGrants = Grant.findByUserSync(this);
2016-05-05 22:00:32 +02:00
for (ServerGroup serverGroup : ServerGroup.findAll()) {
2016-05-05 22:00:32 +02:00
Rank highest = defaultRank;
for (Grant grant : userGrants) {
if (!grant.isActive() || !grant.appliesOn(serverGroup)) {
continue;
}
Rank rank = Rank.findById(grant.getRank());
2016-05-05 22:00:32 +02:00
if (highest == null || rank.getWeight() > highest.getWeight()) {
highest = rank;
}
}
highestRanks.put(serverGroup, highest);
}
return highestRanks;
2016-04-30 20:03:34 +02:00
}
2016-04-28 22:57:44 +02:00
2016-06-16 16:29:33 +02:00
public void getLoginInfo(Server server, String userIp, SingleResultCallback<Map<String, Object>> callback) {
2016-06-16 06:08:44 +02:00
Future<Iterable<Punishment>> punishmentsFuture = Future.future();
2016-06-16 16:29:33 +02:00
Future<Iterable<IpBan>> ipBansFuture = Future.future();
2016-06-16 06:08:44 +02:00
Future<Iterable<Grant>> grantsFuture = Future.future();
Punishment.findByUserAndType(this, ImmutableSet.of(
Punishment.PunishmentType.BLACKLIST,
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.MUTE
), (punishments, error) -> {
if (error != null) {
punishmentsFuture.fail(error);
} else {
punishmentsFuture.complete(punishments);
}
});
2016-06-16 16:29:33 +02:00
if (userIp != null) {
IpBan.findByIp(userIp, (ipBans, error) -> {
if (error != null) {
ipBansFuture.fail(error);
} else {
ipBansFuture.complete(ipBans);
}
});
} else {
ipBansFuture.complete(ImmutableSet.of());
}
2016-06-16 06:08:44 +02:00
Grant.findByUser(this, (grants, error) -> {
if (error != null) {
grantsFuture.fail(error);
} else {
grantsFuture.complete(grants);
}
});
2016-06-16 16:29:33 +02:00
CompositeFuture.all(punishmentsFuture, ipBansFuture, grantsFuture).setHandler((result) -> {
2016-06-16 06:08:44 +02:00
if (result.succeeded()) {
Iterable<Punishment> punishments = result.result().result(0);
2016-06-16 16:29:33 +02:00
Iterable<IpBan> ipBans = result.result().result(1);
Iterable<Grant> grants = result.result().result(2);
2016-06-16 06:08:44 +02:00
2016-06-16 16:29:33 +02:00
callback.onResult(createLoginInfo(server, punishments, ipBans, grants), null);
2016-06-16 06:08:44 +02:00
} else {
callback.onResult(null, result.cause());
}
});
}
// This is only used to help batch requests to mongo
2016-06-16 16:29:33 +02:00
public Map<String, Object> createLoginInfo(Server server, Iterable<Punishment> punishments, Iterable<IpBan> ipBans, Iterable<Grant> grants) {
2016-05-09 05:18:55 +02:00
Punishment activeMute = null;
2016-06-16 06:08:44 +02:00
Punishment activeBan = null;
2016-06-16 16:29:33 +02:00
IpBan activeIpBan = null;
2016-04-28 22:57:44 +02:00
for (Punishment punishment : punishments) {
2016-04-30 20:03:34 +02:00
if (!punishment.isActive()) {
continue;
}
2016-04-28 22:57:44 +02:00
2016-05-09 05:18:55 +02:00
if (punishment.getType() == Punishment.PunishmentType.MUTE) {
activeMute = punishment;
2016-06-16 06:08:44 +02:00
} else if (punishment.getType() == Punishment.PunishmentType.BAN || punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
activeBan = punishment;
2016-05-07 15:34:10 +02:00
}
}
2016-04-28 22:57:44 +02:00
2016-06-16 16:29:33 +02:00
for (IpBan ipBan : ipBans) {
if (ipBan.isActive()) {
activeIpBan = ipBan;
break;
}
}
Rank highestRank = getHighestRankScoped(ServerGroup.findById(server.getServerGroup()), grants);
2016-06-16 16:29:33 +02:00
Map<String, Object> access = ImmutableMap.of(
"allowed", true,
"message", "Public server"
);
if (activeBan != null) {
access = ImmutableMap.of(
"allowed", false,
"message", activeBan.getAccessDenialReason(),
"activeBanId", activeBan.getId()
);
} else if (activeIpBan != null) {
2016-06-17 07:12:32 +02:00
// TODO: ASYNC
BlockingCallback<String> callback = new BlockingCallback<>();
activeIpBan.getAccessDenialReason(callback);
String reason = callback.get();
2016-06-16 16:29:33 +02:00
access = ImmutableMap.of(
"allowed", false,
2016-06-17 07:12:32 +02:00
"message", reason,
2016-06-16 16:29:33 +02:00
"activeIpBanId", activeIpBan.getId()
);
}
2016-05-06 01:35:45 +02:00
2016-05-09 05:18:55 +02:00
// Generics are weird, yes we have to do this.
ImmutableMap.Builder<String, Object> result = ImmutableMap.<String, Object>builder()
.put("user", this)
2016-06-16 16:29:33 +02:00
.put("access", access)
2016-05-09 05:18:55 +02:00
.put("rank", highestRank.getId())
.put("totpSetup", getTotpSecret() != null);
if (activeMute != null) {
result.put("mute", activeMute);
2016-05-07 15:34:10 +02:00
}
2016-05-09 05:18:55 +02:00
return result.build();
2016-04-28 22:57:44 +02:00
}
public void insert() {
BlockingCallback<Void> callback = new BlockingCallback<>();
usersCollection.insertOne(this, callback);
callback.get();
}
public void save() {
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
2016-06-16 06:08:44 +02:00
save(callback);
callback.get();
}
2016-06-16 06:08:44 +02:00
public void save(SingleResultCallback<UpdateResult> callback) {
usersCollection.replaceOne(new Document("_id", id), this, callback);
}
2016-02-12 02:40:06 +01:00
}