Enhance user registration process by adding optional department association and update related UI components
This commit is contained in:
parent
2bea3dfc4c
commit
4ad3ed7f51
|
|
@ -9,12 +9,12 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@Configuration
|
@Configuration
|
||||||
public class DataInitializer {
|
public class DataInitializer {
|
||||||
|
|
||||||
// @Bean
|
@Bean
|
||||||
// public CommandLineRunner initData(UserService userService) {
|
public CommandLineRunner initData(UserService userService) {
|
||||||
// return args -> {
|
return args -> {
|
||||||
// userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com");
|
userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com",null);
|
||||||
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com");
|
userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com",null);
|
||||||
// };
|
};
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ public class AuthController {
|
||||||
|
|
||||||
@PostMapping("/regisater")
|
@PostMapping("/regisater")
|
||||||
public ResponseEntity<String> register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
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");
|
return ResponseEntity.ok("User registered successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ public class UserController {
|
||||||
|
|
||||||
@GetMapping("/home")
|
@GetMapping("/home")
|
||||||
public String homePage(Model model) {
|
public String homePage(Model model) {
|
||||||
model.addAttribute("message", "Bienvenido a Gestión de Inventario");
|
|
||||||
return "home"; // Retorna la vista home.html
|
return "home"; // Retorna la vista home.html
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +38,7 @@ public class UserController {
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
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
|
return "redirect:/login?success"; // Redirige al login tras registrarse
|
||||||
}
|
}
|
||||||
@GetMapping("/management")
|
@GetMapping("/management")
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ public class User {
|
||||||
@Column(nullable = false, unique = true) // No se permite emails duplicados
|
@Column(nullable = false, unique = true) // No se permite emails duplicados
|
||||||
private String mail;
|
private String mail;
|
||||||
|
|
||||||
|
@Column(nullable = true)
|
||||||
|
private Integer departamentos_iddepartamentos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package com.ieslamar.GestionInventario.services;
|
||||||
import com.ieslamar.GestionInventario.entities.Role;
|
import com.ieslamar.GestionInventario.entities.Role;
|
||||||
import com.ieslamar.GestionInventario.entities.User;
|
import com.ieslamar.GestionInventario.entities.User;
|
||||||
import com.ieslamar.GestionInventario.repos.UserRepository;
|
import com.ieslamar.GestionInventario.repos.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
@ -19,12 +18,13 @@ public class UserService {
|
||||||
this.passwordEncoder = passwordEncoder;
|
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 user = new User();
|
||||||
user.setNombre(username);
|
user.setNombre(username);
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
user.setRole(Role.valueOf(role));
|
user.setRole(Role.valueOf(role));
|
||||||
user.setMail(mail);
|
user.setMail(mail);
|
||||||
|
user.setDepartamentos_iddepartamentos(departamentos_iddepartamentos);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
public List<User> getAllUsers() {
|
public List<User> getAllUsers() {
|
||||||
|
|
|
||||||
|
|
@ -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 ;
|
CREATE SCHEMA IF NOT EXISTS `inventario` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
|
||||||
USE `inventario` ;
|
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`
|
-- 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,
|
`role` ENUM('ADMIN', 'USER') CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci' NOT NULL,
|
||||||
`nombre` VARCHAR(255) NOT NULL,
|
`nombre` VARCHAR(255) NOT NULL,
|
||||||
`mail` VARCHAR(45) NOT NULL,
|
`mail` VARCHAR(45) NOT NULL,
|
||||||
|
`departamentos_iddepartamentos` INT NULL,
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE INDEX `UKr43af9ap4edm43mmtq01oddj6` (`nombre` ASC) VISIBLE,
|
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
|
ENGINE = InnoDB
|
||||||
AUTO_INCREMENT = 6
|
AUTO_INCREMENT = 6
|
||||||
DEFAULT CHARACTER SET = utf8mb4
|
DEFAULT CHARACTER SET = utf8mb4
|
||||||
|
|
@ -105,12 +124,19 @@ CREATE TABLE IF NOT EXISTS `inventario`.`producto` (
|
||||||
`nombre` VARCHAR(45) NULL,
|
`nombre` VARCHAR(45) NULL,
|
||||||
`productocol` VARCHAR(45) NULL,
|
`productocol` VARCHAR(45) NULL,
|
||||||
`categoria_idcategoria` INT NOT NULL,
|
`categoria_idcategoria` INT NOT NULL,
|
||||||
|
`departamentos_iddepartamentos` INT NOT NULL,
|
||||||
PRIMARY KEY (`idproducto`),
|
PRIMARY KEY (`idproducto`),
|
||||||
INDEX `fk_producto_categoria1_idx` (`categoria_idcategoria` ASC) VISIBLE,
|
INDEX `fk_producto_categoria1_idx` (`categoria_idcategoria` ASC) VISIBLE,
|
||||||
|
INDEX `fk_producto_departamentos1_idx` (`departamentos_iddepartamentos` ASC) VISIBLE,
|
||||||
CONSTRAINT `fk_producto_categoria1`
|
CONSTRAINT `fk_producto_categoria1`
|
||||||
FOREIGN KEY (`categoria_idcategoria`)
|
FOREIGN KEY (`categoria_idcategoria`)
|
||||||
REFERENCES `inventario`.`categoria` (`idcategoria`)
|
REFERENCES `inventario`.`categoria` (`idcategoria`)
|
||||||
ON DELETE NO ACTION
|
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)
|
ON UPDATE NO ACTION)
|
||||||
ENGINE = InnoDB;
|
ENGINE = InnoDB;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
<link rel="stylesheet" th:href="@{/css/style.css}">
|
<link rel="stylesheet" th:href="@{/css/style.css}">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2 th:text="${message}"></h2>
|
<h2>Bienvenido a Gestión de Inventario</h2>
|
||||||
<div class="container1">
|
<div class="container1">
|
||||||
<!-- Botón visible solo para Administradores -->
|
<!-- Botón visible solo para Administradores -->
|
||||||
<div sec:authorize="hasRole('ADMIN')">
|
<div sec:authorize="hasRole('ADMIN')">
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,11 @@
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit">Registrar</button>
|
<button type="submit">Registrar</button>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
|
<a href="/Inventario/home">
|
||||||
|
<button class="button_salir">Volver al inicio</button>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue