Search in sources :

Example 1 with SecurityUser

use of fr.univlorraine.ecandidat.services.security.SecurityUser in project esup-ecandidat by EsupPortail.

the class UserController method connectCandidatInterne.

/**
 * Connexion d'un candidat
 * @param username
 *                     login
 * @param password
 *                     mot de passe
 */
public void connectCandidatInterne(final String username, final String password) {
    if (loadBalancingController.isLoadBalancingGestionnaireMode()) {
        return;
    }
    final CompteMinima cptMin = candidatController.searchCptMinByNumDossier(username);
    if (cptMin != null) {
        if (!validPwdCandidat(password, cptMin)) {
            return;
        }
        if (!cptMin.getTemValidCptMin() || !cptMin.getTemValidMailCptMin()) {
            Notification.show(applicationContext.getMessage("compteMinima.connect.valid.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
            return;
        }
    } else {
        Notification.show(applicationContext.getMessage("compteMinima.connect.user.error", null, UI.getCurrent().getLocale()), Type.WARNING_MESSAGE);
        return;
    }
    final SecurityUser user = constructSecurityUserCandidat(username, cptMin);
    if (user == null) {
        return;
    }
    // authentication
    final UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(user, username, user.getAuthorities());
    final Authentication authentication = authenticationManagerCandidat.authenticate(authRequest);
    /* Se désinscrit de la réception de notifications */
    uiController.unregisterUiCandidat(MainUI.getCurrent());
    final SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(authentication);
    SecurityContextHolder.setContext(context);
    UI.getCurrent().getSession().getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    final MainUI current = (MainUI) UI.getCurrent();
    uiController.registerUiCandidat(current);
    i18nController.initLanguageUI(true);
    current.navigateToAccueilView();
}
Also used : CompteMinima(fr.univlorraine.ecandidat.entities.ecandidat.CompteMinima) MainUI(fr.univlorraine.ecandidat.MainUI) SecurityUser(fr.univlorraine.ecandidat.services.security.SecurityUser) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 2 with SecurityUser

use of fr.univlorraine.ecandidat.services.security.SecurityUser in project esup-ecandidat by EsupPortail.

the class UserController method connectOther.

/**
 * Connect un membre de commission ou centre cand
 * @param  username
 *                      le username
 * @return          le user connecte
 */
private SecurityUser connectOther(final String username) {
    if (loadBalancingController.isLoadBalancingCandidatMode()) {
        return null;
    }
    List<GrantedAuthority> authoritiesListe = new ArrayList<>();
    final List<DroitProfilInd> listeDroitProfilInd = new ArrayList<>();
    final Individu ind = individuController.getIndividu(username);
    final PreferenceInd pref = (ind != null ? ind.getPreferenceInd() : null);
    if (ind != null) {
        // On recherche les profils autorisé (ctrCand ou commission pour
        // l'utilisateur suivant son login --> On ajoute à la liste
        listeDroitProfilInd.addAll(droitProfilController.searchDroitByLogin(username));
        /* Création de la liste d'autorities */
        SecurityCentreCandidature ctrCand = null;
        SecurityCommission commission = null;
        /* On place la commission favorite si elle existe en tete */
        if (pref != null && pref.getIdCommPref() != null) {
            final Optional<DroitProfilInd> optComm = listeDroitProfilInd.stream().filter(droit -> droit.getCommissionMembre() != null && droit.getCommissionMembre().getCommission() != null && pref.getIdCommPref().equals(droit.getCommissionMembre().getCommission().getIdComm())).findFirst();
            if (optComm.isPresent()) {
                Collections.swap(listeDroitProfilInd, 0, listeDroitProfilInd.indexOf(optComm.get()));
            }
        }
        /* On place le centre de candidature favorit si il existe en tete */
        if (pref != null && pref.getIdCtrCandPref() != null) {
            final Optional<DroitProfilInd> optCtrCand = listeDroitProfilInd.stream().filter(droit -> droit.getGestionnaire() != null && droit.getGestionnaire().getCentreCandidature() != null && pref.getIdCtrCandPref().equals(droit.getGestionnaire().getCentreCandidature().getIdCtrCand())).findFirst();
            if (optCtrCand.isPresent()) {
                Collections.swap(listeDroitProfilInd, 0, listeDroitProfilInd.indexOf(optCtrCand.get()));
            }
        }
        /* On parcourt la liste */
        for (final DroitProfilInd droitProfilInd : listeDroitProfilInd) {
            String codeRole = null;
            if (droitProfilInd.getDroitProfil().isDroitProfilGestionnaireCandidat()) {
                codeRole = ConstanteUtils.ROLE_GESTION_CANDIDAT;
            } else if (droitProfilInd.getDroitProfil().isDroitProfilGestionnaireCandidatLS()) {
                codeRole = ConstanteUtils.ROLE_GESTION_CANDIDAT_LS;
            } else if (droitProfilInd.getDroitProfil().isDroitProfilGestionnaireCtrCand()) {
                codeRole = ConstanteUtils.ROLE_CENTRE_CANDIDATURE;
                final Gestionnaire gestionnaire = droitProfilInd.getGestionnaire();
                if (ctrCand == null && gestionnaire != null && gestionnaire.getCentreCandidature() != null && gestionnaire.getCentreCandidature().getTesCtrCand()) {
                    final List<Integer> listComm = new ArrayList<>();
                    gestionnaire.getCommissions().forEach(e -> listComm.add(e.getIdComm()));
                    ctrCand = new SecurityCentreCandidature(droitProfilInd.getGestionnaire().getCentreCandidature(), new ArrayList<>(droitProfilInd.getDroitProfil().getDroitProfilFoncs()), individuController.getCodCgeForGestionnaire(gestionnaire, username), false, gestionnaire.getTemAllCommGest(), listComm);
                }
            } else if (droitProfilInd.getDroitProfil().isDroitProfilCommission()) {
                codeRole = ConstanteUtils.ROLE_COMMISSION;
                final CommissionMembre membre = droitProfilInd.getCommissionMembre();
                if (commission == null && membre != null && membre.getCommission() != null && membre.getCommission().getTesComm() && membre.getCommission().getCentreCandidature().getTesCtrCand()) {
                    commission = new SecurityCommission(droitProfilInd.getCommissionMembre().getCommission(), new ArrayList<>(droitProfilInd.getDroitProfil().getDroitProfilFoncs()), false);
                }
            }
            if (codeRole != null) {
                final SimpleGrantedAuthority sga = new SimpleGrantedAuthority(codeRole);
                if (!authoritiesListe.contains(sga)) {
                    authoritiesListe.add(sga);
                }
            }
        }
        // gestion des gestionnaires de candidat
        authoritiesListe = traiteDroitGestionnaireCandidat(authoritiesListe, ctrCand, commission);
        // on verifie qu'il y a bien des droits!
        if (authoritiesListe.size() > 0) {
            return new SecurityUserGestionnaire(username, getDisplayNameFromLdap(username), authoritiesListe, ctrCand, commission, pref);
        }
    }
    return null;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) UI(com.vaadin.ui.UI) HttpSessionSecurityContextRepository(org.springframework.security.web.context.HttpSessionSecurityContextRepository) Gestionnaire(fr.univlorraine.ecandidat.entities.ecandidat.Gestionnaire) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CentreCandidature(fr.univlorraine.ecandidat.entities.ecandidat.CentreCandidature) SecurityUserGestionnaire(fr.univlorraine.ecandidat.services.security.SecurityUserGestionnaire) CustomException(fr.univlorraine.ecandidat.utils.CustomException) SecurityCommission(fr.univlorraine.ecandidat.services.security.SecurityCommission) CompteMinima(fr.univlorraine.ecandidat.entities.ecandidat.CompteMinima) Page(com.vaadin.server.Page) PreferenceInd(fr.univlorraine.ecandidat.entities.ecandidat.PreferenceInd) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) PasswordHashService(fr.univlorraine.ecandidat.services.security.PasswordHashService) ConfigAttribute(org.springframework.security.access.ConfigAttribute) Droit(fr.univlorraine.ecandidat.services.security.SecurityFonc.Droit) ConstanteUtils(fr.univlorraine.ecandidat.utils.ConstanteUtils) MethodUtils(fr.univlorraine.ecandidat.utils.MethodUtils) Predicate(java.util.function.Predicate) SecurityCtrCandFonc(fr.univlorraine.ecandidat.services.security.SecurityCtrCandFonc) Collection(java.util.Collection) SecurityCentreCandidature(fr.univlorraine.ecandidat.services.security.SecurityCentreCandidature) Resource(javax.annotation.Resource) MethodSecurityInterceptor(org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor) SecurityUser(fr.univlorraine.ecandidat.services.security.SecurityUser) GrantedAuthority(org.springframework.security.core.GrantedAuthority) List(java.util.List) Type(com.vaadin.ui.Notification.Type) DroitProfilFonc(fr.univlorraine.ecandidat.entities.ecandidat.DroitProfilFonc) SecurityContext(org.springframework.security.core.context.SecurityContext) Optional(java.util.Optional) CommissionMembre(fr.univlorraine.ecandidat.entities.ecandidat.CommissionMembre) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Authentication(org.springframework.security.core.Authentication) View(com.vaadin.navigator.View) MainUI(fr.univlorraine.ecandidat.MainUI) MethodInvocationUtils(org.springframework.security.util.MethodInvocationUtils) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DroitProfilInd(fr.univlorraine.ecandidat.entities.ecandidat.DroitProfilInd) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) PeopleLdap(fr.univlorraine.ecandidat.services.ldap.PeopleLdap) NomenclatureUtils(fr.univlorraine.ecandidat.utils.NomenclatureUtils) Notification(com.vaadin.ui.Notification) UserDetails(org.springframework.security.core.userdetails.UserDetails) SwitchUserFilter(org.springframework.security.web.authentication.switchuser.SwitchUserFilter) SecurityAuthenticationProvider(fr.univlorraine.ecandidat.services.security.SecurityAuthenticationProvider) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ApplicationContext(org.springframework.context.ApplicationContext) SecurityUserCandidat(fr.univlorraine.ecandidat.services.security.SecurityUserCandidat) Commission(fr.univlorraine.ecandidat.entities.ecandidat.Commission) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) Component(org.springframework.stereotype.Component) SecurityCommissionFonc(fr.univlorraine.ecandidat.services.security.SecurityCommissionFonc) Candidat(fr.univlorraine.ecandidat.entities.ecandidat.Candidat) Individu(fr.univlorraine.ecandidat.entities.ecandidat.Individu) Collections(java.util.Collections) Assert(org.springframework.util.Assert) CommissionMembre(fr.univlorraine.ecandidat.entities.ecandidat.CommissionMembre) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ArrayList(java.util.ArrayList) Individu(fr.univlorraine.ecandidat.entities.ecandidat.Individu) SecurityUserGestionnaire(fr.univlorraine.ecandidat.services.security.SecurityUserGestionnaire) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DroitProfilInd(fr.univlorraine.ecandidat.entities.ecandidat.DroitProfilInd) SecurityCentreCandidature(fr.univlorraine.ecandidat.services.security.SecurityCentreCandidature) SecurityCommission(fr.univlorraine.ecandidat.services.security.SecurityCommission) PreferenceInd(fr.univlorraine.ecandidat.entities.ecandidat.PreferenceInd) Gestionnaire(fr.univlorraine.ecandidat.entities.ecandidat.Gestionnaire) SecurityUserGestionnaire(fr.univlorraine.ecandidat.services.security.SecurityUserGestionnaire)

Example 3 with SecurityUser

use of fr.univlorraine.ecandidat.services.security.SecurityUser in project esup-ecandidat by EsupPortail.

the class UserController method constructSecurityUserCandidat.

/**
 * Créer un user Candidat
 * @param  cptMin
 *                      le compte a minima cree
 * @param  username
 *                      le username
 * @return          le user connecte
 */
private SecurityUser constructSecurityUserCandidat(final String username, final CompteMinima cptMin) {
    Integer idCptMin = null;
    String noDossierOPI = null;
    Boolean cptMinValid = false;
    Boolean mailValid = false;
    if (cptMin != null && campagneController.isCampagneActiveCandidat(cptMin.getCampagne())) {
        idCptMin = cptMin.getIdCptMin();
        noDossierOPI = cptMin.getNumDossierOpiCptMin();
        cptMinValid = cptMin.getTemValidCptMin();
        mailValid = cptMin.getTemValidMailCptMin();
        final List<GrantedAuthority> authoritiesListe = new ArrayList<>();
        final SimpleGrantedAuthority sga = new SimpleGrantedAuthority(ConstanteUtils.ROLE_CANDIDAT);
        authoritiesListe.add(sga);
        final Candidat candidat = cptMin.getCandidat();
        String codLangue = null;
        if (candidat != null) {
            codLangue = candidat.getLangue().getCodLangue();
        }
        return new SecurityUserCandidat(username, getDisplayNameCandidat(cptMin), authoritiesListe, idCptMin, noDossierOPI, cptMinValid, mailValid, codLangue);
    } else {
        return new SecurityUser(username, username, new ArrayList<GrantedAuthority>());
    }
}
Also used : SecurityUserCandidat(fr.univlorraine.ecandidat.services.security.SecurityUserCandidat) Candidat(fr.univlorraine.ecandidat.entities.ecandidat.Candidat) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityUserCandidat(fr.univlorraine.ecandidat.services.security.SecurityUserCandidat) SecurityUser(fr.univlorraine.ecandidat.services.security.SecurityUser) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ArrayList(java.util.ArrayList)

Example 4 with SecurityUser

use of fr.univlorraine.ecandidat.services.security.SecurityUser in project esup-ecandidat by EsupPortail.

the class UserController method alimenteSecurityUserCptMin.

/**
 * Alimente la session pour un compte local
 * @param cptMin
 *                   le compte a minima a connecter
 */
public void alimenteSecurityUserCptMin(final CompteMinima cptMin) {
    final SecurityUser user = (SecurityUser) getCurrentUser();
    if (user != null) {
        final List<GrantedAuthority> authoritiesListe = new ArrayList<>();
        final SimpleGrantedAuthority sga = new SimpleGrantedAuthority(ConstanteUtils.ROLE_CANDIDAT);
        authoritiesListe.add(sga);
        final SecurityUserCandidat securityUserCandidat = new SecurityUserCandidat(user.getUsername(), user.getDisplayName(), authoritiesListe, cptMin.getIdCptMin(), cptMin.getNumDossierOpiCptMin(), cptMin.getTemValidCptMin(), cptMin.getTemValidMailCptMin(), null);
        final UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(securityUserCandidat, securityUserCandidat.getUsername(), securityUserCandidat.getAuthorities());
        final Authentication authentication = authenticationManagerCandidat.authenticate(authRequest);
        final SecurityContext context = SecurityContextHolder.createEmptyContext();
        context.setAuthentication(authentication);
        SecurityContextHolder.setContext(context);
        UI.getCurrent().getSession().getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, context);
    }
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityUserCandidat(fr.univlorraine.ecandidat.services.security.SecurityUserCandidat) SecurityUser(fr.univlorraine.ecandidat.services.security.SecurityUser) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ArrayList(java.util.ArrayList) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

SecurityUser (fr.univlorraine.ecandidat.services.security.SecurityUser)4 SecurityUserCandidat (fr.univlorraine.ecandidat.services.security.SecurityUserCandidat)3 ArrayList (java.util.ArrayList)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 Authentication (org.springframework.security.core.Authentication)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 MainUI (fr.univlorraine.ecandidat.MainUI)2 Candidat (fr.univlorraine.ecandidat.entities.ecandidat.Candidat)2 CompteMinima (fr.univlorraine.ecandidat.entities.ecandidat.CompteMinima)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 View (com.vaadin.navigator.View)1 Page (com.vaadin.server.Page)1 Notification (com.vaadin.ui.Notification)1 Type (com.vaadin.ui.Notification.Type)1 UI (com.vaadin.ui.UI)1 CentreCandidature (fr.univlorraine.ecandidat.entities.ecandidat.CentreCandidature)1 Commission (fr.univlorraine.ecandidat.entities.ecandidat.Commission)1 CommissionMembre (fr.univlorraine.ecandidat.entities.ecandidat.CommissionMembre)1 DroitProfilFonc (fr.univlorraine.ecandidat.entities.ecandidat.DroitProfilFonc)1