Identify user by login

0

Hello everyone. I am developing a system that works with user registration and I have the following problem: in the system there will be 3 types of users: Administrator, Normal and Temporary.

Depending on the type of user, the areas that he will be able to access and data that he will be able to consult should be changed. For example, the Administrator user can access the query screen and can consult all the registers present in the system, but the Normal user can only see the data of the own cadastre.

At the moment my first problem is on the Home screen after the user logs in. On this screen there is a toolbar and depending on the type of user, the options that appear should be changed. But I'm not able to mount the code. My goal is that the system needs to return the user type when it logs in to change the options that appear on the Home page.

In my database there is the "user" table, where the basic data such as name, phone, email, etc ... are saved, the "group" table, where user groups are defined system, there will be 3 groups in this table: Administrator, normal and temporary) and the table "user_group" where the "user" and "group" registers are associated.

I'm posting the login and Home screen codes.

In the database part, I'm using PostGreSQL and hibernate.

In the authentication and authorization part, I'm using Spring Security

Home.xhtml

<ui:define name="titulo">Dashboard</ui:define>

<ui:define name="corpo">
    <f:metadata>
        <f:event listener="#{gadoBean.inicializar}" type="preRenderView"/>
    </f:metadata>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <p:panel
        style="margin-top:0px;margin-left:450px;width:480px;border: none !important;">
        <p:panelGrid id="grid2" columns="2" styleClass="semBorda">
            <p:inputText id="pesquisarGado" size="50" style="height:25px;"
                value="#{pesquisaGadoBean.filtro.gadoId}" required="true"
                requiredMessage="Digite o código do gado" />
            <p:button  outcome="/gado/rastrearGado" size="60" icon="ui-icon-search" update="grid2">
                <f:param name="gado" value="#{gado.gadoId}" />
            </p:button>
            <p:message for="pesquisarGado" display="icon" />

        </p:panelGrid>
    </p:panel>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
</ui:define>

Login.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<h:outputStylesheet library="css" name="sistema.css" />
</h:head>

<h:body>
<f:metadata>
    <f:viewParam name="Dummy" />
    <f:event listener="#{loginBean.preRender}" type="preRenderView">    </f:event>
</f:metadata>
<div id="login-dialog">
    <p:messages id="messages" autoUpdate="true" closable="true" />
    <h:form id="frm" prependId="false">

        <p:panel
             styleClass="grid-login">

            <p:graphicImage library="images" name="Rastrbov.png" />
            <h:panelGrid columns="2">
                <h:outputText value="Nome *" style="font-weight:bold;" />
                <p:inputText size="18" id="j_username" value="#{loginBean.nome}"
                    a:placeholder="&#128273;Usuario" />
                <h:outputText value="Senha *" style="font-weight:bold;" />
                <p:password size="18" id="j_password"
                    a:placeholder="&#128273;Senha" />
            </h:panelGrid>
            <h:panelGrid columns="4" width="100%" style="text-align:center">

                    <p:commandButton value="Entrar" action="#{loginBean.login}"
                        ajax="false" styleClass="botaoLogin" icon="ui-icon-circle-arrow-e"/>


                    <p:button value="Voltar" outcome="/Main.xhtml"
                        ajax="false" styleClass="botaoLogin" icon="ui-icon-home"/>
                    <br />


            </h:panelGrid>

                <h:panelGrid column="4" width="100%" style="text-align:center">
                        <h:panelGroup>
                            <p:commandLink style="text-decoration:underline;color:blue;"
                                value="Cadastre-se"
                                onclick="PF('varDialogCadastrarUsuario').show()" type="button" />
                            <br />
                            <p:commandLink style="text-decoration:underline;color:blue;"
                                value="Esqueci a senha"
                                onclick="PF('varDialogResetarSenha').show()" type="button" />
                        </h:panelGroup>
                </h:panelGrid>


        </p:panel>
    </h:form>
    <h:form>

        <p:dialog id="dialogResetarSenha" header="Recuperar Senha"
            widgetVar="varDialogResetarSenha" modal="true" showEffect="fade"
            resizable="false" hideEffect="fade">
            <p:panelGrid columns="1">

                <p:inputText value="#{usuarioLogadoMB.email}" required="true"
                    size="60" requiredMessage="O email é obrigatório"
                    id="emailRecuperaSenha" />
                <p:watermark value="Digite seu email" for="emailRecuperaSenha" />
            </p:panelGrid>
            <p:commandButton value="Enviar senha" icon="ui-icon-circle-check"
                actionListener="#{usuarioLogadoMB.solicitarNovaSenha}"
                oncomplete="if (!args.validationFailed){varDialogResetarSenha.hide()}" />
            <p:commandButton value="Cancelar" type="button"
                icon="ui-icon-circle-close" onclick="varDialogResetarSenha.hide()" />

        </p:dialog>
    </h:form>
    <h:form>

        <p:dialog id="dialogCadastrarUsuario" header="Cadastrar Usuario"
            widgetVar="varDialogCadastrarUsuario" modal="true" showEffect="fade"
            resizable="false" hideEffect="fade">
            <p:panelGrid columns="1">

                <p:inputText id="nome" size="60"
                    value="#{cadastroUsuarioTempBean.usuarioTemp.nome}" />
                <p:watermark value="Digite seu nome" for="nome" />



                <p:inputText id="email" size="60"
                    value="#{cadastroUsuarioTempBean.usuarioTemp.email}" />
                <p:watermark value="Digite seu email" for="email" />

                <p:password id="senha" size="60"
                    value="#{cadastroUsuarioTempBean.usuarioTemp.senha}" />
                <p:watermark value="Digite sua senha" for="senha" />

            </p:panelGrid>

            <p:commandButton value="Confirmar" icon="ui-icon-circle-check"
                actionListener="#{cadastroUsuarioTempBean.salvar}"
                oncomplete="PF('varDialogCadastrarUsuario').hide()" />

            <p:commandButton value="Cancelar" type="button"
                icon="ui-icon-circle-close"
                onclick="PF('varDialogCadastrarUsuario').hide()" />

        </p:dialog>
    </h:form>
</div>

LoginBean.java

@Named
@SessionScoped
public class LoginBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Inject
private FacesContext facesContext;

@Inject
private HttpServletRequest request;

@Inject
private HttpServletResponse response;

private String nome;


public void login() throws ServletException, IOException{
    RequestDispatcher dispatcher = request.getRequestDispatcher("/j_spring_security_check");
    dispatcher.forward(request, response);

    facesContext.responseComplete();
}
public void preRender(){
    if("true".equals(request.getParameter("invalid"))){
        FacesUtil.addErrorMessage("Usuário ou senha invalido!");
    }
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}
}

I may be forgetting some files. By the part of hibernate or some file being imported into the codes I've posted. If so, please let me know that I will post the corresponding code soon.

Thanks for any suggestions or advice

    
asked by anonymous 18.08.2016 / 06:15

0 answers