157 lines
5.3 KiB
Java
157 lines
5.3 KiB
Java
package net.frozenorb.apiv3.models;
|
|
|
|
import lombok.Getter;
|
|
import lombok.Setter;
|
|
import net.frozenorb.apiv3.APIv3;
|
|
import net.frozenorb.apiv3.serialization.ExcludeFromReplies;
|
|
import org.bson.Document;
|
|
import org.mindrot.jbcrypt.BCrypt;
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
import org.mongodb.morphia.annotations.Id;
|
|
|
|
import java.util.*;
|
|
|
|
@Entity(value = "users", noClassnameStored = true)
|
|
public final class User {
|
|
|
|
@Getter @Id private UUID id;
|
|
@Getter private String lastName;
|
|
@Getter @ExcludeFromReplies private Map<String, Date> aliases;
|
|
@Getter @ExcludeFromReplies private String otpCode;
|
|
@Getter @ExcludeFromReplies @Setter private String emailToken;
|
|
@Getter @ExcludeFromReplies @Setter private Date emailTokenSet;
|
|
@Getter @ExcludeFromReplies private String password;
|
|
@Getter @Setter private String email;
|
|
@Getter private int phoneNumber;
|
|
@Getter private String lastSeenOn;
|
|
@Getter private Date lastSeenAt;
|
|
@Getter private Date firstSeen;
|
|
|
|
public static User byId(String id) {
|
|
try {
|
|
return byId(UUID.fromString(id));
|
|
} catch (Exception ex) {
|
|
throw new IllegalArgumentException("Invalid UUID string " + id, ex);
|
|
}
|
|
}
|
|
|
|
public static User byId(UUID id) {
|
|
return APIv3.getDatastore().createQuery(User.class).field("id").equal(id).get();
|
|
}
|
|
|
|
// TODO: FIND ALL USAGES OF THIS AND
|
|
// SEE HOW MANY OF THEM (EX NON GET REQUESTS)
|
|
// WE CAN DROP
|
|
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) {
|
|
return APIv3.getDatastore().createQuery(User.class).field("lastName").equalIgnoreCase(name).get();
|
|
}
|
|
|
|
public static User byEmailToken(String name) {
|
|
return APIv3.getDatastore().createQuery(User.class).field("emailToken").equal(name).get();
|
|
}
|
|
|
|
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;
|
|
this.phoneNumber = -1;
|
|
this.lastSeenOn = null;
|
|
this.lastSeenAt = new Date();
|
|
this.firstSeen = new Date();
|
|
|
|
aliases.put(lastName, new Date());
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public List<Grant> getGrants() {
|
|
return APIv3.getDatastore().createQuery(Grant.class).field("target").equal(id).asList();
|
|
}
|
|
|
|
public List<Grant> getGrants(ServerGroup scope) {
|
|
return APIv3.getDatastore().createQuery(Grant.class).field("target").equal(id).field("scopes").equalIgnoreCase(scope.getId()).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();
|
|
}
|
|
|
|
public List<Punishment> getPunishments(Collection<Punishment.PunishmentType> types) {
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("target").equal(id).field("type").in(types).asList();
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public Map<String, Boolean> calculateGlobalPermissions() {
|
|
return null;
|
|
}
|
|
|
|
public Map<String, Boolean> calculatePermissions(ServerGroup serverGroup) {
|
|
/*ServerGroup scope = ServerGroup.byId("HCTeams");
|
|
Map<ServerGroup, Rank> userRanks = new HashMap<>();
|
|
|
|
|
|
//ServerGroup global = ServerGroup.byId("Global");
|
|
Map<String, Boolean> calculatedPermissions = global.getPermissions(userRanks.get(global));
|
|
|
|
// for global calculations
|
|
|
|
for (ServerGroup serverGroup : ServerGroup.values()) {
|
|
mergePermissions(serverGroup, userRanks.get(serverGroup), calculatedPermissions);
|
|
}
|
|
|
|
// for scoped calculations
|
|
|
|
mergePermissions(scope, userRanks.get(scope), calculatedPermissions);*/
|
|
return null;
|
|
}
|
|
|
|
} |