1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  package net.smartlab.web.auth;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.http.Cookie;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import net.smartlab.web.ActionException;
32  import net.smartlab.web.BusinessException;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  
41  
42  
43  
44  
45  public abstract class AbstractUserAction extends SubjectAction {
46  
47  	
48  
49  
50  
51  
52  
53  	protected final Log logger = LogFactory.getLog(AbstractUserAction.class);
54  
55  	
56  
57  
58  
59  
60  
61  	public static User getUser(HttpServletRequest request) {
62  		return (User)request.getSession().getAttribute(UserAction.SESSION_KEY);
63  	}
64  	
65  	
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  	public String register(ActionForm form, HttpServletRequest request, HttpServletResponse response,
77  			ActionMapping mapping) throws BusinessException, ActionException {
78  			
79  			return domain.register(request.getParameterMap(), request.getParameter("step"));
80  	}
81  
82  	
83  
84  
85  
86  
87  	protected abstract void setRoles(User user);
88  
89  	
90  
91  
92  
93  
94  	protected abstract void setGroups(User user);
95  
96  	
97  
98  
99  
100 
101 
102 
103 
104 
105 
106 
107 	public String login(ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
108 			throws Exception {
109 		this.logout(form, request, response, mapping);
110 		if (logger.isDebugEnabled()) {
111 			logger.debug("login(username = " + request.getParameter("username") + ") - start");
112 		}
113 		Credentials credentials = new Credentials();
114 		super.valorize(form, credentials, request.getLocale());
115 		
116 		credentials.setSecret(request.getParameter("secret"));
117 		User user = domain.login(credentials);
118 		if (user == null) {
119 			return "failure";
120 		} else {
121 			request.getSession().setAttribute(UserAction.SESSION_KEY, user);
122 			if (request.getParameter("remember") != null) {
123 				Cookie cookie = new Cookie("smartweb-auth", credentials.toString());
124 				cookie.setMaxAge(15 * 24 * 60 * 60);
125 				response.addCookie(cookie);
126 			}
127 			user.getPolicy();
128 			return "success";
129 		}
130 	}
131 
132 	
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 	public String autoLogin(ActionForm form, HttpServletRequest request, HttpServletResponse response,
144 			ActionMapping mapping) throws BusinessException {
145 		logger.info("autoLogin() - start");
146 		Cookie[] cookies = request.getCookies();
147 		for (int i = 0; i < cookies.length; i++) {
148 			Cookie cookie = cookies[i];
149 			if (cookie.getName().equals("smartweb-auth")) {
150 				String value = cookie.getValue();
151 				User user = null;
152 				try {
153 					user = domain.login(new Credentials(value));
154 				} catch (IOException e) {
155 					cookie.setMaxAge(0);
156 					response.addCookie(cookie);
157 				}
158 				if (user == null) {
159 					return "failure";
160 				} else {
161 					
162 					request.getSession().setAttribute(UserAction.SESSION_KEY, user);
163 					cookie.setMaxAge(15 * 24 * 60 * 60);
164 					response.addCookie(cookie);
165 					return "success";
166 				}
167 			}
168 		}
169 		return "disabled";
170 	}
171 
172 	
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 	public String logout(ActionForm form, HttpServletRequest request, HttpServletResponse response,
183 			ActionMapping mapping) throws BusinessException {
184 		logger.info("logout() - start");
185 		domain.logout(UserAction.getUser(request));
186 		request.getSession().invalidate();
187 		return "success";
188 	}
189 
190 	
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 	public String update(ActionForm form, HttpServletRequest request, HttpServletResponse response,
202 			ActionMapping mapping) throws BusinessException, ActionException {
203 		User user = AbstractUserAction.getUser(request);
204 		if (user != null) {
205 			super.valorize(form, user, request.getLocale());
206 			domain.updateUser(user);
207 			return "success";
208 		}
209 		return "failure";
210 	}
211 
212 	
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 	public String remove(ActionForm form, HttpServletRequest request, HttpServletResponse response,
224 			ActionMapping mapping) throws BusinessException {
225 		User user = AbstractUserAction.getUser(request);
226 		if (user != null) {
227 			domain.removeUser(Long.toString(user.getId()));
228 		}
229 		return "success";
230 	}
231 
232 	
233 
234 
235 
236 
237 
238 	protected ActionForward cancel(ActionForm form, HttpServletRequest request, HttpServletResponse response,
239 			ActionMapping mapping) throws Exception {
240 		super.reset(form, request, mapping);
241 		return mapping.findForward("cancel");
242 	}
243 }