Josue
6 months ago
12 changed files with 412 additions and 130 deletions
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<Scene Scope="Project" version="2"> |
|||
<Scope Scope="Faces Configuration Only"/> |
|||
<Scope Scope="Project"/> |
|||
<Scope Scope="All Faces Configurations"/> |
|||
</Scene> |
@ -0,0 +1,62 @@ |
|||
/* |
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
|
|||
*/ |
|||
package hola.seguridad; |
|||
|
|||
import java.io.IOException; |
|||
import javax.inject.Named; |
|||
import javax.enterprise.context.SessionScoped; |
|||
import javax.faces.application.NavigationHandler; |
|||
import javax.faces.context.FacesContext; |
|||
import javax.faces.event.PhaseEvent; |
|||
import javax.faces.event.PhaseId; |
|||
import javax.faces.event.PhaseListener; |
|||
|
|||
/** |
|||
* |
|||
* @author Josue |
|||
*/ |
|||
@Named(value = "autorizacionListener") |
|||
@SessionScoped |
|||
public class AutorizacionListener implements PhaseListener { |
|||
|
|||
/** |
|||
* Creates a new instance of AutorizacionListener |
|||
*/ |
|||
public AutorizacionListener() { |
|||
} |
|||
|
|||
|
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
@Override |
|||
public void afterPhase(PhaseEvent event) { |
|||
try { |
|||
//Obtener la pagina actual para validar la sesion
|
|||
String paginaActual = event.getFacesContext().getViewRoot().getViewId(); |
|||
|
|||
//inicializar la sesion en caso de no haber sesion
|
|||
DemoBeanLogin.getSession(); |
|||
|
|||
//Revisar que no sea la pafina index y que no estes logueado
|
|||
//para redireccionar
|
|||
if (!paginaActual.contains("index.xhtml") && DemoBeanLogin.getEstatus() == false) { |
|||
FacesContext.getCurrentInstance().getExternalContext().redirect("faces/index.xhtml?faces-redirect=true"); |
|||
|
|||
NavigationHandler nh = event.getFacesContext().getApplication().getNavigationHandler(); |
|||
nh.handleNavigation(event.getFacesContext(), null, "index"); |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void beforePhase(PhaseEvent event) {} |
|||
|
|||
@Override |
|||
public PhaseId getPhaseId() { |
|||
return PhaseId.RESTORE_VIEW; |
|||
} |
|||
} |
@ -0,0 +1,138 @@ |
|||
/* |
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|||
* Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template
|
|||
*/ |
|||
package hola.seguridad; |
|||
|
|||
import hola.BL.LoginBLLocal; |
|||
import hola.modelo.Usuario; |
|||
import hola.msg.Mensaje; |
|||
import static hola.msg.Mensaje.CAMPOS_INCOMPLETOS; |
|||
import static hola.msg.Mensaje.DATOS_INCORRECTOS; |
|||
import static hola.msg.Mensaje.ELEMENTO_DUPLICADO; |
|||
import static hola.msg.Mensaje.SIN_ERROR; |
|||
import javax.inject.Named; |
|||
import javax.enterprise.context.SessionScoped; |
|||
import java.io.Serializable; |
|||
import javax.ejb.EJB; |
|||
import javax.faces.context.FacesContext; |
|||
import javax.servlet.http.HttpSession; |
|||
|
|||
/** |
|||
* |
|||
* @author Josue |
|||
*/ |
|||
@Named(value = "demoBeanLogin") |
|||
@SessionScoped |
|||
public class DemoBeanLogin implements Serializable { |
|||
|
|||
@EJB |
|||
private LoginBLLocal loginBL; |
|||
private String rol; |
|||
|
|||
private boolean permisos =false ; |
|||
|
|||
//variable para manejar la sesion
|
|||
public static HttpSession httpSession; |
|||
|
|||
/** |
|||
* Creates a new instance of DemoBeanLogin |
|||
*/ |
|||
public DemoBeanLogin() { |
|||
|
|||
|
|||
} |
|||
|
|||
public String getRol() { |
|||
return rol; |
|||
} |
|||
|
|||
public void setRol(String rol) { |
|||
this.rol = rol; |
|||
} |
|||
|
|||
public boolean isPermisos() { |
|||
return permisos; |
|||
} |
|||
|
|||
public void setPermisos(boolean permisos) { |
|||
this.permisos = permisos; |
|||
} |
|||
|
|||
|
|||
private Usuario usuarios = new Usuario(); |
|||
private static String http = "caba"; |
|||
|
|||
public Usuario getUsuarios() { |
|||
return usuarios; |
|||
} |
|||
|
|||
public void setUsuarios(Usuario usuarios) { |
|||
this.usuarios = usuarios; |
|||
} |
|||
|
|||
|
|||
|
|||
public static void getSession() { |
|||
httpSession = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); |
|||
} |
|||
|
|||
public String logout() { |
|||
//quitar atributo
|
|||
//invalidar la session
|
|||
System.out.println(http); |
|||
httpSession.removeAttribute(http); |
|||
httpSession.invalidate(); |
|||
return "index.xhtml"; |
|||
} |
|||
|
|||
public String login() { |
|||
//BL de autenticacion de usuario
|
|||
//poner atributo
|
|||
|
|||
Mensaje mensaje = loginBL.login(usuarios); |
|||
|
|||
switch (mensaje) { |
|||
case SIN_ERROR: |
|||
rol = loginBL.rol(usuarios); |
|||
System.out.println(rol); |
|||
httpSession.setAttribute(http, "true"); |
|||
usuarios = new Usuario(); |
|||
return "Producto.xhtml"; |
|||
case ELEMENTO_DUPLICADO: |
|||
return null; |
|||
case CAMPOS_INCOMPLETOS: |
|||
return null; |
|||
case DATOS_INCORRECTOS: |
|||
return null; |
|||
default: |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static boolean getEstatus() { |
|||
if (httpSession != null |
|||
&& httpSession.getId() != null |
|||
&& !httpSession.getId().isEmpty() |
|||
&& httpSession.getAttribute("caba") != null) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
public void permiso() { |
|||
switch (rol) { |
|||
case "admin": |
|||
permisos = true; |
|||
break; |
|||
case "capturista": |
|||
permisos = false; |
|||
break; |
|||
case "usuario": |
|||
permisos = false; |
|||
break; |
|||
default: |
|||
throw new AssertionError(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
<?xml version='1.0' encoding='UTF-8'?> |
|||
<!-- |
|||
|
|||
Licensed to the Apache Software Foundation (ASF) under one |
|||
or more contributor license agreements. See the NOTICE file |
|||
distributed with this work for additional information |
|||
regarding copyright ownership. The ASF licenses this file |
|||
to you under the Apache License, Version 2.0 (the |
|||
"License"); you may not use this file except in compliance |
|||
with the License. You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, |
|||
software distributed under the License is distributed on an |
|||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|||
KIND, either express or implied. See the License for the |
|||
specific language governing permissions and limitations |
|||
under the License. |
|||
|
|||
--> |
|||
<faces-config version="2.2" |
|||
xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> |
|||
|
|||
<lifecycle> |
|||
<phase-listener> |
|||
hola.seguridad.AutorizacionListener |
|||
</phase-listener> |
|||
</lifecycle> |
|||
|
|||
<navigation-rule> |
|||
<from-view-id>*</from-view-id> |
|||
<navigation-case> |
|||
<from-outcome>accesoDenegado</from-outcome> |
|||
<to-view-id>/producto.xhtml</to-view-id> |
|||
</navigation-case> |
|||
</navigation-rule> |
|||
|
|||
</faces-config> |
@ -0,0 +1,66 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!-- |
|||
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
|||
Click nbfs://nbhost/SystemFileSystem/Templates/Other/xhtml.xhtml to edit this template |
|||
--> |
|||
<!DOCTYPE html> |
|||
<html xmlns="http://www.w3.org/1999/xhtml" |
|||
xmlns:h="http://xmlns.jcp.org/jsf/html" |
|||
xmlns:p="http://primefaces.org/ui"> |
|||
<h:head> |
|||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
|||
<h:outputStylesheet name="./css/default.css"/> |
|||
<h:outputStylesheet name="./css/cssLayout.css"/> |
|||
|
|||
<title>Facelets Template</title> |
|||
<style> |
|||
|
|||
</style> |
|||
</h:head> |
|||
<center> |
|||
|
|||
<h:body style="text-align: center"> |
|||
<h2> Inventario almacen </h2> |
|||
|
|||
<h:form> |
|||
|
|||
|
|||
<p:panel header="Login" style="width:300px"> |
|||
|
|||
<h:panelGrid columns="1" cellpadding="5"> |
|||
|
|||
<p:inputText id="usuario" |
|||
value="#{demoBeanLogin.usuarios.username}" |
|||
title='nombre' |
|||
required="true" |
|||
requiredMessage="Este campo no puede estar vacio" |
|||
placeholder="Ingrese el usuario"> |
|||
</p:inputText> |
|||
|
|||
|
|||
<p:password id="contrasenia" |
|||
value="#{demoBeanLogin.usuarios.npila}" |
|||
title="contraseña" |
|||
redisplay="true" |
|||
required="true" |
|||
|
|||
requiredMessage="Este campo no puede estar vacio" |
|||
placeholder="Ingrse la contraseña" |
|||
style="width: 223px"> |
|||
|
|||
</p:password> |
|||
|
|||
<p:commandButton value="Login" |
|||
action="#{demoBeanLogin.login()}" |
|||
ajax="false" /> |
|||
</h:panelGrid> |
|||
|
|||
|
|||
</p:panel> |
|||
|
|||
</h:form> |
|||
|
|||
</h:body> |
|||
</center> |
|||
</html> |
|||
|
Loading…
Reference in new issue