/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.plugin.security;
import com.evasion.entity.security.Authority;
import com.evasion.entity.security.User;
import com.evasion.plugin.security.dao.AuthorityDAO;
import com.evasion.plugin.security.dao.UserDAO;
import com.evasion.sam.jaas.EvasionGroup;
import com.evasion.sam.jaas.EvasionPrincipal;
import com.evasion.sam.ejb.JaasEjb;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author sebastien
*/
@Stateless(mappedName = "LoginAuthService")
@Remote(value = JaasEjb.class)
public class LoginAuthService implements JaasEjb {
/** LOGGER. */
private static final Logger LOGGER = LoggerFactory.getLogger(
LoginAuthService.class);
@PersistenceContext(unitName = "EvasionPU")
private EntityManager em;
private final UserDAO userDAO = new UserDAO();
private final AuthorityDAO authorityDAO = new AuthorityDAO();
/**
* Constructeur par pour les tests.
* @param em Entity Manager.
*/
protected LoginAuthService(final EntityManager em) {
this.em = em;
init();
}
/**
* Constructeur par defaut.
*/
public LoginAuthService() {
}
@SuppressWarnings("PMD.UnusedPrivateMethod")
@edu.umd.cs.findbugs.annotations.SuppressWarnings("UPM_UNCALLED_PRIVATE_METHOD")
@PostConstruct
private void init() {
userDAO.setEntityManager(em);
authorityDAO.setEntityManager(em);
}
/**
* {@inheritDoc }
*/
@Override
public void postLogin(String userName) {
final User user = userDAO.findById(userName);
updateLastLogin(user);
}
/**
* Récupération de la date de dernière connection d'un utilisateur.
* @param login nom d'utilisateur.
* @return date de dernière connection.
*/
private void updateLastLogin(User user) {
if (user != null) {
LOGGER.debug("Mise à jour de la date de dernière conneciton du user {}", user.getUsername());
user.setLastLogin(new Date());
user = userDAO.merge(user);
}
}
/**
* {@inheritDoc }
*/
@Override
public EvasionGroup getAllRoles(String userName) {
final List<Authority> rolesResult = authorityDAO.getAllAuthorityByUser(userName);
EvasionGroup roles = new EvasionGroup("Roles");
for (Authority authority : rolesResult) {
roles.addMember(new EvasionPrincipal(authority.getAuthorityName()));
}
return roles;
}
/**
* {@inheritDoc }
*/
@Override
public EvasionPrincipal getEvasionPrincipal(String userName) {
final User user = userDAO.findById(userName);
EvasionPrincipal userPrincipal = new EvasionPrincipal(userName);
userPrincipal.setLastLogin(user.getLastLogin());
return userPrincipal;
}
/**
* {@inheritDoc }
*/
@Override
public String getPassword(String userName) {
final User user = userDAO.findById(userName);
if (user == null) {
return null;
} else {
return user.getPassword();
}
}
}