1 18 package org.apache.roller.ui.authoring.struts.actions; 19 20 import java.net.MalformedURLException ; 21 import java.text.MessageFormat ; 22 import java.util.ResourceBundle ; 23 import javax.mail.MessagingException ; 24 import javax.mail.Session ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.action.ActionError; 33 import org.apache.struts.action.ActionErrors; 34 import org.apache.struts.action.ActionForm; 35 import org.apache.struts.action.ActionForward; 36 import org.apache.struts.action.ActionMapping; 37 import org.apache.struts.action.ActionMessage; 38 import org.apache.struts.action.ActionMessages; 39 import org.apache.struts.actions.DispatchAction; 40 import org.apache.struts.util.RequestUtils; 41 import org.apache.roller.RollerException; 42 import org.apache.roller.config.RollerConfig; 43 import org.apache.roller.config.RollerRuntimeConfig; 44 import org.apache.roller.model.Roller; 45 import org.apache.roller.model.RollerFactory; 46 import org.apache.roller.model.UserManager; 47 import org.apache.roller.pojos.PermissionsData; 48 import org.apache.roller.pojos.UserData; 49 import org.apache.roller.pojos.WebsiteData; 50 import org.apache.roller.ui.core.BasePageModel; 51 import org.apache.roller.ui.core.RollerContext; 52 import org.apache.roller.ui.core.RollerRequest; 53 import org.apache.roller.ui.core.RollerSession; 54 import org.apache.roller.ui.authoring.struts.formbeans.InviteMemberForm; 55 import org.apache.roller.util.MailUtil; 56 57 63 public class InviteMemberAction extends DispatchAction { 64 private static Log mLogger = 65 LogFactory.getFactory().getInstance(InviteMemberAction.class); 66 67 68 public ActionForward unspecified( 69 ActionMapping mapping, 70 ActionForm actionForm, 71 HttpServletRequest request, 72 HttpServletResponse response) 73 throws Exception { 74 if (request.getMethod().equals("GET")) { 75 return edit(mapping, actionForm, request, response); 76 } 77 return send(mapping, actionForm, request, response); 78 } 79 80 public ActionForward cancel( 81 ActionMapping mapping, 82 ActionForm actionForm, 83 HttpServletRequest request, 84 HttpServletResponse response) 85 throws Exception { 86 return mapping.findForward("memberPermissions"); 87 } 88 89 public ActionForward edit( 90 ActionMapping mapping, 91 ActionForm actionForm, 92 HttpServletRequest request, 93 HttpServletResponse response) 94 throws Exception { 95 if (!RollerConfig.getBooleanProperty("groupblogging.enabled")) { 97 return mapping.findForward("access-denied"); 98 } 99 100 BasePageModel pageModel = new BasePageModel( 101 "inviteMember.title", request, response, mapping); 102 RollerSession rses = RollerSession.getRollerSession(request); 103 104 if (pageModel.getWebsite() != null && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) { 106 request.setAttribute("model", pageModel); 107 InviteMemberForm form = (InviteMemberForm)actionForm; 108 form.setWebsiteId(pageModel.getWebsite().getId()); 109 ActionForward forward = mapping.findForward("inviteMember.page"); 110 return forward; 111 } else { 112 return mapping.findForward("access-denied"); 113 } 114 } 115 116 public ActionForward send( 117 ActionMapping mapping, 118 ActionForm actionForm, 119 HttpServletRequest request, 120 HttpServletResponse response) 121 throws Exception { 122 if (!RollerConfig.getBooleanProperty("groupblogging.enabled")) { 124 return mapping.findForward("access-denied"); 125 } 126 127 ActionForward forward = mapping.findForward("inviteMember.page"); 128 ActionMessages msgs = new ActionMessages(); 129 ActionMessages errors = new ActionErrors(); 130 InviteMemberForm form = (InviteMemberForm)actionForm; 131 UserManager umgr = RollerFactory.getRoller().getUserManager(); 132 UserData user = umgr.getUserByUserName(form.getUserName()); 133 134 BasePageModel pageModel = new BasePageModel( 135 "inviteMember.title", request, response, mapping); 136 RollerSession rses = RollerSession.getRollerSession(request); 137 138 if (pageModel.getWebsite() != null && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) { 140 141 if (user == null) { 142 errors.add(ActionErrors.GLOBAL_ERROR, 143 new ActionError("inviteMember.error.userNotFound")); 144 } else { 145 RollerRequest rreq = RollerRequest.getRollerRequest(request); 146 WebsiteData website = rreq.getWebsite(); 147 PermissionsData perms = umgr.getPermissions(website, user); 148 if (perms != null && perms.isPending()) { 149 errors.add(ActionErrors.GLOBAL_ERROR, 150 new ActionError("inviteMember.error.userAlreadyInvited")); 151 request.setAttribute("model", new BasePageModel( 152 "inviteMember.title", request, response, mapping)); 153 } else if (perms != null) { 154 errors.add(ActionErrors.GLOBAL_ERROR, 155 new ActionError("inviteMember.error.userAlreadyMember")); 156 request.setAttribute("model", new BasePageModel( 157 "inviteMember.title", request, response, mapping)); 158 } else { 159 String mask = request.getParameter("permissionsMask"); 160 umgr.inviteUser(website, user, Short.parseShort(mask)); 161 RollerFactory.getRoller().flush(); 162 163 request.setAttribute("user", user); 164 try { 165 notifyInvitee(request, website, user); 166 } catch (RollerException e) { 167 errors.add(ActionErrors.GLOBAL_ERROR, 168 new ActionError("error.untranslated", e.getMessage())); 169 } 170 msgs.add(ActionMessages.GLOBAL_MESSAGE, 171 new ActionMessage("inviteMember.userInvited")); 172 173 request.setAttribute("model", new BasePageModel( 174 "inviteMemberDone.title", request, response, mapping)); 175 176 forward = mapping.findForward("memberPermissions"); 177 } 178 } 179 saveErrors(request, errors); 180 saveMessages(request, msgs); 181 182 } else { 183 return mapping.findForward("access-denied"); 184 } 185 return forward; 186 } 187 188 191 private void notifyInvitee( 192 HttpServletRequest request, WebsiteData website, UserData user) 193 throws RollerException { 194 try { 195 Roller roller = RollerFactory.getRoller(); 196 UserManager umgr = roller.getUserManager(); 197 javax.naming.Context ctx = (javax.naming.Context ) 198 new InitialContext ().lookup("java:comp/env"); 199 Session mailSession = 200 (Session )ctx.lookup("mail/Session"); 201 if (mailSession != null) { 202 String userName = user.getUserName(); 203 String from = website.getEmailAddress(); 204 String cc[] = new String [] {from}; 205 String bcc[] = new String [0]; 206 String to[] = new String [] {user.getEmailAddress()}; 207 String subject; 208 String content; 209 210 RollerContext rc = RollerContext.getRollerContext(); 212 String rootURL = RollerRuntimeConfig.getAbsoluteContextURL(); 213 if (rootURL == null || rootURL.trim().length()==0) { 214 rootURL = RequestUtils.serverURL(request) 215 + request.getContextPath(); 216 } 217 String url = rootURL + "/roller-ui/yourWebsites.do"; 218 219 ResourceBundle resources = ResourceBundle.getBundle( 220 "ApplicationResources", 221 website.getLocaleInstance()); 222 StringBuffer sb = new StringBuffer (); 223 sb.append(MessageFormat.format( 224 resources.getString("inviteMember.notificationSubject"), 225 new Object [] { 226 website.getName(), 227 website.getHandle()}) 228 ); 229 subject = sb.toString(); 230 sb = new StringBuffer (); 231 sb.append(MessageFormat.format( 232 resources.getString("inviteMember.notificationContent"), 233 new Object [] { 234 website.getName(), 235 website.getHandle(), 236 user.getUserName(), 237 url 238 })); 239 content = sb.toString(); 240 MailUtil.sendTextMessage( 241 mailSession, from, to, cc, bcc, subject, content); 242 } 243 } catch (NamingException e) { 244 throw new RollerException("ERROR: Notification email(s) not sent, " 245 + "Roller's mail session not properly configured", e); 246 } catch (MessagingException e) { 247 throw new RollerException("ERROR: Notification email(s) not sent, " 248 + "due to Roller configuration or mail server problem.", e); 249 } catch (MalformedURLException e) { 250 throw new RollerException("ERROR: Notification email(s) not sent, " 251 + "Roller site URL is malformed?", e); 252 } catch (RollerException e) { 253 throw new RuntimeException ( 254 "FATAL ERROR: unable to find Roller object", e); 255 } 256 } 257 258 259 } 260 | Popular Tags |