package net.frozenorb.apiv3.model; import lombok.Getter; import net.frozenorb.apiv3.APIv3; import org.bson.types.ObjectId; import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Id; import java.time.Instant; import java.util.Date; import java.util.Set; import java.util.UUID; @Entity(value = "grants", noClassnameStored = true) public final class Grant { @Id private ObjectId id; @Getter private UUID target; @Getter private String reason; @Getter private Set scopes; @Getter private String rank; @Getter private Date expiresAt; @Getter private UUID addedBy; @Getter private Date addedAt; @Getter private UUID removedBy; @Getter private Date removedAt; @Getter private String removalReason; public Grant() {} // For Morphia public Grant(UUID target, String reason, Set scopes, String rankId, Date expiresAt, UUID addedBy) { this.target = target; this.reason = reason; this.scopes = scopes; this.rank = rankId; this.expiresAt = expiresAt; this.addedBy = addedBy; this.addedAt = new Date(); } public void delete(UUID removedBy, String reason) { this.removedBy = removedBy; this.removedAt = new Date(); this.removalReason = reason; APIv3.getDatastore().save(this); } public boolean isActive() { return !(isExpired() || isRemoved()); } public boolean isExpired() { if (expiresAt == null) { return false; // Never expires } else { return expiresAt.after(new Date()); } } public boolean isRemoved() { return removedBy != null; } }