Captive Imagination
September 06, 2010, 09:53:58 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hosting change completed. Please send an e-mail to support@captiveimagination.com if you have any problems.
 
   Home   Help Search Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Proper implementation of Login/Logout  (Read 542 times)
0 Members and 1 Guest are viewing this topic.
arcadLemon
Newbie
*
Offline Offline

Posts: 20


View Profile
« on: July 01, 2007, 05:30:25 PM »


Hello Matt.

What do you think about the code below?
It's an implementation of a login/logout process using the LoginPanel.

As the method login() is in the authenticator, and I need to display a specific form after a successful login, I had to do something like
Code:
public BananaAuthenticator(LoginDemoWindow contactMain) {
caller = contactMain;
}

Is the code below OK or is there any better way to do the same thing?

Code:

import org.apache.commons.lang.StringUtils;
import org.jcommon.security.Authenticator;
import org.jseamless.Button;
import org.jseamless.Label;
import org.jseamless.TextInput;
import org.jseamless.container.Application;
import org.jseamless.container.Form;
import org.jseamless.container.FormItem;
import org.jseamless.event.ActionEvent;
import org.jseamless.event.ActionListener;
import org.jseamlessx.LoginPanel;

public class LoginDemoWindow extends Application {

private static final long serialVersionUID = -1366775191657487249L;
protected static final String[] DOMAINS = { "Ananas Forest",
"Arachide Forest", "Banana Forest", "Jungle", };
private static final String APP_NAME = "Login Test";

private Authenticator aut = new BananaAuthenticator(this);

public LoginDemoWindow() {
super(APP_NAME);

add(getLoginPane());
}

public void displayContactForm() {

FormItem firstNameformRow = new FormItem("First Name");
TextInput firstNameTextInput = new TextInput();
FormItem lastNameFormRow = new FormItem("Last Name");
TextInput lastNameTextInput = new TextInput();
Button submitButton = new Button("Submit");
Button logoutButton = new Button("Logout");

firstNameformRow.add(firstNameTextInput);
lastNameFormRow.add(lastNameTextInput);

logoutButton.addActionListener(handleLogout());

Form contactForm = new Form();
contactForm.add(new Label("Welcome "
+ ((User) getProperty("user")).getUName()));
contactForm.add(logoutButton);
contactForm.add(firstNameformRow);
contactForm.add(lastNameFormRow);
contactForm.add(submitButton);

add(contactForm);

}

private ActionListener handleLogout() {
return new ActionListener() {
private static final long serialVersionUID = -1107120494191953903L;

public void action(ActionEvent evt) {
aut.logout();
}
};
}

public LoginPanel getLoginPane() {
return new LoginPanel("Login", false, aut, null, false);
}

}

class BananaAuthenticator implements Authenticator {

boolean coolUser = false;

LoginDemoWindow caller;

public BananaAuthenticator(LoginDemoWindow contactMain) {
caller = contactMain;
}

@Override
public String getPassword() {
return null;
}

@Override
public String getRealm() {
return null;
}

@Override
public String[] getRealms() {
return LoginDemoWindow.DOMAINS;
}

@Override
public String getUsername() {
return null;
}

@Override
public boolean isAuthenticated() {
return false;
}

@Override
public boolean login(String uname, String pass, String domain,
boolean testMode) {

if (StringUtils.isBlank(uname) || StringUtils.isBlank(pass)
|| StringUtils.isBlank(domain)) {
return false;
}

/** display the real form only after successful login */
caller.removeChildAt(0);

caller.setProperty("user", new User(uname));

caller.displayContactForm();

return true;
}

@Override
public void logout() {
this.coolUser = false;

/** After logout, remove the form, and display login form */
caller.removeChildAt(0);
caller.setProperty("user", null);
caller.add(caller.getLoginPane());
}

}

class User {
String uName;
String lName;

public String getUName() {
return uName;
}

public User(String name) {
super();
uName = name;
}

Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #1 on: July 01, 2007, 08:05:31 PM »

I would split up the logic of the login functionality from the logic of displaying the contact form.  If you write it a bit more modular you can better simplify on both sides as well as make it easier to re-write either side without effecting the other.  In LoginPanel it takes a Runnable that is executed after the user has successfully validated.  This keeps the actual authentication process from having to have any ties to what is done after validation.

Other than that it looks pretty good. Smiley
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.217 seconds with 20 queries.