Enhance user registration process by adding optional department association and update related UI components

This commit is contained in:
jon ander 2025-04-08 20:24:38 +02:00
parent 2bea3dfc4c
commit 4ad3ed7f51
8 changed files with 45 additions and 14 deletions

View File

@ -9,12 +9,12 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class DataInitializer {
// @Bean
// public CommandLineRunner initData(UserService userService) {
// return args -> {
// userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com");
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com");
// };
// }
@Bean
public CommandLineRunner initData(UserService userService) {
return args -> {
userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com",null);
userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
};
}
}

View File

@ -16,7 +16,7 @@ public class AuthController {
@PostMapping("/regisater")
public ResponseEntity<String> register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
userService.registerUser(username, password, role, mail);
userService.registerUser(username, password, role, mail,null);
return ResponseEntity.ok("User registered successfully");
}
}

View File

@ -28,7 +28,6 @@ public class UserController {
@GetMapping("/home")
public String homePage(Model model) {
model.addAttribute("message", "Bienvenido a Gestión de Inventario");
return "home"; // Retorna la vista home.html
}
@ -39,7 +38,7 @@ public class UserController {
@PostMapping("/register")
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
userService.registerUser(username, password, role, mail);
userService.registerUser(username, password, role, mail, null);
return "redirect:/login?success"; // Redirige al login tras registrarse
}
@GetMapping("/management")

View File

@ -27,6 +27,8 @@ public class User {
@Column(nullable = false, unique = true) // No se permite emails duplicados
private String mail;
@Column(nullable = true)
private Integer departamentos_iddepartamentos;
}

View File

@ -3,7 +3,6 @@ package com.ieslamar.GestionInventario.services;
import com.ieslamar.GestionInventario.entities.Role;
import com.ieslamar.GestionInventario.entities.User;
import com.ieslamar.GestionInventario.repos.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@ -19,12 +18,13 @@ public class UserService {
this.passwordEncoder = passwordEncoder;
}
public void registerUser(String username, String password, String role, String mail) {
public void registerUser(String username, String password, String role, String mail, Integer departamentos_iddepartamentos) {
User user = new User();
user.setNombre(username);
user.setPassword(passwordEncoder.encode(password));
user.setRole(Role.valueOf(role));
user.setMail(mail);
user.setDepartamentos_iddepartamentos(departamentos_iddepartamentos);
userRepository.save(user);
}
public List<User> getAllUsers() {

View File

@ -17,6 +17,18 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,N
CREATE SCHEMA IF NOT EXISTS `inventario` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
USE `inventario` ;
-- -----------------------------------------------------
-- Table `inventario`.`departamentos`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `inventario`.`departamentos` ;
CREATE TABLE IF NOT EXISTS `inventario`.`departamentos` (
`iddepartamentos` INT NOT NULL,
`Nombre` VARCHAR(45) NULL,
PRIMARY KEY (`iddepartamentos`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `inventario`.`users`
-- -----------------------------------------------------
@ -28,9 +40,16 @@ CREATE TABLE IF NOT EXISTS `inventario`.`users` (
`role` ENUM('ADMIN', 'USER') CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci' NOT NULL,
`nombre` VARCHAR(255) NOT NULL,
`mail` VARCHAR(45) NOT NULL,
`departamentos_iddepartamentos` INT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `UKr43af9ap4edm43mmtq01oddj6` (`nombre` ASC) VISIBLE,
UNIQUE INDEX `mail_UNIQUE` (`mail` ASC) VISIBLE)
UNIQUE INDEX `mail_UNIQUE` (`mail` ASC) VISIBLE,
INDEX `fk_users_departamentos1_idx` (`departamentos_iddepartamentos` ASC) VISIBLE,
CONSTRAINT `fk_users_departamentos1`
FOREIGN KEY (`departamentos_iddepartamentos`)
REFERENCES `inventario`.`departamentos` (`iddepartamentos`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 6
DEFAULT CHARACTER SET = utf8mb4
@ -105,12 +124,19 @@ CREATE TABLE IF NOT EXISTS `inventario`.`producto` (
`nombre` VARCHAR(45) NULL,
`productocol` VARCHAR(45) NULL,
`categoria_idcategoria` INT NOT NULL,
`departamentos_iddepartamentos` INT NOT NULL,
PRIMARY KEY (`idproducto`),
INDEX `fk_producto_categoria1_idx` (`categoria_idcategoria` ASC) VISIBLE,
INDEX `fk_producto_departamentos1_idx` (`departamentos_iddepartamentos` ASC) VISIBLE,
CONSTRAINT `fk_producto_categoria1`
FOREIGN KEY (`categoria_idcategoria`)
REFERENCES `inventario`.`categoria` (`idcategoria`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_producto_departamentos1`
FOREIGN KEY (`departamentos_iddepartamentos`)
REFERENCES `inventario`.`departamentos` (`iddepartamentos`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

View File

@ -5,7 +5,7 @@
<link rel="stylesheet" th:href="@{/css/style.css}">
</head>
<body>
<h2 th:text="${message}"></h2>
<h2>Bienvenido a Gestión de Inventario</h2>
<div class="container1">
<!-- Botón visible solo para Administradores -->
<div sec:authorize="hasRole('ADMIN')">

View File

@ -30,7 +30,11 @@
</select>
</div>
<button type="submit">Registrar</button>
</form>
<a href="/Inventario/home">
<button class="button_salir">Volver al inicio</button>
</a>
</div>
</body>
</html>