/*
 * $Id: SubEthaLoginModule.java 735 2006-08-20 04:21:14Z lhoriman $
 * $Source: /cvsroot/Similarity4/src/java/com/similarity/util/SimilarityLoginModule.java,v $
 */

package org.subethamail.core.auth;

import java.security.Principal;

import javax.ejb.Startup;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.core.util.SubEtha;
import org.subethamail.core.util.SubEthaEntityManager;
import org.subethamail.entity.EmailAddress;
import org.subethamail.entity.Person;

import com.caucho.security.Authenticator;
import com.caucho.security.Credentials;
import com.caucho.security.PasswordCredentials;


/**
 * Resin Authenticator which authenticates against our user database.  Unfortunately
 * the Resin documentation is nearly nonexistant so we must infer much of this
 * behavior by looking at their examples.
 *
 * @author Jeff Schnitzer
 */
@Startup
@Singleton
public class SubEthaAuthenticator implements Authenticator
{
	/** */
	private static Logger log = LoggerFactory.getLogger(SubEthaAuthenticator.class);

	/** */
	@Inject @SubEtha SubEthaEntityManager em;
	
	/**
	 * Authenticate the user by the password, returning null on failure.
	 */
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public Principal authenticate(Principal prince, Credentials credentials, Object detail)
	{
		if (log.isDebugEnabled())
			log.debug("Authenticating " + prince);
			
		String email = prince.getName();

		EmailAddress ea = this.em.findEmailAddress(email);
		if (ea == null)
		{
			log.debug("Email address not found: " + email);
			return null;
		}

		StringBuilder credPassword = new StringBuilder();
		credPassword.append(((PasswordCredentials)credentials).getPassword());
		
		Person p = ea.getPerson();
		if (!p.checkPassword(credPassword.toString()))
		{
			if (log.isDebugEnabled())
				log.debug("Wrong password: " + credPassword);
			
			return null;
		}
		else
		{
			SubEthaPrincipal sep = new SubEthaPrincipal(p.getId(), email, p.getRoles());
			return sep;
		}
	}

	/** */
	public boolean isUserInRole(Principal user, String role)
	{
		SubEthaPrincipal p = (SubEthaPrincipal)user;
		
		boolean hasRole = p.getRoles().contains(role);
		
		if (log.isTraceEnabled())
			log.trace("Checking " + p.getEmail() + " for role " + role + (hasRole ? " (yes)" : " (no)"));
		
		return hasRole;
	}

	/** */
	public void logout(Principal user)
	{
		// Nothing special needed
	}	
}
@@ -237,16 +237,49 @@
 ncipal;%0D
+%0Aimport java.util.logging.Level;%0D
 %0A%0D%0Aimpor
@@ -458,60 +458,35 @@
 ort 
-org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory
+lombok.extern.java.Log
 ;%0D%0A
+%0D%0A
 impo
@@ -1075,16 +1075,22 @@
 gleton%0D%0A
+@Log%0D%0A
 public c
@@ -1148,102 +1148,8 @@
 %0A%7B%0D%0A
-%09/** */%0D%0A%09private static Logger log = LoggerFactory.getLogger(SubEthaAuthenticator.class);%0D%0A%0D%0A
 %09/**
@@ -1441,49 +1441,31 @@
 %7B%0D%0A%09
-%09if (log.isDebugEnabled())%0D%0A%09%09%09log.debug(
+    log.log(Level.FINE,
 %22Aut
@@ -1476,19 +1476,21 @@
 icating 
-%22 +
+%7B0%7D%22,
  prince)
@@ -1615,27 +1615,39 @@
 %0A%09%09%7B%0D%0A%09%09
-%09log.debug(
+    log.log(Level.FINE,
 %22Email a
@@ -1664,19 +1664,21 @@
  found: 
-%22 +
+%7B0%7D%22,
  email);
@@ -1925,50 +1925,31 @@
 %0D%0A%09%09
-%09if (log.isDebugEnabled())%0D%0A%09%09%09%09log.debug(
+    log.log(Level.FINE,
 %22Wro
@@ -1961,19 +1961,21 @@
 ssword: 
-%22 +
+%7B0%7D%22,
  credPas
@@ -2315,99 +2315,92 @@
 %0D%0A%09%09
-if (log.isTraceEnabled())%0D%0A%09%09%09log.trace(%22Checking %22 + p.getEmail() + %22 for role %22 + 
+log.log(Level.FINER,%22Checking %7B0%7D for role %7B1%7D %7B2%7D%22, new Object%5B%5D%7Bp.getEmail(),
 role
- + 
+,
 (has
@@ -2407,17 +2407,16 @@
 Role ? %22
- 
 (yes)%22 :
@@ -2421,15 +2421,15 @@
  : %22
- 
 (no)%22)
+%7D
 );%0D%0A

switch logging framework