3 Replies Latest reply on Feb 2, 2006 12:59 PM by gavin.king

    @Roles Issue

    mutpup

      I'm getting an error returned back to me on my login page while utilizing Roles, I'm guessing it's something in how I am using it, but I did not see any examples from the web side so I made some assumptions.

      My login.xhtml page

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:h="http://java.sun.com/jsf/html">
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
       <title>Phone Log Login</title>
      </head>
      <body id="pgHome">
      <h:form>
      <div id="document">
       <div id="header">
      
       </div>
       <div id="container">
       <div id="sidebar">
      
       </div>
       <div id="content">
       <div class="section">
       <h1>Login</h1>
       </div>
       <div class="section">
       <fieldset>
       <div class="entry">
       <div class="label"><h:outputLabel for="username">Username:</h:outputLabel></div>
       <div class="input"><h:inputText id="username" value="#{currentUser.username}"/><br/><span class="errors"><h:message for="username" /></span></div>
       </div>
       <div class="entry">
       <div class="label"><h:outputLabel for="password">Password:</h:outputLabel></div>
       <div class="input"><h:inputSecret id="password" value="#{currentUser.password}" /><br/><span class="errors"><h:message for="password" /></span></div>
       </div>
       <div class="entry errors"><h:messages globalOnly="true" /></div>
       <div class="entry">
       <div class="label"> </div>
       <div class="input">
       <h:commandButton value="Register" action="#{login.login}" class="button"/> 
       <h:commandButton value="Cancel" action="login" class="button"/>
       </div>
       </div>
       </fieldset>
       </div>
       </div>
       </div>
       </div>
      </h:form>
      </body>
      </html>
      


      My LoginAction.java

      
      package org.transaria.noc;
      
      import java.util.List;
      import javax.ejb.Interceptors;
      import javax.ejb.Stateless;
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.contexts.Context;
      import org.jboss.seam.ejb.SeamInterceptor;
      
      @Stateless
      @Name("login")
      @Interceptors(SeamInterceptor.class)
      public class LoginAction implements Login {
      
       @In @Out
       private User user;
      
      
       @PersistenceContext
       private EntityManager em;
      
       @In
       private Context sessionContext;
       @In
       private FacesContext facesContext;
      
       public String login() {
       List<User> results = em.createQuery("from User where username=:username and password=:password")
       .setParameter("username", user.getUsername())
       .setParameter("password", user.getPassword())
       .getResultList();
      
       if ( results.size()==0 )
       {
       facesContext.addMessage(null, new FacesMessage("Invalid login"));
       return "login";
       }
       else
       {
       user = results.get(0);
       sessionContext.set("loggedIn", true);
       return "main";
       }
       }
      
      }
      


      My User.java

      
      package org.transaria.noc;
      
      import java.io.Serializable;
      import java.util.Date;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.*;
      
      @Entity
      @Name("user")
      @Table(name="users")
      @Roles(
       {
       @Role(name="user", scope=ScopeType.EVENT),
       @Role(name="currentUser", scope=ScopeType.SESSION)
       }
       )
      public class User implements Serializable {
      
       private static final long serialVersionUID = 5139654161684162176L;
       private long id;
       private String first;
       private String last;
       private String username;
       private String password;
       private Date createdate;
      
       public User (String first, String last, String username, String password) {
       this.first = first;
       this.last = last;
       this.username = username;
       this.password = password;
       }
      
       public User () {
      
       Date now = new Date();
       createdate = now;
       }
       @NotNull
       public Date getCreatedate() {
       return createdate;
       }
       public void setCreatedate(Date createdate) {
       this.createdate = createdate;
       }
       @NotNull
       public String getFirst() {
       return first;
       }
       public void setFirst(String first) {
       this.first = first;
       }
       @Id @GeneratedValue @NotNull
       public long getId() {
       return id;
       }
       public void setId(long id) {
       this.id = id;
       }
       @NotNull
       public String getLast() {
       return last;
       }
       public void setLast(String last) {
       this.last = last;
       }
       @NotNull
       public String getPassword() {
       return password;
       }
       public void setPassword(String password) {
       this.password = password;
       }
       @NotNull
       public String getUsername() {
       return username;
       }
       public void setUsername(String username) {
       this.username = username;
       }
      
      
      
      }
      
      


      I'm assuming that I just use the Role name as a substitute for the "name" of my entity bean.

      Also my currentUser make sense as a Session scope as I always want to be able to access it in order to get the information from the current logged in users bean for use in the application. But as far as just for creating a user that bean can be dumped as soon as it is posted to the database. Is event the right scope for that? I can't seem to find the clear definition of it....