Kawai Wiki

Rev

Rev 150 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.kawai.action;

import org.kawai.dao.UserDAO;
import org.kawai.model.User;
import org.mentawai.core.BaseAction;
import org.mentawai.filter.AuthenticationFree;
import org.mentawai.rule.EqualRule;
import org.mentawai.rule.MethodRule;
import org.mentawai.rule.RegexRule;
import org.mentawai.validation.Validatable;
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 {


    /** 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;
        }
    }
}