Пример #1
0
 /** @return A file upload form that links to the current page. */
 @Nonnull
 @Deprecated
 public HCForm createFormFileUploadSelf() {
   final HCForm aForm = createFormSelf();
   aForm.setFileUploadEncType();
   return aForm;
 }
  @Override
  protected boolean handleCustomActions(
      @Nonnull final WebPageExecutionContext aWPEC, @Nullable final IUser aSelectedObject) {
    if (aWPEC.hasAction(ACTION_RESET_PASSWORD) && aSelectedObject != null) {
      if (!canResetPassword(aSelectedObject)) throw new IllegalStateException("Won't work!");

      final Locale aDisplayLocale = aWPEC.getDisplayLocale();
      final boolean bShowForm = true;
      final FormErrors aFormErrors = new FormErrors();
      if (aWPEC.hasSubAction(ACTION_PERFORM)) {
        final String sPlainTextPassword = aWPEC.getAttr(FIELD_PASSWORD);
        final String sPlainTextPasswordConfirm = aWPEC.getAttr(FIELD_PASSWORD_CONFIRM);

        final List<String> aPasswordErrors =
            GlobalPasswordSettings.getPasswordConstraintList()
                .getInvalidPasswordDescriptions(sPlainTextPassword, aDisplayLocale);
        for (final String sPasswordError : aPasswordErrors)
          aFormErrors.addFieldError(FIELD_PASSWORD, sPasswordError);
        if (!EqualsUtils.equals(sPlainTextPassword, sPlainTextPasswordConfirm))
          aFormErrors.addFieldError(
              FIELD_PASSWORD_CONFIRM,
              EText.ERROR_PASSWORDS_DONT_MATCH.getDisplayText(aDisplayLocale));

        if (aFormErrors.isEmpty()) {
          AccessManager.getInstance().setUserPassword(aSelectedObject.getID(), sPlainTextPassword);
          aWPEC
              .getNodeList()
              .addChild(
                  getStyler()
                      .createSuccessBox(
                          EText.SUCCESS_RESET_PASSWORD.getDisplayTextWithArgs(
                              aDisplayLocale,
                              SecurityUI.getUserDisplayName(aSelectedObject, aDisplayLocale))));
          return true;
        }
      }
      if (bShowForm) {
        // Show input form
        final boolean bHasAnyPasswordConstraint =
            GlobalPasswordSettings.getPasswordConstraintList().hasConstraints();
        final HCForm aForm = aWPEC.getNodeList().addAndReturnChild(createFormSelf());
        final IHCTableForm<?> aTable =
            aForm.addAndReturnChild(
                getStyler().createTableForm(new HCCol(200), HCCol.star(), new HCCol(20)));
        aTable.setSpanningHeaderContent(
            EText.TITLE_RESET_PASSWORD.getDisplayTextWithArgs(
                aDisplayLocale, SecurityUI.getUserDisplayName(aSelectedObject, aDisplayLocale)));

        final String sPassword = EText.LABEL_PASSWORD.getDisplayText(aDisplayLocale);
        aTable
            .createItemRow()
            .setLabel(
                sPassword, bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
            .setCtrl(new HCEditPassword(FIELD_PASSWORD).setPlaceholder(sPassword))
            .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
            .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD));

        final String sPasswordConfirm = EText.LABEL_PASSWORD_CONFIRM.getDisplayText(aDisplayLocale);
        aTable
            .createItemRow()
            .setLabel(
                sPasswordConfirm,
                bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
            .setCtrl(new HCEditPassword(FIELD_PASSWORD_CONFIRM).setPlaceholder(sPasswordConfirm))
            .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
            .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD_CONFIRM));

        final IButtonToolbar<?> aToolbar = aForm.addAndReturnChild(getStyler().createToolbar());
        aToolbar.addHiddenField(CHCParam.PARAM_ACTION, ACTION_RESET_PASSWORD);
        aToolbar.addHiddenField(CHCParam.PARAM_OBJECT, aSelectedObject.getID());
        aToolbar.addHiddenField(CHCParam.PARAM_SUBACTION, ACTION_PERFORM);
        aToolbar.addSubmitButtonSave(aDisplayLocale);
        aToolbar.addButtonCancel(aDisplayLocale);
      }
      return false;
    }
    return true;
  }
  @SuppressWarnings("null")
  @Override
  protected void showInputForm(
      @Nonnull final WebPageExecutionContext aWPEC,
      @Nullable final IUser aSelectedObject,
      @Nonnull final HCForm aForm,
      final boolean bEdit,
      final boolean bCopy,
      @Nonnull final FormErrors aFormErrors) {
    if (bEdit && !isEditAllowed(aSelectedObject)) throw new IllegalStateException("Won't work!");

    final boolean bIsAdministrator = aSelectedObject != null && aSelectedObject.isAdministrator();
    final Locale aDisplayLocale = aWPEC.getDisplayLocale();
    final AccessManager aMgr = AccessManager.getInstance();
    final IHCTableForm<?> aTable =
        aForm.addAndReturnChild(
            getStyler().createTableForm(new HCCol(170), HCCol.star(), new HCCol(20)));
    aTable.setSpanningHeaderContent(
        bEdit
            ? EText.TITLE_EDIT.getDisplayTextWithArgs(
                aDisplayLocale, SecurityUI.getUserDisplayName(aSelectedObject, aDisplayLocale))
            : EText.TITLE_CREATE.getDisplayText(aDisplayLocale));

    if (!useEmailAddressAsLoginName()) {
      final String sLoginName = EText.LABEL_LOGINNAME.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabelMandatory(sLoginName)
          .setCtrl(
              new HCEdit(
                      new RequestField(
                          FIELD_LOGINNAME,
                          aSelectedObject == null ? null : aSelectedObject.getLoginName()))
                  .setPlaceholder(sLoginName))
          .setErrorList(aFormErrors.getListOfField(FIELD_LOGINNAME));
    }

    {
      final String sFirstName = EText.LABEL_FIRSTNAME.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabel(sFirstName)
          .setCtrl(
              new HCEdit(
                      new RequestField(
                          FIELD_FIRSTNAME,
                          aSelectedObject == null ? null : aSelectedObject.getFirstName()))
                  .setPlaceholder(sFirstName))
          .setErrorList(aFormErrors.getListOfField(FIELD_FIRSTNAME));
    }

    {
      final String sLastName = EText.LABEL_LASTNAME.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabel(sLastName, isLastNameMandatory() ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
          .setCtrl(
              new HCEdit(
                      new RequestField(
                          FIELD_LASTNAME,
                          aSelectedObject == null ? null : aSelectedObject.getLastName()))
                  .setPlaceholder(sLastName))
          .setErrorList(aFormErrors.getListOfField(FIELD_LASTNAME));
    }

    {
      final String sEmail = EText.LABEL_EMAIL.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabel(sEmail, isEmailMandatory() ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
          .setCtrl(
              new HCEdit(
                      new RequestField(
                          FIELD_EMAILADDRESS,
                          aSelectedObject == null ? null : aSelectedObject.getEmailAddress()))
                  .setPlaceholder(sEmail))
          .setErrorList(aFormErrors.getListOfField(FIELD_EMAILADDRESS));
    }

    if (!bEdit) {
      // Password is only shown on creation of a new user
      final boolean bHasAnyPasswordConstraint =
          GlobalPasswordSettings.getPasswordConstraintList().hasConstraints();

      final String sPassword = EText.LABEL_PASSWORD.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabel(
              sPassword, bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
          .setCtrl(new HCEditPassword(FIELD_PASSWORD).setPlaceholder(sPassword))
          .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
          .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD));

      final String sPasswordConfirm = EText.LABEL_PASSWORD_CONFIRM.getDisplayText(aDisplayLocale);
      aTable
          .createItemRow()
          .setLabel(
              sPasswordConfirm,
              bHasAnyPasswordConstraint ? ELabelType.MANDATORY : ELabelType.OPTIONAL)
          .setCtrl(new HCEditPassword(FIELD_PASSWORD_CONFIRM).setPlaceholder(sPasswordConfirm))
          .setNote(SecurityUI.createPasswordConstraintTip(aDisplayLocale))
          .setErrorList(aFormErrors.getListOfField(FIELD_PASSWORD_CONFIRM));
    }

    if (bIsAdministrator) {
      // Cannot edit enabled state of administrator
      aTable
          .createItemRow()
          .setLabel(EText.LABEL_ENABLED.getDisplayText(aDisplayLocale))
          .setCtrl(EWebBasicsText.getYesOrNo(aSelectedObject.isEnabled(), aDisplayLocale));
    } else {
      aTable
          .createItemRow()
          .setLabelMandatory(EText.LABEL_ENABLED.getDisplayText(aDisplayLocale))
          .setCtrl(
              new HCCheckBox(
                  new RequestFieldBoolean(
                      FIELD_ENABLED,
                      aSelectedObject == null ? DEFAULT_ENABLED : aSelectedObject.isEnabled())))
          .setErrorList(aFormErrors.getListOfField(FIELD_ENABLED));
    }

    {
      final Collection<String> aUserGroupIDs =
          aSelectedObject == null
              ? aWPEC.getAttrs(FIELD_USERGROUPS)
              : aMgr.getAllUserGroupIDsWithAssignedUser(aSelectedObject.getID());
      final UserGroupForUserSelect aSelect =
          new UserGroupForUserSelect(
              new RequestField(FIELD_USERGROUPS), aDisplayLocale, aUserGroupIDs);
      aTable
          .createItemRow()
          .setLabelMandatory(EText.LABEL_USERGROUPS_0.getDisplayText(aDisplayLocale))
          .setCtrl(aSelect)
          .setErrorList(aFormErrors.getListOfField(FIELD_USERGROUPS));
      if (bIsAdministrator) {
        // Cannot edit user groups of administrator
        aSelect.setReadonly(true);
      }
      showInputFormModifyUserGroupSelect(aSelect);
    }

    // Custom overridable
    showInputFormEnd(aWPEC, aSelectedObject, aForm, bEdit, bCopy, aFormErrors, aTable);
  }
  @Override
  protected final void fillContent(@Nonnull final WPECTYPE aWPEC) {
    final HCNodeList aNodeList = aWPEC.getNodeList();

    // Get the selected object
    final DATATYPE aSelectedObject = getSelectedObject(aWPEC, getSelectedObjectID(aWPEC));

    final boolean bIsEditAllowed = isEditAllowed(aWPEC, aSelectedObject);
    boolean bShowList = true;
    final String sAction = aWPEC.getAction();
    EWebPageFormAction eFormAction = null;
    if (ACTION_VIEW.equals(sAction) && aSelectedObject != null)
      eFormAction = EWebPageFormAction.VIEW;
    else if (ACTION_CREATE.equals(sAction)) eFormAction = EWebPageFormAction.CREATE;
    else if (ACTION_EDIT.equals(sAction) && bIsEditAllowed && aSelectedObject != null)
      eFormAction = EWebPageFormAction.EDIT;
    else if (ACTION_COPY.equals(sAction) && aSelectedObject != null)
      eFormAction = EWebPageFormAction.COPY;
    else if (ACTION_DELETE.equals(sAction) && aSelectedObject != null)
      eFormAction = EWebPageFormAction.DELETE;
    else if (ACTION_UNDELETE.equals(sAction) && aSelectedObject != null)
      eFormAction = EWebPageFormAction.UNDELETE;

    // Try to lock object
    if (beforeProcessing(aWPEC, aSelectedObject, eFormAction).isContinue()) {
      if (eFormAction == EWebPageFormAction.VIEW) {
        // Valid object found - show details
        handleViewObject(aWPEC, aSelectedObject, bIsEditAllowed);

        bShowList = false;
      } else {
        if (eFormAction == EWebPageFormAction.CREATE
            || eFormAction == EWebPageFormAction.EDIT
            || eFormAction == EWebPageFormAction.COPY) {
          final boolean bIsEdit = eFormAction == EWebPageFormAction.EDIT;
          final boolean bIsCopy = eFormAction == EWebPageFormAction.COPY;

          // Create or edit a client
          final FormErrors aFormErrors = new FormErrors();
          boolean bShowInputForm = true;

          if (aWPEC.hasSubAction(CHCParam.ACTION_SAVE)) {
            // try to save
            validateAndSaveInputParameters(aWPEC, aSelectedObject, aFormErrors, bIsEdit);
            if (aFormErrors.isEmpty()) {
              // Save successful
              bShowInputForm = false;

              // Remove an optionally stored state
              FormStateManager.getInstance()
                  .deleteFormState(aWPEC.getAttributeAsString(FIELD_FLOW_ID));
            } else {
              // Show: changes could not be saved...
              aNodeList.addChild(getStyler().createIncorrectInputBox(aWPEC));
            }
          }

          if (bShowInputForm) {
            // Show the input form. Either for the first time or because of form
            // errors a n-th time
            bShowList = false;
            final HCForm aForm =
                isFileUploadForm(aWPEC) ? createFormFileUploadSelf(aWPEC) : createFormSelf(aWPEC);
            aForm.setID(INPUT_FORM_ID);
            aNodeList.addChild(aForm);

            // The unique form ID, that allows to identify on "transaction"
            // -> Used only for "form state remembering"
            aForm.addChild(
                new HCHiddenField(
                    new RequestField(FIELD_FLOW_ID, GlobalIDFactory.getNewStringID())));

            modifyFormBeforeShowInputForm(aWPEC, aForm);

            // Is there as saved state to use?
            final String sRestoreFlowID = aWPEC.getAttributeAsString(FIELD_RESTORE_FLOW_ID);
            if (sRestoreFlowID != null) {
              final FormState aSavedState =
                  FormStateManager.getInstance().getFormStateOfID(sRestoreFlowID);
              if (aSavedState != null) {
                // Restore all form values
                aForm.addChild(
                    new HCScriptOnDocumentReady(
                        JSFormHelper.setAllFormValues(
                            INPUT_FORM_ID, aSavedState.getAsAssocArray())));
              }
            }

            // Show the main input form
            showInputForm(aWPEC, aSelectedObject, aForm, bIsEdit, bIsCopy, aFormErrors);

            // Toolbar on bottom
            if (bIsEdit) {
              if (showEditToolbar(aWPEC, aSelectedObject))
                aForm.addChild(createEditToolbar(aWPEC, aForm, aSelectedObject));
            } else {
              if (showCreateToolbar(aWPEC, aSelectedObject))
                aForm.addChild(createCreateToolbar(aWPEC, aForm, aSelectedObject));
            }
          }
        } else if (eFormAction == EWebPageFormAction.DELETE) {
          bShowList = handleDeleteAction(aWPEC, aSelectedObject);
        } else if (eFormAction == EWebPageFormAction.UNDELETE) {
          bShowList = handleUndeleteAction(aWPEC, aSelectedObject);
        } else {
          // Other proprietary actions
          bShowList = handleCustomActions(aWPEC, aSelectedObject);
        }
      }
    }

    if (bShowList) {
      showListOfExistingObjects(aWPEC);
    }

    // Call after everything
    afterProcessing(aWPEC, aSelectedObject, eFormAction);
  }
Пример #5
0
 /** @return A file upload form that links to the current page. */
 @Nonnull
 public HCForm createFormFileUploadSelf(@Nonnull final ILayoutExecutionContext aLEC) {
   final HCForm aForm = createFormSelf(aLEC);
   aForm.setFileUploadEncType();
   return aForm;
 }