mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 15:55:49 +00:00
Entidades inicio
This commit is contained in:
parent
3420933e82
commit
6a63b08705
8 changed files with 513 additions and 0 deletions
|
@ -0,0 +1,37 @@
|
|||
package org.springframework.cheapy.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Table(name = "authorities")
|
||||
public class Authorities extends BaseEntity {
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "username")
|
||||
User user;
|
||||
|
||||
@Size(min = 3, max = 50)
|
||||
String authority;
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getAuthority() {
|
||||
return authority;
|
||||
}
|
||||
|
||||
public void setAuthority(String authority) {
|
||||
this.authority = authority;
|
||||
}
|
||||
|
||||
}
|
102
src/main/java/org/springframework/cheapy/model/Client.java
Normal file
102
src/main/java/org/springframework/cheapy/model/Client.java
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed 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
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
package org.springframework.cheapy.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object representing an owner.
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "clients")
|
||||
public class Client extends User {
|
||||
|
||||
@NotEmpty
|
||||
private String address;
|
||||
|
||||
@NotEmpty
|
||||
private String timetable;
|
||||
|
||||
@NotEmpty
|
||||
@Digits(fraction = 0, integer = 10)
|
||||
private String telephone;
|
||||
|
||||
@NotEmpty
|
||||
private String description;
|
||||
|
||||
@NotEmpty
|
||||
private String code;
|
||||
|
||||
@NotEmpty
|
||||
private String food;
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getTimetable() {
|
||||
return timetable;
|
||||
}
|
||||
|
||||
public void setTimetable(String timetable) {
|
||||
this.timetable = timetable;
|
||||
}
|
||||
|
||||
public String getTelephone() {
|
||||
return telephone;
|
||||
}
|
||||
|
||||
public void setTelephone(String telephone) {
|
||||
this.telephone = telephone;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getFood() {
|
||||
return food;
|
||||
}
|
||||
|
||||
public void setFood(String food) {
|
||||
this.food = food;
|
||||
}
|
||||
|
||||
}
|
62
src/main/java/org/springframework/cheapy/model/User.java
Normal file
62
src/main/java/org/springframework/cheapy/model/User.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package org.springframework.cheapy.model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@NotBlank
|
||||
String username;
|
||||
|
||||
@NotBlank
|
||||
String password;
|
||||
|
||||
@Email
|
||||
@NotBlank
|
||||
String email;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Authorities getAuthority() {
|
||||
return authority;
|
||||
}
|
||||
|
||||
public void setAuthority(Authorities authority) {
|
||||
this.authority = authority;
|
||||
}
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
|
||||
private Authorities authority;
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package org.springframework.cheapy.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.cheapy.model.Authorities;
|
||||
import org.springframework.cheapy.model.User;
|
||||
|
||||
|
||||
|
||||
public interface AuthoritiesRepository extends CrudRepository<Authorities, Integer>{
|
||||
|
||||
Authorities findByUser(User user);
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
package org.springframework.cheapy.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.cheapy.model.User;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, String> {
|
||||
|
||||
User findByUsername(String currentPrincipalName);
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
package org.springframework.cheapy.service;
|
||||
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.cheapy.model.Authorities;
|
||||
import org.springframework.cheapy.model.User;
|
||||
import org.springframework.cheapy.repository.AuthoritiesRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Mostly used as a facade for all Petclinic controllers Also a placeholder
|
||||
* for @Transactional and @Cacheable annotations
|
||||
*
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@Service
|
||||
public class AuthoritiesService {
|
||||
|
||||
private AuthoritiesRepository authoritiesRepository;
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
public AuthoritiesService(AuthoritiesRepository authoritiesRepository,UserService userService) {
|
||||
this.authoritiesRepository = authoritiesRepository;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Authorities findAuthoritiyByUser(User user) {
|
||||
return this.authoritiesRepository.findByUser(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveAuthorities(Authorities authorities) throws DataAccessException {
|
||||
authoritiesRepository.save(authorities);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveAuthorities(String username, String role) throws DataAccessException {
|
||||
Authorities authority = new Authorities();
|
||||
Optional<User> user = userService.findUser(username);
|
||||
if(user.isPresent()) {
|
||||
authority.setUser(user.get());
|
||||
authority.setAuthority(role);
|
||||
//user.get().getAuthorities().add(authority);
|
||||
authoritiesRepository.save(authority);
|
||||
}else
|
||||
throw new DataAccessException("User '"+username+"' not found!") {};
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
package org.springframework.cheapy.service;
|
||||
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.cheapy.model.User;
|
||||
import org.springframework.cheapy.repository.UserRepository;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
* Mostly used as a facade for all Petclinic controllers Also a placeholder
|
||||
* for @Transactional and @Cacheable annotations
|
||||
*
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void saveUser(User user) throws DataAccessException {
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
public Optional<User> findUser(String username) {
|
||||
return userRepository.findById(username);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User getCurrentUser() throws DataAccessException {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
String currentPrincipalName = authentication.getName(); //Obtiene el nombre del ususario actual
|
||||
return this.userRepository.findByUsername(currentPrincipalName); //Obtiene el usuario con ese nombre
|
||||
}
|
||||
}
|
154
src/main/java/org/springframework/cheapy/web/UserController.java
Normal file
154
src/main/java/org/springframework/cheapy/web/UserController.java
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cheapy.web;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cheapy.model.Authorities;
|
||||
import org.springframework.cheapy.model.User;
|
||||
import org.springframework.cheapy.service.AuthoritiesService;
|
||||
import org.springframework.cheapy.service.UserService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Ken Krebs
|
||||
* @author Arjen Poutsma
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
private UserService userService;
|
||||
|
||||
private AuthoritiesService authoritiesService;
|
||||
|
||||
// @Autowired
|
||||
// public UserController (UserService userService, AuthoritiesService authoritiesService,
|
||||
// ClienteService clienteService, FarmaceuticoService farmaceuticoService, ProveedorService proveedorService) {
|
||||
// this.userService = userService;
|
||||
// this.authoritiesService = authoritiesService;
|
||||
// this.clienteService = clienteService;
|
||||
// this.farmaceuticoService = farmaceuticoService;
|
||||
// this.proveedorService = proveedorService;
|
||||
// }
|
||||
//
|
||||
// @InitBinder
|
||||
// public void setAllowedFields(final WebDataBinder dataBinder) {
|
||||
// dataBinder.setDisallowedFields("id");
|
||||
// }
|
||||
//
|
||||
// @GetMapping("users")
|
||||
// private String showUserDetails(ModelMap model) {
|
||||
// User user = this.userService.getCurrentUser();
|
||||
// Authorities authority = this.authoritiesService.findAuthoritiyByUser(user);
|
||||
//
|
||||
// if(authority.getAuthority().equals("cliente")) {
|
||||
// Cliente cliente = this.clienteService.findClienteUser(user);
|
||||
// model.addAttribute("cliente", cliente);
|
||||
// }else if(authority.getAuthority().equals("proveedor")) {
|
||||
// Proveedor proveedor = this.proveedorService.findProveedorUser(user);
|
||||
// model.addAttribute("proveedor", proveedor);
|
||||
// }else if(authority.getAuthority().equals("farmaceutico")) {
|
||||
// Farmaceutico farmaceutico = this.farmaceuticoService.findFarmaceuticoByUser(user);
|
||||
// model.addAttribute("farmaceutico", farmaceutico);
|
||||
// }
|
||||
//
|
||||
// log.info("El usuario '" + user.getUsername() + "' ha mostrado su informacion personal");
|
||||
// return "users/userDetails";
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/users/new")
|
||||
// public String newUser(ModelMap model) {
|
||||
// Cliente cliente = new Cliente();
|
||||
// model.addAttribute("cliente", cliente);
|
||||
// model.addAttribute("dni", new String());
|
||||
// return "users/userRegister";
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/users/new")
|
||||
// public String creationUser(@ModelAttribute("cliente") Cliente cliente, final BindingResult result, ModelMap model) {
|
||||
// if (result.hasErrors()) {
|
||||
// return "users/userRegister";
|
||||
// } else if(cliente.getUser() == null) {
|
||||
// try {
|
||||
// cliente = this.clienteService.clienteDni(cliente.getDni());
|
||||
// }catch(EntityNotFoundException ex) {
|
||||
// result.rejectValue("dni", "clienteNotFound");
|
||||
// return "users/userRegister";
|
||||
// }
|
||||
// cliente.setUser(new User());
|
||||
// model.addAttribute("cliente", cliente);
|
||||
// return "users/userRegister";
|
||||
// }else {
|
||||
// this.userService.saveUser(cliente.getUser());
|
||||
// this.authoritiesService.saveAuthorities(cliente.getUser().getUsername(), "cliente");
|
||||
// this.clienteService.saveCliente(cliente);
|
||||
// log.info("El cliente con dni '" + cliente.getDni() + "' se ha registrado como usuario");
|
||||
// return "redirect:../";
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/users/password")
|
||||
// public String initChangePassword(ModelMap model) {
|
||||
// User currentUser = this.userService.getCurrentUser();
|
||||
// UserValidate user = new UserValidate(currentUser.getUsername(), "");
|
||||
// model.addAttribute("user", user);
|
||||
// return "users/passwordEdit";
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/users/password")
|
||||
// public String changePassword(@ModelAttribute("user") UserValidate user, final BindingResult result, ModelMap model) {
|
||||
// if(result.hasErrors()) {
|
||||
// return "users/passwordEdit";
|
||||
// }else {
|
||||
// User CurrentUser = this.userService.getCurrentUser();
|
||||
// if(CurrentUser.getPassword().equals(user.getPassword()) && user.getNewPassword().equals(user.getValidPassword())) {
|
||||
// if(!user.getNewPassword().isEmpty()) {
|
||||
// CurrentUser.setPassword(user.getNewPassword());
|
||||
// this.userService.saveUser(CurrentUser);
|
||||
// log.info("El usuario '" + CurrentUser.getUsername() + "' ha cambiado satisfactoriamente su contraseña");
|
||||
// return "redirect:../";
|
||||
// }else {
|
||||
// FieldError err = new FieldError("PassException", "newPassword", "Introduce una nueva contraseña");
|
||||
// result.addError(err);
|
||||
// log.warn("El usuario '" + CurrentUser.getUsername() + "' ha tenido un error 'PassException'");
|
||||
// return "users/passwordEdit";
|
||||
// }
|
||||
// }else if(!CurrentUser.getPassword().equals(user.getPassword())){
|
||||
// FieldError err = new FieldError("PassException", "password", "Contraseña incorrecta");
|
||||
// result.addError(err);
|
||||
// log.warn("El usuario '" + CurrentUser.getUsername() + "' ha tenido un error 'PassException'");
|
||||
// return "users/passwordEdit";
|
||||
// }else {
|
||||
// FieldError err = new FieldError("PassException", "newPassword", "Las contraseñas no coinciden");
|
||||
// result.addError(err);
|
||||
// log.warn("El usuario '" + CurrentUser.getUsername() + "' ha tenido un error 'PassException'");
|
||||
// return "users/passwordEdit";
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
Loading…
Reference in a new issue