001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.ui.authoring.struts.actions;
019:
020: import java.net.MalformedURLException;
021: import java.text.MessageFormat;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.ResourceBundle;
025: import javax.mail.MessagingException;
026: import javax.mail.Session;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.struts.action.ActionForm;
035: import org.apache.struts.action.ActionForward;
036: import org.apache.struts.action.ActionMapping;
037: import org.apache.struts.actions.DispatchAction;
038: import org.apache.struts.util.RequestUtils;
039: import org.apache.roller.RollerException;
040: import org.apache.roller.config.RollerRuntimeConfig;
041: import org.apache.roller.business.Roller;
042: import org.apache.roller.business.RollerFactory;
043: import org.apache.roller.business.UserManager;
044: import org.apache.roller.pojos.PermissionsData;
045: import org.apache.roller.pojos.UserData;
046: import org.apache.roller.pojos.WebsiteData;
047: import org.apache.roller.ui.authoring.struts.formbeans.InvitationsForm;
048: import org.apache.roller.ui.core.BasePageModel;
049: import org.apache.roller.ui.core.RollerContext;
050: import org.apache.roller.ui.core.RollerRequest;
051: import org.apache.roller.ui.core.RollerSession;
052: import org.apache.roller.util.MailUtil;
053: import org.apache.struts.action.ActionError;
054: import org.apache.struts.action.ActionErrors;
055: import org.apache.struts.action.ActionMessage;
056: import org.apache.struts.action.ActionMessages;
057:
058: /**
059: * Allow viewing and deletion of invitations.
060: *
061: * @struts.action path="/roller-ui/authoring/invitations" parameter="method" name="invitationsForm"
062: * @struts.action-forward name="invitations.page" path=".Invitations"
063: */
064: public class InvitationsAction extends DispatchAction {
065: private static Log mLogger = LogFactory.getFactory().getInstance(
066: InvitationsAction.class);
067:
068: /** If method param is not specified, use HTTP verb to pick method to call */
069: public ActionForward unspecified(ActionMapping mapping,
070: ActionForm actionForm, HttpServletRequest request,
071: HttpServletResponse response) throws Exception {
072: return view(mapping, actionForm, request, response);
073: }
074:
075: public ActionForward view(ActionMapping mapping,
076: ActionForm actionForm, HttpServletRequest request,
077: HttpServletResponse response) throws Exception {
078: InvitationsPageModel pageModel = new InvitationsPageModel(
079: request, response, mapping);
080: RollerSession rses = RollerSession.getRollerSession(request);
081: if (pageModel.getWebsite() != null
082: && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) {
083: request.setAttribute("model", pageModel);
084: return mapping.findForward("invitations.page");
085: }
086: return mapping.findForward("access-denied");
087: }
088:
089: /** Forwads back to the member permissions page */
090: public ActionForward cancel(ActionMapping mapping,
091: ActionForm actionForm, HttpServletRequest request,
092: HttpServletResponse response) throws Exception {
093: return mapping.findForward("memberPermissions");
094: }
095:
096: public ActionForward revoke(ActionMapping mapping,
097: ActionForm actionForm, HttpServletRequest request,
098: HttpServletResponse response) throws Exception {
099:
100: InvitationsForm invitationForm = (InvitationsForm) actionForm;
101: Roller roller = RollerFactory.getRoller();
102: UserManager umgr = roller.getUserManager();
103: PermissionsData perms = umgr.getPermissions(invitationForm
104: .getPermissionId());
105: ActionErrors errors = new ActionErrors();
106: if (perms == null) {
107: errors.add(null, new ActionError(
108: "invitations.error.notFound"));
109: saveErrors(request, errors);
110: return view(mapping, actionForm, request, response);
111: }
112: RollerSession rses = RollerSession.getRollerSession(request);
113: if (rses.isUserAuthorizedToAdmin(perms.getWebsite())) {
114: umgr.removePermissions(perms);
115: roller.flush();
116: try {
117: notifyInvitee(request, perms.getWebsite(), perms
118: .getUser());
119: ActionMessages msgs = new ActionMessages();
120: msgs.add(ActionMessages.GLOBAL_MESSAGE,
121: new ActionMessage("invitations.revoked"));
122: saveMessages(request, msgs);
123: } catch (RollerException e) {
124: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
125: "error.untranslated", e.getMessage()));
126: saveErrors(request, errors);
127: }
128: return view(mapping, actionForm, request, response);
129: }
130: return mapping.findForward("access-denied");
131: }
132:
133: /**
134: * Inform invitee that invitation has been revoked.
135: */
136: private void notifyInvitee(HttpServletRequest request,
137: WebsiteData website, UserData user) throws RollerException {
138: try {
139: Roller roller = RollerFactory.getRoller();
140: UserManager umgr = roller.getUserManager();
141: javax.naming.Context ctx = (javax.naming.Context) new InitialContext()
142: .lookup("java:comp/env");
143: Session mailSession = (Session) ctx.lookup("mail/Session");
144: if (mailSession != null) {
145: String userName = user.getUserName();
146: String from = website.getEmailAddress();
147: String cc[] = new String[] { from };
148: String bcc[] = new String[0];
149: String to[] = new String[] { user.getEmailAddress() };
150: String subject;
151: String content;
152:
153: // Figure URL to entry edit page
154: RollerContext rc = RollerContext.getRollerContext();
155: String rootURL = RollerRuntimeConfig
156: .getAbsoluteContextURL();
157: if (rootURL == null || rootURL.trim().length() == 0) {
158: rootURL = RequestUtils.serverURL(request)
159: + request.getContextPath();
160: }
161:
162: ResourceBundle resources = ResourceBundle.getBundle(
163: "ApplicationResources", website
164: .getLocaleInstance());
165: StringBuffer sb = new StringBuffer();
166: sb.append(MessageFormat.format(resources
167: .getString("invitations.revokationSubject"),
168: new Object[] { website.getName(),
169: website.getHandle() }));
170: subject = sb.toString();
171: sb = new StringBuffer();
172: sb
173: .append(MessageFormat
174: .format(
175: resources
176: .getString("invitations.revokationContent"),
177: new Object[] {
178: website.getName(),
179: website.getHandle(),
180: user.getUserName() }));
181: content = sb.toString();
182: MailUtil.sendTextMessage(mailSession, from, to, cc,
183: bcc, subject, content);
184: }
185: } catch (NamingException e) {
186: throw new RollerException(
187: "ERROR: Revokation email(s) not sent, "
188: + "Roller's mail session not properly configured",
189: e);
190: } catch (MessagingException e) {
191: throw new RollerException(
192: "ERROR: Revokation email(s) not sent, "
193: + "due to Roller configuration or mail server problem.",
194: e);
195: } catch (MalformedURLException e) {
196: throw new RollerException(
197: "ERROR: Revokation email(s) not sent, "
198: + "Roller site URL is malformed?", e);
199: } catch (RollerException e) {
200: throw new RuntimeException(
201: "FATAL ERROR: unable to find Roller object", e);
202: }
203: }
204:
205: public static class InvitationsPageModel extends BasePageModel {
206: private List pendings = new ArrayList();
207:
208: public InvitationsPageModel(HttpServletRequest request,
209: HttpServletResponse response, ActionMapping mapping)
210: throws RollerException {
211: super ("invitations.title", request, response, mapping);
212: Roller roller = RollerFactory.getRoller();
213: RollerSession rollerSession = RollerSession
214: .getRollerSession(request);
215: RollerRequest rreq = RollerRequest
216: .getRollerRequest(request);
217: WebsiteData website = rreq.getWebsite();
218: pendings = roller.getUserManager().getPendingPermissions(
219: website);
220: }
221:
222: public List getPendings() {
223: return pendings;
224: }
225:
226: public void setPendings(List pendings) {
227: this.pendings = pendings;
228: }
229: }
230: }
|