0 Replies Latest reply on Nov 8, 2010 5:36 AM by cinephil

    Automatic login after identification

    cinephil

      Hello Seamians !

       

      I'm developping my first Seam app.

       

      This app will be open to a restricted list of studients registered in the database of the app.

       

      For the authentication of the users, I use the Authenticator.java automaticaly generated by New Seam Web Project, adapted by myself.

      Login page works fine, redirect to the page I want in relation to the type of the user and the link "Logout" appears at the top right of the page, meaning that the app recognize the user connected.

       

      I've added to the Login page a link to an Identification page for the studients never connected to the app. It's inspired from Authnticator.java and works fine, redirect to the good page when the studient is found in the database but there is still "Login" instead of "Logout" at the top-right of the page.

       

      What can I do to log in automaticaly the studient found in the database ?

       

      My adapted Authenticator.java :

      @Name("authenticator")
      public class Authenticator
      {
          @Logger private Log log;

          @In Identity identity;
          @In Credentials credentials;
         
          @In EntityManager entityManager;
         
          private String pageSuivante = "/home.xhtml";
         
          public String getPageSuivante()
          {
              return this.pageSuivante;
          }
         
          public void setPageSuivante(String page)
          {
              this.pageSuivante = page;
          }

          public boolean authenticate()
          {
             
              try
              {
                  log.info("authenticating {0}", credentials.getUsername());
                 
                  Query query = entityManager.createQuery(
                          "FROM ThUtilisateurUti u " +
                          "WHERE u.utiLogin = :username " +
                              "AND u.utiMotPasse = :password");
                  query.setParameter("username", credentials.getUsername());
                  query.setParameter("password", ThUtilisateurUti.generateMD5(credentials.getPassword()));
                 
                  ThUtilisateurUti user = (ThUtilisateurUti) query.getSingleResult();
         
                  int typeUtilisateur = user.getTeTypeUtilisateurTu().getTuId();
                 
                  identity.addRole(user.getTeTypeUtilisateurTu().getTuLibelle());
                 
                 
                  switch (typeUtilisateur)
                  {
                      case 1 : // Administrateur
                          setPageSuivante("/home.xhtml");
                          return true;
                         
                      case 2 : // Étudiant
                          setPageSuivante("/accueilEtudiant.xhtml");
                          return true;
                         
                      case 3 : // Gestionnaire
                  }
                  setPageSuivante("/login.xhtml");
                  //return true;
                  return false;
                 
              }
              catch (NoResultException ex)
              {
                  //setPageSuivante("/login.xhtml");
                  return false;
              }
          }

      }

       

      My Identification.java in its actual state :

      @Scope(EVENT)
      @Name("identification")
      public class Identification
      {
          @Logger private Log log;

          @In EntityManager entityManager;
          @In StatusMessages statusMessages;
         
          //@In private TePersonnePrs personne;
          //@In private ThEtudiantEtu etudiant;
         
          private String nom;
          private String prenom;
          private Date dateNaissance;

          private String pageSuivante = "/home.xhtml";

          public String getPageSuivante()
          {
              return this.pageSuivante;
          }
         
          public void setPageSuivante(String page)
          {
              this.pageSuivante = page;
          }

          public boolean identifier()
          {
              try
              {
                  log.info("identification.identifier() "
                          + "Tentative d'identification avec les paramètres suivants : "
                          + "#{identification.nom}, #{identification.prenom}, #{identification.dateNaissance}");
         
                  Query query = entityManager.createQuery(
                          "from ThEtudiantEtu e " +
                          "where e.prsNom = :nom " +
                              "and e.prsPrenom = :prenom " +
                              "and e.etuDateNaissance = :dateNaissance ");
                  query.setParameter("nom", this.getNom());
                  query.setParameter("prenom", this.getPrenom());
                  query.setParameter("dateNaissance", this.getDateNaissance());
                 
                  ThEtudiantEtu etudiant = (ThEtudiantEtu) query.getSingleResult();
                 
                  setPageSuivante("/accueilEtudiant.xhtml");
                  return true;
              }
              catch (NoResultException ex)
              {
                  return false;
              }
             
          }

          // add additional action methods
       
          @Length(max = 30)
          public String getNom()
          {
              return nom;
          }
       
          public void setNom(String nom)
          {
              this.nom = nom;
          }

          @Length(max = 30)
          public void setPrenom(String prenom) {
              this.prenom = prenom;
          }

          public String getPrenom() {
              return prenom;
          }

          public void setDateNaissance(Date dateNaissance) {
              this.dateNaissance = dateNaissance;
          }

          public Date getDateNaissance() {
              return dateNaissance;
          }
       
      }

       

      I've tried to add role in identity, like in Authenticator.java but it doesn't work.