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 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;
|
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;
|
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-03-21 23:28:17 +01:00
|
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
|
|
import org.mongodb.morphia.annotations.Id;
|
2016-05-03 01:34:30 +02:00
|
|
|
import org.mongodb.morphia.annotations.Indexed;
|
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)
|
2016-05-14 05:23:46 +02:00
|
|
|
@AllArgsConstructor
|
2016-05-05 22:00:32 +02:00
|
|
|
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-05-03 01:34:30 +02:00
|
|
|
@Getter @Indexed private String lastUsername;
|
2016-05-15 05:58:37 +02:00
|
|
|
@Getter @ExcludeFromReplies private Map<String, Date> aliases = new HashMap<>();
|
2016-05-03 01:34:30 +02:00
|
|
|
@Getter @Setter @ExcludeFromReplies private String totpSecret;
|
|
|
|
@Getter @Indexed @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;
|
2016-05-09 21:43:27 +02:00
|
|
|
@Getter private boolean online;
|
2016-03-21 23:28:17 +01:00
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public static User byId(String id) {
|
|
|
|
try {
|
|
|
|
return byId(UUID.fromString(id));
|
|
|
|
} catch (Exception ex) {
|
2016-05-01 06:34:02 +02:00
|
|
|
return null;
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 00:58:08 +01:00
|
|
|
public static User byId(UUID id) {
|
2016-05-14 05:36:17 +02:00
|
|
|
if (UUIDUtils.isAcceptableUUID(id)) {
|
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("id").equal(id).get();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-22 00:58:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 08:44:10 +02:00
|
|
|
public static Map<UUID, User> byIdGrouped(Iterable<UUID> ids) {
|
|
|
|
Map<UUID, User> result = new HashMap<>();
|
|
|
|
Set<UUID> uuidsToSearch = new HashSet<>();
|
|
|
|
|
|
|
|
for (UUID id : ids) {
|
|
|
|
result.put(id, null);
|
|
|
|
|
|
|
|
if (UUIDUtils.isAcceptableUUID(id)) {
|
|
|
|
uuidsToSearch.add(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
APIv3.getDatastore().createQuery(User.class).field("id").in(uuidsToSearch).forEach((user) -> {
|
|
|
|
result.put(user.getId(), user);
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-01 02:08:58 +02:00
|
|
|
public static User byEmailToken(String name) {
|
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("emailToken").equal(name).get();
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
2016-05-01 02:08:58 +02:00
|
|
|
public static User byLastUsername(String lastUsername) {
|
|
|
|
return APIv3.getDatastore().createQuery(User.class).field("lastUsername").equal(lastUsername).get();
|
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 hasPermissionScoped(String permission, ServerGroup scope) {
|
2016-05-05 22:00:32 +02:00
|
|
|
Rank highestRank = getHighestRank(scope);
|
|
|
|
Map<String, Boolean> scopedPermissions = PermissionUtils.mergePermissions(
|
|
|
|
PermissionUtils.getDefaultPermissions(highestRank),
|
|
|
|
scope.calculatePermissions(highestRank)
|
|
|
|
);
|
|
|
|
|
|
|
|
return scopedPermissions.containsKey(permission) && scopedPermissions.get(permission);
|
2016-04-17 21:23:02 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 02:08:58 +02:00
|
|
|
public boolean hasPermissionAnywhere(String permission) {
|
2016-05-05 22:00:32 +02:00
|
|
|
Map<String, Boolean> globalPermissions = PermissionUtils.getDefaultPermissions(getHighestRank());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public List<Grant> getGrants() {
|
2016-05-15 05:58:37 +02:00
|
|
|
return APIv3.getDatastore().createQuery(Grant.class).field("user").equal(id).asList();
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<IPLogEntry> getIPLog() {
|
|
|
|
return APIv3.getDatastore().createQuery(IPLogEntry.class).field("user").equal(id).asList();
|
|
|
|
}
|
|
|
|
|
2016-05-01 02:08:58 +02:00
|
|
|
public IPLogEntry getIPLogEntry(String ip) {
|
2016-05-15 05:58:37 +02:00
|
|
|
IPLogEntry existing = APIv3.getDatastore().createQuery(IPLogEntry.class).field("user").equal(id).field("userIp").equal(ip).get();
|
2016-05-01 02:08:58 +02:00
|
|
|
|
|
|
|
if (existing == null) {
|
|
|
|
existing = new IPLogEntry(this, ip);
|
|
|
|
APIv3.getDatastore().save(existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
2016-04-27 02:46:34 +02:00
|
|
|
public List<Punishment> getPunishments() {
|
2016-05-15 05:58:37 +02:00
|
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("user").equal(id).asList();
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 08:44:10 +02:00
|
|
|
public List<Punishment> getPunishments(Iterable<Punishment.PunishmentType> types) {
|
2016-05-15 05:58:37 +02:00
|
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("user").equal(id).field("type").in(types).asList();
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public UserMetaEntry getMeta(ServerGroup group) {
|
2016-05-01 06:34:02 +02:00
|
|
|
return APIv3.getDatastore().createQuery(UserMetaEntry.class).field("user").equal(id).field("serverGroup").equal(group.getId()).get();
|
2016-04-27 02:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-28 06:29:35 +02:00
|
|
|
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();
|
2016-05-09 21:43:27 +02:00
|
|
|
|
|
|
|
if (!online) {
|
|
|
|
this.lastSeenAt = new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.online = true;
|
2016-05-28 06:29:35 +02:00
|
|
|
return true;
|
2016-05-09 21:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void leftServer() {
|
2016-05-01 02:08:58 +02:00
|
|
|
this.lastSeenAt = new Date();
|
2016-05-09 21:43:27 +02:00
|
|
|
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.byLastUsername(username)) != null) {
|
|
|
|
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-28 06:29:35 +02:00
|
|
|
APIv3.getDatastore().save(this);
|
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
|
|
|
}
|
|
|
|
|
2016-05-05 22:00:32 +02:00
|
|
|
public Rank getHighestRank() {
|
|
|
|
return getHighestRank(null);
|
|
|
|
}
|
|
|
|
|
2016-04-30 20:03:34 +02:00
|
|
|
public Rank getHighestRank(ServerGroup serverGroup) {
|
2016-05-29 08:44:10 +02:00
|
|
|
return getHighestRank(serverGroup, getGrants());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is only used to help batch requests to mongo
|
|
|
|
public Rank getHighestRank(ServerGroup serverGroup, Iterable<Grant> grants) {
|
2016-05-09 05:18:55 +02:00
|
|
|
Rank highest = null;
|
2016-04-30 20:03:34 +02:00
|
|
|
|
2016-05-29 08:44:10 +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.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-05-05 22:00:32 +02:00
|
|
|
public Map<ServerGroup, Rank> getHighestRanks() {
|
|
|
|
Map<ServerGroup, Rank> highestRanks = new HashMap<>();
|
|
|
|
Rank defaultRank = Rank.byId("default");
|
|
|
|
List<Grant> userGrants = getGrants();
|
|
|
|
|
|
|
|
for (ServerGroup serverGroup : ServerGroup.values()) {
|
|
|
|
Rank highest = defaultRank;
|
|
|
|
|
|
|
|
for (Grant grant : userGrants) {
|
|
|
|
if (!grant.isActive() || !grant.appliesOn(serverGroup)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rank rank = Rank.byId(grant.getRank());
|
|
|
|
|
|
|
|
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) {
|
2016-05-29 08:44:10 +02:00
|
|
|
return getLoginInfo(
|
|
|
|
server,
|
|
|
|
getPunishments(ImmutableSet.of(
|
|
|
|
Punishment.PunishmentType.BLACKLIST,
|
|
|
|
Punishment.PunishmentType.BAN,
|
|
|
|
Punishment.PunishmentType.MUTE
|
|
|
|
)),
|
|
|
|
getGrants()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is only used to help batch requests to mongo
|
|
|
|
public Map<String, Object> getLoginInfo(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
|
|
|
|
2016-05-29 08:44:10 +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
|
|
|
|
2016-05-29 08:44:10 +02:00
|
|
|
Rank highestRank = getHighestRank(ServerGroup.byId(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
|
|
|
}
|
|
|
|
|
2016-02-12 02:40:06 +01:00
|
|
|
}
|