Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,120 changes: 1,120 additions & 0 deletions logs/app.log

Large diffs are not rendered by default.

47 changes: 44 additions & 3 deletions src/main/java/com/podzilla/auth/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
import com.podzilla.auth.model.User;
import com.podzilla.auth.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

private final AdminService adminService;
private static final Logger LOGGER =
LoggerFactory.getLogger(AdminController.class);

public AdminController(final AdminService adminService) {
this.adminService = adminService;
Expand All @@ -29,4 +37,37 @@ public List<User> getUsers() {
return adminService.getUsers();
}

@PatchMapping("/users/{userId}/activate")
@Operation(summary = "Activate or deactivate a user",
description = "Allows an admin to activate"
+ " or deactivate a specific user.")
@ApiResponse(responseCode = "200",
description = "User activation status updated successfully")
public void updateUserActivation(
@Parameter(description = "ID of the user to activate/deactivate")
@PathVariable final Long userId,

@Parameter(description = "Set to true to activate,"
+ " false to deactivate the user")
@RequestParam final boolean isActive) {

LOGGER.debug("Admin requested to update activation status for "
+ "userId={}"
+ " to isActive={}", userId, isActive);
adminService.updateUserActivation(userId, isActive);
}


@DeleteMapping("/users/{userId}")
@Operation(summary = "Delete a user",
description = "Allows an admin to delete a specific user account.")
@ApiResponse(responseCode = "200",
description = "User deleted successfully")
public void deleteUser(
@Parameter(description = "ID of the user to delete")
@PathVariable final Long userId) {

LOGGER.debug("Admin requested to delete user with userId={}", userId);
adminService.deleteUser(userId);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/podzilla/auth/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.podzilla.auth.controller;

import com.podzilla.auth.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
@RestController
@RequestMapping("/user")
public class UserController {

private static final Logger LOGGER =
LoggerFactory.getLogger(UserController.class);

private final UserService userService;

public UserController(final UserService userService) {
this.userService = userService;
}

@PutMapping("/update/{userId}")
@Operation(summary = "Update user name",
description = "Allows user to update their name.")
@ApiResponse(responseCode = "200",
description = "User profile updated successfully")
public void updateProfile(@PathVariable final Long userId,
@Valid @RequestBody final String name) {
LOGGER.debug("Received updateProfile request for userId={}", userId);
userService.updateUserProfile(userId, name);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/podzilla/auth/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ public class User {
orphanRemoval = true)
private Set<RefreshToken> refreshTokens = new HashSet<>();

@Column(columnDefinition = "BOOLEAN DEFAULT TRUE")
private Boolean enabled = true;


public User(final String name, final String email,
final String password) {
this.name = name;
this.email = email;
this.password = password;
this.enabled = true;
}
}
32 changes: 31 additions & 1 deletion src/main/java/com/podzilla/auth/service/AdminService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,50 @@

import com.podzilla.auth.model.User;
import com.podzilla.auth.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminService {

private static final Logger LOGGER =
LoggerFactory.getLogger(AdminService.class);

private final UserRepository userRepository;
private final UserService userService;

public AdminService(final UserRepository userRepository) {
public AdminService(final UserRepository userRepository,
final UserService userService) {
this.userRepository = userRepository;
this.userService = userService;
}

public List<User> getUsers() {
return userRepository.findAll();
}

@Transactional
public void updateUserActivation(final Long userId,
final boolean isActive) {
User user = userService.getUserOrThrow(userId);
LOGGER.debug("Updating isActive status for userId={} "
+ "from {} to {}", userId, user.getEnabled(), isActive);
user.setEnabled(isActive);
userRepository.save(user);
LOGGER.debug("User activation status updated "
+ "successfully for userId={}", userId);
}


@Transactional
public void deleteUser(final Long userId) {
User user = userService.getUserOrThrow(userId);
LOGGER.debug("Deleting user with userId={}", userId);
userRepository.delete(user);
LOGGER.debug("User deleted successfully with userId={}", userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public UserDetails loadUserByUsername(final String email) {
return new org.springframework.security.core.userdetails.User(
user.getEmail(),
user.getPassword(),
user.getEnabled(),
true,
true,
true,
authorities
);
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/podzilla/auth/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.podzilla.auth.service;

import com.podzilla.auth.exception.NotFoundException;
import com.podzilla.auth.model.User;
import com.podzilla.auth.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class UserService {

private static final Logger LOGGER =
LoggerFactory.getLogger(UserService.class);

private final UserRepository userRepository;

public UserService(final UserRepository userRepository) {
this.userRepository = userRepository;
}


@Transactional
public void updateUserProfile(final Long userId, final String name) {
User user = getUserOrThrow(userId);
LOGGER.debug("Updating name for userId={}", userId);
user.setName(name);
userRepository.save(user);
LOGGER.debug("User profile updated successfully for userId={}", userId);
}


public User getUserOrThrow(final Long userId) {
LOGGER.debug("Fetching user with id={}", userId);
return userRepository.findById(userId)
.orElseThrow(() -> {
LOGGER.warn("User not found with id={}", userId);
return new NotFoundException("User with id "
+ userId + " does not exist.");
});
}
}