This commit is contained in:
高子兴 2024-07-06 07:19:31 +08:00
parent f51a66073b
commit 5747e255e9
3 changed files with 37 additions and 68 deletions

View File

@ -9,6 +9,7 @@ import java.util.stream.Collectors;
public class UserHS2User {
public static User convert(UserHS userHS) {
User user = new User();
user.setId(userHS.getId());
user.setAccount(userHS.getUsername());
user.setPassword(userHS.getPassword());
user.setName(userHS.getNickname());

View File

@ -1,39 +0,0 @@
package org.cmh.backend.UserManagement.controller;
import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.UserServiceTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static org.springframework.http.ResponseEntity.ok;
@RestController
public class UserController {
@Autowired
private UserServiceTemp userService;
@CrossOrigin(origins = "http://localhost:5173")
@PostMapping("/checkLogin")
public ResponseEntity<User> login(@RequestBody Map<String,String> credentials) {
String account = credentials.get("account");
String password = credentials.get("password");
User user = userService.getUserByAccountAndPassword(account, password);
return ok(user);
}
@CrossOrigin(origins = "http://localhost:5173")
@PostMapping("/checkRegister")
public String register(@RequestBody User user) {
if(userService.registerUser(user) != null){
return "注册成功";
}else
return "注册错误";
}
}

View File

@ -1,22 +1,22 @@
package org.cmh.backend.UserManagement.controller;
import io.jsonwebtoken.JwtParser;
import jakarta.transaction.Transactional;
import org.cmh.backend.OrganizationManagement.service.OrganizationService;
import org.cmh.backend.UserManagement.adpter.User2UserHS;
import org.cmh.backend.UserManagement.adpter.UserHS2User;
import org.cmh.backend.UserManagement.service.UserManagementService;
import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.UserManagementService;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.authentication.dto.UserProfileResponse;
import org.cmh.backend.authentication.model.UserHS;
import org.cmh.backend.authentication.repository.UserRepository;
import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@ -28,6 +28,8 @@ public class UserManagementController {
private OrganizationService organizationService;
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@PostMapping("/addUser")
public ResponseEntity<UserProfileResponse> addUser(@RequestBody User user) {
@ -42,10 +44,10 @@ public class UserManagementController {
newuser.getRole(),
newuser.getCreatedAt()
);
if(organizationService.getByName(newuser.getDepartment()) != null){
if (organizationService.getByName(newuser.getDepartment()) != null) {
userService.addUser(newuser);
return new ResponseEntity<>(response, HttpStatus.OK);
}else{
} else {
return null;
}
}
@ -56,45 +58,50 @@ public class UserManagementController {
public List<User> getAll(@RequestParam String token) {
String username = JwtUtil.extractUsername(token);
UserHS userHS = userService.getUserByUsername(username);
List<UserHS> userHSList = userService.getAllUsers();
//这里分权限进行不同请求
//超级管理员
if(userHS.getSuperAdmin()){
return UserHS2User.convertList(userHSList);
}else{
return null;
if (userHS.getSuperAdmin()) {
return UserHS2User.convertList(userService.getAllUsers());
} else {
return UserHS2User.convertList(userService.getUsersByTenant(userHS.getTenant()));
}
}
@PostMapping("/update")
public ResponseEntity<UserProfileResponse> update(@RequestBody User user) {
UserHS newuser = User2UserHS.convert(user);
UserProfileResponse response = new UserProfileResponse(
newuser.getUsername(),
newuser.getNickname(),
newuser.getGender(),
newuser.getPhoneNumber(),
newuser.getEmail(),
newuser.getDepartment(),
newuser.getRole(),
newuser.getCreatedAt()
);
if(organizationService.getByName(newuser.getDepartment()) != null){
userService.addUser(newuser);
UserHS reqUser = User2UserHS.convert(user);
UserHS tarUser = userService.getUserByUsername(reqUser.getUsername());
if (tarUser != null) {
tarUser.setNickname(reqUser.getNickname());
tarUser.setGender(reqUser.getGender());
tarUser.setPhoneNumber(reqUser.getPhoneNumber());
tarUser.setEmail(reqUser.getEmail());
tarUser.setRole(reqUser.getRole());
UserProfileResponse response = new UserProfileResponse(
reqUser.getUsername(),
reqUser.getNickname(),
reqUser.getGender(),
reqUser.getPhoneNumber(),
reqUser.getEmail(),
reqUser.getDepartment(),
reqUser.getRole(),
reqUser.getCreatedAt()
);
if (organizationService.getByName(reqUser.getDepartment()) != null) {
tarUser.setDepartment(reqUser.getDepartment());
}
userRepository.save(tarUser);
return new ResponseEntity<>(response, HttpStatus.OK);
}else{
return null;
} else {
return new ResponseEntity<>(null, HttpStatus.OK);
}
}
@PostMapping("/delete")
@Transactional
//不确定这里返回值应该是什么
public void delete(@RequestBody User user) {
UserHS userHS = userService.getUserByUsername(user.getName());
if(userHS != null){
if (userHS != null) {
//根据用户名删除用户
userService.deleteUser(userHS.getUsername());
}