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.ResourceBundle;
023: import javax.mail.MessagingException;
024: import javax.mail.Session;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts.action.ActionError;
033: import org.apache.struts.action.ActionErrors;
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.action.ActionMessage;
038: import org.apache.struts.action.ActionMessages;
039: import org.apache.struts.actions.DispatchAction;
040: import org.apache.struts.util.RequestUtils;
041: import org.apache.roller.RollerException;
042: import org.apache.roller.config.RollerConfig;
043: import org.apache.roller.config.RollerRuntimeConfig;
044: import org.apache.roller.business.Roller;
045: import org.apache.roller.business.RollerFactory;
046: import org.apache.roller.business.UserManager;
047: import org.apache.roller.pojos.PermissionsData;
048: import org.apache.roller.pojos.UserData;
049: import org.apache.roller.pojos.WebsiteData;
050: import org.apache.roller.ui.core.BasePageModel;
051: import org.apache.roller.ui.core.RollerContext;
052: import org.apache.roller.ui.core.RollerRequest;
053: import org.apache.roller.ui.core.RollerSession;
054: import org.apache.roller.ui.authoring.struts.formbeans.InviteMemberForm;
055: import org.apache.roller.util.MailUtil;
056:
057: /**
058: * Allows website admin to invite new members to website.
059: *
060: * @struts.action path="/roller-ui/authoring/inviteMember" parameter="method" name="inviteMemberForm"
061: * @struts.action-forward name="inviteMember.page" path=".InviteMember"
062: */
063: public class InviteMemberAction extends DispatchAction {
064: private static Log mLogger = LogFactory.getFactory().getInstance(
065: InviteMemberAction.class);
066:
067: /** If method param is not specified, use HTTP verb to pick method to call */
068: public ActionForward unspecified(ActionMapping mapping,
069: ActionForm actionForm, HttpServletRequest request,
070: HttpServletResponse response) throws Exception {
071: if (request.getMethod().equals("GET")) {
072: return edit(mapping, actionForm, request, response);
073: }
074: return send(mapping, actionForm, request, response);
075: }
076:
077: public ActionForward cancel(ActionMapping mapping,
078: ActionForm actionForm, HttpServletRequest request,
079: HttpServletResponse response) throws Exception {
080: return mapping.findForward("memberPermissions");
081: }
082:
083: public ActionForward edit(ActionMapping mapping,
084: ActionForm actionForm, HttpServletRequest request,
085: HttpServletResponse response) throws Exception {
086: // if group blogging is disabled then you can't change permissions
087: if (!RollerConfig.getBooleanProperty("groupblogging.enabled")) {
088: return mapping.findForward("access-denied");
089: }
090:
091: BasePageModel pageModel = new BasePageModel(
092: "inviteMember.title", request, response, mapping);
093: RollerSession rses = RollerSession.getRollerSession(request);
094:
095: // Ensure use has admin perms for this weblog
096: if (pageModel.getWebsite() != null
097: && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) {
098: request.setAttribute("model", pageModel);
099: InviteMemberForm form = (InviteMemberForm) actionForm;
100: form.setWebsiteId(pageModel.getWebsite().getId());
101: ActionForward forward = mapping
102: .findForward("inviteMember.page");
103: return forward;
104: } else {
105: return mapping.findForward("access-denied");
106: }
107: }
108:
109: public ActionForward send(ActionMapping mapping,
110: ActionForm actionForm, HttpServletRequest request,
111: HttpServletResponse response) throws Exception {
112: // if group blogging is disabled then you can't change permissions
113: if (!RollerConfig.getBooleanProperty("groupblogging.enabled")) {
114: return mapping.findForward("access-denied");
115: }
116:
117: ActionForward forward = mapping
118: .findForward("inviteMember.page");
119: ActionMessages msgs = new ActionMessages();
120: ActionMessages errors = new ActionErrors();
121: InviteMemberForm form = (InviteMemberForm) actionForm;
122: UserManager umgr = RollerFactory.getRoller().getUserManager();
123: UserData user = umgr.getUserByUserName(form.getUserName());
124:
125: BasePageModel pageModel = new BasePageModel(
126: "inviteMember.title", request, response, mapping);
127: RollerSession rses = RollerSession.getRollerSession(request);
128:
129: // Ensure use has admin perms for this weblog
130: if (pageModel.getWebsite() != null
131: && rses.isUserAuthorizedToAdmin(pageModel.getWebsite())) {
132:
133: if (user == null) {
134: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
135: "inviteMember.error.userNotFound"));
136: } else {
137: RollerRequest rreq = RollerRequest
138: .getRollerRequest(request);
139: WebsiteData website = rreq.getWebsite();
140: PermissionsData perms = umgr.getPermissions(website,
141: user);
142: if (perms != null && perms.isPending()) {
143: errors
144: .add(
145: ActionErrors.GLOBAL_ERROR,
146: new ActionError(
147: "inviteMember.error.userAlreadyInvited"));
148: request.setAttribute("model", new BasePageModel(
149: "inviteMember.title", request, response,
150: mapping));
151: } else if (perms != null) {
152: errors
153: .add(
154: ActionErrors.GLOBAL_ERROR,
155: new ActionError(
156: "inviteMember.error.userAlreadyMember"));
157: request.setAttribute("model", new BasePageModel(
158: "inviteMember.title", request, response,
159: mapping));
160: } else {
161: String mask = request
162: .getParameter("permissionsMask");
163: umgr.inviteUser(website, user, Short
164: .parseShort(mask));
165: RollerFactory.getRoller().flush();
166:
167: request.setAttribute("user", user);
168: try {
169: notifyInvitee(request, website, user);
170: } catch (RollerException e) {
171: errors.add(ActionErrors.GLOBAL_ERROR,
172: new ActionError("error.untranslated", e
173: .getMessage()));
174: }
175: msgs.add(ActionMessages.GLOBAL_MESSAGE,
176: new ActionMessage(
177: "inviteMember.userInvited"));
178:
179: request.setAttribute("model", new BasePageModel(
180: "inviteMemberDone.title", request,
181: response, mapping));
182:
183: forward = mapping.findForward("memberPermissions");
184: }
185: }
186: saveErrors(request, errors);
187: saveMessages(request, msgs);
188:
189: } else {
190: return mapping.findForward("access-denied");
191: }
192: return forward;
193: }
194:
195: /**
196: * Inform invitee of new invitation.
197: */
198: private void notifyInvitee(HttpServletRequest request,
199: WebsiteData website, UserData user) throws RollerException {
200: try {
201: Roller roller = RollerFactory.getRoller();
202: UserManager umgr = roller.getUserManager();
203: javax.naming.Context ctx = (javax.naming.Context) new InitialContext()
204: .lookup("java:comp/env");
205: Session mailSession = (Session) ctx.lookup("mail/Session");
206: if (mailSession != null) {
207: String userName = user.getUserName();
208: String from = website.getEmailAddress();
209: String cc[] = new String[] { from };
210: String bcc[] = new String[0];
211: String to[] = new String[] { user.getEmailAddress() };
212: String subject;
213: String content;
214:
215: // Figure URL to entry edit page
216: RollerContext rc = RollerContext.getRollerContext();
217: String rootURL = RollerRuntimeConfig
218: .getAbsoluteContextURL();
219: if (rootURL == null || rootURL.trim().length() == 0) {
220: rootURL = RequestUtils.serverURL(request)
221: + request.getContextPath();
222: }
223: String url = rootURL + "/roller-ui/yourWebsites.do";
224:
225: ResourceBundle resources = ResourceBundle.getBundle(
226: "ApplicationResources", website
227: .getLocaleInstance());
228: StringBuffer sb = new StringBuffer();
229: sb.append(MessageFormat.format(resources
230: .getString("inviteMember.notificationSubject"),
231: new Object[] { website.getName(),
232: website.getHandle() }));
233: subject = sb.toString();
234: sb = new StringBuffer();
235: sb.append(MessageFormat.format(resources
236: .getString("inviteMember.notificationContent"),
237: new Object[] { website.getName(),
238: website.getHandle(),
239: user.getUserName(), url }));
240: content = sb.toString();
241: MailUtil.sendTextMessage(mailSession, from, to, cc,
242: bcc, subject, content);
243: }
244: } catch (NamingException e) {
245: throw new RollerException(
246: "ERROR: Notification email(s) not sent, "
247: + "Roller's mail session not properly configured",
248: e);
249: } catch (MessagingException e) {
250: throw new RollerException(
251: "ERROR: Notification email(s) not sent, "
252: + "due to Roller configuration or mail server problem.",
253: e);
254: } catch (MalformedURLException e) {
255: throw new RollerException(
256: "ERROR: Notification email(s) not sent, "
257: + "Roller site URL is malformed?", e);
258: } catch (RollerException e) {
259: throw new RuntimeException(
260: "FATAL ERROR: unable to find Roller object", e);
261: }
262: }
263:
264: }
|