Forgot Password

Hi,
I use Cuba 7.2.15. I’m trying to configure allow the user to reset password if forgot it. I saw that add-on “forgot Password” is no compatible with Cuba 7.2.15. Is there any other add-ons for this Cuba version or a guide step by step?
Thanks

Hi,
The latest compatible version of the add-on is 7.1, which is not far away from the 7.2.15.

You can take the free source code of the add-on: GitHub - cubait/cuba-component-forgot-password: A CUBA framework component that adds forgot password functionality to CUBA. It also offer a REST API to be consumed by third party applications or by JS clients.
and adapt it to your project (copy functionality that you need).

Or you may adapt the add-on to the CUBA 7.2 yourself.

Hi Alexander,
I implemented the code to reset the password based on GitHub - cuba-platform/sample-user-registration: Example of users self-registration , but I have the error “the user does not exist”.
I configured the properties of cuba.email and to allow the user Anonymous change reset password.
I extend the login

ExtLogingScreen.java

@Subscribe("restorePasswordBtn")
public void onRestorePasswordBtnClick(Button.ClickEvent event) {
    // Create "RestorePassword" screen with dialog mode
    RestorePasswordScreen restorePasswordScreen = screens.create(RestorePasswordScreen.class, OpenMode.DIALOG);
    restorePasswordScreen.show();
    // Add a listener to be notified when the "Restore password" screen is closed with COMMIT_ACTION_ID
    restorePasswordScreen.addAfterCloseListener(afterCloseEvent -> {
        CloseAction closeAction = afterCloseEvent.getCloseAction();
        if (closeAction == WINDOW_COMMIT_AND_CLOSE_ACTION) {
              loginField.setValue(restorePasswordScreen.getLogin());
            // clear password field
             passwordField.setValue(null);
            // Set focus in login field
             passwordField.focus();
        }
    });
}

}

ExtLogingScreen.xml

<actions>
    <action id="submit"
            caption="mainMsg://loginWindow.okButton"
            icon="app/images/login-button.png"
            invoke="login" shortcut="ENTER"/>
</actions>
<layout stylename="c-login-main-layout" expand="loginWrapper">
    <vbox id="loginWrapper">
        <vbox id="loginMainBox"
              align="MIDDLE_CENTER"
              margin="true"
              stylename="c-login-panel"
              width="AUTO">
            <hbox id="loginTitleBox"
                  align="MIDDLE_CENTER"
                  spacing="true"
                  stylename="c-login-title">
                <image id="logoImage"
                       align="MIDDLE_LEFT"
                       height="AUTO"
                       scaleMode="SCALE_DOWN"
                       stylename="c-login-icon"
                       width="AUTO"/>
                <label id="welcomeLabel"
                       align="MIDDLE_LEFT"
                       stylename="c-login-caption"
                       value="mainMsg://loginWindow.welcomeLabel"/>
            </hbox>
            <capsLockIndicator id="capsLockIndicator"
                               align="MIDDLE_CENTER"
                               stylename="c-login-capslockindicator"/>
            <vbox id="loginForm"
                  spacing="true"
                  stylename="c-login-form">
                <cssLayout id="loginCredentials"
                           stylename="c-login-credentials">
                    <textField id="loginField"
                               htmlName="loginField"
                               inputPrompt="mainMsg://loginWindow.loginPlaceholder"
                               stylename="c-login-username"/>
                    <passwordField id="passwordField"
                                   autocomplete="true"
                                   htmlName="passwordField"
                                   inputPrompt="mainMsg://loginWindow.passwordPlaceholder"
                                   capsLockIndicator="capsLockIndicator"
                                   stylename="c-login-password"/>
                </cssLayout>
                <hbox id="rememberLocalesBox"
                      stylename="c-login-remember-locales">
                    <checkBox id="rememberMeCheckBox"
                              caption="mainMsg://loginWindow.rememberMe"
                              stylename="c-login-remember-me"/>
                    <lookupField id="localesSelect"
                                 nullOptionVisible="false"
                                 stylename="c-login-locale"
                                 textInputAllowed="false"/>
                </hbox>
                <button id="loginButton"
                        align="MIDDLE_CENTER"
                        action="submit"
                        stylename="c-login-submit-button"/>
                <hbox id="additionalActions"
                      align="MIDDLE_CENTER"
                      colspan="2"
                      margin="true,false,false,false"
                      spacing="true">
                    <linkButton id="restorePasswordBtn"
                                caption="Restore password"/>
                </hbox>
            </vbox>
        </vbox>
    </vbox>
    <label id="poweredByLink"
           align="MIDDLE_CENTER"
           htmlEnabled="true"
           stylename="c-powered-by"
           value="mainMsg://cuba.poweredBy"/>
</layout>

RestorePasswordScreen.java

public String getLogin() {
    return loginField.getValue();
}
@Subscribe("okBtn")
public void onOkBtnClick(Button.ClickEvent event) {
    if (validateScreen()) {
        String login = getLogin().toLowerCase();
        LoadContext<User> lc = LoadContext.create(User.class);
        lc.setView(View.MINIMAL);
        lc.setQueryString("select u from sec$User u where u.loginLowerCase = :login and (u.active = true or u.active is null)")
                .setParameter("login", login);
        User targetUser = dataManager.load(lc);
        if (targetUser == null) {
            warningLabel.setVisible(true);
            loginField.focus();
        } else {
       userManagementService.changePasswordsAtLogonAndSendEmails(Collections.singletonList(targetUser.getId()));
            notifications.create(Notifications.NotificationType.TRAY)
                    .withCaption("Success")
                    .withDescription("E-mail with your new password has been sent")
                    .show();
            close(WINDOW_COMMIT_AND_CLOSE_ACTION);
        }
    }
}

@Subscribe("cancelBtn")
public void onCancelBtnClick1(Button.ClickEvent event) {
    close(WINDOW_CLOSE_ACTION);
}
private boolean validateScreen() {
    ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.NAME);
    ValidationErrors errors = screenValidation.validateUiComponents(getWindow());
    if (errors.isEmpty())
        return true;
    screenValidation.showValidationErrors(this, errors);
    return false;
}

RestorePasswordScreen.xml

<window focusComponent="loginField">
<dialogMode width="AUTO"/>
<layout spacing="true">
<label id="warningLabel"
       icon="font-icon:WARNING"
       visible="false"
       value="El usuario no existe"/>

<textField id="loginField"
           width="300px"
           caption="Login"
           required="true"
           requiredMessage="Por favor, introduce tu login"/>

<hbox align="MIDDLE_CENTER"
      colspan="2"
      spacing="true">
    <button id="okBtn"
            align="TOP_RIGHT"
            caption="Envía nueva contraseña"/>
    <button id="cancelBtn"
            caption="Cancel"/>
</hbox>
</layout>

It’s hard to understand what’s wrong based only on this message.
The full stack trace would help - it will contain the problematic place in the code.

Hi
when I click on Restore Password in the Login Screen and enter the user’s login I tried the error “Exception in UserManagementService.changePasswordsAtLogonAndSendEmails(…): com.haulmont.cuba.core.global.AccessDeniedException: ENTITY_OP sec$User”.

On the other hand, when I enter the application and enter the login in the restore password screen, the tragetUser is loaded with the corresponding user. But it doesn’t send the email. I don’t know why the user can’t load from the Login screen and why it doesn’t send the mail.
I have configured the properties

web-app.properties

cuba.web.allowAnonymousAccess = true
cuba.web.loginScreenId=ExtLogin
cuba.web.linkHandlerActions = open|o|reset

app.properties

cuba.email.fromAddress = vagarcia@properly.es
cuba.email.smtpHost = smtp.Pepipost.net
cuba.email.smtpPort = 465
cuba.email.smtpSslEnabled = true

Allow Anoymus access the RestorePasswordScreen and LoginScreen and allow sec$User Entity.

Thanks

Hi,

You need to create some role that gives UPDATE permission to the sec$User entity, and assign this role to the anonymous user. Currently it doesn’t have necessary permissions.

What methods do you call to send email? Are there any errors in the log when email should be sent?
If no errors, and you are using one of sendEmailAsync() methods - then you need to configure a scheduled task. See points #5 - 7 in this guide: Email Sending Guide - CUBA Platform. Developer’s Manual

It works perfectly. Thank you