Commit 17f11a8b authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

remove or mock morphia stuff

parent 5cca4c3f
package com.telephoners.krakyournet.ctf.auth;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.repositories.UsersRepository;
......@@ -19,12 +18,14 @@ public class UserAuthenticator implements Authenticator<BasicCredentials, User>
}
@Override
public Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException
public java.util.Optional<User> authenticate(BasicCredentials credentials) throws AuthenticationException
{
return null;
/*
User user = usersRepository.authenticateUser(credentials);
if (user != null) {
return Optional.of(user);
}
return Optional.absent();
return Optional.absent();*/
}
}
\ No newline at end of file
package com.telephoners.krakyournet.ctf.beans;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Flag {
private String value;
private String description;
......@@ -16,12 +14,12 @@ public class Flag {
this.points = points;
}
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
// @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
public String getValue() {
return value;
}
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
// @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
public void setValue(String value) {
this.value = value;
}
......
package com.telephoners.krakyournet.ctf.beans;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import org.bson.types.ObjectId;
import org.joda.time.Instant;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import static com.google.common.base.Preconditions.checkNotNull;
@Entity("solutions")
public class Solution
{
@Id
private ObjectId id;
private Task task;
private Team team;
private Flag flag;
......
......@@ -2,21 +2,13 @@ package com.telephoners.krakyournet.ctf.beans;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Reference;
import java.util.List;
@Entity("teams")
public class Team {
@Id
private ObjectId id;
private String name;
private String description;
@Reference
private List<User> members;
@JsonCreator
......@@ -50,29 +42,4 @@ public class Team {
public List<User> getMembers() {
return members;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Team team = (Team) o;
if (id != null ? !id.equals(team.id) : team.id != null) return false;
if (name != null ? !name.equals(team.name) : team.name != null) return false;
if (description != null ? !description.equals(team.description) : team.description != null) return false;
return members != null ? members.equals(team.members) : team.members == null;
}
@Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (members != null ? members.hashCode() : 0);
return result;
}
}
......@@ -2,16 +2,10 @@ package com.telephoners.krakyournet.ctf.beans;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import java.security.Principal;
@Entity("users")
public class User implements Principal {
@Id
private ObjectId id;
private String name;
private String password;
private String email;
......@@ -44,27 +38,6 @@ public class User implements Principal {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (name != null ? !name.equals(user.name) : user.name != null) return false;
if (password != null ? !password.equals(user.password) : user.password != null) return false;
return email != null ? email.equals(user.email) : user.email == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
public void setName(String name) {
this.name = name;
}
......
......@@ -4,20 +4,14 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.helpers.PublicProperty;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import javax.ws.rs.core.Response;
import java.net.URISyntaxException;
import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class Task
{
@Id
private ObjectId id;
@PublicProperty
private String name;
@PublicProperty
......@@ -78,33 +72,6 @@ public abstract class Task
this.description = description;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Task task = (Task) o;
if (level != task.level) return false;
if (id != null ? !id.equals(task.id) : task.id != null) return false;
if (name != null ? !name.equals(task.name) : task.name != null) return false;
if (description != null ? !description.equals(task.description) : task.description != null) return false;
return flags != null ? flags.equals(task.flags) : task.flags == null;
}
@Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + level;
result = 31 * result + (flags != null ? flags.hashCode() : 0);
return result;
}
@JsonIgnore
public abstract Response getTaskResponse() throws URISyntaxException;
}
......@@ -3,12 +3,10 @@ package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.beans.Flag;
import org.mongodb.morphia.annotations.Entity;
import javax.ws.rs.core.Response;
import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TextTask extends Task
{
......
......@@ -3,14 +3,12 @@ package com.telephoners.krakyournet.ctf.beans.tasks;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.telephoners.krakyournet.ctf.beans.Flag;
import org.mongodb.morphia.annotations.Entity;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
@Entity("tasks")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WebTask extends Task
{
......
......@@ -7,7 +7,6 @@ import com.telephoners.krakyournet.ctf.modules.ApplicationModule;
import io.dropwizard.cli.ConfiguredCommand;
import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.Namespace;
import org.mongodb.morphia.Datastore;
public class PurgeDatabaseCommand extends ConfiguredCommand<ApplicationConfiguration>
{
......@@ -23,7 +22,7 @@ public class PurgeDatabaseCommand extends ConfiguredCommand<ApplicationConfigura
protected void run(Bootstrap<ApplicationConfiguration> bootstrap, Namespace namespace, ApplicationConfiguration applicationConfiguration) throws Exception
{
Injector injector = Guice.createInjector(new ApplicationModule(applicationConfiguration));
Datastore datastore = injector.getInstance(Datastore.class);
datastore.getDB().dropDatabase();
// Datastore datastore = injector.getInstance(Datastore.class);
// datastore.getDB().dropDatabase();
}
}
package com.telephoners.krakyournet.ctf.repositories;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public abstract class Repository<T>
{
protected final Datastore datastore;
@Inject
public Repository(Datastore datastore)
public Repository()
{
this.datastore = datastore;
}
public void add(T item)
{
datastore.save(item);
// datastore.save(item);
}
public List<T> getAll()
{
return datastore.createQuery(getRepositoryType()).asList();
return null;
// return datastore.createQuery(getRepositoryType()).asList();
}
public void clean()
{
datastore.getCollection(getRepositoryType()).drop();
// datastore.getCollection(getRepositoryType()).drop();
}
Class getRepositoryType()
......
......@@ -5,7 +5,6 @@ import com.telephoners.krakyournet.ctf.beans.Solution;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.exceptions.SolutionAlreadySubmittedException;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import javax.inject.Singleton;
......@@ -20,15 +19,15 @@ public class SolutionsRepository extends Repository<Solution>
private final TasksRepository tasksRepository;
@Inject
public SolutionsRepository(TasksRepository tasksRepository,
Datastore datastore)
public SolutionsRepository(TasksRepository tasksRepository)
{
super(datastore);
this.tasksRepository = tasksRepository;
}
public Map<Integer, List<Flag>> getTeamSolutions(Team team)
{
return null;
/*
//todo: do not identify team by name? conflict in team ids?
return datastore.createQuery(Solution.class)
.filter("team.name", team.getName()).asList()
......@@ -47,17 +46,20 @@ public class SolutionsRepository extends Repository<Solution>
taskSolutions -> taskSolutions.getKey().getLevel(),
taskSolutions -> taskSolutions.getValue().stream()
.map(Solution::getFlag).collect(Collectors.toList())
));
)); */
}
private boolean isAlreadySubmittedSolution(Solution solution)
{
return false;
/*
List<Solution> solutions = datastore.find(Solution.class)
.filter("flag.value", solution.getFlag().getValue()) //todo: do not filter by field
.filter("team.name", solution.getTeam().getName()) //todo: whole object instead
.asList();
return !solutions
.isEmpty();
.isEmpty();*/
}
public List<Integer> getCompletedTasks(Team team)
......
package com.telephoners.krakyournet.ctf.repositories;
import com.sun.tools.javac.util.List;
import com.telephoners.krakyournet.ctf.beans.tasks.Task;
import com.telephoners.krakyournet.ctf.exceptions.TaskNotFoundException;
import com.telephoners.krakyournet.ctf.helpers.DBObjectUtils;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
@Singleton
public class TasksRepository extends Repository<Task>
{
private final Datastore datastore;
@Inject
public TasksRepository(Datastore datastore)
public TasksRepository()
{
super(datastore);
this.datastore = datastore;
}
public Task getByLevel(int level)
{
return null;
/*
return datastore.createQuery(Task.class)
.filter("level", level)
.asList()
.stream()
.findFirst()
.orElseThrow(TaskNotFoundException::new);
.orElseThrow(TaskNotFoundException::new);*/
}
public List<Task> getAllPublic()
{
return null;/*
return datastore.createQuery(Task.class)
.order("level")
.retrievedFields(true, DBObjectUtils.getPublicFields(Task.class))
.asList();
.asList();*/
}
}
\ No newline at end of file
......@@ -2,8 +2,6 @@ package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.exceptions.TeamNotFoundException;
import org.mongodb.morphia.Datastore;
import javax.inject.Inject;
import javax.inject.Singleton;
......@@ -11,25 +9,22 @@ import javax.inject.Singleton;
@Singleton
public class TeamsRepository extends Repository<Team>
{
private final Datastore datastore;
private final UsersRepository usersRepository;
@Inject
public TeamsRepository(Datastore datastore,
UsersRepository usersRepository)
public TeamsRepository(UsersRepository usersRepository)
{
super(datastore);
this.datastore = datastore;
this.usersRepository = usersRepository;
}
public Team getTeamByUser(User user)
{
return null;
/*
return datastore.createQuery(Team.class).asList().stream()
.filter(team -> team.getMembers().contains(user))
.findFirst()
.orElseThrow(TeamNotFoundException::new);
.orElseThrow(TeamNotFoundException::new);*/
}
public Team getTeamByUserName(String username)
......
......@@ -3,11 +3,8 @@ package com.telephoners.krakyournet.ctf.repositories;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.telephoners.krakyournet.ctf.beans.User;
import com.telephoners.krakyournet.ctf.exceptions.UserNotFoundException;
import com.telephoners.krakyournet.ctf.providers.MessageDigestProvider;
import io.dropwizard.auth.basic.BasicCredentials;
import org.apache.commons.codec.binary.Hex;
import org.mongodb.morphia.Datastore;
@Singleton
public class UsersRepository extends Repository<User>
......@@ -15,27 +12,30 @@ public class UsersRepository extends Repository<User>
private final MessageDigestProvider messageDigestProvider;
@Inject
public UsersRepository(Datastore datastore, MessageDigestProvider messageDigestProvider)
public UsersRepository(MessageDigestProvider messageDigestProvider)
{
super(datastore);
this.messageDigestProvider = messageDigestProvider;
}
public User getUserByName(String username)
{
return null;
/*
return datastore.createQuery(User.class)
.field("name").equal(username)
.asList()
.stream()
.findFirst()
.orElseThrow(UserNotFoundException::new);
.orElseThrow(UserNotFoundException::new);*/
}
public User authenticateUser(BasicCredentials basicCredentials)
{
return null;
/*
return datastore.createQuery(User.class)
.field("name").equal(basicCredentials.getUsername())
.field("password").equal(Hex.encodeHexString(messageDigestProvider.getMessageDigest().digest(basicCredentials.getPassword().getBytes())))
.get();
.get();*/
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment