View Javadoc
1   /*
2    * Copyright 2014 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  
21  import org.springframework.beans.factory.annotation.Autowired;
22  import org.springframework.security.access.annotation.Secured;
23  import org.springframework.stereotype.Component;
24  import org.springframework.web.context.request.RequestContextHolder;
25  
26  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
27  import com.hack23.cia.service.api.action.user.SetGoogleAuthenticatorCredentialRequest;
28  import com.hack23.cia.web.impl.ui.application.action.ViewAction;
29  import com.hack23.cia.web.impl.ui.application.util.UserContextUtil;
30  import com.hack23.cia.web.impl.ui.application.views.common.labelfactory.LabelFactory;
31  import com.hack23.cia.web.impl.ui.application.views.common.menufactory.api.UserHomeMenuItemFactory;
32  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.CommonsViews;
33  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.UserHomePageMode;
34  import com.hack23.cia.web.impl.ui.application.views.pageclicklistener.SetGoogleAuthenticatorCredentialClickListener;
35  import com.vaadin.server.FontAwesome;
36  import com.vaadin.ui.Button;
37  import com.vaadin.ui.Layout;
38  import com.vaadin.ui.MenuBar;
39  import com.vaadin.ui.Panel;
40  import com.vaadin.ui.UI;
41  import com.vaadin.ui.VerticalLayout;
42  
43  /**
44   * The Class UserHomeSecuritySettingsPageModContentFactoryImpl.
45   */
46  @Component
47  public final class UserHomeSecuritySettingsPageModContentFactoryImpl extends AbstractUserHomePageModContentFactoryImpl {
48  
49  	/** The Constant ENABLE_GOOGLE_AUTHENTICATOR. */
50  	private static final String ENABLE_GOOGLE_AUTHENTICATOR = "Enable Google Authenticator";
51  
52  	/** The Constant USERHOME. */
53  	private static final String USERHOME = "Userhome:";
54  
55  	/** The Constant SECURITY_SETTINGS. */
56  	private static final String SECURITY_SETTINGS = "Security Settings";
57  
58  	/** The user home menu item factory. */
59  	@Autowired
60  	private UserHomeMenuItemFactory userHomeMenuItemFactory;
61  
62  	/**
63  	 * Instantiates a new user home security settings page mod content factory
64  	 * impl.
65  	 */
66  	public UserHomeSecuritySettingsPageModContentFactoryImpl() {
67  		super();
68  	}
69  
70  	@Override
71  	public boolean matches(final String page, final String parameters) {
72  		return NAME.equals(page) && parameters.contains(UserHomePageMode.SECURITY_SETTINGS.toString());
73  	}
74  
75  	@Secured({ "ROLE_USER", "ROLE_ADMIN" })
76  	@Override
77  	public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
78  		final VerticalLayout panelContent = createPanelContent();
79  
80  		final String pageId = getPageId(parameters);
81  
82  		userHomeMenuItemFactory.createUserHomeMenuBar(menuBar, pageId);
83  
84  		LabelFactory.createHeader2Label(panelContent, SECURITY_SETTINGS);
85  
86  		final Long userIdFromSecurityContext = UserContextUtil.getUserInternalIdFromSecurityContext();
87  
88  		if (userIdFromSecurityContext == null) {
89  			UI.getCurrent().getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
90  		} else {
91  
92  			final Button googleAuthButton = new Button(ENABLE_GOOGLE_AUTHENTICATOR, FontAwesome.USER_SECRET);
93  			googleAuthButton.setId(ENABLE_GOOGLE_AUTHENTICATOR);
94  
95  			final SetGoogleAuthenticatorCredentialRequest googleAuthRequest = new SetGoogleAuthenticatorCredentialRequest();
96  			googleAuthRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
97  			googleAuthButton.addClickListener(
98  					new SetGoogleAuthenticatorCredentialClickListener(googleAuthRequest));
99  
100 			panelContent.addComponent(googleAuthButton);
101 
102 		}
103 
104 		panel.setCaption(USERHOME + SECURITY_SETTINGS);
105 
106 		getPageActionEventHelper().createPageEvent(ViewAction.VISIT_USER_HOME_VIEW, ApplicationEventGroup.USER, NAME,
107 				parameters, pageId);
108 
109 		return panelContent;
110 
111 	}
112 
113 }