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.ArrayList ; 23 import java.util.List ; 24 import java.util.ResourceBundle ; 25 import javax.mail.MessagingException ; 26 import javax.mail.Session ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NamingException ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 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.actions.DispatchAction; 38 import org.apache.struts.util.RequestUtils; 39 import org.apache.roller.RollerException; 40 import org.apache.roller.config.RollerRuntimeConfig; 41 import org.apache.roller.model.Roller; 42 import org.apache.roller.model.RollerFactory; 43 import org.apache.roller.model.UserManager; 44 import org.apache.roller.pojos.PermissionsData; 45 import org.apache.roller.pojos.UserData; 46 import org.apache.roller.pojos.WebsiteData; 47 import org.apache.roller.ui.authoring.struts.formbeans.InvitationsForm; 48 import org.apache.roller.ui.core.BasePageModel; 49 import org.apache.roller.ui.core.RollerContext; 50 import org.apache.roller.ui.core.RollerRequest; 51 import org.apache.roller.ui.core.RollerSession; 52 import org.apache.roller.util.MailUtil; 53 import org.apache.struts.action.ActionError; 54 import org.apache.struts.action.ActionErrors; 55 import org.apache.struts.action.ActionMessage; 56 import org.apache.struts.action.ActionMessages; 57 58 64 public class InvitationsAction extends DispatchAction { 65 private static Log mLogger = 66 LogFactory.getFactory().getInstance(InvitationsAction.class); 67 68 69 public ActionForward unspecified( 70 ActionMapping mapping, 71 ActionForm actionForm, 72 HttpServletRequest request, 73 HttpServletResponse response) 74 throws Exception { 75 return view(mapping, actionForm, request, response); 76 } 77 78 public ActionForward view( 79 ActionMapping mapping, 80 ActionForm actionForm, 81 HttpServletRequest request, 82 HttpServletResponse response) 83 throws Exception { 84 InvitationsPageModel pageModel = 85 new InvitationsPageModel(request, response, mapping); 86 RollerSession rses = RollerSession.getRollerSession(request); 87 if (pageModel.getWebsite() != null && 88 rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) { 89 request.setAttribute("model", pageModel); 90 return mapping.findForward("invitations.page"); 91 } 92 return mapping.findForward("access-denied"); 93 } 94 95 96 public ActionForward cancel( 97 ActionMapping mapping, 98 ActionForm actionForm, 99 HttpServletRequest request, 100 HttpServletResponse response) 101 throws Exception { 102 return mapping.findForward("memberPermissions"); 103 } 104 105 public ActionForward revoke( 106 ActionMapping mapping, 107 ActionForm actionForm, 108 HttpServletRequest request, 109 HttpServletResponse response) 110 throws Exception { 111 112 InvitationsForm invitationForm = (InvitationsForm)actionForm; 113 Roller roller = RollerFactory.getRoller(); 114 UserManager umgr = roller.getUserManager(); 115 PermissionsData perms = umgr.getPermissions(invitationForm.getPermissionId()); 116 ActionErrors errors = new ActionErrors(); 117 if (perms == null) { 118 errors.add(null, new ActionError("invitations.error.notFound")); 119 saveErrors(request, errors); 120 return view(mapping, actionForm, request, response); 121 } 122 RollerSession rses = RollerSession.getRollerSession(request); 123 if (rses.isUserAuthorizedToAdmin(perms.getWebsite())) { 124 umgr.removePermissions(perms); 125 roller.flush(); 126 try { 127 notifyInvitee(request, perms.getWebsite(), perms.getUser()); 128 } catch (RollerException e) { 129 errors.add(ActionErrors.GLOBAL_ERROR, 130 new ActionError("error.untranslated", e.getMessage())); 131 } 132 ActionMessages msgs = new ActionMessages(); 133 msgs.add(ActionMessages.GLOBAL_MESSAGE, 134 new ActionMessage("invitations.revoked")); 135 saveMessages(request, msgs); 136 return view(mapping, actionForm, request, response); 137 } 138 return mapping.findForward("access-denied"); 139 } 140 141 144 private void notifyInvitee( 145 HttpServletRequest request, WebsiteData website, UserData user) 146 throws RollerException { 147 try { 148 Roller roller = RollerFactory.getRoller(); 149 UserManager umgr = roller.getUserManager(); 150 javax.naming.Context ctx = (javax.naming.Context ) 151 new InitialContext ().lookup("java:comp/env"); 152 Session mailSession = 153 (Session )ctx.lookup("mail/Session"); 154 if (mailSession != null) { 155 String userName = user.getUserName(); 156 String from = website.getEmailAddress(); 157 String cc[] = new String [] {from}; 158 String bcc[] = new String [0]; 159 String to[] = new String [] {user.getEmailAddress()}; 160 String subject; 161 String content; 162 163 RollerContext rc = RollerContext.getRollerContext(); 165 String rootURL = RollerRuntimeConfig.getAbsoluteContextURL(); 166 if (rootURL == null || rootURL.trim().length()==0) { 167 rootURL = RequestUtils.serverURL(request) 168 + request.getContextPath(); 169 } 170 171 ResourceBundle resources = ResourceBundle.getBundle( 172 "ApplicationResources", 173 website.getLocaleInstance()); 174 StringBuffer sb = new StringBuffer (); 175 sb.append(MessageFormat.format( 176 resources.getString("invitations.revokationSubject"), 177 new Object [] { 178 website.getName(), 179 website.getHandle()}) 180 ); 181 subject = sb.toString(); 182 sb = new StringBuffer (); 183 sb.append(MessageFormat.format( 184 resources.getString("invitations.revokationContent"), 185 new Object [] { 186 website.getName(), 187 website.getHandle(), 188 user.getUserName() 189 })); 190 content = sb.toString(); 191 MailUtil.sendTextMessage( 192 mailSession, from, to, cc, bcc, subject, content); 193 } 194 } catch (NamingException e) { 195 throw new RollerException("ERROR: Revokation email(s) not sent, " 196 + "Roller's mail session not properly configured", e); 197 } catch (MessagingException e) { 198 throw new RollerException("ERROR: Revokation email(s) not sent, " 199 + "due to Roller configuration or mail server problem.", e); 200 } catch (MalformedURLException e) { 201 throw new RollerException("ERROR: Revokation email(s) not sent, " 202 + "Roller site URL is malformed?", e); 203 } catch (RollerException e) { 204 throw new RuntimeException ( 205 "FATAL ERROR: unable to find Roller object", e); 206 } 207 } 208 209 public static class InvitationsPageModel extends BasePageModel { 210 private List pendings = new ArrayList (); 211 212 public InvitationsPageModel(HttpServletRequest request, 213 HttpServletResponse response, ActionMapping mapping) throws RollerException { 214 super("invitations.title", request, response, mapping); 215 Roller roller = RollerFactory.getRoller(); 216 RollerSession rollerSession = RollerSession.getRollerSession(request); 217 RollerRequest rreq = RollerRequest.getRollerRequest(request); 218 WebsiteData website = rreq.getWebsite(); 219 pendings = roller.getUserManager().getPendingPermissions(website); 220 } 221 public List getPendings() { 222 return pendings; 223 } 224 public void setPendings(List pendings) { 225 this.pendings = pendings; 226 } 227 } 228 } 229 | Popular Tags |