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); model.addAttribute("camp", campBean);
return "/registration/registration"; return "/registration/registration";
} }
Boolean result = service.register(bean); Boolean result = service.register(bean, getCurrentUser(request));
return result ? "/registration/success" : "/error"; return result ? "/registration/success" : "/error";
} }

View File

@ -18,6 +18,7 @@ import org.jooq.DeleteConditionStep;
import org.jooq.InsertResultStep; import org.jooq.InsertResultStep;
import org.jooq.InsertValuesStep12; import org.jooq.InsertValuesStep12;
import org.jooq.InsertValuesStep13; import org.jooq.InsertValuesStep13;
import org.jooq.Record1;
import org.jooq.Record7; import org.jooq.Record7;
import org.jooq.SelectConditionStep; import org.jooq.SelectConditionStep;
import org.jooq.exception.DataAccessException; import org.jooq.exception.DataAccessException;
@ -76,7 +77,7 @@ public class RegistrationGateway {
* @param login the login * @param login the login
* @return true or false * @return true or false
*/ */
public Boolean isLoginAvailable(String login) { public Boolean isLoginNotYetInUse(String login) {
SelectConditionStep<TProfileRecord> sql = jooq SelectConditionStep<TProfileRecord> sql = jooq
// @formatter:off // @formatter:off
.selectFrom(T_PROFILE) .selectFrom(T_PROFILE)
@ -96,48 +97,61 @@ public class RegistrationGateway {
public Boolean register(RegistrationBean bean) { public Boolean register(RegistrationBean bean) {
LambdaResultWrapper lrw = new LambdaResultWrapper(); LambdaResultWrapper lrw = new LambdaResultWrapper();
jooq.transaction(t -> { jooq.transaction(t -> {
if (bean.getRegisterInKeycloak()) { if (bean.getLogin() != null && !bean.getLogin().isEmpty()) {
if (!isLoginAvailable(bean.getLogin())) { Boolean loginNotYetInUse = isLoginNotYetInUse(bean.getLogin());
if (bean.getRegisterInKeycloak() && !loginNotYetInUse) {
throw new DataAccessException("login already in use: " + bean.getLogin()); throw new DataAccessException("login already in use: " + bean.getLogin());
} }
// TODO: check if teacher is at least 2 years older than the camp participants // TODO: check if teacher is at least 2 years older than the camp participants
String oldPassword = new StrongPasswordEncryptor().encryptPassword(bean.getPassword()); Integer fkProfile = null;
InsertResultStep<TProfileRecord> sql1 = DSL.using(t) if (loginNotYetInUse) {
// @formatter:off String oldPassword = new StrongPasswordEncryptor().encryptPassword(bean.getPassword());
.insertInto(T_PROFILE, InsertResultStep<TProfileRecord> sql1 = DSL.using(t)
T_PROFILE.FORENAME, // @formatter:off
T_PROFILE.SURNAME, .insertInto(T_PROFILE,
T_PROFILE.USERNAME, T_PROFILE.FORENAME,
T_PROFILE.PASSWORD, T_PROFILE.SURNAME,
T_PROFILE.DUEDATE, T_PROFILE.USERNAME,
T_PROFILE.UUID) T_PROFILE.PASSWORD,
.values(bean.getForename(), bean.getSurname(), bean.getLogin(), oldPassword, LocalDateTime.now().plus(356, ChronoUnit.DAYS), UUID.nameUUIDFromBytes(bean.getLogin().getBytes()).toString()) T_PROFILE.DUEDATE,
.returning(T_PROFILE.PK); T_PROFILE.UUID)
// @formatter:on .values(bean.getForename(), bean.getSurname(), bean.getLogin(), oldPassword, LocalDateTime.now().plus(356, ChronoUnit.DAYS), UUID.nameUUIDFromBytes(bean.getLogin().getBytes()).toString())
LOGGER.debug(sql1.toString()); .returning(T_PROFILE.PK);
Integer fkProfile = sql1.fetchOne().getPk(); // @formatter:on
LOGGER.debug(sql1.toString());
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 // register the person for camp participation
InsertValuesStep13<TPersonRecord, String, String, EnumSex, LocalDate, String, String, String, String, String, EnumCamprole, Integer, String, Integer> sql2 = DSL InsertValuesStep13<TPersonRecord, String, String, EnumSex, LocalDate, String, String, String, String, String, EnumCamprole, Integer, String, Integer> sql2 = DSL
.using(t) .using(t)
// @formatter:off // @formatter:off
.insertInto(T_PERSON, .insertInto(T_PERSON,
T_PERSON.FORENAME, T_PERSON.FORENAME,
T_PERSON.SURNAME, T_PERSON.SURNAME,
T_PERSON.SEX, T_PERSON.SEX,
T_PERSON.BIRTHDATE, T_PERSON.BIRTHDATE,
T_PERSON.STREET, T_PERSON.STREET,
T_PERSON.ZIP, T_PERSON.ZIP,
T_PERSON.CITY, T_PERSON.CITY,
T_PERSON.EMAIL, T_PERSON.EMAIL,
T_PERSON.PHONE, T_PERSON.PHONE,
T_PERSON.CAMPROLE, T_PERSON.CAMPROLE,
T_PERSON.FK_CAMP, T_PERSON.FK_CAMP,
T_PERSON.COMMENT, T_PERSON.COMMENT,
T_PERSON.FK_PROFILE) T_PERSON.FK_PROFILE)
.values(bean.getForename(), bean.getSurname(), bean.getSex(), .values(bean.getForename(), bean.getSurname(), bean.getSex(),
bean.getBirthDate(), bean.getStreet(), bean.getZip(), bean.getCity(), bean.getEmail(), bean.getBirthDate(), bean.getStreet(), bean.getZip(), bean.getCity(), bean.getEmail(),
bean.getPhone(), bean.getCampRole(), bean.getFkCamp(), bean.getComment(), fkProfile); bean.getPhone(), bean.getCampRole(), bean.getFkCamp(), bean.getComment(), fkProfile);
// @formatter:on // @formatter:on
LOGGER.debug(sql2.toString()); LOGGER.debug(sql2.toString());
lrw.add(sql2.execute()); lrw.add(sql2.execute());
// register the login for the portal // register the login for the portal

View File

@ -38,7 +38,11 @@ public class RegistrationService {
* @param bean the bean * @param bean the bean
* @return true if successful, false otherwise * @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); Boolean result = gateway.register(bean);
if (result && bean.getRegisterInKeycloak()) { if (result && bean.getRegisterInKeycloak()) {
keycloak.register(bean.getForename(), bean.getSurname(), bean.getLogin(), bean.getPassword(), bean.getEmail()); 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 @Override
public boolean isValid(Object value, ConstraintValidatorContext context) { public boolean isValid(Object value, ConstraintValidatorContext context) {
Object login = new BeanWrapperImpl(value).getPropertyValue(field); Object login = new BeanWrapperImpl(value).getPropertyValue(field);
Boolean result = gateway.isLoginAvailable((String) login); Boolean result = gateway.isLoginNotYetInUse((String) login);
if (!result) { if (!result) {
context.buildConstraintViolationWithTemplate(message).addPropertyNode(field).addConstraintViolation() context.buildConstraintViolationWithTemplate(message).addPropertyNode(field).addConstraintViolation()
.disableDefaultConstraintViolation(); .disableDefaultConstraintViolation();

View File

@ -86,7 +86,7 @@
<textarea class="form-control" placeholder="Sonstiges" th:field="*{comment}"></textarea> <textarea class="form-control" placeholder="Sonstiges" th:field="*{comment}"></textarea>
</div> </div>
</div> </div>
<div class="row"> <div class="row" th:if="${#strings.isEmpty(currentUser)}">
<div class="col-sm-12 rowdist"> <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."> <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 <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>
</div> </div>
<div id="createlogin" class="row"> <div id="createlogin" class="row" th:if="${#strings.isEmpty(currentUser)}">
<div class="col-sm-6 rowdist"> <div class="col-sm-6 rowdist">
<span class="error" th:each="error : ${#fields.errors('login')}">[[${error}]]<br /></span> <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'}" /> <input type="text" placeholder="Login" th:field="*{login}" th:class="${#fields.hasErrors('login') ? 'inputerror' : 'form-control'}" />