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

147 lines
5.0 KiB
Java

package net.frozenorb.apiv3.model;
import com.google.common.collect.Collections2;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.result.UpdateResult;
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
@Entity
@AllArgsConstructor
public final class Grant {
private static final MongoCollection<Grant> grantsCollection = APIv3.getDatabase().getCollection("grants", Grant.class);
@Getter @Id private String id;
@Getter private UUID user;
@Getter private String reason;
@Getter private Set<String> scopes;
@Getter private String rank;
@Getter private Instant expiresAt;
@Getter private UUID addedBy;
@Getter private Instant addedAt;
@Getter private UUID removedBy;
@Getter private Instant removedAt;
@Getter private String removalReason;
public static List<Grant> findAllSync() {
return SyncUtils.blockMulti(grantsCollection.find().sort(new Document("addedAt", -1)));
}
public static List<Grant> findByRankSync(Collection<Rank> ranks) {
Collection<String> convertedRanks = ranks.stream().map(Rank::getId).collect(Collectors.toList());
return SyncUtils.blockMulti(grantsCollection.find(new Document("rank", new Document("$in", convertedRanks))));
}
public static Grant findByIdSync(String id) {
return SyncUtils.blockOne(grantsCollection.find(new Document("_id", id)));
}
public static List<Grant> findByUserSync(User user) {
return findByUserSync(user.getId());
}
public static List<Grant> findByUserSync(UUID user) {
return SyncUtils.blockMulti(grantsCollection.find(new Document("user", user)));
}
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find().sort(new Document("addedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
}
public static void findById(String id, SingleResultCallback<Grant> callback) {
grantsCollection.find(new Document("_id", id)).first(callback);
}
public static void findByUser(User user, SingleResultCallback<List<Grant>> callback) {
findByUser(user.getId(), callback);
}
public static void findByUser(UUID user, SingleResultCallback<List<Grant>> callback) {
grantsCollection.find(new Document("user", user)).into(new ArrayList<>(), callback);
}
public static void findByUserGrouped(Iterable<UUID> users, SingleResultCallback<Map<UUID, List<Grant>>> callback) {
grantsCollection.find(new Document("user", new Document("$in", users))).into(new ArrayList<>(), (grants, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {
Map<UUID, List<Grant>> result = new HashMap<>();
for (UUID user : users) {
result.put(user, new ArrayList<>());
}
for (Grant grant : grants) {
result.get(grant.getUser()).add(grant);
}
callback.onResult(result, null);
}
});
}
public Grant() {} // For Jackson
public Grant(User user, String reason, Set<ServerGroup> scopes, Rank rank, Instant expiresAt, User addedBy) {
this.id = new ObjectId().toString();
this.user = user.getId();
this.reason = reason;
this.scopes = new HashSet<>(Collections2.transform(scopes, ServerGroup::getId));
this.rank = rank.getId();
this.expiresAt = expiresAt;
this.addedBy = addedBy == null ? null : addedBy.getId();
this.addedAt = Instant.now();
}
public boolean isActive() {
return !(isExpired() || isRemoved());
}
public boolean isExpired() {
return expiresAt != null && expiresAt.isBefore(Instant.now());
}
public boolean isRemoved() {
return removedBy != null;
}
public boolean appliesOn(ServerGroup serverGroup) {
return isGlobal() || scopes.contains(serverGroup.getId());
}
public boolean isGlobal() {
return scopes.isEmpty();
}
public void insert() {
BlockingCallback<Void> callback = new BlockingCallback<>();
grantsCollection.insertOne(this, callback);
callback.get();
}
public void delete(User removedBy, String reason) {
this.removedBy = removedBy.getId();
this.removedAt = Instant.now();
this.removalReason = reason;
BlockingCallback<UpdateResult> callback = new BlockingCallback<>();
grantsCollection.replaceOne(new Document("_id", id), this, callback);
callback.get();
}
}