Method body ready

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-23 14:21:16 +01:00
parent e647698591
commit 507d9fddc0
Signed by: y0rune
GPG Key ID: F204C385F57EB348
3 changed files with 43 additions and 4 deletions

View File

@ -25,12 +25,16 @@ public class UserService {
} }
public User checkForUser(String email, String usosId) { 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) { if (usosId == null) {
Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim()); Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
if (user.isPresent()) { if (user.isPresent()) {
return user.get(); return user.get();
} else { } 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); return this.repo.save(newUser);
} }
} else { } else {
@ -38,7 +42,7 @@ public class UserService {
if (user.isPresent()) { if (user.isPresent()) {
return user.get(); return user.get();
} else { } 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); return this.repo.save(newUser);
} }
} }

View File

@ -7,11 +7,20 @@ import java.io.IOException;
import java.util.Date; import java.util.Date;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.entities.User;
import com.plannaplan.models.ConfigData; import com.plannaplan.models.ConfigData;
import com.plannaplan.models.TourData; 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.ConfiguratorService;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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") @Api(tags = { "ConfigController" }, value = "ConfigController", description = "All endpoints to configure an app")
public class ConfigController { public class ConfigController {
@Value("${plannaplan.frontendUrl}")
private String serviceUrl;
@Value("${plannaplan.dev}")
private boolean isDev;
@Autowired @Autowired
private ConfiguratorService contrl; private ConfiguratorService contrl;
@Autowired
private UserService userService;
@PostMapping(path = "/config", consumes = { "multipart/form-data" }) @PostMapping(path = "/config", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')") @PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Imports data to system. To call you need to provide ADMIN token") @ApiOperation("Imports data to system. To call you need to provide ADMIN token")
@ -64,8 +83,25 @@ public class ConfigController {
} }
public ResponseEntity<String> initAdmin(@RequestParam("ticket") String ticket){ public ResponseEntity<String> 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);
}
} }
} }

View File

@ -62,7 +62,6 @@ public class TokenController {
} catch (Exception e) { } catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
} }
} }
@GetMapping("/token/refresh") @GetMapping("/token/refresh")