Identify user by login

Asked

Viewed 380 times

0

Hey there, guys. I am developing a system that deals 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 he will be able to access and the data he will be able to query should be changed. For example, the Administrator user can have access to the query screen and can consult all the entries present in the system, but the Normal user can only see the data of the registration itself.

At the moment my first problem is about the home screen (Home) after the user logs in. On this screen there is a toolbar and depending on the type of user, the options that appear must be changed. But I’m not getting the code. My goal is that the system needs to return the type of user when it logs in to change the options that appear in the home.

In my database there is the table "user", where the basic data such as name, telephone, email, etc..., the table "group", where the user groups are defined (in the case of this system, there will be 3 groups in this table: Administrator, normal and temporary) and the table "usuario_grupo" where "usuario" and "group" records are associated".

I am posting the codes referring to the login screen and Home.

In the database part, I am 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;
}
}

It may be that I am forgetting some file. For the part of Hibernate or some file that is being imported in the codes I posted. If so, please let me know that I will soon post the corresponding code.

I thank you for any suggestion or advice

  • Your home.xhtml does not contain the code of the parent file, so it is not possible to verify the code where you try to create the menu. Maybe you can use a rendered in the p:submenu or in the p:menuitem, using a back end function that would check whether the logged in user contains the role needed to view that item.

  • Okay, but in this case, would you have any suggestions on how to resolve this situation? Because in fact, items should be made available when the user logs in and not when he hovers over menu items. That is, in my opinion, I believe I should put some verification function when the user clicks the login button

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.