Kawai Wiki

Rev

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

Rev Author Line No. Line
8 soliveira 1
package org.kawai.action;
6 soliveira 2
 
8 soliveira 3
import org.kawai.dao.UserDAO;
4
import org.kawai.model.User;
6 soliveira 5
import org.mentawai.core.BaseAction;
6
import org.mentawai.filter.AuthenticationFree;
7
import org.mentawai.rule.EqualRule;
8
import org.mentawai.rule.MethodRule;
9
import org.mentawai.rule.RegexRule;
10
import org.mentawai.validation.Validatable;
149 soliveira 11
import org.mentawai.validation.ValidationInterceptor;
6 soliveira 12
import org.mentawai.validation.Validator;
13
 
196 helio.frota 14
/**
15
 * Action class UserAction.
16
 *
17
 * @author Sergio Oliveira
18
 *
19
 */
149 soliveira 20
public class UserAction extends BaseAction implements Validatable, AuthenticationFree, ValidationInterceptor {
6 soliveira 21
 
22
 
196 helio.frota 23
    /** Attribute userDAO of UserAction. */
24
    private final UserDAO userDAO;
25
 
26
    /**
27
     * Parametric constructor.
28
     * @param userDAO UserDAO
29
     */
30
    public UserAction(UserDAO userDAO) {
31
        this.userDAO = userDAO;
32
    }
33
 
34
    /**
35
     * {@inheritDoc}
36
     */
37
    // You cannot log to register...
38
    @Override
39
    public boolean bypassAuthentication(String method) {
40
 
41
        if (method == null) return false;
42
 
43
        if (method.equals("add")) return true;
44
        if (method.equals("check")) return true;
45
 
46
        return false;
47
    }
48
 
49
    /**
50
     * {@inheritDoc}
51
     */
52
    // Validate the fields for the user form...
53
    @Override
54
    public void prepareValidator(Validator val, String method) {
55
 
56
        String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
57
 
58
        if (method != null && method.equals("add") && isPost()) {
59
 
60
            val.requiredFields("Required Field", "username", "password", "groupId");
61
 
62
            val.requiredLists("Required Field", "groupId");
63
 
64
            val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
65
 
66
            val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
67
 
68
            val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
69
        }
70
    }
71
 
72
    /**
73
     * {@inheritDoc}
74
     */
75
    @Override
76
    public boolean beforeValidation(String method) {
77
        return true; // go ahead with validation...
78
    }
79
 
80
    /**
81
     * {@inheritDoc}
82
     */
83
    @Override
84
    public void afterValidation(String method, boolean wasOk) {
85
 
86
        if (method == null) return;
87
 
88
        if (method.equals("add") && !wasOk) {
89
 
90
            output.setValue("showForm", "addNewUser");
91
        }
92
    }
93
 
94
    boolean checkUsernameAdd(String username) {
95
 
96
        return userDAO.findByUsername(username) == null;
97
    }
98
 
99
    public String check() {
100
 
101
        if (!isPost()) return ERROR;
102
 
103
        String username = input.getString("username");
104
 
105
        String sessionUsername = input.getString("sessionUsername");
106
 
107
        if (isEmpty(username)) return ERROR;
108
 
109
        User u = userDAO.findByUsername(username);
110
 
111
        if (u == null) return SUCCESS; // username does not exist
112
 
113
        if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
114
 
115
        return ALREADY;
116
    }
117
 
118
    public String add() {
119
 
120
        if (!isPost()) {
121
 
122
            // we only want to allow post to add an user...
123
 
124
            return ERROR;
125
 
126
        } else {
127
 
128
            User u = input.getObject(User.class);
129
 
130
            userDAO.insert(u);
131
 
132
            addMessage("User " + u.getUsername() + " added!", true);
133
 
134
            return CREATED;
135
        }
136
    }
137
}