001: package ru.emdev.EmForge.security;
002:
003: import java.awt.Graphics2D;
004: import java.awt.image.BufferedImage;
005: import java.util.Random;
006:
007: import javax.faces.application.FacesMessage;
008: import javax.faces.context.FacesContext;
009:
010: import org.acegisecurity.providers.encoding.PasswordEncoder;
011: import org.springframework.beans.factory.InitializingBean;
012:
013: import ru.emdev.EmForge.email.EmailServices;
014: import ru.emdev.EmForge.messages.RegNotificationEmail;
015: import ru.emdev.EmForge.security.dao.Role;
016: import ru.emdev.EmForge.security.dao.User;
017: import ru.emdev.EmForge.security.dao.UserDao;
018: import ru.emdev.EmForge.security.web.SiteRole;
019: import ru.emdev.EmForge.util.RandomGUID;
020: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
021: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
022: import ru.emdev.EmForge.wiki.web.bean.Crumb;
023:
024: import com.octo.captcha.service.image.ImageCaptchaService;
025:
026: /**
027: * Controller, responsible for creation of new users
028: */
029: public class RegisterController extends BaseControllerImpl implements
030: InitializingBean {
031:
032: public static final String PAGE_NAME = "register.faces";
033: public static final String TITLE = "Register New User";
034:
035: public static final String CAPTCHA_ID = "EmForge";
036:
037: private UserDao m_userDao;
038: private PasswordEncoder m_passwordEncoder;
039: private EmailServices m_emailServices;
040: private ImageCaptchaService m_imageCaptchaService;
041:
042: private User m_user;
043: private String m_password;
044: private String m_confirmPassword;
045: private String m_secureText;
046:
047: private Boolean m_useActivation;
048: private Boolean m_assignWriterRole;
049:
050: public void setUserDao(UserDao i_userDao) {
051:
052: m_userDao = i_userDao;
053: }
054:
055: public void setPasswordEncoder(PasswordEncoder i_passwordEncoder) {
056:
057: m_passwordEncoder = i_passwordEncoder;
058: }
059:
060: public void setEmailServices(EmailServices i_emailServices) {
061:
062: m_emailServices = i_emailServices;
063: }
064:
065: public void setUseActivation(Boolean i_useActivation) {
066:
067: m_useActivation = i_useActivation;
068: }
069:
070: public Boolean getUseActivation() {
071:
072: return m_useActivation;
073: }
074:
075: public void setAssignWriterRole(Boolean i_assignWriterRole) {
076:
077: m_assignWriterRole = i_assignWriterRole;
078: }
079:
080: public Boolean getAssignWriterRole() {
081:
082: return m_assignWriterRole;
083: }
084:
085: public void setImageCaptchaService(
086: ImageCaptchaService i_imageCaptchaService) {
087:
088: m_imageCaptchaService = i_imageCaptchaService;
089: }
090:
091: public void afterPropertiesSet() throws Exception {
092:
093: if (m_userDao == null) {
094: throw new IllegalArgumentException(
095: "userDao should be specified for Register Controller");
096: }
097:
098: super .afterPropertiesSet();
099: }
100:
101: @Override
102: public MainMenuItem getSelectionItemOnMainMenu() {
103:
104: return MainMenuItem.TASKS;
105: }
106:
107: @Override
108: public String getTitleImpl() {
109:
110: return TITLE;
111: }
112:
113: @Override
114: public Crumb getTrailCrumbInfo() {
115:
116: return new Crumb(TITLE, PAGE_NAME);
117: }
118:
119: @Override
120: protected void init() {
121:
122: // create new user
123: m_user = new User();
124: }
125:
126: public User getUser() {
127:
128: return m_user;
129: }
130:
131: public String getPassword() {
132:
133: return m_password;
134: }
135:
136: public void setPassword(String i_password) {
137:
138: m_password = i_password;
139: }
140:
141: public String getConfirmPassword() {
142:
143: return m_confirmPassword;
144: }
145:
146: public void setConfirmPassword(String i_password) {
147:
148: m_confirmPassword = i_password;
149: }
150:
151: public String getSecureText() {
152:
153: return m_secureText;
154: }
155:
156: public void setSecureText(String i_secureText) {
157:
158: m_secureText = i_secureText;
159: }
160:
161: protected BufferedImage generateCaptcha() {
162:
163: try {
164: return m_imageCaptchaService
165: .getImageChallengeForID(CAPTCHA_ID);
166: } catch (Exception ex) {
167: logger.error("Cannot generate captcha image", ex);
168: return null;
169: }
170: }
171:
172: public int getCaptchaWidth() {
173:
174: return 250;
175: }
176:
177: public int getCaptchaHeight() {
178:
179: return 100;
180: }
181:
182: /**
183: * Paints Captcha Image
184: *
185: * @param g2d
186: * @param obj
187: */
188: public void paintCaptcha(Graphics2D g2d, Object obj) {
189:
190: BufferedImage secureImage = generateCaptcha();
191:
192: try {
193: g2d.setClip(0, 0, secureImage.getWidth(), secureImage
194: .getHeight());
195: g2d.drawImage(secureImage, 0, 0, null);
196: } catch (Exception ex) {
197: logger.error("Cannot generate captcha image", ex);
198: }
199: }
200:
201: /**
202: * Performs Submit Action
203: *
204: * @return
205: */
206: public String submit() {
207:
208: try {
209: // encode password
210: if (m_passwordEncoder != null) {
211: m_user.setPassword(m_passwordEncoder.encodePassword(
212: m_password, ""));
213: }
214:
215: // Assign the user to default roles
216: Role role = m_userDao.ensureRole(SiteRole.USER.getId());
217: m_user.addRole(role);
218: if (m_assignWriterRole) {
219: role = m_userDao.ensureRole(SiteRole.WRITER.getId());
220: m_user.addRole(role);
221: }
222:
223: // now, generate activation code
224: String activationCode = new RandomGUID().toString();
225:
226: if (m_useActivation) {
227: m_user.setActivationCode(activationCode);
228: } else {
229: // activate user right here
230: m_user.setActive(true);
231: }
232:
233: try {
234: // save user
235: m_userDao.saveUser(m_user);
236: } catch (Exception ex) {
237: logger.error("Cannot store user", ex);
238: addMessage("Cannot store user", ex.getMessage(),
239: FacesMessage.SEVERITY_ERROR);
240: return null;
241: }
242:
243: if (m_useActivation) {
244: // send email notification
245: RegNotificationEmail message = (RegNotificationEmail) m_emailServices
246: .getEmailFactory().createEmail(
247: "regnotification");
248:
249: message.setNotificationCode(activationCode);
250: message.setToName(m_user.getEmail());
251:
252: try {
253: m_emailServices.getEmailSender().sendMessage(
254: message);
255: } catch (Exception ex) {
256: logger
257: .error("Cannot send registration notification email to "
258: + m_user.getEmail());
259: FacesContext context = FacesContext
260: .getCurrentInstance();
261: FacesMessage fm = new FacesMessage(
262: "Cannot send registration notification email to "
263: + m_user.getEmail(), null);
264: fm.setSeverity(FacesMessage.SEVERITY_ERROR);
265: context.addMessage(null, fm);
266: return null;
267: }
268: }
269:
270: return "success";
271: } finally {
272: // generate new secure text
273: // generateCaptcha();
274: }
275: }
276:
277: /** Generates Random Text for displaying on the image */
278: public String getRandomString() {
279:
280: String str = new String(
281: "QAa0bcLdUK2eHfJgTP8XhiFj61DOklNm9nBoI5pGqYVrs3CtSuMZvwWx4yE7zR");
282: StringBuffer sb = new StringBuffer();
283: Random r = new Random();
284: int te = 0;
285: for (int i = 1; i <= 6; i++) {
286: te = r.nextInt(62);
287: sb.append(str.charAt(te));
288: }
289:
290: return sb.toString();
291: }
292: }
|