Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Refactor LoginAction for readability.
Browse files Browse the repository at this point in the history
Override default login error message.
  • Loading branch information
Carlos A. Munoz committed Sep 10, 2013
1 parent 2b71b53 commit cc63074
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 127 deletions.
124 changes: 34 additions & 90 deletions zanata-war/src/main/java/org/zanata/action/LoginAction.java
Expand Up @@ -33,6 +33,9 @@
import org.zanata.security.ZanataCredentials;
import org.zanata.security.openid.OpenIdProviderType;

import lombok.Getter;
import lombok.Setter;

/**
* This action takes care of logging a user into the system. It contains logic
* to handle the different authentication mechanisms offered by the system.
Expand All @@ -55,122 +58,63 @@ public class LoginAction implements Serializable
@In
private ApplicationConfiguration applicationConfiguration;

@In
private AccountDAO accountDAO;

@In(create = true)
private InactiveAccountAction inactiveAccountAction;

@Getter @Setter
private String username;

@Getter @Setter
private String password;

private String authProvider;


public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

public String getAuthProvider()
{
return authProvider;
}

public void setAuthProvider(String authProvider)
{
this.authProvider = authProvider;
}
@Getter @Setter
private String openId = "http://";

/**
* Prepares authentication credentials based on the passed parameters.
*/
private void prepareCredentials()
public String login()
{
AuthenticationType authType = null;
OpenIdProviderType openIdProviderType = null;

credentials.setUsername( username );
credentials.setPassword( password );

// All others
if (authProvider == null)
credentials.setUsername(username);
credentials.setPassword(password);
if (applicationConfiguration.isInternalAuth())
{
if (applicationConfiguration.isInternalAuth())
{
authType = AuthenticationType.INTERNAL;
}
else if (applicationConfiguration.isJaasAuth())
{
authType = AuthenticationType.JAAS;
}
else if (applicationConfiguration.isKerberosAuth())
{
authType = AuthenticationType.KERBEROS;
}
credentials.setAuthType(AuthenticationType.INTERNAL);
}
// Open Id / internal auth
else
else if (applicationConfiguration.isJaasAuth())
{
try
{
// If it is open Id
openIdProviderType = OpenIdProviderType.valueOf(authProvider);
authType = AuthenticationType.OPENID;
}
catch (Exception e)
{
// If it's not open id, it might be another authentication type
openIdProviderType = null;
authType = AuthenticationType.valueOf(authProvider);
}
credentials.setAuthType(AuthenticationType.JAAS);
}

credentials.setAuthType( authType );
credentials.setOpenIdProviderType( openIdProviderType );
}

public String login()
{
this.prepareCredentials();
String loginResult = null;
String loginResult;

switch (credentials.getAuthType())
{
case OPENID:
loginResult = authenticationManager.openIdLogin();
break;
case INTERNAL:
credentials.setAuthType(AuthenticationType.INTERNAL);
loginResult = authenticationManager.internalLogin();
break;
case JAAS:
credentials.setAuthType(AuthenticationType.JAAS);
loginResult = authenticationManager.jaasLogin();
break;
// Kerberos auth happens on its own
default:
throw new RuntimeException("login() only supports internal or jaas authentication");
}

return loginResult;
}

public String login( String authProvider )
/**
* Only for open id.
* @param authProvider Open Id authentication provider.
*/
public String openIdLogin(String authProvider)
{
this.authProvider = authProvider;
return login();
OpenIdProviderType providerType = OpenIdProviderType.valueOf(authProvider);

if( providerType == OpenIdProviderType.Generic )
{
credentials.setUsername(openId);
}

credentials.setAuthType(AuthenticationType.OPENID);
credentials.setOpenIdProviderType(providerType);
return authenticationManager.openIdLogin();
}
}
@@ -0,0 +1,51 @@
/*
* Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.security;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.international.StatusMessage;

import static org.jboss.seam.annotations.Install.APPLICATION;

/**
* Override the {@link org.jboss.seam.security.FacesSecurityEvents} component to
* alter default values.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("org.jboss.seam.security.facesSecurityEvents")
@Scope(ScopeType.APPLICATION)
@Install(precedence = APPLICATION, classDependencies = "javax.faces.context.FacesContext")
@BypassInterceptors
@Startup
public class FacesSecurityEvents extends org.jboss.seam.security.FacesSecurityEvents
{
@Override
public StatusMessage.Severity getLoginFailedMessageSeverity()
{
return StatusMessage.Severity.ERROR;
}
}
48 changes: 28 additions & 20 deletions zanata-war/src/main/webapp/WEB-INF/template/template.xhtml
Expand Up @@ -18,24 +18,24 @@
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

<title><ui:insert name="page_title" /></title>

<meta name="description" content="" />
<meta name="author" content="" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
<meta http-equiv="cleartype" content="on" />

<link rel="shortcut icon" href="#{applicationConfiguration.webAssetsUrl}/favicon.ico" type="image/x-icon" />

<!--[if gt IE 8]><!-->
<link type="text/css" rel="stylesheet" href="#{applicationConfiguration.webAssetsStyleUrl}" />
<!--<![endif]-->

<!--[if (lt IE 9) & (!IEMobile)]>
<script src="js/vendor/selectivizr-min.js"></script>
<![endif]-->

<title>#{messages['jsf.Zanata']}: <ui:insert name="page_title" /></title>
<link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/fontello/css/fontello.css" />
<link type="text/css" rel="stylesheet"
Expand All @@ -48,23 +48,31 @@
<ui:include src="left_menu.xhtml" />
<ui:include src="right_menu.xhtml" />
<div class="off-canvas__inner">
<ui:include src="banner.xhtml" />

<main role="main" class="l--pad-v-1">
<div class="l__wrapper">
<ui:insert name="center_content" />
</div>
</main>

<footer role="contentinfo" class="l--push--v-2">
<div class="l__wrapper">
</div>
</footer>
</div>
<ui:include src="banner.xhtml" />

<a:outputPanel ajaxRendered="true" layout="block" styleClass="l__wrapper l--push-top-1">
<h:messages id="messages" globalOnly="true" styleClass="list--no-bullets"
errorClass="message--danger l--push-bottom-half"
infoClass="message--highlight l--push-bottom-half"
warnClass="message--warning l--push-bottom-half"
rendered="#{showGlobalMessages != 'false'}"/>
</a:outputPanel>

<main role="main" class="l--pad-v-1">
<div class="l__wrapper">
<ui:insert name="center_content"/>
</div>
</main>

<footer role="contentinfo" class="l--push--v-2">
<div class="l__wrapper">
</div>
</footer>
</div>
</div>

<ui:include src="piwik.xhtml"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="#{applicationConfiguration.webAssetsUrl}/assets/js/script.js"></script>
</h:body>
Expand Down
31 changes: 14 additions & 17 deletions zanata-war/src/main/webapp/account/login.xhtml
Expand Up @@ -11,21 +11,13 @@

<ui:define name="page_title">#{messages['jsf.Login']}</ui:define>
<ui:define name="center_content">
<div>
<a4j:outputPanel ajaxRendered="true">
<h:messages id="messages" globalOnly="true" styleClass="message"
errorClass="errormsg icon-info-circle-2" infoClass="infomsg icon-info-circle-2"
warnClass="warnmsg icon-attention"
rendered="#{showGlobalMessages != 'false'}"/>
</a4j:outputPanel>
</div>
<div class="g--centered">
<div class="g__item w--1-2 w--3-4-l w--2-3-m">
<div class="g__item w--1-2 #{applicationConfiguration.multiAuth ? 'w--3-4-l w--2-3-m' : 'w--2-5-l w--1-4-h'}">
<div class="l--push-v-2">
<h1 class="l--push-all-0 beta">Log In</h1>

<div class="g">
<s:div styleClass="l--push-top-half g__item w--7-12 w--1-m"
<s:div styleClass="l--push-top-half g__item #{applicationConfiguration.multiAuth ? 'w--7-12 w--1-m' : ''}"
rendered="#{applicationConfiguration.internalAuth or applicationConfiguration.jaasAuth}">
<s:fragment rendered="#{applicationConfiguration.multiAuth}">
<h2 class="gamma heading--sub">with Zanata</h2>
Expand All @@ -39,13 +31,18 @@

<div class="l--push-top-half l--clearfix">
<label for="password" class="l--float-left">Password</label>
<span class="txt--meta l--float-right"><a href="#">Forgot password?</a></span>
<span class="txt--meta l--float-right">
<s:link view="/account/password_reset_request.xhtml" propagation="none">
#{messages['jsf.ForgotYourPassword']}
</s:link>
</span>
</div>
<h:inputSecret id="password" value="#{loginAction.password}"/>

<div class="l--push-top-half">
<h:commandButton action="#{loginAction.login}" value="Log In" styleClass="button--primary"/>
<span class="l--push-h-quarter">or</span> <a href="/signup">Sign Up</a>
<span class="l--push-h-quarter">or</span>
<s:link view="/account/register.xhtml" value="Sign Up"/>
</div>
</h:form>
</s:div>
Expand All @@ -56,23 +53,23 @@
</s:fragment>
<ul class="list--no-bullets">
<li class="l--push-top-half">
<h:commandLink action="#{loginAction.login('Fedora')}" styleClass="button--full button--fedora" value="Fedora"/>
<h:commandLink action="#{loginAction.openIdLogin('Fedora')}" styleClass="button--full button--fedora" value="Fedora"/>
</li>
<li class="l--push-top-half">
<h:commandLink action="#{loginAction.login('Google')}" styleClass="button--full button--google" value="Google"/>
<h:commandLink action="#{loginAction.openIdLogin('Google')}" styleClass="button--full button--google" value="Google"/>
</li>
<li class="l--push-top-half">
<h:commandLink action="#{loginAction.login('Yahoo')}" styleClass="button--full button--yahoo" value="Yahoo"/>
<h:commandLink action="#{loginAction.openIdLogin('Yahoo')}" styleClass="button--full button--yahoo" value="Yahoo"/>
</li>
<li class="l--push-top-half l--pad-top-half l--pad-h-half bg--pop-higher">
<label for="openid">Open ID</label>

<div class="g--tighter">
<div class="g__item w--2-3">
<h:inputText id="openid" value="#{loginAction.username}" />
<h:inputText id="openid" value="#{loginAction.openId}" />
</div>
<div class="g__item w--1-3">
<h:commandButton action="#{loginAction.login('Generic')}" styleClass="button--full" value="Go"/>
<h:commandButton action="#{loginAction.openIdLogin('Generic')}" styleClass="button--full" value="Go" />
</div>
</div>
</li>
Expand Down

0 comments on commit cc63074

Please sign in to comment.