2023-08-27 11:59:08 +01:00
|
|
|
package stirling.software.SPDF.controller.web;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-12-25 12:58:49 +00:00
|
|
|
import java.util.Iterator;
|
2023-08-27 11:59:08 +01:00
|
|
|
import java.util.List;
|
2024-03-06 23:14:02 +01:00
|
|
|
import java.util.Map;
|
2023-08-27 11:59:08 +01:00
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
2024-04-29 15:01:22 -06:00
|
|
|
import org.springframework.security.oauth2.core.user.OAuth2User;
|
2023-08-27 11:59:08 +01:00
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
2024-04-29 15:01:22 -06:00
|
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
2023-12-25 12:58:49 +00:00
|
|
|
import stirling.software.SPDF.model.Authority;
|
|
|
|
import stirling.software.SPDF.model.Role;
|
2023-08-27 11:59:08 +01:00
|
|
|
import stirling.software.SPDF.model.User;
|
|
|
|
import stirling.software.SPDF.repository.UserRepository;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
@Controller
|
|
|
|
@Tag(name = "Account Security", description = "Account Security APIs")
|
|
|
|
public class AccountWebController {
|
|
|
|
|
2024-04-29 15:01:22 -06:00
|
|
|
@Autowired ApplicationProperties applicationProperties;
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
@GetMapping("/login")
|
|
|
|
public String login(HttpServletRequest request, Model model, Authentication authentication) {
|
|
|
|
if (authentication != null && authentication.isAuthenticated()) {
|
|
|
|
return "redirect:/";
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 20:43:48 +01:00
|
|
|
model.addAttribute(
|
|
|
|
"oAuth2Enabled", applicationProperties.getSecurity().getOAUTH2().getEnabled());
|
2024-04-29 15:01:22 -06:00
|
|
|
|
2024-02-16 22:49:06 +01:00
|
|
|
model.addAttribute("currentPage", "login");
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
if (request.getParameter("error") != null) {
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
model.addAttribute("error", request.getParameter("error"));
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2023-08-27 11:59:08 +01:00
|
|
|
if (request.getParameter("logout") != null) {
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
model.addAttribute("logoutMessage", "You have been logged out.");
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
return "login";
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
@Autowired
|
|
|
|
private UserRepository userRepository; // Assuming you have a repository for user operations
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
|
|
|
@GetMapping("/addUsers")
|
2023-08-30 22:52:38 +01:00
|
|
|
public String showAddUserForm(Model model, Authentication authentication) {
|
2023-12-25 12:58:49 +00:00
|
|
|
List<User> allUsers = userRepository.findAll();
|
|
|
|
Iterator<User> iterator = allUsers.iterator();
|
2024-03-06 23:14:02 +01:00
|
|
|
Map<String, String> roleDetails = Role.getAllRoleDetails();
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-12-25 12:58:49 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
User user = iterator.next();
|
|
|
|
if (user != null) {
|
|
|
|
for (Authority authority : user.getAuthorities()) {
|
|
|
|
if (authority.getAuthority().equals(Role.INTERNAL_API_USER.getRoleId())) {
|
|
|
|
iterator.remove();
|
2024-03-06 23:14:02 +01:00
|
|
|
roleDetails.remove(Role.INTERNAL_API_USER.getRoleId());
|
2023-12-25 12:58:49 +00:00
|
|
|
break; // Break out of the inner loop once the user is removed
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
model.addAttribute("users", allUsers);
|
2023-08-30 22:52:38 +01:00
|
|
|
model.addAttribute("currentUsername", authentication.getName());
|
2024-03-06 23:14:02 +01:00
|
|
|
model.addAttribute("roleDetails", roleDetails);
|
2023-08-27 11:59:08 +01:00
|
|
|
return "addUsers";
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 22:56:51 +00:00
|
|
|
@PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")
|
2023-08-27 11:59:08 +01:00
|
|
|
@GetMapping("/account")
|
|
|
|
public String account(HttpServletRequest request, Model model, Authentication authentication) {
|
|
|
|
if (authentication == null || !authentication.isAuthenticated()) {
|
|
|
|
return "redirect:/";
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2023-08-27 11:59:08 +01:00
|
|
|
if (authentication != null && authentication.isAuthenticated()) {
|
|
|
|
Object principal = authentication.getPrincipal();
|
2024-04-29 15:01:22 -06:00
|
|
|
String username = null;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
if (principal instanceof UserDetails) {
|
|
|
|
// Cast the principal object to UserDetails
|
|
|
|
UserDetails userDetails = (UserDetails) principal;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
// Retrieve username and other attributes
|
2024-04-29 15:01:22 -06:00
|
|
|
username = userDetails.getUsername();
|
|
|
|
|
|
|
|
// Add oAuth2 Login attributes to the model
|
|
|
|
model.addAttribute("oAuth2Login", false);
|
|
|
|
}
|
|
|
|
if (principal instanceof OAuth2User) {
|
|
|
|
// Cast the principal object to OAuth2User
|
|
|
|
OAuth2User userDetails = (OAuth2User) principal;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2024-04-29 15:01:22 -06:00
|
|
|
// Retrieve username and other attributes
|
|
|
|
username = userDetails.getAttribute("email");
|
|
|
|
|
|
|
|
// Add oAuth2 Login attributes to the model
|
|
|
|
model.addAttribute("oAuth2Login", true);
|
|
|
|
}
|
|
|
|
if (username != null) {
|
2023-08-27 11:59:08 +01:00
|
|
|
// Fetch user details from the database
|
|
|
|
Optional<User> user =
|
2024-04-14 23:07:03 +02:00
|
|
|
userRepository.findByUsernameIgnoreCase(
|
2023-08-27 11:59:08 +01:00
|
|
|
username); // Assuming findByUsername method exists
|
|
|
|
if (!user.isPresent()) {
|
|
|
|
// Handle error appropriately
|
|
|
|
return "redirect:/error"; // Example redirection in case of error
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
// Convert settings map to JSON string
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
String settingsJson;
|
2023-12-30 19:11:27 +00:00
|
|
|
try {
|
2023-08-27 11:59:08 +01:00
|
|
|
settingsJson = objectMapper.writeValueAsString(user.get().getSettings());
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
// Handle JSON conversion error
|
|
|
|
e.printStackTrace();
|
|
|
|
return "redirect:/error"; // Example redirection in case of error
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
// Add attributes to the model
|
|
|
|
model.addAttribute("username", username);
|
|
|
|
model.addAttribute("role", user.get().getRolesAsString());
|
|
|
|
model.addAttribute("settings", settingsJson);
|
2023-09-03 16:40:40 +01:00
|
|
|
model.addAttribute("changeCredsFlag", user.get().isFirstLogin());
|
2024-02-16 22:49:06 +01:00
|
|
|
model.addAttribute("currentPage", "account");
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-08-27 11:59:08 +01:00
|
|
|
return "redirect:/";
|
|
|
|
}
|
|
|
|
return "account";
|
|
|
|
}
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
@PreAuthorize("!hasAuthority('ROLE_DEMO_USER')")
|
|
|
|
@GetMapping("/change-creds")
|
2023-12-25 12:58:49 +00:00
|
|
|
public String changeCreds(
|
2023-08-27 11:59:08 +01:00
|
|
|
HttpServletRequest request, Model model, Authentication authentication) {
|
|
|
|
if (authentication == null || !authentication.isAuthenticated()) {
|
|
|
|
return "redirect:/";
|
|
|
|
}
|
|
|
|
if (authentication != null && authentication.isAuthenticated()) {
|
|
|
|
Object principal = authentication.getPrincipal();
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
if (principal instanceof UserDetails) {
|
|
|
|
// Cast the principal object to UserDetails
|
|
|
|
UserDetails userDetails = (UserDetails) principal;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
// Retrieve username and other attributes
|
|
|
|
String username = userDetails.getUsername();
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2023-08-27 11:59:08 +01:00
|
|
|
// Fetch user details from the database
|
|
|
|
Optional<User> user =
|
2024-04-14 23:07:03 +02:00
|
|
|
userRepository.findByUsernameIgnoreCase(
|
2023-08-27 11:59:08 +01:00
|
|
|
username); // Assuming findByUsername method exists
|
|
|
|
if (!user.isPresent()) {
|
|
|
|
// Handle error appropriately
|
|
|
|
return "redirect:/error"; // Example redirection in case of error
|
|
|
|
}
|
|
|
|
// Add attributes to the model
|
|
|
|
model.addAttribute("username", username);
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-03 16:40:40 +01:00
|
|
|
return "redirect:/";
|
|
|
|
}
|
|
|
|
return "change-creds";
|
|
|
|
}
|
2023-08-27 11:59:08 +01:00
|
|
|
}
|