Skip to content

Commit

Permalink
Upgrade User Management
Browse files Browse the repository at this point in the history
  • Loading branch information
vknaisl committed Oct 17, 2019
1 parent a5fb75e commit 1e2ef72
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 130 deletions.
6 changes: 3 additions & 3 deletions src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.springframework.core.env.Profiles.of;

import nl.dtls.adminpanel.database.DevelopmentDummyDataLoader;
import nl.dtls.adminpanel.database.fixtures.DummyDataLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
Expand All @@ -14,7 +14,7 @@
public class AdminPanelApplication implements ApplicationRunner {

@Autowired
private DevelopmentDummyDataLoader developmentDummyDataLoader;
private DummyDataLoader dummyDataLoader;

@Autowired
private Environment environment;
Expand All @@ -30,7 +30,7 @@ public static void main(String[] args) {
@Override
public void run(ApplicationArguments args) {
if (environment.acceptsProfiles(of(Profiles.DEVELOPMENT))) {
developmentDummyDataLoader.load();
dummyDataLoader.load();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package nl.dtls.adminpanel.api.controller.exception;

import javax.servlet.http.HttpServletResponse;
import nl.dtls.adminpanel.api.dto.error.ErrorDTO;
import nl.dtls.adminpanel.entity.exception.ResourceNotFoundException;
import nl.dtls.adminpanel.entity.exception.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class ExceptionControllerAdvice {

private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionControllerAdvice.class);

@ExceptionHandler({ValidationException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorDTO handleBadRequest(Exception e, HttpServletResponse response) {
LOGGER.error(e.getMessage());
return new ErrorDTO(HttpStatus.BAD_REQUEST, e.getMessage());
}

@ExceptionHandler({BadCredentialsException.class, CredentialsExpiredException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody
public ErrorDTO handleUnauthorized(Exception e, HttpServletResponse response) {
LOGGER.error(e.getMessage());
return new ErrorDTO(HttpStatus.UNAUTHORIZED, e.getMessage());
}

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public ErrorDTO handleResourceNotFound(ResourceNotFoundException e,
HttpServletResponse response) {
LOGGER.error(e.getMessage());
return new ErrorDTO(HttpStatus.NOT_FOUND, e.getMessage());
}

}
51 changes: 51 additions & 0 deletions src/main/java/nl/dtls/adminpanel/api/dto/error/ErrorDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package nl.dtls.adminpanel.api.dto.error;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.http.HttpStatus;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class ErrorDTO {

@JsonProperty
private long timestamp;

@JsonProperty
private int status;

@JsonProperty
private String error;

@JsonProperty()
@JsonInclude(value = JsonInclude.Include.NON_NULL)
private Map<String, ?> errors;

@JsonProperty
private String message;

@JsonProperty
private String path;

public ErrorDTO(HttpStatus status, String message, Map<String, ?> errors) {
this.timestamp = new Date().getTime();
this.status = status.value();
this.error = status.getReasonPhrase();
this.message = message;
this.errors = errors;
}

public ErrorDTO(HttpStatus status, String message) {
this(status, message, null);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package nl.dtls.adminpanel.database.fixtures;

import nl.dtls.adminpanel.database.fixtures.data.ApplicationFixtures;
import nl.dtls.adminpanel.database.fixtures.data.InstanceFixtures;
import nl.dtls.adminpanel.database.fixtures.data.ServerFixtures;
import nl.dtls.adminpanel.database.fixtures.data.UserFixtures;
import nl.dtls.adminpanel.database.repository.ApplicationRepository;
import nl.dtls.adminpanel.database.repository.InstanceRepository;
import nl.dtls.adminpanel.database.repository.ServerRepository;
import nl.dtls.adminpanel.database.repository.UserRepository;
import nl.dtls.adminpanel.entity.Application;
import nl.dtls.adminpanel.entity.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class DummyDataLoader {

@Autowired
private UserRepository userRepository;

@Autowired
private UserFixtures userFixtures;

@Autowired
private ServerRepository serverRepository;

@Autowired
private ServerFixtures serverFixtures;

@Autowired
private InstanceRepository instanceRepository;

@Autowired
private InstanceFixtures instanceFixtures;

@Autowired
private ApplicationRepository applicationRepository;

@Autowired
private ApplicationFixtures applicationFixtures;

@Autowired
private PasswordEncoder passwordEncoder;

public void load() {
userRepository.deleteAll();
userRepository.save(userFixtures.albert());
userRepository.save(userFixtures.nikola());

applicationRepository.deleteAll();
Application fdpApplication = applicationFixtures.fdpApplication();
applicationRepository.save(fdpApplication);

serverRepository.deleteAll();
Server fdpServer = serverFixtures.fdpServer();
serverRepository.save(fdpServer);

instanceRepository.deleteAll();
instanceRepository.save(instanceFixtures.stagingFdpInstance(fdpApplication, fdpServer));
}

}
Original file line number Diff line number Diff line change
@@ -1,81 +1,12 @@
package nl.dtls.adminpanel.database;
package nl.dtls.adminpanel.database.fixtures.data;

import java.util.ArrayList;
import java.util.HashMap;
import nl.dtls.adminpanel.database.repository.ApplicationRepository;
import nl.dtls.adminpanel.database.repository.InstanceRepository;
import nl.dtls.adminpanel.database.repository.ServerRepository;
import nl.dtls.adminpanel.database.repository.UserRepository;
import nl.dtls.adminpanel.entity.Application;
import nl.dtls.adminpanel.entity.Instance;
import nl.dtls.adminpanel.entity.Server;
import nl.dtls.adminpanel.entity.Template;
import nl.dtls.adminpanel.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class DevelopmentDummyDataLoader {

@Value("${dummy.server.username:}")
private String serverUsername;

@Value("${dummy.server.hostname:}")
private String serverHostname;

@Value("${dummy.server.privateKey:}")
private String serverPrivateKey;

@Value("${dummy.server.publicKey:}")
private String serverPublicKey;

@Value("${dummy.instance.url:}")
private String instanceUrl;

@Value("${dummy.instance.jwtSecret:}")
private String instanceJwtSecret;

@Autowired
private UserRepository userRepository;

@Autowired
private ServerRepository serverRepository;

@Autowired
private InstanceRepository instanceRepository;

@Autowired
private ApplicationRepository applicationRepository;

@Autowired
private PasswordEncoder passwordEncoder;

public void load() {
userRepository.deleteAll();
userRepository.save(admin());

applicationRepository.deleteAll();
Application fdpApplication = fdpApplication();
applicationRepository.save(fdpApplication);

serverRepository.deleteAll();
Server fdpServer = fdpServer();
serverRepository.save(fdpServer);

instanceRepository.deleteAll();
instanceRepository.save(stagingFdpInstance(fdpApplication, fdpServer));
}

public User admin() {
return new User(
"7e64818d-6276-46fb-8bb1-732e6e09f7e9",
"Admin",
"[email protected]",
passwordEncoder.encode("password")
);
}
public class ApplicationFixtures {

public Application fdpApplication() {
return new Application(
Expand Down Expand Up @@ -183,60 +114,4 @@ public Application fdpApplication() {
}});
}

public Server fdpServer() {
return new Server(
"166c48ba-64d9-473a-8b59-7c1fdbb901f0",
"FDP Server",
serverUsername,
serverHostname,
serverPrivateKey,
serverPublicKey);
}

public Instance stagingFdpInstance(Application fdpApplication,
Server fdpServer) {
return new Instance(
"6f29daa8-1c43-49c3-9d1b-dc422e333d1e",
"FDP Staging Instance",
instanceUrl,
"/tmp/fdp-test",
new HashMap<>() {{
put("server_image", "fairdata/fairdatapoint:develop");
put("server_port", "81");
put("jwt_secret", instanceJwtSecret);
put("repository_type", 1);
put("repository_agraph_url", "http://localhost:10035/repositories/fdp");
put("repository_agraph_username", "user");
put("repository_agraph_password", "password");
put("repository_graphDb_url", "http://localhost:7200");
put("repository_graphDb_repository", "test");
put("repository_blazegraph_url", "http://localhost:8079/blazegraph");
put("repository_blazegraph_repository", "fdp");
put("repository_native_dir", "/tmp/fdp-store/");
put("metadata_rootSpecs",
"https://www.purl.org/fairtools/fdp/schema/0.1/fdpMetadata");
put("metadata_catalogSpecs",
"https://www.purl.org/fairtools/fdp/schema/0.1/catalogMetadata");
put("metadata_datasetSpecs",
"https://www.purl.org/fairtools/fdp/schema/0.1/datasetMetadata");
put("metadata_distributionSpecs",
"https://www.purl.org/fairtools/fdp/schema/0.1/distributionMetadata");
put("metadata_publisherURI", "http://localhost");
put("metadata_publisherName", "localhost");
put("metadata_language", "http://id.loc.gov/vocabulary/iso639-1/en");
put("metadata_license", "http://rdflicense.appspot.com/rdflicense/cc-by-nc-nd3.0");
put("metadata_accessRightsDescription", "This resource has no access restriction");
put("fairSearch_fdpSubmitUrl", "http://localhost:8080/fse/submitFdp");
put("pidSystem_type", 1);
put("pidSystem_purl_baseUrl", "http://purl.org/YOUR-PURL-DOMAIN/fdp");
put("scss_customizations", "$color-primary: red");
put("scss_extra", "body { background: pink; }");
}},
new HashMap<>() {{
// put("logo.png", new ObjectId("5d9dcdea7c889a377583cfef"));
}},
fdpApplication,
fdpServer);
}

}
Loading

0 comments on commit 1e2ef72

Please sign in to comment.