Kawai Wiki

Rev

Rev 148 | Rev 150 | Go to most recent revision | 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
 
149 soliveira 14
public class UserAction extends BaseAction implements Validatable, AuthenticationFree, ValidationInterceptor {
6 soliveira 15
 
16
        private final UserDAO userDAO;
17
 
18
        public UserAction(UserDAO userDAO) {
19
                this.userDAO = userDAO;
20
        }
21
 
22
        // You cannot log to register...
23
        @Override
24
        public boolean bypassAuthentication(String method) {
25
 
26
                if (method == null) return false;
27
 
28
                if (method.equals("add")) return true;
29
                if (method.equals("check")) return true;
30
 
31
                return false;
32
        }
33
 
34
        // Validate the fields for the user form...
35
        @Override
36
        public void prepareValidator(Validator val, String method) {
37
 
38
                String username_regex = "^[A-Za-z][A-Za-z0-9\\-\\_\\.]*[A-Za-z0-9]$";
39
 
40
                if (method != null && method.equals("add") && isPost()) {
41
 
149 soliveira 42
                        val.requiredFields("Required Field", "username", "password", "groupId");
6 soliveira 43
 
149 soliveira 44
                        val.requiredLists("Required Field", "groupId");
6 soliveira 45
 
149 soliveira 46
                        val.add("username", RegexRule.getInstance(username_regex), "Invalid Username");
6 soliveira 47
 
149 soliveira 48
                        val.add("username", MethodRule.getInstance(this, "checkUsernameAdd"), "Username already exists!");
6 soliveira 49
 
149 soliveira 50
                        val.add("password", EqualRule.getInstance("password", "passconf"), "Passwords do not match!");
51
                }
52
        }
53
 
54
        @Override
55
        public boolean beforeValidation(String method) {
56
                return true; // go ahead with validation...
57
        }
58
 
59
        @Override
60
        public void afterValidation(String method, boolean wasOk) {
61
 
62
                if (method == null) return;
63
 
64
                if (method.equals("add") && !wasOk) {
6 soliveira 65
 
149 soliveira 66
                        output.setValue("showForm", "addNewUser");
6 soliveira 67
                }
68
        }
69
 
130 soliveira 70
        boolean checkUsernameAdd(String username) {
6 soliveira 71
 
72
                return userDAO.findByUsername(username) == null;
73
        }
74
 
75
        public String check() {
76
 
77
                if (!isPost()) return ERROR;
78
 
79
                String username = input.getString("username");
80
 
81
                String sessionUsername = input.getString("sessionUsername");
82
 
83
                if (isEmpty(username)) return ERROR;
84
 
85
                User u = userDAO.findByUsername(username);
86
 
87
                if (u == null) return SUCCESS; // username does not exist
88
 
89
                if (sessionUsername != null && u.getUsername().equals(sessionUsername)) return SUCCESS;
90
 
91
                return ALREADY;
92
        }
93
 
94
        public String add() {
95
 
96
                if (!isPost()) {
97
 
98
                        // we only want to allow post to add an user...
99
 
100
                        return ERROR;
101
 
102
                } else {
103
 
104
                        User u = input.getObject(User.class);
105
 
106
                        userDAO.insert(u);
107
 
108
                        setSessionObj(u);
109
 
110
                        setSessionGroup(u.getGroup());
111
 
149 soliveira 112
                        addMessage("User " + u.getUsername() + " added!", true);
6 soliveira 113
 
114
                        return CREATED;
115
                }
116
        }
117
}