UserHomeSecuritySettingsPageModContentFactoryImpl.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.views.user.home.pagemode;

  20. import java.util.Arrays;
  21. import java.util.Collections;
  22. import java.util.List;

  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.security.access.annotation.Secured;
  25. import org.springframework.stereotype.Component;
  26. import org.springframework.web.context.request.RequestContextHolder;

  27. import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
  28. import com.hack23.cia.service.api.action.user.ChangePasswordRequest;
  29. import com.hack23.cia.service.api.action.user.DeleteAccountRequest;
  30. import com.hack23.cia.service.api.action.user.DisableGoogleAuthenticatorCredentialRequest;
  31. import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialRequest;
  32. import com.hack23.cia.web.impl.ui.application.action.ViewAction;
  33. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.UserHomeMenuItemFactory;
  34. import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.pagecommands.PageCommandUserHomeConstants;
  35. import com.hack23.cia.web.impl.ui.application.views.common.pagemode.CardInfoRowUtil;
  36. import com.hack23.cia.web.impl.ui.application.views.common.rows.RowUtil;
  37. import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
  38. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.ChangePasswordClickListener;
  39. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.DeleteAccountClickListener;
  40. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.DisableGoogleAuthenticatorCredentialClickListener;
  41. import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.SetGoogleAuthenticatorCredentialClickListener;
  42. import com.jarektoro.responsivelayout.ResponsiveRow;
  43. import com.vaadin.ui.Button.ClickListener;
  44. import com.vaadin.ui.FormLayout;
  45. import com.vaadin.ui.Layout;
  46. import com.vaadin.ui.MenuBar;
  47. import com.vaadin.ui.Panel;
  48. import com.vaadin.ui.VerticalLayout;

  49. /**
  50.  * The Class UserHomeSecuritySettingsPageModContentFactoryImpl.
  51.  */
  52. @Component
  53. public final class UserHomeSecuritySettingsPageModContentFactoryImpl extends AbstractUserHomePageModContentFactoryImpl {


  54.     /** The Constant AS_LIST. */
  55.     private static final List<String> AS_LIST = Collections.singletonList("userpassword");


  56.     /** The user home menu item factory. */
  57.     @Autowired
  58.     private UserHomeMenuItemFactory userHomeMenuItemFactory;

  59.     /**
  60.      * Instantiates a new user home security settings page mod content factory impl.
  61.      */
  62.     public UserHomeSecuritySettingsPageModContentFactoryImpl() {
  63.         super();
  64.     }

  65.     /**
  66.      * Creates the change password button.
  67.      *
  68.      * @return the vertical layout
  69.      */
  70.     private VerticalLayout createChangePasswordButton() {

  71.         final VerticalLayout formLayout = new VerticalLayout();
  72.         formLayout.setSizeFull();

  73.         final Panel formPanel = new Panel();
  74.         formPanel.setSizeFull();

  75.         formLayout.addComponent(formPanel);

  76.         final FormLayout formContent = new FormLayout();
  77.         formPanel.setContent(formContent);

  78.         final ChangePasswordRequest request = new ChangePasswordRequest();
  79.         request.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
  80.         request.setCurrentPassword("");
  81.         request.setNewPassword("");
  82.         request.setRepeatNewPassword("");

  83.         final ClickListener listener = new ChangePasswordClickListener(request);
  84.         getFormFactory().addRequestInputFormFields(formContent, request,
  85.                 ChangePasswordRequest.class, Arrays.asList("currentPassword","newPassword","repeatNewPassword"), "Change password", listener);

  86.         return formLayout;
  87.     }

  88.     @Secured({ "ROLE_USER", "ROLE_ADMIN" })
  89.     @Override
  90.     public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
  91.         final VerticalLayout panelContent = createPanelContent();

  92.         final String pageId = getPageId(parameters);

  93.         userHomeMenuItemFactory.createUserHomeMenuBar(menuBar, pageId);
  94.         CardInfoRowUtil.createPageHeader(panel, panelContent,
  95.             UserHomeViewConstants.TITLE_PREFIX + UserHomeViewConstants.SECURITY_SETTINGS_TITLE,
  96.             UserHomeViewConstants.SECURITY_SETTINGS_TITLE,
  97.             UserHomeViewConstants.SECURITY_SETTINGS_DESC);


  98.         final VerticalLayout overviewLayout = new VerticalLayout();
  99.         overviewLayout.setSizeFull();
  100.         panelContent.addComponent(overviewLayout);
  101.         panelContent.setExpandRatio(overviewLayout, ContentRatio.LARGE);

  102.         final ResponsiveRow grid = RowUtil.createGridLayout(overviewLayout);

  103.         RowUtil.createRowComponent(grid, createChangePasswordButton(), UserHomeViewConstants.LABEL_CHANGE_PASSWORD);
  104.         RowUtil.createRowComponent(grid, createEnableGoogleAuthButton(), UserHomeViewConstants.LABEL_ENABLE_MFA);
  105.         RowUtil.createRowComponent(grid, createDisableGoogleAuthButton(), UserHomeViewConstants.LABEL_DISABLE_MFA);
  106.         RowUtil.createRowComponent(grid, createDeleteAccountButton(), UserHomeViewConstants.LABEL_DELETE_ACCOUNT);

  107.         getPageActionEventHelper().createPageEvent(ViewAction.VISIT_USER_HOME_VIEW, ApplicationEventGroup.USER, NAME,
  108.                 parameters, pageId);

  109.         return panelContent;

  110.     }

  111.     /**
  112.      * Creates the disable google auth button.
  113.      *
  114.      * @return the vertical layout
  115.      */
  116.     private VerticalLayout createDisableGoogleAuthButton() {

  117.         final VerticalLayout formLayout = new VerticalLayout();
  118.         formLayout.setSizeFull();

  119.         final Panel formPanel = new Panel();
  120.         formPanel.setSizeFull();

  121.         formLayout.addComponent(formPanel);

  122.         final FormLayout formContent = new FormLayout();
  123.         formPanel.setContent(formContent);

  124.         final DisableGoogleAuthenticatorCredentialRequest request = new DisableGoogleAuthenticatorCredentialRequest();
  125.         request.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
  126.         request.setUserpassword("");
  127.         final ClickListener listener = new DisableGoogleAuthenticatorCredentialClickListener(request);
  128.         getFormFactory().addRequestInputFormFields(formContent, request,
  129.                 DisableGoogleAuthenticatorCredentialRequest.class, AS_LIST, UserHomeViewConstants.BUTTON_DISABLE_GOOGLE_AUTH, listener);

  130.         return formLayout;
  131.     }

  132.     /**
  133.      * Creates the enable google auth button.
  134.      *
  135.      * @return the vertical layout
  136.      */
  137.     private VerticalLayout createEnableGoogleAuthButton() {
  138.         final VerticalLayout formLayout = new VerticalLayout();
  139.         formLayout.setSizeFull();

  140.         final Panel formPanel = new Panel();
  141.         formPanel.setSizeFull();

  142.         formLayout.addComponent(formPanel);

  143.         final FormLayout formContent = new FormLayout();
  144.         formPanel.setContent(formContent);

  145.         final SetGoogleAuthenticatorCredentialRequest request = new SetGoogleAuthenticatorCredentialRequest();
  146.         request.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
  147.         request.setUserpassword("");
  148.         final ClickListener listener = new SetGoogleAuthenticatorCredentialClickListener(request);
  149.         getFormFactory().addRequestInputFormFields(formContent, request, SetGoogleAuthenticatorCredentialRequest.class,
  150.                 AS_LIST, UserHomeViewConstants.BUTTON_ENABLE_GOOGLE_AUTH, listener);

  151.         return formLayout;
  152.     }

  153.     /**
  154.      * Creates the delete account button.
  155.      *
  156.      * @return the vertical layout
  157.      */
  158.     private VerticalLayout createDeleteAccountButton() {

  159.         final VerticalLayout formLayout = new VerticalLayout();
  160.         formLayout.setSizeFull();

  161.         final Panel formPanel = new Panel();
  162.         formPanel.setSizeFull();

  163.         formLayout.addComponent(formPanel);

  164.         final FormLayout formContent = new FormLayout();
  165.         formPanel.setContent(formContent);

  166.         final DeleteAccountRequest request = new DeleteAccountRequest();
  167.         request.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
  168.         request.setUserpassword("");
  169.         final ClickListener listener = new DeleteAccountClickListener(request);
  170.         getFormFactory().addRequestInputFormFields(formContent, request,
  171.                 DeleteAccountRequest.class, AS_LIST, UserHomeViewConstants.BUTTON_DELETE_ACCOUNT, listener);

  172.         return formLayout;
  173.     }


  174.     @Override
  175.     public boolean matches(final String page, final String parameters) {
  176.         return PageCommandUserHomeConstants.COMMAND_USERHOME_SECURITY_SETTINGS.matches(page, parameters);
  177.     }

  178. }