register from dashboard (background functionality)

This commit is contained in:
Jottyfan 2022-12-10 15:23:31 +01:00
parent f13a645549
commit 3e1e5e12ab
5 changed files with 60 additions and 42 deletions

View File

@ -48,7 +48,7 @@ public class RegistrationController extends CommonController {
model.addAttribute("camp", campBean);
return "/registration/registration";
}
Boolean result = service.register(bean);
Boolean result = service.register(bean, getCurrentUser(request));
return result ? "/registration/success" : "/error";
}

View File

@ -18,6 +18,7 @@ import org.jooq.DeleteConditionStep;
import org.jooq.InsertResultStep;
import org.jooq.InsertValuesStep12;
import org.jooq.InsertValuesStep13;
import org.jooq.Record1;
import org.jooq.Record7;
import org.jooq.SelectConditionStep;
import org.jooq.exception.DataAccessException;
@ -76,7 +77,7 @@ public class RegistrationGateway {
* @param login the login
* @return true or false
*/
public Boolean isLoginAvailable(String login) {
public Boolean isLoginNotYetInUse(String login) {
SelectConditionStep<TProfileRecord> sql = jooq
// @formatter:off
.selectFrom(T_PROFILE)
@ -96,11 +97,14 @@ public class RegistrationGateway {
public Boolean register(RegistrationBean bean) {
LambdaResultWrapper lrw = new LambdaResultWrapper();
jooq.transaction(t -> {
if (bean.getRegisterInKeycloak()) {
if (!isLoginAvailable(bean.getLogin())) {
if (bean.getLogin() != null && !bean.getLogin().isEmpty()) {
Boolean loginNotYetInUse = isLoginNotYetInUse(bean.getLogin());
if (bean.getRegisterInKeycloak() && !loginNotYetInUse) {
throw new DataAccessException("login already in use: " + bean.getLogin());
}
// TODO: check if teacher is at least 2 years older than the camp participants
Integer fkProfile = null;
if (loginNotYetInUse) {
String oldPassword = new StrongPasswordEncryptor().encryptPassword(bean.getPassword());
InsertResultStep<TProfileRecord> sql1 = DSL.using(t)
// @formatter:off
@ -115,7 +119,17 @@ public class RegistrationGateway {
.returning(T_PROFILE.PK);
// @formatter:on
LOGGER.debug(sql1.toString());
Integer fkProfile = sql1.fetchOne().getPk();
fkProfile = sql1.fetchOne().getPk();
} else {
SelectConditionStep<Record1<Integer>> sql1 = DSL.using(t)
// @formatter:off
.select(T_PROFILE.PK)
.from(T_PROFILE)
.where(T_PROFILE.USERNAME.eq(bean.getLogin()));
// @formatter:on
LOGGER.debug(sql1.toString());
fkProfile = sql1.fetchOne().get(T_PROFILE.PK);
}
// register the person for camp participation
InsertValuesStep13<TPersonRecord, String, String, EnumSex, LocalDate, String, String, String, String, String, EnumCamprole, Integer, String, Integer> sql2 = DSL
.using(t)

View File

@ -38,7 +38,11 @@ public class RegistrationService {
* @param bean the bean
* @return true if successful, false otherwise
*/
public Boolean register(RegistrationBean bean) {
public Boolean register(RegistrationBean bean, String currentUser) {
if (currentUser != null) {
bean.setRegisterInKeycloak(false); // already registered
bean.setLogin(currentUser);
}
Boolean result = gateway.register(bean);
if (result && bean.getRegisterInKeycloak()) {
keycloak.register(bean.getForename(), bean.getSurname(), bean.getLogin(), bean.getPassword(), bean.getEmail());

View File

@ -29,7 +29,7 @@ public class UnusedUsernameValidator implements ConstraintValidator<UnusedUserna
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
Object login = new BeanWrapperImpl(value).getPropertyValue(field);
Boolean result = gateway.isLoginAvailable((String) login);
Boolean result = gateway.isLoginNotYetInUse((String) login);
if (!result) {
context.buildConstraintViolationWithTemplate(message).addPropertyNode(field).addConstraintViolation()
.disableDefaultConstraintViolation();

View File

@ -86,7 +86,7 @@
<textarea class="form-control" placeholder="Sonstiges" th:field="*{comment}"></textarea>
</div>
</div>
<div class="row">
<div class="row" th:if="${#strings.isEmpty(currentUser)}">
<div class="col-sm-12 rowdist">
<div class="form-check" title="Die Anmeldedaten können in den Kontoeinstellungen bearbeitet und für die nächsten Freizeitanmeldungen verwendet werden.">
<input id="save" type="checkbox" class="form-check-input" th:field="*{registerInKeycloak}" onchange="$('#createlogin').toggle();" /> <label class="form-check-label" for="save">Anmeldedaten
@ -94,7 +94,7 @@
</div>
</div>
</div>
<div id="createlogin" class="row">
<div id="createlogin" class="row" th:if="${#strings.isEmpty(currentUser)}">
<div class="col-sm-6 rowdist">
<span class="error" th:each="error : ${#fields.errors('login')}">[[${error}]]<br /></span>
<input type="text" placeholder="Login" th:field="*{login}" th:class="${#fields.hasErrors('login') ? 'inputerror' : 'form-control'}" />