From 507d9fddc00c21e134e73c2de785578dafccb131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 23 Dec 2020 14:21:16 +0100 Subject: [PATCH] Method body ready MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/services/UserService.java | 8 +++- .../controllers/ConfigController.java | 38 ++++++++++++++++++- .../controllers/TokenController.java | 1 - 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 62a3325..8c66268 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -25,12 +25,16 @@ public class UserService { } public User checkForUser(String email, String usosId) { + return this.checkForUser(email, usosId, UserRoles.STUDENT); + } + + public User checkForUser(String email, String usosId, UserRoles roleIfNotExist) { if (usosId == null) { Optional user = this.repo.getByAuthority(email.replace("\n", "").trim()); if (user.isPresent()) { return user.get(); } else { - final User newUser = new User(null, null, email.replace("\n", "").trim(), UserRoles.STUDENT); + final User newUser = new User(null, null, email.replace("\n", "").trim(), roleIfNotExist); return this.repo.save(newUser); } } else { @@ -38,7 +42,7 @@ public class UserService { if (user.isPresent()) { return user.get(); } else { - final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, UserRoles.STUDENT); + final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, roleIfNotExist); return this.repo.save(newUser); } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index 432e416..660b1c3 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -7,11 +7,20 @@ import java.io.IOException; import java.util.Date; import com.plannaplan.App; +import com.plannaplan.entities.User; import com.plannaplan.models.ConfigData; import com.plannaplan.models.TourData; +import com.plannaplan.security.cas.CasUserIdentity; +import com.plannaplan.security.cas.CasValidationExcepiton; +import com.plannaplan.security.cas.CasValidator; +import com.plannaplan.security.cas.CustomUAMCasValidator; +import com.plannaplan.security.cas.DefaultUAMCasValidator; import com.plannaplan.services.ConfiguratorService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -33,9 +42,19 @@ import io.swagger.annotations.ApiParam; @Api(tags = { "ConfigController" }, value = "ConfigController", description = "All endpoints to configure an app") public class ConfigController { + @Value("${plannaplan.frontendUrl}") + private String serviceUrl; + + @Value("${plannaplan.dev}") + private boolean isDev; + @Autowired private ConfiguratorService contrl; + @Autowired + private UserService userService; + + @PostMapping(path = "/config", consumes = { "multipart/form-data" }) @PreAuthorize("hasRole('ROLE_ADMIN')") @ApiOperation("Imports data to system. To call you need to provide ADMIN token") @@ -64,8 +83,25 @@ public class ConfigController { } public ResponseEntity initAdmin(@RequestParam("ticket") String ticket){ + if (this.userService.adminExists()){ + return new ResponseEntity<>("Admin had been already created.", HttpStatus.FORBIDDEN); + } - return null; + final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket) + : new CustomUAMCasValidator(serviceUrl, ticket); + + try { + final CasUserIdentity casUserIdentity = validator.validate(); + final String usosId = casUserIdentity.getUsosId(); + final String authority = casUserIdentity.getEmail(); + this.userService.save(new User(null, null, authority, usosId, UserRoles.ADMIN)); + + return new ResponseEntity<>("Success", HttpStatus.OK); + } catch (CasValidationExcepiton e) { + return new ResponseEntity<>("CAS validation failed", HttpStatus.UNAUTHORIZED); + } catch (Exception e) { + return new ResponseEntity<>("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR); + } } } \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/controllers/TokenController.java b/restservice/src/main/java/com/plannaplan/controllers/TokenController.java index d771768..18b66c8 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/TokenController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/TokenController.java @@ -62,7 +62,6 @@ public class TokenController { } catch (Exception e) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } - } @GetMapping("/token/refresh")