APIv3/src/main/java/net/frozenorb/apiv3/models/Grant.java
Colin McDonald 04e4db8fcd More work
2016-05-05 16:00:32 -04:00

84 lines
2.4 KiB
Java

package net.frozenorb.apiv3.models;
import com.google.common.collect.Collections2;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Entity(value = "grants", noClassnameStored = true)
public final class Grant {
@Getter @Id private String id;
@Getter @Indexed private UUID target;
@Getter private String reason;
@Getter private Set<String> scopes;
@Getter @Indexed private String rank;
@Getter private Date expiresAt;
@Getter private UUID addedBy;
@Getter @Indexed private Date addedAt;
@Getter private UUID removedBy;
@Getter private Date removedAt;
@Getter private String removalReason;
public static Grant byId(String id) {
return APIv3.getDatastore().createQuery(Grant.class).field("id").equal(id).get();
}
public Grant() {} // For Morphia
public Grant(User target, String reason, Set<ServerGroup> scopes, Rank rank, Date expiresAt, User addedBy) {
this.id = new ObjectId().toString();
this.target = target.getId();
this.reason = reason;
this.scopes = new HashSet<>(Collections2.transform(scopes, ServerGroup::getId));
this.rank = rank.getId();
this.expiresAt = (Date) expiresAt.clone();
this.addedBy = addedBy.getId();
this.addedAt = new Date();
}
public void delete(User removedBy, String reason) {
this.removedBy = removedBy.getId();
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;
}
public boolean appliesOn(ServerGroup serverGroup) {
return isGlobal() || scopes.contains(serverGroup.getId());
}
public boolean isGlobal() {
return scopes.isEmpty();
}
}