Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
restrict contact admin form to logged in users
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Oct 4, 2011
1 parent 4fbb1d2 commit 39d77d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 100 deletions.
117 changes: 26 additions & 91 deletions server/zanata-war/src/main/java/org/zanata/action/SendEmailAction.java
Expand Up @@ -30,13 +30,16 @@
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.faces.Renderer;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.zanata.dao.PersonDAO;
import org.zanata.model.HAccount;

import com.beust.jcommander.ParameterException;

/**
* Sends an email to a specified role.
*
Expand Down Expand Up @@ -70,36 +73,17 @@ public class SendEmailAction implements Serializable
private String fromName;
private String fromLoginName;
private String replyEmail;
private String noReplyEmail;
private String toName;
private String toEmail;
private String subject;
private String message;
private String emailType;

public enum EmailType
{
CONTACT_ADMIN, CONTACT_LANGUAGE_TEAM, JOIN_LANGUAGE_TEAM
}

private EmailType type;

@Create
public void onCreate()
{
fromName = authenticatedAccount.getPerson().getName();
fromLoginName = authenticatedAccount.getUsername();
replyEmail = authenticatedAccount.getPerson().getEmail();

// TODO have these set based on the page
type = EmailType.CONTACT_ADMIN;

// TODO get these from config based on the above
toName = "Administrator";
toEmail = "damason@redhat.com";

// TODO get this from config
noReplyEmail = "no-reply@zanata.org";

subject = "";
message = "";
}
Expand Down Expand Up @@ -136,40 +120,6 @@ public void setReplyEmail(String replyEmail)
this.replyEmail = replyEmail;
}

@Email
public String getNoReplyEmail()
{
return noReplyEmail;
}

@Email
public void setNoReplyEmail(String noReplyEmail)
{
this.noReplyEmail = noReplyEmail;
}

public String getToName()
{
return toName;
}

public void setRoleName(String roleName)
{
this.toName = roleName;
}

@Email
public String getToEmail()
{
return toEmail;
}

@Email
public void setToEmail(String roleEmail)
{
this.toEmail = roleEmail;
}

public String getSubject()
{
return subject;
Expand Down Expand Up @@ -201,11 +151,22 @@ public void setHtmlMessage(String encodedMessage)
this.message = encodedMessage;
}

public String getEmailType()
{
return emailType;
}

public void setEmailType(String emailType)
{
this.emailType = emailType;
}

/**
* TODO this probably won't actually do the send - that is handled by seam
* and jsf.
* Sends the email by rendering an appropriate email template with the values
* in this bean.
*
* @return 'success' if it worked, 'failure' otherwise
* @return a view to redirect to. This should be replaced with configuration
* in pages.xml
*/
public String send()
{
Expand All @@ -214,28 +175,27 @@ public String send()

try
{
if (type == EmailType.CONTACT_ADMIN)
if (emailType != null && emailType.equals("contact_admin"))
{
renderer.render(ADMIN_EMAIL_TEMPLATE);
// TODO use localizable string here
FacesMessages.instance().add("Your message has been sent to the administrator");
log.info("Sent email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', toName '{3}', toEmail '{4}', subject '{5}', message '{6}'", fromName, fromLoginName, replyEmail, toName, toEmail, subject, message);
// return "success";
FacesMessages.instance().add("#{messages['jsf.email.admin.SentNotification']}");
log.info("Sent email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', subject '{3}', message '{4}'", fromName, fromLoginName, replyEmail, subject, message);
// TODO navigation should not be handled by the backing bean.
return HELP_MAIN_VIEW;
// return "success";
}
else
{
throw new NotImplementedException("Other email types not implemented.");
throw new Exception("Invalid email type: " + (emailType != null ? emailType : "null"));
}
}
catch (Exception e)
{
FacesMessages.instance().add("There was a problem sending the message: " + e.getMessage());
log.error("Failed to send email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', toName '{3}', toEmail '{4}', subject '{5}', message '{6}'", e, fromName, fromLoginName, replyEmail, toName, toEmail, subject, message);
// return "failure";
log.error("Failed to send email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', subject '{3}', message '{4}'", e, fromName, fromLoginName, replyEmail, subject, message);
// TODO navigation should not be handled by the backing bean.
return CONTACT_ADMIN_VIEW;
// return "failure";
}
}

Expand All @@ -245,34 +205,9 @@ public String send()
*/
public String cancel()
{
log.info("Canceled sending email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', toName '{3}', toEmail '{4}', subject '{5}', message '{6}'", fromName, fromLoginName, replyEmail, toName, toEmail, subject, message);
log.info("Canceled sending email: fromName '{0}', fromLoginName '{1}', replyEmail '{2}', subject '{3}', message '{4}'", fromName, fromLoginName, replyEmail, subject, message);
FacesMessages.instance().add("Sending message canceled");
return HELP_MAIN_VIEW;
}

// @Transactional
// public String validate() throws LoginException
// {
// if (activationKey != null && !activationKey.isEmpty())
// {
// KeyParameter keyPair =
// EmailChangeActivationService.parseKey(activationKey);
//
// HPerson person = personDAO.findById(new Long(keyPair.getId()), true);
// HAccount account = person.getAccount();
// if
// (!account.getUsername().equals(identity.getCredentials().getUsername()))
// {
// throw new LoginException();
// }
// person.setEmail(keyPair.getEmail());
// account.setEnabled(true);
// personDAO.makePersistent(person);
// personDAO.flush();
// FacesMessages.instance().add("You have successfully changed your email account.");
// log.info("update email address to {0} successfully", keyPair.getEmail());
// }
// return "/home.xhtml";
// }

}
6 changes: 4 additions & 2 deletions server/zanata-war/src/main/resources/messages.properties
Expand Up @@ -145,10 +145,12 @@ jsf.EditProfile=Edit Profile
jsf.EditProject=Edit Project
jsf.EditVersion=Edit Version

jsf.email.admin.SubjectPrefix=Zanata user email from '#{sendEmail.fromLoginName}':
jsf.email.admin.Administrator=Administrator
jsf.email.admin.DearAdmin=Dear #{messages['jsf.email.admin.Administrator']},
jsf.email.admin.ReplyInstructions=You can reply to #{sendEmail.fromName} at #{sendEmail.replyEmail}
jsf.email.admin.SentNotification=Your message has been sent to the administrator
jsf.email.admin.SubjectPrefix=Zanata user email from '#{sendEmail.fromLoginName}':
jsf.email.admin.UserMessageIntro=Zanata user '#{sendEmail.fromName}' with id '#{sendEmail.fromLoginName}' has sent the following message:
jsf.email.DearRecipient=Dear #{sendEmail.toName},

jsf.email.MessageBody=Message
jsf.email.ReplyAddress=Reply to
Expand Down
6 changes: 3 additions & 3 deletions server/zanata-war/src/main/webapp/WEB-INF/pages.xml
Expand Up @@ -192,11 +192,11 @@

<!-- Help -->

<!-- TODO have this set up the SendEmailAction bean by inserting 'admin' into it for the contact admin page.
<page view-id="/help/contact_admin.xhtml">
<restrict>#{identity.loggedIn}</restrict>
<param name="emailType" value="#{sendEmail.emailType}" />
</page>
-->


<!-- Project -->

Expand Down
4 changes: 2 additions & 2 deletions server/zanata-war/src/main/webapp/help/email_admin.xhtml
Expand Up @@ -6,13 +6,13 @@
xmlns:s="http://jboss.com/products/seam/taglib"
charset="UTF-8" importance="normal">
<m:from name="#{messages['jsf.Zanata']}" >#{applicationConfiguration.fromEmailAddr}</m:from>
<m:to name="#{messages['jsf.Zanata']} #{sendEmail.toName}">#{applicationConfiguration.adminEmail}</m:to>
<m:to name="#{messages['jsf.Zanata']} #{messages['jsf.email.admin.Administrator']}">#{applicationConfiguration.adminEmail}</m:to>
<m:replyTo name="#{sendEmail.fromName}" >#{sendEmail.replyEmail}</m:replyTo>
<m:subject>#{messages['jsf.email.admin.SubjectPrefix']} #{sendEmail.subject}</m:subject>
<m:body>
<html>
<body>
<p>#{messages['jsf.email.DearRecipient']}</p>
<p>#{messages['jsf.email.admin.DearAdmin']}</p>
<p>#{messages['jsf.email.admin.UserMessageIntro']}</p>
<hr/>
<h:outputText value="#{sendEmail.htmlMessage}" escape="false" />
Expand Down
6 changes: 4 additions & 2 deletions server/zanata-war/src/main/webapp/help/view.xhtml
Expand Up @@ -15,12 +15,14 @@
</ui:define>

<ui:define name="left_content">
<rich:panel id="loggedIn">
<rich:panel id="loggedIn" rendered="#{identity.loggedIn}">
<f:facet name="header">#{messages['jsf.Actions']}</f:facet>
<s:link styleClass="action_link" value="#{messages['jsf.EditHelpPageContent']}"
view="/help/edit_help_content.xhtml" propagation="none"
rendered="#{s:hasRole('admin')}"></s:link>
<s:link styleClass="action_link" value="#{messages['jsf.ContactAdmin']}" view="/help/contact_admin.xhtml" propagation="none"></s:link>
<s:link styleClass="action_link" value="#{messages['jsf.ContactAdmin']}" view="/help/contact_admin.xhtml" propagation="none">
<f:param name="emailType" value="contact_admin" />
</s:link>
</rich:panel>

</ui:define>
Expand Down

0 comments on commit 39d77d3

Please sign in to comment.