Merge branch 'develop' into 016-ver-modificar-eliminar-Cliente#64

This commit is contained in:
abemorcardc 2021-04-09 17:34:03 +02:00 committed by GitHub
commit f9bb074afb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 291 additions and 11 deletions

View file

@ -44,6 +44,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers("/logout").authenticated()
.antMatchers("/usuarios/new").permitAll()
.antMatchers("/usuarios/**").hasAnyAuthority("usuario")
.antMatchers("/admin/**").hasAnyAuthority("admin")
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")

View file

@ -43,5 +43,4 @@ public class User{
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View file

@ -12,9 +12,9 @@ import javax.validation.constraints.NotBlank;
@Table(name = "usuarios")
public class Usuario extends BaseEntity{
/** nombre, apellidos, dni, direccion, telefono, email, username
* (id,nombre, apellidos, dni, direccion, telefono, email, usuar)
*/
/* nombre, apellidos, dni, direccion, telefono, email, username
(id,nombre, apellidos, dni, direccion, telefono, email, usuar)*/
private static final long serialVersionUID = 1L;
@NotBlank
@ -89,11 +89,11 @@ public class Usuario extends BaseEntity{
this.email = email;
}
public User getUser() {
public User getUsuar() {
return usuar;
}
public void setUser(User username) {
public void setUsuar(User username) {
this.usuar = username;
}

View file

@ -2,7 +2,6 @@ package org.springframework.cheapy.repository;
import java.util.List;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.data.repository.Repository;

View file

@ -0,0 +1,18 @@
package org.springframework.cheapy.repository;
import java.util.List;
import org.springframework.cheapy.model.Usuario;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;
public interface UsuarioRepository extends Repository<Usuario, String> {
@Query("SELECT usuario FROM Usuario usuario WHERE username =:username")
@Transactional(readOnly = true)
Usuario findByUsername(String username);
void save(Usuario usuario);
}

View file

@ -0,0 +1,35 @@
package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.Usuario;
import org.springframework.cheapy.repository.UsuarioRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UsuarioService {
private UsuarioRepository usuarioRepository;
@Autowired
public UsuarioService(final UsuarioRepository usuarioRepository) {
this.usuarioRepository = usuarioRepository;
}
@Transactional
public Usuario getCurrentUsuario() throws DataAccessException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = authentication.getName();
return this.usuarioRepository.findByUsername(username);
}
@Transactional
public void saveUsuario(final Usuario usuario) throws DataAccessException {
this.usuarioRepository.save(usuario);
}
}

View file

@ -0,0 +1,86 @@
package org.springframework.cheapy.web;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.model.Usuario;
import org.springframework.cheapy.service.UsuarioService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UsuarioController {
private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuariosForm";
private final UsuarioService usuarioService;
public UsuarioController(final UsuarioService usuarioService) {
this.usuarioService = usuarioService;
}
@GetMapping("/usuarios/show")
public String processShowForm(Map<String, Object> model) {
Usuario usuario = this.usuarioService.getCurrentUsuario();
model.put("usuario", usuario);
return "usuarios/usuariosShow";
}
@GetMapping(value = "/usuarios/edit")
public String updateUsuario(final ModelMap model, HttpServletRequest request) {
Usuario usuario = this.usuarioService.getCurrentUsuario();
model.addAttribute("usuario", usuario);
return UsuarioController.VIEWS_USUARIO_CREATE_OR_UPDATE_FORM;
}
@PostMapping(value = "/usuarios/edit")
public String updateUsuario(@Valid final Usuario usuarioEdit, final BindingResult result,
final ModelMap model, HttpServletRequest request) {
Usuario usuario = this.usuarioService.getCurrentUsuario();
BeanUtils.copyProperties(usuario, usuarioEdit, "nombre", "apellidos", "dni", "direccion", "telefono", "usuar");
usuarioEdit.getUsuar().setUsername(usuario.getNombre());
usuarioEdit.getUsuar().setEnabled(true);
this.usuarioService.saveUsuario(usuarioEdit);
return "redirect:/usuarios/show";
}
@GetMapping(value = "/usuarios/disable")
public String disableUsuario(final ModelMap model) {
Usuario usuario = this.usuarioService.getCurrentUsuario();
model.put("usuario", usuario);
return "usuarios/usuariosDisable";
}
@PostMapping(value = "/usuarios/disable")
public String disableUsuarioForm(final ModelMap model, final HttpServletRequest request) {
Usuario usuario = this.usuarioService.getCurrentUsuario();
usuario.getUsuar().setEnabled(false);
this.usuarioService.saveUsuario(usuario);
try {
request.logout();
} catch (ServletException e) {
e.printStackTrace();
}
return "redirect:/login";
}
}

View file

@ -24,10 +24,10 @@ INSERT INTO authorities VALUES ('lolo','usuario');
INSERT INTO users (dtype,username,password,enabled) VALUES ('User','pepe','pepe', TRUE );
INSERT INTO authorities VALUES ('pepe','usuario');
INSERT INTO usuarios VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin');
INSERT INTO usuarios VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '666973647', 'Paco@gmail.com','paco');
INSERT INTO usuarios VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo');
INSERT INTO usuarios VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe');
INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin');
INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '666973647', 'Paco@gmail.com','paco');
INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo');
INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe');
INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (1,'bar manoli','manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli');
INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (2,'bar david','david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david');

View file

@ -0,0 +1,45 @@
<%@ page session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<cheapy:layout pageName="usuarios">
<h2 style="text-align:center;padding:5px">
<c:if test="${usuario['new']}"><fmt:message key="new"/> </c:if> <fmt:message key="usuario"/>
</h2>
<form:form modelAttribute="usuario" class="form-horizontal" id="add-usuario-form">
<div class="form-group has-feedback">
<cheapy:inputField label="Nombre" name="nombre"/>
<cheapy:inputField label="Apellidos" name="apellidos"/>
<cheapy:inputField label="DNI" name="dni"/>
<cheapy:inputField label="Direccion" name="direccion"/>
<cheapy:inputField label="Telefono" name="telefono"/>
<cheapy:inputField label="Email" name="email"/>
<cheapy:inputField label="Password" name="usuar.password"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="btn-mod">
<c:choose>
<c:when test="${usuario['new']}">
<button class="btn btn-default" type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true" style="padding: 5px"> </span>
Crear usuario</button>
</c:when>
<c:otherwise>
<button class="btn btn-default" type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true" style="padding: 5px"> </span>
Modificar</button>
</c:otherwise>
</c:choose>
</div>
</div>
</div>
</form:form>
</cheapy:layout>

View file

@ -0,0 +1,26 @@
<%@ page session="false" trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<cheapy:layout pageName="usuario">
<jsp:body>
<h2 class="text-center" style="font-family: 'Lobster'; font-size: 30px; color: rgb(0, 64, 128); padding:30px"><em>¿Está seguro de que quiere eliminar su cuenta?</em></h2>
<form:form modelAttribute="usuario" class="form-horizontal">
<div class="btns-edit2">
<button type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-trash" aria-hidden="true" style="padding: 5px"> </span>
Dar de baja</button>
</div>
</form:form>
</jsp:body>
</cheapy:layout>

View file

@ -0,0 +1,62 @@
<%@ page session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<cheapy:layout pageName="usuario">
<h2 style="text-align:center;padding:5px"><fmt:message key="usuario"/></h2>
<table class="table table-striped" id="usuarioTable">
<thead>
<tr>
<th><fmt:message key="nombre"/></th>
<td><c:out value="${usuario.nombre}"/></td>
</tr>
<tr>
<th><fmt:message key="apellidos"/></th>
<td><c:out value="${usuario.apellidos}"/></td>
</tr>
<tr>
<th><fmt:message key="dni"/></th>
<td><c:out value="${usuario.dni}"/></td>
</tr>
<tr>
<th><fmt:message key="direccion"/></th>
<td><c:out value="${usuario.direccion}"/> </td>
</tr>
<tr>
<th><fmt:message key="telefono"/></th>
<td><c:out value="${usuario.telefono}"/></td>
</tr>
<tr>
<th><fmt:message key="email"/></th>
<td><c:out value="${usuario.email}"/></td>
</tr>
</thead>
</table>
<div class="btn-menu">
<sec:authorize access="hasAnyAuthority('usuario')">
<sec:authentication var="principal" property="principal" />
<div class="btns-edit">
<spring:url value="edit" var="editUrl"/>
<button type="button" role="link" onclick="window.location='${fn:escapeXml(editUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-edit" aria-hidden="true" style="padding: 5px"> </span>
Editar usuario</button>
<spring:url value="disable" var="deactivateUrl"/>
<button type="button" role="link" onclick="window.location='${fn:escapeXml(deactivateUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-trash" aria-hidden="true" style="padding: 5px"> </span>
Desactivar usuario</button>
</div>
</sec:authorize>
</div>
</cheapy:layout>

View file

@ -38,6 +38,15 @@
<span class="glyphicon glyphicon-cutlery" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="clientShow"/> </button>
</div>
</sec:authorize>
<sec:authorize access="hasAnyAuthority('usuario')">
<div class="btn-home">
<button type="button" role="link" onclick="window.location='/usuarios/show'" style="font-family: 'Lobster'; font-size: 20px;margin:5px;">
<span class="glyphicon glyphicon-cutlery" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="showUsuario"/> </button>
</div>
</sec:authorize>
</div>