diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 270aac2..3bdcfa7 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -20,12 +20,13 @@ public class UserService extends EventWatcher { } public String login(String authority) throws UserNotFoundException { - User user = this.repo.getByAuthority(authority); + User user = this.repo.getByAuthority(authority.replace("\n", "").trim()); if (user == null) { throw new UserNotFoundException("Can not find user with given authority"); } String token = UUID.randomUUID().toString(); user.setToken(token); + this.repo.save(user); return token; } @@ -34,7 +35,7 @@ public class UserService extends EventWatcher { } public User getUserByEmail(String email) { - return this.repo.getByAuthority(email); + return this.repo.getByAuthority(email.replace("\n", "").trim()); } } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java index e21bf45..7a764f6 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/UserServiceTest.java @@ -45,6 +45,7 @@ public class UserServiceTest { String token = this.userService.login(TEST_USER_MAIL); System.out.println("Returned token: " + token); assertTrue(token != null); + assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null); } catch (UserNotFoundException e) { e.printStackTrace(); assertTrue(false); diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 50d5f57..ca8b681 100644 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -24,13 +24,13 @@ public class App { SpringApplication.run(App.class, args); } - // @EventListener(ApplicationReadyEvent.class) - // public void importData() { - // User testUser = new User(); - // testUser.setEmail("noteexisitingmail@notexistingdomain.com"); - // testUser.setName("Tom"); - // testUser.setSurname("Kovalsky"); - // testUser.setRole(UserRoles.TEST_USER); - // this.userService.save(testUser); - // } + @EventListener(ApplicationReadyEvent.class) + public void importData() { + User testUser = new User(); + testUser.setEmail("filizy@st.amu.edu.pl"); + testUser.setName("Filip"); + testUser.setSurname("Izydorczyk"); + testUser.setRole(UserRoles.STUDENT); + this.userService.save(testUser); + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/TokenController.java b/restservice/src/main/java/com/plannaplan/controllers/TokenController.java index 09d4528..f6ef1d1 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/TokenController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/TokenController.java @@ -1,8 +1,11 @@ package com.plannaplan.controllers; +import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.security.CasValidationExcepiton; import com.plannaplan.security.CasValidator; +import com.plannaplan.services.UserService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; @@ -16,19 +19,23 @@ public class TokenController { public static String SERVICE_URL = "http://localhost:3000"; + @Autowired + private UserService userService; + @GetMapping("/token") public ResponseEntity getToken(@RequestParam("ticket") final String ticket) { - CasValidator validator = new CasValidator(SERVICE_URL,ticket); + CasValidator validator = new CasValidator(SERVICE_URL, ticket); - try{ + try { String authority = validator.validate(); - return new ResponseEntity<>(authority,HttpStatus.OK); - } - catch(CasValidationExcepiton e){ - return new ResponseEntity<>("Wrong ticket",HttpStatus.UNAUTHORIZED); - } - catch(Exception e){ - return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR); + String token = this.userService.login(authority); + return new ResponseEntity<>(token, HttpStatus.OK); + } catch (CasValidationExcepiton e) { + return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED); + } catch (UserNotFoundException e) { + return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND); + } catch (Exception e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }