2020-08-04 17:30:03 +02:00
package com.plannaplan.controllers ;
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.RestController ;
import java.io.IOException ;
2020-12-09 15:02:06 +01:00
import java.util.Date ;
2020-08-04 17:30:03 +02:00
2020-09-15 11:31:30 +02:00
import com.plannaplan.App ;
2020-12-23 14:21:16 +01:00
import com.plannaplan.entities.User ;
2020-08-04 17:30:03 +02:00
import com.plannaplan.models.ConfigData ;
2020-12-09 15:02:06 +01:00
import com.plannaplan.models.TourData ;
2021-01-19 11:33:14 +01:00
import com.plannaplan.responses.models.ConfigTourResponse ;
2020-12-23 14:21:16 +01:00
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 ;
2020-09-23 19:12:25 +02:00
import com.plannaplan.services.ConfiguratorService ;
2020-12-23 14:21:16 +01:00
import com.plannaplan.services.UserService ;
import com.plannaplan.types.UserRoles ;
2020-08-04 17:30:03 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-12-23 14:21:16 +01:00
import org.springframework.beans.factory.annotation.Value ;
2020-12-09 15:02:06 +01:00
import org.springframework.format.annotation.DateTimeFormat ;
2020-08-04 17:30:03 +02:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
2020-10-16 16:31:10 +02:00
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity ;
2020-08-04 17:30:03 +02:00
import org.springframework.web.bind.annotation.PostMapping ;
2020-09-15 11:31:30 +02:00
import org.springframework.web.bind.annotation.RequestMapping ;
2020-08-04 17:30:03 +02:00
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.multipart.MultipartFile ;
2020-11-04 16:40:02 +01:00
import io.swagger.annotations.Api ;
2020-11-04 16:58:26 +01:00
import io.swagger.annotations.ApiOperation ;
2020-11-04 17:23:29 +01:00
import io.swagger.annotations.ApiParam ;
2021-01-19 11:33:14 +01:00
import org.springframework.web.bind.annotation.GetMapping ;
2020-11-04 16:40:02 +01:00
2021-01-16 13:57:58 +01:00
/ * *
* Rest controller to Config related endpoints . More detailed api docs is
* available via swagger
* /
2020-08-04 17:30:03 +02:00
@RestController
@CrossOrigin
2020-09-15 11:31:30 +02:00
@RequestMapping ( " /api/ " + App . API_VERSION + " /configurator " )
2020-10-16 16:31:10 +02:00
@EnableGlobalMethodSecurity ( prePostEnabled = true )
2020-11-04 16:40:02 +01:00
@Api ( tags = { " ConfigController " } , value = " ConfigController " , description = " All endpoints to configure an app " )
2020-08-04 17:30:03 +02:00
public class ConfigController {
2020-12-23 14:21:16 +01:00
@Value ( " ${plannaplan.frontendUrl} " )
private String serviceUrl ;
@Value ( " ${plannaplan.dev} " )
private boolean isDev ;
2021-01-16 13:57:58 +01:00
2020-08-04 17:30:03 +02:00
@Autowired
2020-09-23 19:12:25 +02:00
private ConfiguratorService contrl ;
2020-08-04 17:30:03 +02:00
2020-12-23 14:21:16 +01:00
@Autowired
2021-01-16 13:57:58 +01:00
private UserService userService ;
/ * *
* @param file file . xlsx that contains courses and groups with
* apoinnted rules
* @param firstTourBegin formated string dd . MM . yyyy
* @param firstTourEnd formated string dd . MM . yyyy
* @param secondTourBegin formated string dd . MM . yyyy
* @param secondTourEnd formated string dd . MM . yyyy
* @return was config success
* /
2020-12-09 15:02:06 +01:00
@PostMapping ( path = " /config " , consumes = { " multipart/form-data " } )
2020-10-16 16:31:10 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') " )
2020-11-04 16:58:26 +01:00
@ApiOperation ( " Imports data to system. To call you need to provide ADMIN token " )
2020-11-04 17:23:29 +01:00
public ResponseEntity < String > configApp (
2020-12-09 15:02:06 +01:00
@RequestParam ( " file " ) @ApiParam ( value = " file .xlsx that contains courses and groups with apoinnted rules " ) MultipartFile file ,
2020-12-09 15:10:19 +01:00
@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 ) {
2020-08-04 17:30:03 +02:00
try {
2020-12-09 15:02:06 +01:00
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 ( ) ) ;
2020-08-04 17:30:03 +02:00
this . contrl . config ( data ) ;
return new ResponseEntity < > ( " Sucess " , HttpStatus . OK ) ;
} catch ( IOException e ) {
return new ResponseEntity < > ( e . getMessage ( ) , HttpStatus . INTERNAL_SERVER_ERROR ) ;
}
}
2021-01-16 13:57:58 +01:00
/ * *
* @param firstTourBegin formated string dd . MM . yyyy
* @param firstTourEnd formated string dd . MM . yyyy
* @param secondTourBegin formated string dd . MM . yyyy
* @param secondTourEnd formated string dd . MM . yyyy
* @return was operation successful
* /
2020-12-26 15:33:17 +01:00
@PostMapping ( path = " /config/tours " )
@PreAuthorize ( " hasRole('ROLE_ADMIN') " )
@ApiOperation ( " Set tours dates. To call you need to provide ADMIN token " )
public ResponseEntity < String > configToursApp (
@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 ) {
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 ) ;
this . contrl . saveTours ( firstTour , secondTour ) ;
return new ResponseEntity < > ( " Sucess " , HttpStatus . OK ) ;
}
2021-01-16 13:57:58 +01:00
/ * *
* @param file file . xlsx that contains courses and groups with
* @return was operation successfull
* /
2020-12-26 12:09:55 +01:00
@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 (
2021-01-16 13:57:58 +01:00
@RequestParam ( " file " ) @ApiParam ( value = " file .xlsx that contains courses and groups with apoinnted rules " ) MultipartFile file ) {
2020-12-26 12:09:55 +01:00
try {
this . contrl . importCoursesStream ( file . getInputStream ( ) ) ;
return new ResponseEntity < > ( " Sucess " , HttpStatus . OK ) ;
} catch ( IOException e ) {
return new ResponseEntity < > ( e . getMessage ( ) , HttpStatus . INTERNAL_SERVER_ERROR ) ;
}
}
2021-01-16 13:57:58 +01:00
/ * *
* @param ticket CAS ticket to get user wuthority
* @return was admin created
* /
2020-12-23 14:26:29 +01:00
@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. " )
2021-01-16 13:57:58 +01:00
public ResponseEntity < String > initAdmin (
@RequestParam ( " ticket " ) @ApiParam ( value = " Ticket for validation. " ) String ticket ) {
if ( this . userService . adminExists ( ) ) {
2020-12-23 14:21:16 +01:00
return new ResponseEntity < > ( " Admin had been already created. " , HttpStatus . FORBIDDEN ) ;
}
2021-01-16 13:57:58 +01:00
2020-12-23 14:21:16 +01:00
final CasValidator validator = isDev ? new DefaultUAMCasValidator ( serviceUrl , ticket )
2021-01-16 13:57:58 +01:00
: new CustomUAMCasValidator ( serviceUrl , ticket ) ;
2020-12-23 14:21:16 +01:00
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 ) ;
}
2021-01-16 13:57:58 +01:00
}
2021-01-19 11:33:14 +01:00
/ * *
* @return api response entity
* /
@GetMapping ( path = " /admin/tour " )
@PreAuthorize ( " hasRole('ROLE_ADMIN') " )
@ApiOperation ( " It will return what tour is currently on. You need to provide admin token. " )
public ResponseEntity < ConfigTourResponse > getTourDate ( ) {
final ConfigTourResponse response = new ConfigTourResponse ( this . contrl . getCurrentConfig ( ) . getCurrentState ( ) ) ;
return new ResponseEntity < > ( response , HttpStatus . OK ) ;
}
2020-08-04 17:30:03 +02:00
}