Package com.evasion.plugin.security

Source Code of com.evasion.plugin.security.UserAuthService

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.plugin.security;

import com.evasion.ejb.local.UserAuthServiceLocal;
import com.evasion.ejb.remote.UserAuthServiceRemote;
import com.evasion.entity.security.Authority;
import com.evasion.entity.security.GroupSec;
import com.evasion.entity.security.User;
import com.evasion.exception.PersistenceViolationException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author sebastien
*/
@Stateless
@Local(value = UserAuthServiceLocal.class)
@Remote(value = UserAuthServiceRemote.class)
public class UserAuthService implements UserAuthServiceLocal, UserAuthServiceRemote {

    /**
     * LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            UserAuthService.class);
    @PersistenceContext(unitName = "EvasionPU")
    private EntityManager em;

    protected UserAuthService(EntityManager em) {
        this.em = em;
    }
   
    public UserAuthService() {
    }
   

    /* Interface pour la gestion des utilisateurs */
    public List<User> listUsers() {
        Query query = em.createNamedQuery(User.FIND_ALL);
        return query.getResultList();
    }

    public User findUserByUserName(String u) {
        User user;
        user = em.find(User.class, u);
        return user;
    }

    public User createUser(User u) throws PersistenceViolationException {
        validGrantedAuthority(u);
        encodPassword(u);
        if (findUserByUserName(u.getUsername()) == null) {
            em.persist(u);
        } else {
            throw new PersistenceViolationException("user already exists");
        }
        return u;
    }

    public void deleteUser(User u) {
        em.remove(em.merge(u));
    }

    public User updateUser(User u) {
        final User userBDD = em.find(User.class, u.getUsername());
        LOGGER.debug("Mise à jour du user: {}", u.toString());

        LOGGER.debug("Mise à jour du user: {}", userBDD.getLastLogin());
        validGrantedAuthority(u);
        if (!userBDD.getPassword().equals(u.getPassword())) {
            encodPassword(u);
        }
        return em.merge(u);
    }

    /* Interface pour la gestion des groupes */
    public List<GroupSec> listGroups() {
        Query query = em.createNamedQuery("findAllGroups");
        return query.getResultList();
    }


    private static void validGrantedAuthority(User user) {
        if (user == null) {
            throw new IllegalArgumentException();
        }
        boolean result;
        result = (user.getAuthorities() != null && !user.getAuthorities().isEmpty())
                || (user.getGroups() != null && !user.getGroups().isEmpty());
        if (!result) {
            HashSet<Authority> auth = (new HashSet<Authority>());
            auth.add(new Authority("ROLE_USER"));
            user.addAllAuthority(auth);
        }
    }

    private static void encodPassword(User user) {
        try {
            LOGGER.debug("Encodage du password");
            // Encodage du passord et salt;
            Class clazz = Class.forName("com.evasion.sam.PasswordEncoder");
            Method meth = clazz.getDeclaredMethod("encodePassword", Object.class, String.class);
            String encodedPassword = (String) meth.invoke(null, user, user.getPassword());
            user.setPassword(encodedPassword);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UserAuthService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
TOP

Related Classes of com.evasion.plugin.security.UserAuthService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.