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

281 lines
8.9 KiB
Java
Raw Normal View History

2016-04-08 13:12:31 +02:00
package net.frozenorb.apiv3.models;
2016-02-12 02:40:06 +01:00
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.client.MongoCollection;
import com.mongodb.client.result.UpdateResult;
import eu.dozd.mongo.annotation.Entity;
import eu.dozd.mongo.annotation.Id;
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-04-28 22:57:44 +02:00
import net.frozenorb.apiv3.serialization.ExcludeFromReplies;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
2016-05-09 05:18:55 +02:00
import net.frozenorb.apiv3.utils.MojangUtils;
2016-05-05 22:00:32 +02:00
import net.frozenorb.apiv3.utils.PermissionUtils;
import net.frozenorb.apiv3.utils.SyncUtils;
2016-05-14 05:36:17 +02:00
import net.frozenorb.apiv3.utils.UUIDUtils;
2016-04-27 02:46:34 +02:00
import org.bson.Document;
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
@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-03-22 00:58:08 +01:00
@Getter @Id private UUID id;
@Getter private String lastUsername;
@Getter @ExcludeFromReplies private Map<String, Date> aliases = new HashMap<>();
@Getter @ExcludeFromReplies @Setter private String totpSecret;
@Getter @ExcludeFromReplies @Setter private String emailToken;
2016-05-11 00:37:07 +02:00
@Getter @ExcludeFromReplies @Setter private Date 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-03-21 23:28:17 +01:00
@Getter private Date lastSeenAt;
2016-05-06 01:35:45 +02:00
@Getter private Date firstSeenAt;
@Getter private boolean online;
2016-03-21 23:28:17 +01:00
public static List<User> findAll() {
return SyncUtils.blockMulti(usersCollection.find());
}
public static User findById(String id) {
UUID uuid;
2016-04-27 02:46:34 +02:00
try {
uuid = UUID.fromString(id);
} catch (IllegalArgumentException ex) {
2016-05-01 06:34:02 +02:00
return null;
2016-04-27 02:46:34 +02:00
}
return findById(uuid);
2016-04-27 02:46:34 +02:00
}
public static User findById(UUID id) {
2016-05-14 05:36:17 +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 findByEmailToken(String emailToken) {
return SyncUtils.blockOne(usersCollection.find(new Document("emailToken", emailToken)));
2016-04-08 13:12:31 +02:00
}
public static User findByLastUsername(String lastUsername) {
return SyncUtils.blockOne(usersCollection.find(new Document("lastUsername", lastUsername)));
2016-04-27 23:58:00 +02:00
}
2016-03-21 23:28:17 +01:00
public User() {} // For Morphia
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-03-21 23:28:17 +01:00
this.lastSeenAt = new Date();
2016-05-06 01:35:45 +02:00
this.firstSeenAt = new Date();
2016-03-21 23:28:17 +01:00
2016-05-09 05:18:55 +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) {
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)
);
}
return globalPermissions.containsKey(permission) && globalPermissions.get(permission);
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) {
this.lastSeenAt = new Date();
}
this.online = true;
return true;
}
public void leftServer() {
2016-05-01 02:08:58 +02:00
this.lastSeenAt = new Date();
this.online = false;
2016-05-09 05:18:55 +02:00
}
public void updateUsername(String username) {
if (!username.equals(lastUsername)) {
this.lastUsername = username;
User withNewUsername;
while ((withNewUsername = User.findByLastUsername(username)) != null) {
2016-05-09 05:18:55 +02:00
String newUsername = MojangUtils.getName(withNewUsername.getId());
withNewUsername.updateUsername(newUsername);
}
}
2016-05-05 22:00:32 +02:00
this.aliases.put(username, new Date());
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();
return 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.findByUser(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.findByUser(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-04-30 20:03:34 +02:00
public Map<String, Object> getLoginInfo(Server server) {
return createLoginInfo(
server,
Punishment.findByUserAndType(this, ImmutableSet.of(
Punishment.PunishmentType.BLACKLIST,
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.MUTE
)),
Grant.findByUser(this)
);
}
// This is only used to help batch requests to mongo
public Map<String, Object> createLoginInfo(Server server, Iterable<Punishment> punishments, Iterable<Grant> grants) {
2016-05-09 05:18:55 +02:00
Punishment activeMute = null;
2016-04-30 20:03:34 +02:00
String accessDenialReason = 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;
} else {
accessDenialReason = punishment.getAccessDenialReason();
2016-05-07 15:34:10 +02:00
}
}
2016-04-28 22:57:44 +02:00
Rank highestRank = getHighestRankScoped(ServerGroup.findById(server.getServerGroup()), grants);
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)
.put("access", ImmutableMap.of(
"allowed", accessDenialReason == null,
"message", accessDenialReason == null ? "Public server" : accessDenialReason
))
.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<>();
usersCollection.replaceOne(new Document("_id", id), this, callback);
callback.get();
}
2016-02-12 02:40:06 +01:00
}