diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java index d5299935..de9dc9a8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/Constants.java @@ -102,6 +102,8 @@ public final class Constants { public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; + public static final Long LIGHT_ADMIN_USER_ID = 1L; + public static final DateTimeFormatter STANDARD_DATE_TIME_MILLIS_FORMATTER = DateTimeFormat .forPattern(DEFAULT_DATE_TIME_MILLIS_FORMAT) .withZoneUTC(); diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserRole.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserRole.java index 03bc5cba..d1168083 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserRole.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserRole.java @@ -12,6 +12,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.List; +import java.util.stream.Collectors; import org.springframework.security.core.GrantedAuthority; @@ -61,11 +62,9 @@ public enum UserRole implements Entity, GrantedAuthority { } public static List getNamesForAllRoles(){ - return List.of( - SEB_SERVER_ADMIN.getName(), - INSTITUTIONAL_ADMIN.getName(), - EXAM_ADMIN.getName(), - EXAM_SUPPORTER.getName()); + return Arrays.stream(UserRole.values()) + .map(UserRole::getName) + .toList(); } } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/AdminUserInitializer.java b/src/main/java/ch/ethz/seb/sebserver/webservice/AdminUserInitializer.java index 6398f79e..aa164f60 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/AdminUserInitializer.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/AdminUserInitializer.java @@ -14,6 +14,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.EntityType; import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.AdditionalAttributesDAO; @@ -151,7 +152,9 @@ class AdminUserInitializer { .flatMap(account -> this.userDAO.setActive(account, true)) .map(account -> { printAdminCredentials(this.adminName, generateAdminPassword); - if(this.webserviceInfo.isLightSetup()) writeInitialAdminCredentialsIntoDB(this.adminName, generateAdminPassword); + if(this.webserviceInfo.isLightSetup()) { + writeInitialAdminCredentialsIntoDB(this.adminName, generateAdminPassword); + } return account; }) .getOrThrow(); @@ -179,7 +182,7 @@ class AdminUserInitializer { } private void writeInitialAdminCredentialsIntoDB(final String name, final CharSequence pwd){ - Result.tryCatch(() -> { + try { final Map attributes = new HashMap<>(); attributes.put( Domain.USER.ATTR_USERNAME, @@ -188,11 +191,11 @@ class AdminUserInitializer { Domain.USER.ATTR_PASSWORD, String.valueOf(pwd)); - this.additionalAttributesDAO.saveAdditionalAttributes( - EntityType.USER, - 2L, - attributes); - }); + this.additionalAttributesDAO.saveAdditionalAttributes(EntityType.USER, Constants.LIGHT_ADMIN_USER_ID, attributes); + + } catch (final Exception e) { + log.error("Unable to write initial admin credentials into the additional attributes table: ", e); + } } private CharSequence generateAdminPassword() { diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SEBClientConnectionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SEBClientConnectionServiceImpl.java index ef1bd463..83328640 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SEBClientConnectionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/SEBClientConnectionServiceImpl.java @@ -653,16 +653,20 @@ public class SEBClientConnectionServiceImpl implements SEBClientConnectionServic response.setStatus(HttpStatus.OK.value()); - outputStream.flush(); - }catch(Exception e){ final APIMessage errorMessage = APIMessage.ErrorMessage.GENERIC.of(e.getMessage()); outputStream.write(Utils.toByteArray(this.jsonMapper.writeValueAsString(errorMessage))); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); } finally { - outputStream.flush(); - outputStream.close(); + + try { + outputStream.flush(); + outputStream.close(); + + } catch (IOException e) { + log.error("error while flushing / closing output stream", e); + } } } @@ -981,4 +985,4 @@ public class SEBClientConnectionServiceImpl implements SEBClientConnectionServic return (exam != null && exam.lmsSetupId == null && status == ConnectionStatus.READY) || status == ConnectionStatus.ACTIVE; } -} +} \ No newline at end of file diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LightController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LightController.java index a34b3047..96bf02e5 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LightController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LightController.java @@ -8,6 +8,7 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api; +import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.API; import ch.ethz.seb.sebserver.gbl.api.EntityType; import ch.ethz.seb.sebserver.gbl.async.AsyncServiceSpringConfig; @@ -55,8 +56,6 @@ public class LightController { this.executor = executor; } - //this.examAPI_V1_Endpoint + API.EXAM_API_CONFIGURATION_LIGHT_ENDPOINT - //http://localhost:8080/exam-api/discovery/light-config @RequestMapping( path = API.EXAM_API_CONFIGURATION_LIGHT_ENDPOINT, method = RequestMethod.GET, @@ -65,14 +64,10 @@ public class LightController { final HttpServletRequest request, final HttpServletResponse response){ - //temp solution: get first active seb client config we can get --> - //in a light setup there should be only one setup so this step is not necessary and we can just use the first and only item in the db - String modelId = getSebClientConfigId(); - return CompletableFuture.runAsync( () -> { try { - this.sebClientConnectionService.streamLightExamConfig(modelId, response); + this.sebClientConnectionService.streamLightExamConfig("1", response); } catch (IOException e) { throw new RuntimeException(e); } @@ -91,11 +86,11 @@ public class LightController { final HttpServletResponse response){ try { - final String username = this.additionalAttributesDAO.getAdditionalAttribute(EntityType.USER, 2L, Domain.USER.ATTR_USERNAME) + final String username = this.additionalAttributesDAO.getAdditionalAttribute(EntityType.USER, Constants.LIGHT_ADMIN_USER_ID, Domain.USER.ATTR_USERNAME) .getOrThrow() .getValue(); - final String password = this.additionalAttributesDAO.getAdditionalAttribute(EntityType.USER, 2L, Domain.USER.ATTR_PASSWORD) + final String password = this.additionalAttributesDAO.getAdditionalAttribute(EntityType.USER, Constants.LIGHT_ADMIN_USER_ID, Domain.USER.ATTR_PASSWORD) .getOrThrow() .getValue(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java index 346f12f0..4b08afc7 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java @@ -9,6 +9,7 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api; import ch.ethz.seb.sebserver.WebSecurityConfig; +import ch.ethz.seb.sebserver.gbl.Constants; import ch.ethz.seb.sebserver.gbl.api.API; import ch.ethz.seb.sebserver.gbl.api.APIMessage; import ch.ethz.seb.sebserver.gbl.api.APIMessage.APIMessageException; @@ -42,6 +43,8 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.session.ScreenProctoringSer import ch.ethz.seb.sebserver.webservice.servicelayer.validation.BeanValidationService; import ch.ethz.seb.sebserver.webservice.weblayer.oauth.RevokeTokenEndpoint; import org.mybatis.dynamic.sql.SqlTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.MediaType; @@ -64,6 +67,8 @@ import java.util.List; @RequestMapping("${sebserver.webservice.api.admin.endpoint}" + API.USER_ACCOUNT_ENDPOINT) public class UserAccountController extends ActivatableEntityController { + private static final Logger log = LoggerFactory.getLogger(UserAccountController.class); + private final ApplicationEventPublisher applicationEventPublisher; private final UserDAO userDAO; private final PasswordEncoder userPasswordEncoder; @@ -201,12 +206,7 @@ public class UserAccountController extends ActivatableEntityController this.userActivityLogDAO.log(UserLogActivityType.PASSWORD_CHANGE, e)) .map(this::synchronizeUserWithSPS) - .map(userInfo -> { - if(this.webserviceInfo.isLightSetup()){ - return removeInitialAdminPasswordFromDB(userInfo); - } - return userInfo; - }) + .map(this::removeInitialAdminPasswordFromDB) .getOrThrow(); } @@ -314,10 +314,18 @@ public class UserAccountController extends ActivatableEntityController { - this.additionalAttributesDAO.delete(EntityType.USER, 2L, Domain.USER.ATTR_USERNAME); - this.additionalAttributesDAO.delete(EntityType.USER, 2L, Domain.USER.ATTR_PASSWORD); - }); + if(!this.webserviceInfo.isLightSetup()){ + return userInfo; + } + + try{ + this.additionalAttributesDAO.delete(EntityType.USER, Constants.LIGHT_ADMIN_USER_ID, Domain.USER.ATTR_USERNAME); + this.additionalAttributesDAO.delete(EntityType.USER, Constants.LIGHT_ADMIN_USER_ID, Domain.USER.ATTR_PASSWORD); + + }catch(final Exception e){ + log.error("Unable to delete initial admin credentials from the additional attributes table: ", e); + + } return userInfo; } diff --git a/src/main/resources/config/application-dev-ws.properties b/src/main/resources/config/application-dev-ws.properties index 750f47a7..e4a9be9c 100644 --- a/src/main/resources/config/application-dev-ws.properties +++ b/src/main/resources/config/application-dev-ws.properties @@ -27,7 +27,7 @@ sebserver.init.database.integrity.try-fix=true # webservice setup configuration sebserver.init.adminaccount.gen-on-init=false -sebserver.webservice.light.setup=false +sebserver.webservice.light.setup=true sebserver.webservice.distributed=false #sebserver.webservice.master.delay.threshold=10000 sebserver.webservice.http.external.scheme=http