How to make a login screen and password using JSF

1

I made a login screen, in this screen the user can register through a button register, done the registration he can access the system informing password and CPF.

My system will be for environmental complaints, and will have a prosecutor who will receive all complaints made by users.

My question is, how will I do it so that when the user logs in, he or she has access to the user's screen, and when the taxpayer logs on to the tax screen?

Login Bean Screen:

package Bean;

import DAO.UsuarioDAO;
import Domain.Usuario;
import Util.MensagesUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;



/**
 *
 * @author alexandre
 */

@ManagedBean
@RequestScoped
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Usuario usuario = new Usuario();
    private UsuarioDAO usuarioDao;
    private ArrayList<Usuario> itens;



    public String login() throws SQLException {
        usuarioDao = new UsuarioDAO();
         usuario = usuarioDao.obterUsuario(usuario);

         if (usuario != null) {
            return "/telaOpcaoUsuario.xhtml";
        }
         else {
             MensagesUtil.adicionarMensagemErro("Usuario e Senha invalidos !");
             return "/login.xhtml";
         }
    }

    public void Cadastrar() {
     try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.salvar(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Salvo Com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();
            MensagesUtil.adicionarMensagemErro(ex.getMessage());
        }
    }

    public void Excluir() {
        try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.excluir(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Removido com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();
            MensagesUtil.adicionarMensagemErro(ex.getMessage());
        }
    }

    public void Editar() {
        try {
            usuarioDao = new UsuarioDAO();
            usuarioDao.editar(usuario);

            setItens(usuarioDao.listar());
            MensagesUtil.adicionarMensagemSucesso("Usuario Editado Com Sucesso");

        } catch (SQLException ex) {
            ex.printStackTrace();;
            MensagesUtil.adicionarMensagemErro(ex.getMessage());

        }
    }


    public UsuarioDAO getUsuarioDao() {
        return usuarioDao;
    }

    public void setUsuarioDao(UsuarioDAO usuarioDao) {
        this.usuarioDao = usuarioDao;
    }

    public ArrayList<Usuario> getItens() {
        return itens;
    }

    public void setItens(ArrayList<Usuario> itens) {
        this.itens = itens;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
}

XHTML LOGIN screen:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                template="/WEB-INF/template/layout.xhtml">

 <ui:define name="menu">

        <h:outputText value="Bem Vindo ao Sistema DMVA"/>
 </ui:define>

    <ui:define name="metadata"/> 

    <ui:define name="conteudo">
    <h:body>

    <p:messages autoUpdate="true" globalOnly="true" />  
        <h:form id="frm-login">
           <div align="center" >     
          <p:panelGrid  columns="1" columnClasses="ui-grid-col-12" >  
              <p:toolbar >
                <f:facet name="left" >
                    <h:outputText value="Faça Seu Login"  /> 
                </f:facet>                
              </p:toolbar> 

     <!-- PainelGroup Serve para unir os elementos em um coluna -->   
                <h:panelGroup> 
                    <p:outputLabel for="cpf" value="CPF" style="margin-left:17px; font-weight:bold"/>
                    <p:inputMask id="cpf" value="#{loginBean.usuario.cpf}" mask="999.999.999-99" />

                </h:panelGroup>

                <h:panelGroup>
                     <p:outputLabel for="senha" value="Senha" style="font-weight:bold" />
                    <p:password id="senha" maxlength="5" size="20" value="#{loginBean.usuario.senha}"/>
                </h:panelGroup>
                <p:commandButton value="Entrar" action="#{loginBean.login}" update="@form"/>
                <p:commandLink value="Cadastrar" type="button" onclick="PF('dlg3').show();" />      
         </p:panelGrid>
               </div>  
        </h:form>

    <p:dialog header="Cadastre Novo Usuário!!! " widgetVar="dlg3" showEffect="explode" hideEffect="bounce" height="100%" >
        <h:form id="frm-cadastro">        
    <h:panelGrid columns="2" >
        <h:outputLabel for="nome" value="Nome:" style="font-weight:bold"/>
        <p:inputText id="nome" value="#{loginBean.usuario.nome}"/>

        <h:outputLabel for="Endereco" value="Endereço" style="font-weight:bold"/>
         <p:inputText id="Endereco" value="#{loginBean.usuario.endereco}" />

        <h:outputLabel for="cpf" value="CPF:" style="font-weight:bold"/>
        <p:inputMask id="cpf" value="#{loginBean.usuario.cpf}" mask="999.999.999-99" />

        <h:outputLabel for="telefone" value="Telefone:" style="font-weight:bold"/>
         <p:inputMask id="telefone" value="#{loginBean.usuario.telefone}" mask="(99)-9999-9999" />

        <h:outputLabel for="senha" value="Senha" style="font-weight:bold"/>
         <p:password id="Senha" value="#{loginBean.usuario.senha}" />


    </h:panelGrid>
         <p:commandButton value="Salvar" action="#{loginBean.Cadastrar}" oncomplete="PF('dlg3').hide();" update="@form" />
    </h:form>
    </p:dialog>
  </h:body> 


     </ui:define>
</ui:composition>
    
asked by anonymous 01.06.2018 / 01:14

1 answer

2

You will need to identify the users' profile in some way, once you have the profiles defined, simply change the targeting page.

if (usuario != null) {
    if (usuario.getPerfil() == 1) {
        return "/telaFISCAL.xhtml";
    } else if (usuario.getPerfil == 2) {
        return "/telaNORMAL.xhtml";
    } else {
        return "/telaERRO.xhtml"
    }
}

If you do not already have any profile type, you may need to change the registration system. Or just add the column to the table and go manually by changing all the users in the database.

I put the profiles as 1 and 2 , because it is usually done table relationships, in which case the user table would not be written "fiscal" but would have the ID of the profile code, relating the user table to the profile table, but you could also have the fixed profile as direct text in the user table, so the if would be more or less if ("FISCAL".equals(usuario.getPerfil())) {}

    
01.06.2018 / 12:46