-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: getUser and banUser added to UserRestController
- Loading branch information
1 parent
6d8026f
commit 808597e
Showing
1 changed file
with
48 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 48 additions & 2 deletions
50
back/src/main/java/net/daw/alist/controllers/rest/UserRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
package net.daw.alist.controllers.rest; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import jdk.jshell.execution.Util; | ||
import net.daw.alist.models.Post; | ||
import net.daw.alist.models.User; | ||
import net.daw.alist.services.UserService; | ||
import net.daw.alist.utils.Utils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/api/users") | ||
public class UserRestController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private Utils utils; | ||
|
||
@GetMapping("/{username}") | ||
public ResponseEntity<User> getUser(@PathVariable String username) { | ||
Optional<User> optionalUser = userService.findByUsername(username); | ||
if (optionalUser.isPresent()) { | ||
User user = optionalUser.get(); | ||
return new ResponseEntity<>(user, HttpStatus.OK); | ||
} | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@PutMapping ("/{id}") | ||
public ResponseEntity<User> banUser(Authentication authentication, @PathVariable Long id) { | ||
String userRole = utils.getCurrentUserRole(authentication); | ||
if (!userRole.equals("ADMIN")) | ||
return new ResponseEntity<>(HttpStatus.FORBIDDEN); | ||
Optional<User> optionalUser = userService.findByID(id); | ||
if (optionalUser.isPresent()) { | ||
User user = optionalUser.get(); | ||
if (user.isLocked()) { | ||
userService.unbanUser(user.getUsername()); | ||
} else { | ||
userService.banUser(user.getUsername()); | ||
} | ||
return new ResponseEntity<>(user, HttpStatus.OK); | ||
} | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
//TODO: upvotes, followers | ||
} |