Skip to content

Commit

Permalink
merge from dev-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemanja Tisma committed Nov 14, 2018
2 parents f14a9b1 + 5d6537f commit 7128d8a
Show file tree
Hide file tree
Showing 127 changed files with 2,983 additions and 1,797 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ local.properties

# Generated files
.idea/**/contentModel.xml
.idea/**/codeStyleConfig.xml

# Sensitive or high-churn files
.idea/**/dataSources/
Expand Down
16 changes: 9 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ language: java
sudo: false
install: true
jdk:
- oraclejdk10
- openjdk10
before_install:
- echo arangodb3 arangodb/password password marugotoDbPw | sudo debconf-set-selections
- echo arangodb3 arangodb/password_again password marugotoDbPw | sudo debconf-set-selections
- curl -s -L https://raw.githubusercontent.com/brennv/arangodb-travis/master/setup_arangodb.sh
-o setup_arangodb.sh
- chmod +x setup_arangodb.sh
- "./setup_arangodb.sh"
- chmod a+x setup_arangodb.sh
- ./setup_arangodb.sh
script:
- "./mvnw install -DskipTests=true -Dmaven.javadoc.skip=true -B -V"
- "./mvnw test -B"
Expand All @@ -23,3 +19,9 @@ deploy:
file: target/file.war
on:
repo: uzh/marugoto
notifications:
slack:
rooms:
- secure: nPX5zHvsb84p85bQlB4yeiP28JMUd6Ku46XHONuL97LHU3gfcyC76AjEJA5roih0M0QnmDraaG2f4ZEAHK+1DJUhiCuY+fnerbQics3oBtkDBJU0WP8YbbiEuKOM+awhCONdqIqgzAKDga6Q1+aTqCsFU/q5Hnug6BUQ7psnE6nzjmNA0ILdj+3/1zWl7xmagiienKm3eTzyxCyI0ZcBYvhQV8pZAEwEzQdysmjLZeqnmhRKAmzSEnz+ZjTsUJyBSBP8WXqKhcQ/fh2gwSGrRz2Vggrq1sA/PUV7ZkmSSZdrq+fFzwL+/R+2u6IOO9fr5JkZjOfINNdk4bM4mkhnnjKzoWDybKJ5gxVlBiUpxrEz487E4IdOb1GPdf4sC3foFihCAjUaIIuWzxpVWGOb3FQjpdg3pQ2rvaXqJXXIgccNIOiYj2q6RP4fHpysgIbIS1ktBuIRzivXp6/qLbbTj+kzVQ9q4QrdnQbr0IHzWMS6W3wsNi5gwDSB+Rioxrm1Ws86wwUDAzJ1wccOxjg5me5DdhQmBdkTFtvcXADy+N+8YWHGgvxO0q8KL8dJEcGjVWCMCmbUspmLNQvzhWB8xvhKwYVCycW5NxxBiIZtZ50adKwR8NgW39E5v2QZS+9z6rNSTL/8OJ69tV6NqhBABy8yWsOCI0sMz5jzo4bZsEg=
on_success: never
on_failure: always
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,27 @@ public void run(ApplicationArguments applicationArguments) {
}
//check if every collection is added
operations.collection("chapter");
operations.collection("storyline");
operations.collection("storylineState");
operations.collection("component");
operations.collection("exerciseState");
operations.collection("notebookEntry");
operations.collection("page");
operations.collection("pageState");
operations.collection("exerciseState");
operations.collection("pageTransition");
operations.collection("user");
operations.collection("personalNote");
operations.collection("storyline");
operations.collection("storylineState");
operations.collection("textComponent");
operations.collection("textExercise");
operations.collection("personalNote");
operations.collection("notebookEntry");

operations.collection("topic");
operations.collection("user");
logger.info("------------------------------------------------");
}

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.naming.AuthenticationException;

import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -16,40 +17,43 @@

import ch.uzh.marugoto.backend.resource.AuthToken;
import ch.uzh.marugoto.backend.resource.AuthUser;
import ch.uzh.marugoto.backend.security.Constants;
import ch.uzh.marugoto.backend.security.JwtTokenUtil;
import ch.uzh.marugoto.backend.security.JwtTokenProvider;
import ch.uzh.marugoto.core.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
public class AuthenticationController extends BaseController {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private JwtTokenUtil jwtTokenUtil;

private JwtTokenProvider jwtTokenProvider;
@Autowired
private UserService userService;

@ApiOperation(value = "Generates you an access token regarding the login credentials.")
@RequestMapping(value = "auth/generate-token", method = RequestMethod.POST)
public AuthToken register(@RequestBody AuthUser loginUser) throws org.springframework.security.core.AuthenticationException, AuthenticationException {
var authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(loginUser.getMail(), loginUser.getPassword()));

SecurityContextHolder.getContext().setAuthentication(authentication);

var user = userService.loadUserByUsername(loginUser.getMail());
var token = jwtTokenUtil.generateToken(user);
userService.updateLastLoginAt(authenticationFacade.getAuthenticatedUser());

return new AuthToken(Constants.TOKEN_PREFIX + " " + token);

var token = jwtTokenProvider.generateToken(authentication);
var refreshToken = jwtTokenProvider.generateRefreshToken(authentication);
userService.updateLastLoginAt(getAuthenticatedUser());

log.info("Token generated: " + jwtTokenProvider.getUserFromToken(token).getUsername() + " [" + LocalDateTime.now() + "]");

return new AuthToken(token, refreshToken);
}

@ApiOperation(value = "Returns authenticated user", authorizations = { @Authorization(value = "apiKey") })
@RequestMapping(value = "auth/validate", method = RequestMethod.GET)
public Object validate() throws javax.naming.AuthenticationException {
var user = authenticationFacade.getAuthenticatedUser();
var user = getAuthenticatedUser();

var res = new HashMap<String, Object>();
res.put("mail", user.getMail());
Expand All @@ -58,4 +62,16 @@ public Object validate() throws javax.naming.AuthenticationException {

return res;
}

@ApiOperation(value = "Get new tokens if the token is access token is expired", authorizations = { @Authorization(value = "apiKey") })
@RequestMapping(value = "auth/refresh-token", method = RequestMethod.GET)
public AuthToken refreshToken() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
var token = jwtTokenProvider.generateToken(authentication);
var refreshToken = jwtTokenProvider.generateRefreshToken(authentication);

log.info("Token refreshed: " + jwtTokenProvider.getUserFromToken(token).getUsername() + " [" + LocalDateTime.now() + "]");

return new AuthToken(token, refreshToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import ch.uzh.marugoto.backend.security.IAuthenticationFacade;
import ch.uzh.marugoto.backend.security.AuthenticationFacade;
import ch.uzh.marugoto.core.data.entity.User;

/**
Expand All @@ -21,10 +21,10 @@
@RequestMapping("api")
public abstract class BaseController {

protected final Logger Log = LogManager.getLogger(this.getClass());
protected final Logger log = LogManager.getLogger(this.getClass());

@Autowired
protected IAuthenticationFacade authenticationFacade;
private AuthenticationFacade authenticationFacade;


protected User getAuthenticatedUser() throws AuthenticationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void throwWithInnerException() throws Exception {
@GetMapping("dev/date")
public Map<String, Object> returnDate() throws ParseException {
var result = new HashMap<String, Object>();
result.put("date", new SimpleDateFormat("yyyy/MM/dd").parse("2000/01/01"));
result.put("date", new SimpleDateFormat("yyyy/MM/dd").parse("2000/01/01").toString());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package ch.uzh.marugoto.backend.controller;

import java.util.Map;

import javax.naming.AuthenticationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -12,9 +8,15 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.naming.AuthenticationException;

import ch.uzh.marugoto.core.data.entity.Page;
import ch.uzh.marugoto.core.data.entity.TransitionChosenOptions;
import ch.uzh.marugoto.core.data.entity.User;
import ch.uzh.marugoto.core.exception.PageTransitionNotAllowedException;
import ch.uzh.marugoto.core.service.PageService;
import ch.uzh.marugoto.core.service.StateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
Expand All @@ -25,27 +27,35 @@
*/
@RestController
public class PageController extends BaseController {
@Autowired
private PageService pageService;


@Autowired
private StateService stateService;

@ApiOperation(value = "Load page by ID.", authorizations = { @Authorization(value = "apiKey") })
@GetMapping("pages/page/{id}")
public Map<String, Object> getPage(@ApiParam("ID of page") @PathVariable String id) throws AuthenticationException {
Page page = pageService.getPage("page/" + id, getAuthenticatedUser());
var response = stateService.getAllStates(page, getAuthenticatedUser());
@ApiOperation(value = "Load current page.", authorizations = { @Authorization(value = "apiKey") })
@GetMapping("pages/current")
public HashMap<String, Object> getPage() throws AuthenticationException {
User authenticatedUser = getAuthenticatedUser();

//open first page from module, if there is no pageState
if (authenticatedUser.getCurrentPageState() == null) {
stateService.startModule(authenticatedUser);
}

var response = stateService.getStates(authenticatedUser);
Page page = authenticatedUser.getCurrentPageState().getPage();
response.put("page", page);
return response;
}

@ApiOperation(value = "Triggers page transition and state updates.", authorizations = { @Authorization(value = "apiKey") })
@ApiOperation(value = "Handles a pagetransition from the current page to another page.", authorizations = { @Authorization(value = "apiKey") })
@RequestMapping(value = "pageTransitions/doPageTransition/pageTransition/{pageTransitionId}", method = RequestMethod.POST)
public Map<String, Object> doPageTransition(@ApiParam("ID of page transition") @PathVariable String pageTransitionId,
public Map<String, Object> doPageTransition(@ApiParam("ID of page updateStatesAfterTransition") @PathVariable String pageTransitionId,
@ApiParam("Is chosen by player ") @RequestParam("chosenByPlayer") boolean chosenByPlayer) throws AuthenticationException, PageTransitionNotAllowedException {
Page nextPage = pageService.doTransition(chosenByPlayer, "pageTransition/" + pageTransitionId, getAuthenticatedUser());
var response = stateService.getAllStates(nextPage, getAuthenticatedUser());
User user = getAuthenticatedUser();
TransitionChosenOptions chosenBy = chosenByPlayer ? TransitionChosenOptions.player : TransitionChosenOptions.autoTransition;
Page nextPage = stateService.doPageTransition(chosenBy, "pageTransition/" + pageTransitionId, user);

var response = stateService.getStates(user);
response.put("page", nextPage);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

package ch.uzh.marugoto.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import ch.uzh.marugoto.core.data.entity.PageState;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.naming.AuthenticationException;

import ch.uzh.marugoto.core.data.entity.ExerciseState;
import ch.uzh.marugoto.core.service.ComponentService;
import ch.uzh.marugoto.core.data.entity.User;
import ch.uzh.marugoto.core.service.ExerciseStateService;
import ch.uzh.marugoto.core.service.PageTransitionStateService;
import ch.uzh.marugoto.core.service.StateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
Expand All @@ -22,33 +28,28 @@
@RestController
public class StateController extends BaseController {

@Autowired
private PageTransitionStateService pageTransitionStateService;
@Autowired
private StateService stateService;

@Autowired
private ComponentService componentService;

private ExerciseStateService exerciseStateService;

@ApiOperation(value = "Returns all state objects", authorizations = { @Authorization(value = "apiKey") })
@GetMapping("states")
public Map<String, Object> getPageStates() throws Exception {
PageState pageState = getAuthenticatedUser().getCurrentPageState();
if (pageState == null) {
throw new Exception("No existing states for the user");
}
return stateService.getAllStates(pageState.getPage(), getAuthenticatedUser());
public Map<String, Object> getStatesForCurrentPage() throws Exception {
User authenticatedUser = getAuthenticatedUser();
return stateService.getStates(authenticatedUser);
}

@ApiOperation(value = "Updates exercise state in 'real time' and checks if exercise is correct", authorizations = {
@Authorization(value = "apiKey") })
@ApiOperation(value = "Updates exercise state in 'real time' and checks if exercise is correct", authorizations = { @Authorization(value = "apiKey") })
@RequestMapping(value = "states/exerciseState/{exerciseStateId}", method = RequestMethod.PUT)
public Map<String, Object> updateExerciseState(@ApiParam("ID of exercise state") @PathVariable String exerciseStateId,
@ApiParam("Input state from exercise") @RequestParam("inputState") String inputState) {
ExerciseState exerciseState = stateService.updateExerciseState("exerciseState/" + exerciseStateId, inputState);
boolean correct = componentService.isExerciseCorrect(exerciseState);
@ApiParam("Input state from exercise") @RequestParam("inputState") String inputState) throws AuthenticationException {
exerciseStateService.updateExerciseState("exerciseState/" + exerciseStateId, inputState);
boolean statesChanged = pageTransitionStateService.updatePageTransitionStatesAvailability(getAuthenticatedUser());
var objectMap = new HashMap<String, Object>();
objectMap.put("exerciseCorrect", correct);
objectMap.put("stateChanged", false);
objectMap.put("statesChanged", statesChanged);
return objectMap;
}
}
Loading

0 comments on commit 7128d8a

Please sign in to comment.