Kawai Wiki

Compare Revisions

Ignore whitespace Rev 195 → Rev 196

/trunk/src/main/java/org/kawai/action/UserAction.java
11,103 → 11,127
import org.mentawai.validation.ValidationInterceptor;
import org.mentawai.validation.Validator;
 
/**
* Action class UserAction.
*
* @author Sergio Oliveira
*
*/
public class UserAction extends BaseAction implements Validatable, AuthenticationFree, ValidationInterceptor {
private final UserDAO userDAO;
public UserAction(UserDAO userDAO) {
this.userDAO = userDAO;
}
 
// You cannot log to register...
@Override
public boolean bypassAuthentication(String method) {
if (method == null) return false;
if (method.equals("add")) return true;
if (method.equals("check")) return true;
return false;
}
// Validate the fields for the user form...
@Override
public void prepareValidator(Validator val, String method) {
String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
if (method != null && method.equals("add") && isPost()) {
val.requiredFields("Required Field", "username", "password", "groupId");
val.requiredLists("Required Field", "groupId");
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
}
}
@Override
public boolean beforeValidation(String method) {
return true; // go ahead with validation...
}
@Override
public void afterValidation(String method, boolean wasOk) {
if (method == null) return;
if (method.equals("add") && !wasOk) {
output.setValue("showForm", "addNewUser");
}
}
boolean checkUsernameAdd(String username) {
return userDAO.findByUsername(username) == null;
}
public String check() {
if (!isPost()) return ERROR;
String username = input.getString("username");
String sessionUsername = input.getString("sessionUsername");
if (isEmpty(username)) return ERROR;
User u = userDAO.findByUsername(username);
if (u == null) return SUCCESS; // username does not exist
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
return ALREADY;
}
public String add() {
if (!isPost()) {
// we only want to allow post to add an user...
return ERROR;
} else {
User u = input.getObject(User.class);
userDAO.insert(u);
 
addMessage("User " + u.getUsername() + " added!", true);
return CREATED;
}
}
}
/** Attribute userDAO of UserAction. */
private final UserDAO userDAO;
 
/**
* Parametric constructor.
* @param userDAO UserDAO
*/
public UserAction(UserDAO userDAO) {
this.userDAO = userDAO;
}
 
/**
* {@inheritDoc}
*/
// You cannot log to register...
@Override
public boolean bypassAuthentication(String method) {
 
if (method == null) return false;
 
if (method.equals("add")) return true;
if (method.equals("check")) return true;
 
return false;
}
 
/**
* {@inheritDoc}
*/
// Validate the fields for the user form...
@Override
public void prepareValidator(Validator val, String method) {
 
String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
 
if (method != null && method.equals("add") && isPost()) {
 
val.requiredFields("Required Field", "username", "password", "groupId");
 
val.requiredLists("Required Field", "groupId");
 
val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
 
val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
 
val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
}
}
 
/**
* {@inheritDoc}
*/
@Override
public boolean beforeValidation(String method) {
return true; // go ahead with validation...
}
 
/**
* {@inheritDoc}
*/
@Override
public void afterValidation(String method, boolean wasOk) {
 
if (method == null) return;
 
if (method.equals("add") && !wasOk) {
 
output.setValue("showForm", "addNewUser");
}
}
 
boolean checkUsernameAdd(String username) {
 
return userDAO.findByUsername(username) == null;
}
 
public String check() {
 
if (!isPost()) return ERROR;
 
String username = input.getString("username");
 
String sessionUsername = input.getString("sessionUsername");
 
if (isEmpty(username)) return ERROR;
 
User u = userDAO.findByUsername(username);
 
if (u == null) return SUCCESS; // username does not exist
 
if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
 
return ALREADY;
}
 
public String add() {
 
if (!isPost()) {
 
// we only want to allow post to add an user...
 
return ERROR;
 
} else {
 
User u = input.getObject(User.class);
 
userDAO.insert(u);
 
addMessage("User " + u.getUsername() + " added!", true);
 
return CREATED;
}
}
}
/trunk/src/main/java/org/kawai/action/FileAction.java
12,20 → 12,26
import org.mentawai.core.ApplicationManager;
import org.mentawai.filter.AuthenticationFree;
 
/**
* Action class LoginAction.
*
* @author Sergio Oliveira
*
*/
public class FileAction extends AbstractAction implements AuthenticationFree {
 
private static final String UPLOAD_DIR = "uploads";
 
public FileAction(UserDAO userDAO, PageDAO pageDAO) {
super(userDAO, pageDAO);
}
 
@Override
public boolean bypassAuthentication(String method) {
if (method != null && method.equals("upload")) return false;
return false;
}
 
private String getFolderFromName(String name) {
name = name.toLowerCase();
if (name.endsWith(".jpg") || name.endsWith(".gif") || name.endsWith(".jpeg") || name.endsWith(".png")) {
36,28 → 42,28
}
return "others";
}
 
public String upload() {
 
FileItem fileItem = (FileItem) input.getValue("Filedata");
 
if (fileItem != null) {
 
String filename = fileItem.getName();
String folder = getFolderFromName(filename);
String dir = ApplicationManager.getRealPath() + File.separator + UPLOAD_DIR + File.separator + folder + File.separator;
 
File file = new File(dir + filename);
if (file.exists()) {
output.setValue("status", "error");
output.setValue("msg", "File already exists: " + filename);
return AJAX;
}
 
try {
 
FileOutputStream fos = new FileOutputStream(file);
 
InputStream is = fileItem.getInputStream();
byte[] data = new byte[4096];
int read;
66,32 → 72,32
}
is.close();
fos.close();
 
output.setValue("status", "success");
output.setValue("msg", "File uploaded to folder <i>" + folder + "</i>: " + filename);
 
return AJAX;
 
} catch(Exception e) {
e.printStackTrace();
output.setValue("status", "error");
output.setValue("msg", "Error saving file: " + filename);
return AJAX;
}
 
} else {
output.setValue("status", "error");
output.setValue("msg", "Error uploading file!");
return AJAX;
}
}
 
public String listFiles() {
 
File[] images = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "images")).listFiles();
File[] archive = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "archive")).listFiles();
File[] others = (new File(ApplicationManager.getRealPath() + File.separator + "uploads" + File.separator + "others")).listFiles();
 
Set<String> files = new TreeSet<String>();
if (images != null) for(File f : images) {
if (f.isHidden()) continue;
105,7 → 111,7
if (f.isHidden()) continue;
files.add("/uploads/others/" + f.getName());
}
 
output.setValue("files", files);
return AJAX;
}