2016-04-08 13:12:31 +02:00
|
|
|
package net.frozenorb.apiv3.models;
|
2016-02-08 02:51:02 +01:00
|
|
|
|
2016-04-08 13:12:31 +02:00
|
|
|
import com.google.common.collect.Collections2;
|
2016-06-10 04:39:23 +02:00
|
|
|
import com.mongodb.async.client.MongoCollection;
|
|
|
|
import com.mongodb.client.result.DeleteResult;
|
|
|
|
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-08 03:06:41 +01:00
|
|
|
import lombok.Getter;
|
2016-03-21 23:28:17 +01:00
|
|
|
import net.frozenorb.apiv3.APIv3;
|
2016-06-10 04:39:23 +02:00
|
|
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
|
|
|
import net.frozenorb.apiv3.utils.SyncUtils;
|
|
|
|
import org.bson.Document;
|
2016-03-21 23:28:17 +01:00
|
|
|
import org.bson.types.ObjectId;
|
2016-02-08 03:06:41 +01:00
|
|
|
|
2016-05-29 08:44:10 +02:00
|
|
|
import java.util.*;
|
2016-02-08 02:52:03 +01:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
@Entity
|
2016-05-14 05:23:46 +02:00
|
|
|
@AllArgsConstructor
|
2016-03-21 23:28:17 +01:00
|
|
|
public final class Grant {
|
2016-02-08 02:51:02 +01:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
private static final MongoCollection<Grant> grantsCollection = APIv3.getDatabase().getCollection("grants", Grant.class);
|
|
|
|
|
2016-05-02 23:35:05 +02:00
|
|
|
@Getter @Id private String id;
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private UUID user;
|
2016-02-12 01:10:46 +01:00
|
|
|
@Getter private String reason;
|
2016-05-06 01:35:45 +02:00
|
|
|
@Getter private Set<String> scopes = new HashSet<>(); // So on things w/o scopes we still load properly (Morphia drops empty sets)
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private String rank;
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date expiresAt;
|
2016-02-12 21:12:05 +01:00
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
@Getter private UUID addedBy;
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private Date addedAt;
|
2016-02-12 01:10:46 +01:00
|
|
|
|
|
|
|
@Getter private UUID removedBy;
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date removedAt;
|
2016-02-12 01:10:46 +01:00
|
|
|
@Getter private String removalReason;
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static List<Grant> findAll() {
|
|
|
|
return SyncUtils.blockMulti(grantsCollection.find());
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static List<Grant> findByRank(Iterable<Rank> ranks) {
|
|
|
|
return SyncUtils.blockMulti(grantsCollection.find(new Document("rank", new Document("$in", ranks))));
|
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static Grant findById(String id) {
|
|
|
|
return SyncUtils.blockOne(grantsCollection.find(new Document("_id", id)));
|
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static List<Grant> findByUser(User user) {
|
|
|
|
return findByUser(user.getId());
|
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public static List<Grant> findByUser(UUID user) {
|
|
|
|
return SyncUtils.blockMulti(grantsCollection.find(new Document("user", user)));
|
2016-05-29 08:44:10 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public Grant() {} // For Morphia
|
2016-02-12 06:31:57 +01:00
|
|
|
|
2016-05-10 18:30:21 +02:00
|
|
|
public Grant(User user, String reason, Set<ServerGroup> scopes, Rank rank, Date expiresAt, User addedBy) {
|
2016-05-02 23:35:05 +02:00
|
|
|
this.id = new ObjectId().toString();
|
2016-05-10 18:30:21 +02:00
|
|
|
this.user = user.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.reason = reason;
|
2016-04-08 13:12:31 +02:00
|
|
|
this.scopes = new HashSet<>(Collections2.transform(scopes, ServerGroup::getId));
|
|
|
|
this.rank = rank.getId();
|
2016-05-06 01:35:45 +02:00
|
|
|
this.expiresAt = expiresAt;
|
|
|
|
this.addedBy = addedBy == null ? null : addedBy.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.addedAt = new Date();
|
2016-02-12 06:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isActive() {
|
|
|
|
return !(isExpired() || isRemoved());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isExpired() {
|
|
|
|
if (expiresAt == null) {
|
|
|
|
return false; // Never expires
|
|
|
|
} else {
|
2016-05-06 01:35:45 +02:00
|
|
|
return expiresAt.before(new Date());
|
2016-02-12 06:31:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isRemoved() {
|
|
|
|
return removedBy != null;
|
2016-02-12 01:10:46 +01:00
|
|
|
}
|
2016-02-08 03:06:41 +01:00
|
|
|
|
2016-04-28 22:57:44 +02:00
|
|
|
public boolean appliesOn(ServerGroup serverGroup) {
|
2016-04-30 20:03:34 +02:00
|
|
|
return isGlobal() || scopes.contains(serverGroup.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isGlobal() {
|
|
|
|
return scopes.isEmpty();
|
2016-04-28 22:57:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
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 = new Date();
|
|
|
|
this.removalReason = reason;
|
|
|
|
|
|
|
|
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
|
|
|
|
grantsCollection.deleteOne(new Document("_id", id), callback);
|
|
|
|
callback.get();
|
|
|
|
}
|
|
|
|
|
2016-02-08 02:51:02 +01:00
|
|
|
}
|