Commit 255a9077 authored by CADC Software Admin's avatar CADC Software Admin
Browse files

Merge branch 'release_candidate'

parents 1a216fc6 7d0ad659
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -92,10 +92,11 @@
  <property name="commons-logging" value="${ext.lib}/commons-logging.jar"/>
  <property name="unboundid" value="${ext.lib}/unboundid-ldapsdk-se.jar"/>
  <property name="servlet-api" value="${ext.lib}/servlet-api.jar"/>
  <property name="mail" value="${ext.lib}/mail.jar"/>

  <property name="cadc" value="${cadcAC}:${cadcAC-Server}:${cadcUtil}"/>
  <property name="client.cadc.jars" value="${cadcAC}:${cadcAC-Server}:${cadcLog}:${cadcUtil}"/>
  <property name="client.external.jars" value="${unboundid}:${log4j}:${servlet-api}"/>
  <property name="client.external.jars" value="${unboundid}:${log4j}:${servlet-api}:${mail}"/>

  <property name="jars" value="${cadc}:${client.cadc.jars}:${client.external.jars}"/>

+32 −0
Original line number Diff line number Diff line
###
#
# This file is used by the cadcAccessControl-Admin tool for sending
# account approval messages to newly approved users.
#
# If this file is not present the admin tool will continue to function
# but without sending an email.
#
# 5 fields are requried:
#
#    smtp.host=<host>             The SMTP host name.
#    smtp.sender=<email addr>     The user who will send the email.
#    smtp.replyto=<reply to addr> The reply to email address.
#    mail.subject                 The subject of the email.
#    mail.body=body               The email body.  The %s character in the
#                                 body will be replaced with the user's
#                                 userid (if present).  The # character in
#                                 the body will be replaced with a
#                                 carriage return.
#
# 1 field is optional:
#
#    smtp.bcc=<bcc addr>          A single bcc email address
#
###

smtp.host=example.host
smtp.sender=id@example.com
smtp.replyto=id@example.com
smtp.bcc=id@example.com
mail.subject=New Account
mail.body=Dear User##Your new account is %s ##Thank you
+132 −0
Original line number Diff line number Diff line
@@ -71,14 +71,27 @@ package ca.nrc.cadc.ac.admin;

import java.security.AccessControlException;
import java.security.Principal;
import java.util.Date;
import java.util.IllegalFormatException;
import java.util.Properties;
import java.util.Set;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.security.auth.x500.X500Principal;

import org.apache.log4j.Logger;

import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserNotFoundException;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.net.TransientException;
import ca.nrc.cadc.util.PropertiesReader;

/**
 * This class approves the specified pending user by moving the user
@@ -89,6 +102,16 @@ import ca.nrc.cadc.net.TransientException;
public class ApproveUser extends AbstractUserCommand
{
    private static final Logger log = Logger.getLogger(ApproveUser.class);

    private static final String EMAIL_CONFIG = "ac-admin-email.properties";

    private static final String EMAIL_HOST = "smtp.host";
    private static final String EMAIL_SENDER = "smtp.sender";
    private static final String EMAIL_REPLYTO = "smtp.replyto";
    private static final String EMAIL_BCC = "smtp.bcc";
    private static final String EMAIL_SUBJECT = "mail.subject";
    private static final String EMAIL_BODY = "mail.body";

    private String dn;

    /**
@@ -114,10 +137,13 @@ public class ApproveUser extends AbstractUserCommand
            throw new IllegalArgumentException("Invalid DN format: " + dn);
        }

        boolean approved = false;

        try
        {
            this.getUserPersistence().approvePendingUser(this.getPrincipal());
            this.systemOut.println("User " + this.getPrincipal().getName() + " was approved successfully.");
            approved = true;
        }
        catch (UserNotFoundException e)
        {
@@ -135,6 +161,12 @@ public class ApproveUser extends AbstractUserCommand
            return;
        }

        if (approved)
        {
            // email the user if configuration is available
            emailUser(user);
        }

        user.getIdentities().add(dnPrincipal);
        this.getUserPersistence().modifyUser(user);
        String noWhiteSpaceDN = dn.replaceAll("\\s","");
@@ -142,4 +174,104 @@ public class ApproveUser extends AbstractUserCommand
        this.printUser(user);

    }

    private void emailUser(User<Principal>  user)
    {
        try
        {
            PropertiesReader pr = new PropertiesReader(EMAIL_CONFIG);
            String host = pr.getFirstPropertyValue(EMAIL_HOST);
            String sender = pr.getFirstPropertyValue(EMAIL_SENDER);
            String replyto = pr.getFirstPropertyValue(EMAIL_REPLYTO);
            String subject = pr.getFirstPropertyValue(EMAIL_SUBJECT);
            String body = pr.getFirstPropertyValue(EMAIL_BODY);
            String bcc = pr.getFirstPropertyValue(EMAIL_BCC);

            log.debug("email host: " + host);
            log.debug("email sender: " + sender);
            log.debug("email replyto: " + replyto);
            log.debug("email subject: " + subject);
            log.debug("email bcc: " + bcc);
            log.debug("email body: " + body);

            if (host == null || sender == null || subject == null || body == null || replyto == null)
            {
                // do not email, missing configuration
                log.warn("Missing email configuration, not emailing user");
                return;
            }

            Set<PersonalDetails> pds = user.getDetails(PersonalDetails.class);
            String recipient = null;
            if (pds != null && !pds.isEmpty())
            {
                PersonalDetails pd = pds.iterator().next();
                recipient = pd.email;
            }
            if (recipient == null)
            {
                log.warn("No user email address, not emailing");
                return;
            }

            HttpPrincipal p = user.getIdentities(HttpPrincipal.class).iterator().next();

            // try to put the userid in the body
            String populatedBody = null;
            try
            {
                populatedBody = String.format(body, p.getName());
            }
            catch (IllegalFormatException e)
            {
                log.info("userid not inserted into message body");
                populatedBody = null;
            }

            if (populatedBody == null)
            {
                populatedBody = body;
            }

            log.debug("email body populated: " + populatedBody);

            // add the carriage returns
            populatedBody = populatedBody.replaceAll("#", "\n");

            log.debug("body with carriage returns: " + populatedBody);

            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            Session session = Session.getInstance(props, null);

            try
            {
                MimeMessage msg = new MimeMessage(session);
                Address senderAddress = new InternetAddress(sender);
                Address recipientAddress = new InternetAddress(recipient);
                Address replytoAddress = new InternetAddress(replyto);
                msg.setFrom(senderAddress);
                msg.setRecipient(Message.RecipientType.TO, recipientAddress);
                msg.setReplyTo(new Address[] {replytoAddress});
                if (bcc != null)
                {
                    Address bccAddress = new InternetAddress(bcc);
                    msg.addRecipient(Message.RecipientType.BCC, bccAddress);
                }
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setText(populatedBody);
                Transport.send(msg);
                this.systemOut.println("Emailed approval message to user.");
            }
            catch (Exception e)
            {
                log.warn("Failed to send email address: " + e.getMessage(), e);
            }
        }
        catch (Exception e)
        {
            log.warn("Failed to email user", e);
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -136,6 +136,8 @@ public class CmdLineParser
    {
    	int count = 0;

    	this.logLevel = Level.WARN;

        // only one log level is allowed
    	if (am.isSet("v") || am.isSet("verbose"))
    	{
+4 −4
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@ public class CmdLineParserTest
    	{
    	    String[] mArgs = {"-h"};
    	    CmdLineParser parser = new CmdLineParser(mArgs, sysOut, sysErr);
    	    Assert.assertEquals(Level.OFF, parser.getLogLevel());
    	    Assert.assertEquals(Level.WARN, parser.getLogLevel());
    	}
    	catch (Exception e)
    	{
@@ -104,7 +104,7 @@ public class CmdLineParserTest
    	{
    	    String[] mArgs = {"--help"};
    	    CmdLineParser parser = new CmdLineParser(mArgs, sysOut, sysErr);
    	    Assert.assertEquals(Level.OFF, parser.getLogLevel());
    	    Assert.assertEquals(Level.WARN, parser.getLogLevel());
    	}
    	catch (Exception e)
    	{
@@ -116,7 +116,7 @@ public class CmdLineParserTest
    	{
    	    String[] mArgs = {"--list", "-h"};
    	    CmdLineParser parser = new CmdLineParser(mArgs, sysOut, sysErr);
    	    Assert.assertEquals(Level.OFF, parser.getLogLevel());
    	    Assert.assertEquals(Level.WARN, parser.getLogLevel());
    	}
    	catch (Exception e)
    	{
@@ -144,7 +144,7 @@ public class CmdLineParserTest
    	{
    	    String[] args = {"--list",};
    	    CmdLineParser parser = new CmdLineParser(args, sysOut, sysErr);
    	    Assert.assertEquals(Level.OFF, parser.getLogLevel());
    	    Assert.assertEquals(Level.WARN, parser.getLogLevel());
    	}
    	catch (Exception e)
    	{