package team2.abcbank.beans;_x000D_
_x000D_
import javax.ejb.EJB;_x000D_
import javax.faces.application.FacesMessage;_x000D_
import javax.faces.context.ExternalContext;_x000D_
import javax.naming.Context;_x000D_
import javax.naming.InitialContext;_x000D_
import javax.naming.NamingException;_x000D_
import javax.security.auth.login.LoginException;_x000D_
import javax.servlet.http.HttpSession;_x000D_
_x000D_
import org.jboss.security.SecurityAssociation;_x000D_
import org.jboss.security.auth.callback.CallbackHandlerPolicyContextHandler;_x000D_
import org.jboss.web.tomcat.security.login.WebAuthentication;_x000D_
_x000D_
import team2.abcbank.ejb.shared.AccountManagerIF;_x000D_
import team2.abcbank.ejb.shared.AccountOfficeIF;_x000D_
import team2.abcbank.ejb.shared.BankException;_x000D_
import team2.abcbank.ejb.shared.LoginBeanIF;_x000D_
import team2.abcbank.jaas.MyCallbackHandler;_x000D_
_x000D_
public class BankAccessBean extends EvenMoreCommonBean_x000D_
{_x000D_
	private String username = null;_x000D_
	private String password = null;_x000D_
	private String captcha = null;_x000D_
_x000D_
	protected LoginBeanIF loginBean = null;_x000D_
	protected AccountManagerIF accountManager = null;_x000D_
	protected AccountOfficeIF accountOffice = null;_x000D_
	protected long accountNumber = -1;_x000D_
_x000D_
	public BankAccessBean()_x000D_
	{_x000D_
		try_x000D_
		{_x000D_
			Context ctx = new InitialContext();_x000D_
			loginBean = (LoginBeanIF) ctx.lookup("java:comp/env/LoginBeanRef");_x000D_
			accountManager = (AccountManagerIF) ctx.lookup("java:comp/env/AccountManagerRef");_x000D_
			accountOffice = (AccountOfficeIF) ctx.lookup("java:comp/env/AccountOfficeRef");_x000D_
		}_x000D_
		catch (NamingException e)_x000D_
		{_x000D_
			e.printStackTrace();_x000D_
		}_x000D_
	}_x000D_
_x000D_
	public String getBankStatus()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.getBankStatus()");_x000D_
		String status = "database onbereikbaar";_x000D_
		try_x000D_
		{_x000D_
			status = loginBean.getBankIsOpen() ? "bank is open" : "bank is closed";_x000D_
			status += loginBean.getTransactionManagerIsIdle() ? " and idle" : " and busy";_x000D_
		}_x000D_
		catch (Throwable t)_x000D_
		{_x000D_
			t.printStackTrace();_x000D_
		}_x000D_
		return status;_x000D_
	}_x000D_
_x000D_
	public String logout()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.logout()");_x000D_
		// zie jboss-web.xml : flushOnSessionInvalidation_x000D_
		// dat zorgt dat logout() op de loginmodules gecalled wordt_x000D_
		// en alle rechten weer netjes ingetrokken worden_x000D_
		if (false)_x000D_
		{_x000D_
			try_x000D_
			{_x000D_
				if (accountOffice.getPendingTransactions(accountNumber).length > 0)_x000D_
				{_x000D_
					addMessage(FacesMessage.SEVERITY_ERROR, "Kan niet uitloggen als er nog transacties open staan.");_x000D_
					return "transactionLeft";_x000D_
				}_x000D_
			}_x000D_
			catch (BankException e)_x000D_
			{_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
		HttpSession session = (HttpSession) getExternalContext().getSession(false);_x000D_
		if (session != null)_x000D_
		{_x000D_
			session.invalidate();_x000D_
		}_x000D_
		return "logoutOutcome";_x000D_
	}_x000D_
_x000D_
	public String login()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.login()");_x000D_
_x000D_
		ExternalContext ec = getExternalContext();_x000D_
		boolean error = false;_x000D_
		// alleen maar inloggen als de user nog niet ingelogd is_x000D_
		if (ec.getRemoteUser() == null)_x000D_
		{_x000D_
			try_x000D_
			{_x000D_
				// de callbackHandler die WebAuthentication opbouwt voor zijn username + credentials parameters_x000D_
				// delegeert onbekende callbacks naar deze callbackHandler_x000D_
				// zo kunnen we dus communiceren via de callbacks_x000D_
				// doc: http://docs.jboss.org/jbossas/javadoc/4.0.5/security/org/jboss/security/auth/callback/CallbackHandlerPolicyContextHandler.html_x000D_
				// doc: https://jira.jboss.org/jira/browse/JBAS-2345_x000D_
				CallbackHandlerPolicyContextHandler.setCallbackHandler(new MyCallbackHandler(this));_x000D_
_x000D_
				// doe de programmatic web logon:_x000D_
				// doc: http://jboss.org/community/docs/DOC-12656_x000D_
				// dit object doet zowel authentication als authorization_x000D_
				WebAuthentication wa = new WebAuthentication();_x000D_
				boolean success = wa.login(username, password);_x000D_
				if (!success)_x000D_
				{_x000D_
					error = true;_x000D_
					// gespecificeerd in:_x000D_
					// https://jira.jboss.org/jira/browse/SECURITY-278?focusedCommentId=12433231#action_12433231_x000D_
					Object o = SecurityAssociation.getContextInfo("org.jboss.security.exception");_x000D_
					if (o instanceof LoginException)_x000D_
					{_x000D_
						LoginException le = (LoginException) o;_x000D_
						addMessage(FacesMessage.SEVERITY_ERROR, "Login fout: " + le.getMessage());_x000D_
					}_x000D_
					else_x000D_
					{_x000D_
						addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
					}_x000D_
				}_x000D_
			}_x000D_
			catch (Throwable t)_x000D_
			{_x000D_
				error = true;_x000D_
				addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
				t.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// mogelijke outcomes voor redirection_x000D_
		// zie JSF navigation rules in faces-config.xml_x000D_
		if (error)_x000D_
			return "loginError";_x000D_
		if (ec.isUserInRole("AccountManager"))_x000D_
			return "loggedInAsAccountManager";_x000D_
		if (ec.isUserInRole("AccountOffice"))_x000D_
			return "loggedInAsAccountOffice";_x000D_
_x000D_
		// kom hier uit als JBoss iets geks doet:_x000D_
		// de authenticatie is gelukt, maar de authorisatie is mislukt_x000D_
		addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
		return "loginError";_x000D_
	}_x000D_
_x000D_
	public String getUsername()_x000D_
	{_x000D_
		return username;_x000D_
	}_x000D_
_x000D_
	public void setUsername(String username)_x000D_
	{_x000D_
		this.username = username;_x000D_
	}_x000D_
_x000D_
	public String getPassword()_x000D_
	{_x000D_
		return password;_x000D_
	}_x000D_
_x000D_
	public void setPassword(String password)_x000D_
	{_x000D_
		this.password = password;_x000D_
	}_x000D_
_x000D_
	public String getCaptcha()_x000D_
	{_x000D_
		return captcha;_x000D_
	}_x000D_
_x000D_
	public void setCaptcha(String captcha)_x000D_
	{_x000D_
		this.captcha = captcha;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * for jaas mycallbackhandler_x000D_
	 */_x000D_
	public LoginBeanIF getLoginBean()_x000D_
	{_x000D_
		return loginBean;_x000D_
	}_x000D_
_x000D_
	public AccountManagerIF getAccountManager()_x000D_
	{_x000D_
		return accountManager;_x000D_
	}_x000D_
_x000D_
	public AccountOfficeIF getAccountOffice()_x000D_
	{_x000D_
		return accountOffice;_x000D_
	}_x000D_
_x000D_
	public long getAccountNumber()_x000D_
	{_x000D_
		return accountNumber;_x000D_
	}_x000D_
_x000D_
	public void setAccountNumber(long accountNumber)_x000D_
	{_x000D_
		this.accountNumber = accountNumber;_x000D_
	}_x000D_
}
// mogelijke outcomes voor redirection_x000D_
package team2.abcbank.beans;_x000D_
_x000D_
import javax.ejb.EJB;_x000D_
import javax.faces.application.FacesMessage;_x000D_
import javax.faces.context.ExternalContext;_x000D_
import javax.naming.Context;_x000D_
import javax.naming.InitialContext;_x000D_
import javax.naming.NamingException;_x000D_
import javax.security.auth.login.LoginException;_x000D_
import javax.servlet.http.HttpSession;_x000D_
_x000D_
import org.jboss.security.SecurityAssociation;_x000D_
import org.jboss.security.auth.callback.CallbackHandlerPolicyContextHandler;_x000D_
import org.jboss.web.tomcat.security.login.WebAuthentication;_x000D_
_x000D_
import team2.abcbank.ejb.shared.AccountManagerIF;_x000D_
import team2.abcbank.ejb.shared.AccountOfficeIF;_x000D_
import team2.abcbank.ejb.shared.BankException;_x000D_
import team2.abcbank.ejb.shared.LoginBeanIF;_x000D_
import team2.abcbank.jaas.MyCallbackHandler;_x000D_
_x000D_
public class BankAccessBean extends EvenMoreCommonBean_x000D_
{_x000D_
	private String username = null;_x000D_
	private String password = null;_x000D_
	private String captcha = null;_x000D_
_x000D_
	protected LoginBeanIF loginBean = null;_x000D_
	protected AccountManagerIF accountManager = null;_x000D_
	protected AccountOfficeIF accountOffice = null;_x000D_
	protected long accountNumber = -1;_x000D_
_x000D_
	public BankAccessBean()_x000D_
	{_x000D_
		try_x000D_
		{_x000D_
			Context ctx = new InitialContext();_x000D_
			loginBean = (LoginBeanIF) ctx.lookup("java:comp/env/LoginBeanRef");_x000D_
			accountManager = (AccountManagerIF) ctx.lookup("java:comp/env/AccountManagerRef");_x000D_
			accountOffice = (AccountOfficeIF) ctx.lookup("java:comp/env/AccountOfficeRef");_x000D_
		}_x000D_
		catch (NamingException e)_x000D_
		{_x000D_
			e.printStackTrace();_x000D_
		}_x000D_
	}_x000D_
_x000D_
	public String getBankStatus()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.getBankStatus()");_x000D_
		String status = "database onbereikbaar";_x000D_
		try_x000D_
		{_x000D_
			status = loginBean.getBankIsOpen() ? "bank is open" : "bank is closed";_x000D_
			status += loginBean.getTransactionManagerIsIdle() ? " and idle" : " and busy";_x000D_
		}_x000D_
		catch (Throwable t)_x000D_
		{_x000D_
			t.printStackTrace();_x000D_
		}_x000D_
		return status;_x000D_
	}_x000D_
_x000D_
	public String logout()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.logout()");_x000D_
		// zie jboss-web.xml : flushOnSessionInvalidation_x000D_
		// dat zorgt dat logout() op de loginmodules gecalled wordt_x000D_
		// en alle rechten weer netjes ingetrokken worden_x000D_
		if (false)_x000D_
		{_x000D_
			try_x000D_
			{_x000D_
				if (accountOffice.getPendingTransactions(accountNumber).length > 0)_x000D_
				{_x000D_
					addMessage(FacesMessage.SEVERITY_ERROR, "Kan niet uitloggen als er nog transacties open staan.");_x000D_
					return "transactionLeft";_x000D_
				}_x000D_
			}_x000D_
			catch (BankException e)_x000D_
			{_x000D_
				e.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
		HttpSession session = (HttpSession) getExternalContext().getSession(false);_x000D_
		if (session != null)_x000D_
		{_x000D_
			session.invalidate();_x000D_
		}_x000D_
		return "logoutOutcome";_x000D_
	}_x000D_
_x000D_
	public String login()_x000D_
	{_x000D_
		System.out.println("BankAccessBean.login()");_x000D_
_x000D_
		ExternalContext ec = getExternalContext();_x000D_
		boolean error = false;_x000D_
		// alleen maar inloggen als de user nog niet ingelogd is_x000D_
		if (ec.getRemoteUser() == null)_x000D_
		{_x000D_
			try_x000D_
			{_x000D_
				// de callbackHandler die WebAuthentication opbouwt voor zijn username + credentials parameters_x000D_
				// delegeert onbekende callbacks naar deze callbackHandler_x000D_
				// zo kunnen we dus communiceren via de callbacks_x000D_
				// doc: http://docs.jboss.org/jbossas/javadoc/4.0.5/security/org/jboss/security/auth/callback/CallbackHandlerPolicyContextHandler.html_x000D_
				// doc: https://jira.jboss.org/jira/browse/JBAS-2345_x000D_
				CallbackHandlerPolicyContextHandler.setCallbackHandler(new MyCallbackHandler(this));_x000D_
_x000D_
				// doe de programmatic web logon:_x000D_
				// doc: http://jboss.org/community/docs/DOC-12656_x000D_
				// dit object doet zowel authentication als authorization_x000D_
				WebAuthentication wa = new WebAuthentication();_x000D_
				boolean success = wa.login(username, password);_x000D_
				if (!success)_x000D_
				{_x000D_
					error = true;_x000D_
					// gespecificeerd in:_x000D_
					// https://jira.jboss.org/jira/browse/SECURITY-278?focusedCommentId=12433231#action_12433231_x000D_
					Object o = SecurityAssociation.getContextInfo("org.jboss.security.exception");_x000D_
					if (o instanceof LoginException)_x000D_
					{_x000D_
						LoginException le = (LoginException) o;_x000D_
						addMessage(FacesMessage.SEVERITY_ERROR, "Login fout: " + le.getMessage());_x000D_
					}_x000D_
					else_x000D_
					{_x000D_
						addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
					}_x000D_
				}_x000D_
			}_x000D_
			catch (Throwable t)_x000D_
			{_x000D_
				error = true;_x000D_
				addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
				t.printStackTrace();_x000D_
			}_x000D_
		}_x000D_
_x000D_
		// mogelijke outcomes<SUF>
		// zie JSF navigation rules in faces-config.xml_x000D_
		if (error)_x000D_
			return "loginError";_x000D_
		if (ec.isUserInRole("AccountManager"))_x000D_
			return "loggedInAsAccountManager";_x000D_
		if (ec.isUserInRole("AccountOffice"))_x000D_
			return "loggedInAsAccountOffice";_x000D_
_x000D_
		// kom hier uit als JBoss iets geks doet:_x000D_
		// de authenticatie is gelukt, maar de authorisatie is mislukt_x000D_
		addMessage(FacesMessage.SEVERITY_ERROR, "Systeem fout");_x000D_
		return "loginError";_x000D_
	}_x000D_
_x000D_
	public String getUsername()_x000D_
	{_x000D_
		return username;_x000D_
	}_x000D_
_x000D_
	public void setUsername(String username)_x000D_
	{_x000D_
		this.username = username;_x000D_
	}_x000D_
_x000D_
	public String getPassword()_x000D_
	{_x000D_
		return password;_x000D_
	}_x000D_
_x000D_
	public void setPassword(String password)_x000D_
	{_x000D_
		this.password = password;_x000D_
	}_x000D_
_x000D_
	public String getCaptcha()_x000D_
	{_x000D_
		return captcha;_x000D_
	}_x000D_
_x000D_
	public void setCaptcha(String captcha)_x000D_
	{_x000D_
		this.captcha = captcha;_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * for jaas mycallbackhandler_x000D_
	 */_x000D_
	public LoginBeanIF getLoginBean()_x000D_
	{_x000D_
		return loginBean;_x000D_
	}_x000D_
_x000D_
	public AccountManagerIF getAccountManager()_x000D_
	{_x000D_
		return accountManager;_x000D_
	}_x000D_
_x000D_
	public AccountOfficeIF getAccountOffice()_x000D_
	{_x000D_
		return accountOffice;_x000D_
	}_x000D_
_x000D_
	public long getAccountNumber()_x000D_
	{_x000D_
		return accountNumber;_x000D_
	}_x000D_
_x000D_
	public void setAccountNumber(long accountNumber)_x000D_
	{_x000D_
		this.accountNumber = accountNumber;_x000D_
	}_x000D_
}