Skip to content

Commit

Permalink
Fixes (#998)
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Muravja <[email protected]>
  • Loading branch information
kyberorg committed Feb 15, 2023
1 parent eabcf0d commit f37eea2
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 38 deletions.
2 changes: 1 addition & 1 deletion frontend/css/axe_form_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
.space-after-fields {
padding-left: 1rem;
max-width: 99%;
}
}
2 changes: 1 addition & 1 deletion frontend/css/common_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,4 @@ vaadin-icon[icon="vaadin:sign-in"] {

.fit-in-window > * {
flex: 1 0 auto;
}
}
2 changes: 1 addition & 1 deletion frontend/css/forgot_password_page.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.result-span {
padding-left: 1rem;
}
}
7 changes: 6 additions & 1 deletion frontend/css/profile_page.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
.fit-in-section {
width: min-content;
}
}
}

.telegram-details {
display: block;
max-width: 290px;
}
2 changes: 1 addition & 1 deletion frontend/css/registration_page.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

.info-button {
border-radius: 100%;
}
}
6 changes: 4 additions & 2 deletions src/main/java/pm/axe/core/IdentGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static String generateTokenIdent(final Token token) {

private static String generateAccountConfirmationIdent(final Token token) {
String prefix = token.getTokenType().getIdentPrefix();
String randomPart = RandomStringUtils.randomAlphanumeric(Axe.C.FOUR);
return String.join("", prefix, randomPart);
String randomNumber = RandomStringUtils.randomNumeric(1);
String randomChar = RandomStringUtils.randomAlphanumeric(1);
String randomNum = RandomStringUtils.randomNumeric(1);
return String.join("", prefix, randomNumber, randomChar, randomNum);
}
}
17 changes: 10 additions & 7 deletions src/main/java/pm/axe/services/user/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public class AccountService {
private static final String TAG = "[" + AccountService.class.getSimpleName() + "]";
private static final String ERR_ACCOUNT_IS_EMPTY = "Account is null";
private static final String ERR_NO_EMAIL_ACCOUNT = "User has no Email Account";

private final AccountDao accountDao;
private final SymmetricCryptTool cryptTool;
Expand Down Expand Up @@ -309,8 +308,6 @@ public OperationResult deleteAccount(final Account account) {
* {@link OperationResult#malformedInput()}, when new email address not valid.
* {@link OperationResult#generalFail()} with {@link #ERR_ENCRYPTION_FAILED} message,
* when encryption failed.
* {@link OperationResult#generalFail()} with {@link #ERR_NO_EMAIL_ACCOUNT} message,
* when {@link User} has no {@link AccountType#EMAIL} {@link Account}.
*/
public OperationResult updateEmailAccount(final User user, final String email) {
if (user == null) return OperationResult.malformedInput().withMessage("User cannot be NULL");
Expand All @@ -321,7 +318,9 @@ public OperationResult updateEmailAccount(final User user, final String email) {


Optional<Account> emailAccount = getAccount(user, AccountType.EMAIL);
boolean userHasEmailAccount;
if (emailAccount.isPresent()) {
userHasEmailAccount = true;
String encryptedEmail;
OperationResult encryptEmailResult = cryptTool.encrypt(email);
if (encryptEmailResult.ok()) {
Expand All @@ -334,13 +333,17 @@ public OperationResult updateEmailAccount(final User user, final String email) {
emailAccount.get().setAccountName(encryptedEmail);
emailAccount.get().setConfirmed(false);
} else {
return OperationResult.generalFail().withMessage(ERR_NO_EMAIL_ACCOUNT);
userHasEmailAccount = false;
}

try {
accountDao.save(emailAccount.get());
log.info("{} Updated email account for {} {}", TAG, User.class.getSimpleName(), user.getUsername());
return OperationResult.success().addPayload(emailAccount.get());
if (userHasEmailAccount) {
accountDao.save(emailAccount.get());
log.info("{} Updated email account for {} {}", TAG, User.class.getSimpleName(), user.getUsername());
return OperationResult.success().addPayload(emailAccount.get());
} else {
return createEmailAccount(user, email);
}
} catch (CannotCreateTransactionException e) {
return OperationResult.databaseDown();
} catch (Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pm/axe/ui/elements/TelegramSpan.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pm.axe.ui.elements;

import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.HasStyle;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.Icon;
Expand All @@ -17,7 +18,7 @@
/**
* {@link Span} with message "Send code to link your account with Telegram".
*/
public final class TelegramSpan extends Composite<Span> {
public final class TelegramSpan extends Composite<Span> implements HasStyle {

/**
* Creates new {@link TelegramSpan}.
Expand Down
72 changes: 49 additions & 23 deletions src/main/java/pm/axe/ui/pages/user/profile/tabs/ProfileTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public void tabInit(final User user) {
add(accountsSection);

usernameRequirements.hide();
if (StringUtils.isBlank(emailField.getValue())) {
emailField.focus();
}
}

private Section createAccountSection() {
Expand All @@ -83,7 +86,7 @@ private Section createAccountSection() {
Details emailUsageDetails = createEmailUsageDetails();
HorizontalLayout telegramLayout = createTelegramLayout();

Stream.of(usernameLayout, emailLayout, telegramLayout).forEach(VaadinUtils::setCentered);
Stream.of(usernameLayout, emailLayout).forEach(VaadinUtils::setCentered);

Section section = new Section("Accounts");
section.setContent(usernameLayout, usernameRequirements, emailLayout, emailUsageDetails, telegramLayout);
Expand All @@ -94,6 +97,7 @@ private Section createAccountSection() {
private HorizontalLayout createUsernameLayout() {
usernameField.setLabel("Username");
usernameField.setValue(user.getUsername());
usernameField.setWidthFull();
usernameField.addValueChangeListener(this::onUsernameChanged);
usernameField.setClearButtonVisible(true);
usernameField.setReadOnly(true);
Expand All @@ -117,22 +121,25 @@ private HorizontalLayout createEmailLayout() {
emailField.setLabel("E-mail");
emailField.setClearButtonVisible(true);
emailField.setReadOnly(true);
emailField.setWidthFull();
emailField.addValueChangeListener(this::onEmailChanged);

Optional<String> currentEmail = getCurrentEmail();
currentEmail.ifPresent(emailField::setValue);
currentEmail.ifPresent(e -> {
VaadinIcon confirmationStatusIcon = isCurrentEmailConfirmed()
? VaadinIcon.CHECK : VaadinIcon.ELLIPSIS_CIRCLE;
emailField.setSuffixComponent(confirmationStatusIcon.create());
});
currentEmail.ifPresent(e -> setConfirmationStatus());

editEmailButton.setText("Edit");
editEmailButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
saveEmailButton.setText("Save");
saveEmailButton.addThemeVariants(ButtonVariant.LUMO_SUCCESS, ButtonVariant.LUMO_PRIMARY);

emailLayout.add(emailField, editEmailButton);
if (currentEmail.isPresent()) {
emailLayout.add(emailField, editEmailButton);
} else {
emailField.setReadOnly(false);
emailLayout.add(emailField, saveEmailButton);
}

emailLayout.addClassName("fit-in-section");
VaadinUtils.fitLayoutInWindow(emailLayout);
VaadinUtils.setSmallSpacing(emailLayout);
Expand Down Expand Up @@ -168,6 +175,7 @@ private HorizontalLayout createTelegramLayout() {
Optional<Token> tgToken = getTelegramToken();
if (tgToken.isPresent()) {
Details telegramDetails = new Details("Link Telegram Account");
telegramDetails.setClassName("telegram-details");
telegramDetails.setOpened(true);
TelegramSpan telegramSpan = TelegramSpan.create(tgToken.get());
telegramDetails.setContent(telegramSpan);
Expand Down Expand Up @@ -255,16 +263,17 @@ private void onEmailChanged(final AbstractField.ComponentValueChangeEvent<EmailF
final String email = event.getValue().trim();
boolean isValidEmail = EmailValidator.getInstance().isValid(email);
if (StringUtils.isBlank(email)) {
usernameField.setInvalid(false);
usernameField.setErrorMessage("");
emailField.setInvalid(false);
emailField.setErrorMessage("");
return;
}

Optional<String> currentEmail = getCurrentEmail();
if (currentEmail.isEmpty()) return;
boolean isSameAsCurrent = currentEmail.get().equals(email);
if (isSameAsCurrent) {
return;
if (currentEmail.isPresent()) {
boolean isSameAsCurrent = currentEmail.get().equals(email);
if (isSameAsCurrent) {
return;
}
}

if (isValidEmail) {
Expand All @@ -285,20 +294,25 @@ private void onEditEmail(final ClickEvent<Button> event) {
}

private void onSaveEmail(final ClickEvent<Button> event) {
//is same as current ?
Optional<String> currentEmail = getCurrentEmail();
if (currentEmail.isEmpty()) return;
final String email = emailField.getValue().trim();
boolean isSameAsCurrent = currentEmail.get().equals(email);
if (isSameAsCurrent) {
if (StringUtils.isBlank(email)) {
setConfirmationStatus(); //for some reason is removes it.
//FIXME update existing to void
emailField.setReadOnly(true);
emailLayout.replace(saveEmailButton, editEmailButton);
return;
}

if (StringUtils.isBlank(email)) {
onInvalidInput(emailField, "Please provide valid email");
return;
//is same as current ?
Optional<String> currentEmail = getCurrentEmail();
if (currentEmail.isPresent()) {
boolean isSameAsCurrent = currentEmail.get().equals(email);
if (isSameAsCurrent) {
setConfirmationStatus(); //for some reason is removes it.
emailField.setReadOnly(true);
emailLayout.replace(saveEmailButton, editEmailButton);
return;
}
}

boolean isValidEmail = EmailValidator.getInstance().isValid(email);
Expand All @@ -321,13 +335,13 @@ private void onSaveEmail(final ClickEvent<Button> event) {
OperationResult emailUpdateResult = accountService.updateEmailAccount(user, email);
if (emailUpdateResult.notOk()) {
currentEmailRecord.ifPresent(accountService::rollbackAccount);
if (currentEmailRecord.isPresent()) {
if (currentEmail.isPresent()) {
emailField.setValue(currentEmail.get());
} else {
emailField.setValue("");
}

ErrorUtils.showErrorNotification("Failed to update email. Server error");
return;
}

//creating confirmation token
Expand All @@ -339,6 +353,7 @@ private void onSaveEmail(final ClickEvent<Button> event) {
currentEmailRecord.ifPresent(accountService::rollbackAccount);
return;
}

//sending confirmation letter
OperationResult sendConfirmationLetterResult =
userOpsService.sendConfirmationLetter(confirmationToken.get(), email, userAccount);
Expand Down Expand Up @@ -399,6 +414,17 @@ private boolean isCurrentEmailConfirmed() {
return emailAccount.map(Account::isConfirmed).orElse(false);
}

private void setConfirmationStatus() {
boolean hasEmail = getCurrentEmail().isPresent();
if (hasEmail) {
VaadinIcon confirmationStatusIcon = isCurrentEmailConfirmed()
? VaadinIcon.CHECK : VaadinIcon.ELLIPSIS_CIRCLE;
emailField.setSuffixComponent(confirmationStatusIcon.create());
} else {
emailField.setSuffixComponent(new Div());
}
}

private Optional<Account> getTelegramAccount() {
return accountService.getAccount(user, AccountType.TELEGRAM);
}
Expand Down

0 comments on commit f37eea2

Please sign in to comment.