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-02-08 03:06:41 +01:00
|
|
|
import lombok.Getter;
|
2016-03-21 23:28:17 +01:00
|
|
|
import net.frozenorb.apiv3.APIv3;
|
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
|
|
import org.mongodb.morphia.annotations.Id;
|
2016-02-08 03:06:41 +01:00
|
|
|
|
2016-04-17 21:23:02 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
2016-02-08 02:52:03 +01:00
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Entity(value = "grants", noClassnameStored = true)
|
|
|
|
public final class Grant {
|
2016-02-08 02:51:02 +01:00
|
|
|
|
2016-04-17 21:23:02 +02:00
|
|
|
@Getter @Id private ObjectId id;
|
2016-02-12 01:10:46 +01:00
|
|
|
@Getter private UUID target;
|
|
|
|
@Getter private String reason;
|
|
|
|
@Getter private Set<String> scopes;
|
|
|
|
@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-03-21 23:28:17 +01: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-04-08 13:12:31 +02:00
|
|
|
public static Grant byId(String id) {
|
|
|
|
return APIv3.getDatastore().createQuery(Grant.class).field("id").equal(new ObjectId(id)).get();
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public Grant() {} // For Morphia
|
2016-02-12 06:31:57 +01:00
|
|
|
|
2016-04-08 13:12:31 +02:00
|
|
|
public Grant(User target, String reason, Set<ServerGroup> scopes, Rank rank, Date expiresAt, User addedBy) {
|
|
|
|
this.target = target.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-03-21 23:28:17 +01:00
|
|
|
this.expiresAt = expiresAt;
|
2016-04-08 13:12:31 +02:00
|
|
|
this.addedBy = addedBy.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.addedAt = new Date();
|
2016-02-12 06:31:57 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 13:12:31 +02:00
|
|
|
public void delete(User removedBy, String reason) {
|
|
|
|
this.removedBy = removedBy.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.removedAt = new Date();
|
2016-02-12 06:31:57 +01:00
|
|
|
this.removalReason = reason;
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
APIv3.getDatastore().save(this);
|
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-03-21 23:28:17 +01:00
|
|
|
return expiresAt.after(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-02-08 02:51:02 +01:00
|
|
|
}
|