SecurityBean.java 2.47 KB
package fi.insomnia.bortal.beans;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.hibernate.validator.util.LoggerFactory;
import org.slf4j.Logger;

import fi.insomnia.bortal.facade.LogEntryFacade;
import fi.insomnia.bortal.facade.LogEntryTypeFacade;
import fi.insomnia.bortal.model.LogEntry;
import fi.insomnia.bortal.model.LogEntryType;
import fi.insomnia.bortal.model.User;

/**
 * Session Bean implementation class SercurityBean
 */
@Stateless
public class SecurityBean implements SecurityBeanLocal {

    private final Logger logger = org.slf4j.LoggerFactory.getLogger(SecurityBean.class);
    @EJB
    LogEntryTypeFacade typeFacade;
    @EJB
    LogEntryFacade entryFacade;

    /**
     * Default constructor.
     */
    public SecurityBean() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void logPermissionDenied(User user, Exception exception) {
        LogEntryType type = typeFacade.findOrCreate(SecurityLogType.permissionDenied);
        LogEntry entry = new LogEntry();
        entry.setType(type);
        entry.setTime(Calendar.getInstance());
        entry.setDescription(exception.getMessage());
        entry.setUser(user);
        logger.debug(entry.toString(), exception);
        entryFacade.create(entry);
    }

    public void logException(User user, Exception exception) {
        LogEntryType type = typeFacade.findOrCreate(SecurityLogType.unknownException);
        LogEntry entry = new LogEntry();
        
        entry.setType(type);
        entry.setTime(Calendar.getInstance());
        entry.setDescription(exception.getMessage());
        entry.setUser(user);

        logger.debug(entry.toString(), exception);
        entryFacade.create(entry);
    }

    public void logMessage(User user, String description) {
        logMessage(SecurityLogType.genericMessage, user, description);
    }

    public void logMessage(SecurityLogType paramType, User user, String description) {
        LogEntryType type = typeFacade.findOrCreate(paramType);
        LogEntry entry = new LogEntry();

        entry.setType(type);
        entry.setTime(Calendar.getInstance());
        entry.setDescription(description);
        entry.setUser(user);

        entryFacade.create(entry);
    }

    public void logMessage(String description) {
        logMessage(SecurityLogType.genericMessage, description);
    }

    public void logMessage(SecurityLogType type, String description) {
        logMessage(type, null, description);
    }
}