Registration Screen Set User Inactive by Default

Hello,

We are using an older version of the User Registration example for a project. We would like to set new users to be in-active by default.

Looking through the manual and forum I found a similar post, but because I am inexperience with Java I am confused how to implement.

Any suggestions on the best way to implement would be much appreciated.

Here is my RegisterScreen.java

import com.company.sample.service.RegistrationService;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.validation.MethodParametersValidationException;
import com.haulmont.cuba.gui.Notifications;
import com.haulmont.cuba.gui.components.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

/**

  • “Register” screen.
    */
    public class RegisterScreen extends AbstractWindow {

    @Inject
    private PasswordField passwordField;
    @Inject
    private RegistrationService registrationService;
    @Inject
    private TextField regPhoneNumberCell;
    @Inject
    private TextField regDepartment;
    @Inject
    private TextField regOriNum;
    @Inject
    private TextField regPhoneNumberLand;
    @Inject
    private TextField regEmail;
    @Inject
    private TextField regFirstName;
    @Inject
    private TextField regLastName;
    @Inject
    private Notifications notifications;
    @Inject
    private DataManager dataManager;

    /**

    • “Cancel” button click handler.
      */
      public void onCancelBtnClick() {
      close(CLOSE_ACTION_ID);
      }

    /**

    • “OK” button click handler.
      */
      public void onOkBtnClick() {
      String regEmail = getEmail();
      String regPhoneNumberCell = getPhoneNumberCell();
      String regPhoneNumberLand = getPhoneNumberLand();
      String tollPhoneNumberLand = getPhoneNumberLand();
      String tollPhoneNumberCell = getPhoneNumberCell();
      String regDepartment = getDepartment();
      String regOriNum = getOriNum();
      String regPassword = getPassword();

      if (!isValidEmail(regEmail) ) {
      showNotification(“Error - Email Incorrect”, NotificationType.HUMANIZED);
      return;
      }

      if (!isValidPasswordLength(regPassword) ) {
      showNotification(“Error - Password should be more than 6 characters and consist of letters, numbers”, NotificationType.HUMANIZED);
      return;
      }

      if (!isValidDepartment(regDepartment) ) {
      showNotification(“Error - Department required”, NotificationType.HUMANIZED);
      return;
      }

      if (!isValidOriNum(regOriNum) ) {
      showNotification(“Error - OriNum required”, NotificationType.HUMANIZED);
      return;
      }

      if (!isValidCellPhone(regPhoneNumberCell)) {
      showNotification(“Error - Invalid Cell Number”, NotificationType.HUMANIZED);
      return;
      }

      if (isTollNumberCell(tollPhoneNumberCell) ) {
      showNotification("Error - Toll numbers are not invalid ", NotificationType.HUMANIZED);
      return;
      }

      if (!isValidLandPhone(regPhoneNumberLand)){
      showNotification(“Error - Invalid Land Number”, NotificationType.HUMANIZED);
      return;
      }

      if (isTollNumberLand(tollPhoneNumberLand) ) {
      showNotification("Error - Toll numbers are not invalid ", NotificationType.HUMANIZED);
      return;
      }

       if      (isValidEmail(regEmail) &&
               isValidPasswordLength(regPassword) &&
               isValidCellPhone(regPhoneNumberCell) &&
               isValidLandPhone(regPhoneNumberLand) &&
               !isTollNumberLand(tollPhoneNumberLand) &&
               !isTollNumberCell(tollPhoneNumberCell) &&
               isValidDepartment(regDepartment)&&
               isValidOriNum(regOriNum)
       )
      

      try {
      registrationService.registerUser(getLogin(), getPassword(),getFirstName(),getLastName(),getEmail(), getPhoneNumberCell(), getPhoneNumberLand(), getDepartment(),getOriNum());
      showNotification("Created user " + getLogin(), NotificationType.TRAY);

       close(COMMIT_ACTION_ID);
      

      } catch (MethodParametersValidationException e) {
      showNotification(messages.getMessage(“com.company.sample.validation”, “UserExistsValidator.message”), NotificationType.HUMANIZED);
      }

      else {
      showNotification(“Complete Required Fields”, NotificationType.HUMANIZED);

      }
      }

    /**

    • @return entered login
      */
      public String getLogin() {
      return regEmail.getValue().toString();
      }

    /**

    • @return entered password
      */
      public String getPassword() {
      return passwordField.getValue();
      }

    public String getFirstName() {
    return regFirstName.getValue();
    }
    public String getLastName() {
    return regLastName.getValue();
    }
    public String getEmail() { return regEmail.getValue(); }
    public String getPhoneNumberCell() { return regPhoneNumberCell.getValue(); }
    public String getPhoneNumberLand() { return regPhoneNumberLand.getValue(); }
    public String getOriNum() { return regOriNum.getValue(); }
    public String getDepartment() { return regDepartment.getValue(); }
    public void onLookup(Component source) {
    }

    public static boolean isValidDepartment(String regDepartment) {
    //Department required - Validate that field length is > 0
    if (regDepartment != null && regDepartment.length() > 0) {
    String fieldRegex = “[a-z0-9]”;
    Pattern fieldPat = Pattern.compile(fieldRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = fieldPat.matcher(regDepartment);
    return matcher.find();
    }
    return false;
    }

    public static boolean isValidOriNum(String regOriNum) {
    //ORINum required - Validate that field length is > 0
    if (regOriNum != null && regOriNum.length() > 0) {
    String fieldRegex = “[a-z0-9]”;
    Pattern fieldPat = Pattern.compile(fieldRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = fieldPat.matcher(regOriNum);
    return matcher.find();
    }
    return false;
    }

    public static boolean isValidEmail(String regEmail) {
    //Valid e-mails only .gov OR .org
    if (regEmail != null) {
    String emailRegex = “([a-z0-9][-a-z0-9_\+\.][a-z0-9])@([a-z0-9][-a-z0-9\.][a-z0-9]\.(gov|org))”; //Limit to Gov/Org - Previous validation “^[A-Z0-9._%±]+@[A-Z0-9.-]+\.[A-Z]{2,6}$”;
    Pattern emailPat = Pattern.compile(emailRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = emailPat.matcher(regEmail);
    return matcher.find();
    }
    return false;
    }

    public static boolean isValidCellPhone(String regPhoneNumberCell) {
    //US Phone validation
    if (regPhoneNumberCell != null) {
    String phoneRegex = “((\(\d{3}\)?)|(\d{3}))([\s-./]?)(\d{3})([\s-./]?)(\d{4})”;
    Pattern phonePat = Pattern.compile(phoneRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = phonePat.matcher(regPhoneNumberCell);
    return matcher.find();
    }
    return false;
    }

    public static boolean isValidLandPhone(String regPhoneNumberLand) {
    //US Phone validation
    if (regPhoneNumberLand != null) {
    String phoneRegex = “((\(\d{3}\)?)|(\d{3}))([\s-./]?)(\d{3})([\s-./]?)(\d{4})”;
    Pattern phonePat = Pattern.compile(phoneRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = phonePat.matcher(regPhoneNumberLand);
    return matcher.find();
    }
    return false;
    }

    public static boolean isTollNumberLand(String tollPhoneNumberLand) {
    //Toll numbers are not allowed
    if (tollPhoneNumberLand != null) {
    String phoneRegex = “(\+)?(1-)?\(?(\\$|#|800|855|866|877|888)\)?[\\s.-]([0-9]{3})?[\\s.-]\)?[\\s.-][0-9]{3}[\\s.-][0-9]{4}”;
    Pattern phonePat = Pattern.compile(phoneRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = phonePat.matcher(tollPhoneNumberLand);
    return matcher.find();
    }
    return false;
    }

    public static boolean isTollNumberCell(String tollPhoneNumberCell) {
    //Toll numbers are not allowed
    if (tollPhoneNumberCell != null) {
    String phoneRegex = “(\+)?(1-)?\(?(\\$|#|800|855|866|877|888)\)?[\\s.-]([0-9]{3})?[\\s.-]\)?[\\s.-][0-9]{3}[\\s.-][0-9]{4}”;
    Pattern phonePat = Pattern.compile(phoneRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = phonePat.matcher(tollPhoneNumberCell);
    return matcher.find();
    }
    return false;
    }

    public static boolean isValidPasswordLength(String passwordField) {
    if (passwordField != null && passwordField.length() > 6) {
    String fieldRegex = "^[a-z0-9\.@#\$%&!()]+$"; //(only contains letter [a-z] digits[0-9], special characters(@#$%&!())
    Pattern fieldPat = Pattern.compile(fieldRegex,Pattern.CASE_INSENSITIVE);
    Matcher matcher = fieldPat.matcher(passwordField);
    return matcher.find();
    }
    return false;

    }

}

The solution for me was to create a SQL trigger (SQL Server in this instance)

CREATE TRIGGER [dbo].[tr_inactivatenewuser] ON [dbo].[SEC_USER]
AFTER INSERT
AS
BEGIN
UPDATE sec_user
SET active = 0
FROM inserted
WHERE SEC_USER.id = inserted.id
AND SEC_USER.[LOGIN] NOT IN (‘admin’, ‘anonymous’)

END