Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-26 12:09:55 +01:00
parent 2ba4082373
commit 9e6ee6dbb3
Signed by: y0rune
GPG Key ID: F204C385F57EB348
2 changed files with 103 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import com.plannaplan.repositories.AppConfigRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -62,4 +63,14 @@ public class ConfiguratorService {
return repsonse.get(0); return repsonse.get(0);
} }
/**
*
* @param inputStream This input stream contains new courses to import.
*/
public void importCoursesStream(InputStream inputStream) {
FileReader reader = new FileReader(inputStream);
FileData coursesData = reader.read();
migrator.migrate(coursesData);
}
} }

View File

@ -82,6 +82,48 @@ public class ConfigController {
} }
} }
@PostMapping(path = "/config/courses", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Imports data to system. To call you need to provide ADMIN token")
public ResponseEntity<String> configAppChangeCources(
@RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file)
{
try {
this.contrl.importCoursesStream(file.getInputStream());
return new ResponseEntity<>("Sucess", HttpStatus.OK);
} catch (IOException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping(path = "/config/tours", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Imports data to system. To call you need to provide ADMIN token")
public ResponseEntity<String> configAppsasaas(
@RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file,
@RequestParam("firstTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour begin in format dd.MM.yyyy") Date firstTourBegin,
@RequestParam("firstTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour ends in format dd.MM.yyyy") Date firstTourEnd,
@RequestParam("secondTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour begin in format dd.MM.yyyy") Date secondTourBegin,
@RequestParam("secondTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour ends in format dd.MM.yyyy") Date secondTourEnd) {
try {
if (!(firstTourBegin.before(firstTourEnd)
&& (firstTourEnd.before(secondTourBegin) || firstTourEnd.equals(secondTourBegin))
&& secondTourBegin.before(secondTourEnd))) {
return new ResponseEntity<>("Bad dates", HttpStatus.BAD_REQUEST);
}
final TourData firstTour = new TourData(firstTourBegin, firstTourEnd);
final TourData secondTour = new TourData(secondTourBegin, secondTourEnd);
final ConfigData data = new ConfigData(firstTour, secondTour, file.getInputStream());
this.contrl.config(data);
return new ResponseEntity<>("Sucess", HttpStatus.OK);
} catch (IOException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping(path = "/admin/init") @PostMapping(path = "/admin/init")
@ApiOperation("It can be run only in the initialization of the application. It will create admin user to manage the application.") @ApiOperation("It can be run only in the initialization of the application. It will create admin user to manage the application.")
public ResponseEntity<String> initAdmin(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){ public ResponseEntity<String> initAdmin(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){
@ -106,4 +148,54 @@ public class ConfigController {
} }
} }
@PostMapping(path = "/admin/addnewdeanery", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Adding the new deanery user into application")
public ResponseEntity<String> AddNewDeaneryUser(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){
// if (this.userService.adminExists()){
// return new ResponseEntity<>("Admin had been already created.", HttpStatus.FORBIDDEN);
// }
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.DEANERY));
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);
}
}
@PostMapping(path = "/admin/addnewadmin", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Adding the new administrator user into application")
public ResponseEntity<String> AddNewAdminUser(@RequestParam("ticket") @ApiParam(value = "Ticket for validation.") String ticket){
// if (this.userService.adminExists()){
// return new ResponseEntity<>("Admin had been already created.", HttpStatus.FORBIDDEN);
// }
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);
}
}
} }