Kawai Wiki


Rev 130 | Rev 149 | Go to most recent revision | 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.mail.Letter;
import org.mentawai.mail.SimpleEmail;
import org.mentawai.mail.TextLetter;
import org.mentawai.rule.EmailRule;
import org.mentawai.rule.EqualRule;
import org.mentawai.rule.MethodRule;
import org.mentawai.rule.RegexRule;
import org.mentawai.validation.Validatable;
import org.mentawai.validation.Validator;

public class UserAction extends BaseAction implements Validatable, AuthenticationFree {
        private final UserDAO userDAO;
        public UserAction(UserDAO userDAO) {
                this.userDAO = userDAO;

        // You cannot log to register...
        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...
        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", "email", "groupId", "languageId");
                        val.requiredLists("required_field", "groupId", "languageId");
                        val.add("username", RegexRule.getInstance(username_regex), "bad_username");
                        val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "username_already_exists");
                        val.add("email", EmailRule.getInstance(), "bad_email");
                        val.add("password", EqualRule.getInstance("password", "passconf"), "pass_no_match");
        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);

                        return CREATED;