增加、删除用户时的逻辑优化

This commit is contained in:
myh 2024-01-04 15:22:48 +08:00
parent dbf5e8760f
commit aab93e0141
5 changed files with 19 additions and 16 deletions

View File

@ -22,10 +22,9 @@ public class UsersController {
//添加新用户 //添加新用户
@PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')") @PreAuthorize("hasRole('管理员') or hasAuthority('添加用户')")
@PostMapping("/add") @PostMapping("/add")
public Users addUsers(@RequestBody @NotNull wrapperUserAndPassword wrapperUserAndPassword) { public Users addUsers(@RequestBody @NotNull Users user,
return usersService.addUser( @RequestParam(name = "password") @NotNull String password) {
wrapperUserAndPassword.user, return usersService.addUser(user, password);
wrapperUserAndPassword.userPassword);
} }
@Data @Data

View File

@ -10,15 +10,15 @@ public interface UsersPasswordRepository
extends JpaRepository<UsersPassword, Long> { extends JpaRepository<UsersPassword, Long> {
//修改密码 //修改密码
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2") @Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.user.id = ?2")
int updatePassword(String password, Long userID); int updatePassword(String password, Long userID);
//删除账户时删除密码 //删除账户时删除密码
void deleteByUsersId(Long userID); void deleteByUserId(Long userID);
//根据用户ID查找密码 //根据用户ID查找密码
UsersPassword findByUsersId(Long userID); UsersPassword findByUserId(Long userID);
//根据用户名查找密码 //根据用户名查找密码
UsersPassword findByUsersName(String userName); UsersPassword findByUserName(String userName);
} }

View File

@ -18,7 +18,12 @@ public class UsersPassword {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "usersId", referencedColumnName = "id") @JoinColumn(name = "usersId", referencedColumnName = "id")
private Users users; private Users user;
private String password; private String password;
public UsersPassword(Users user, String password) {
this.user = user;
this.password = password;
}
} }

View File

@ -9,7 +9,7 @@ import java.util.Optional;
public interface UsersService extends UserDetailsService { public interface UsersService extends UserDetailsService {
Users addUser(Users user, UsersPassword userPassword); Users addUser(Users user, String password);
void deleteUserById(Long userId); void deleteUserById(Long userId);

View File

@ -15,7 +15,6 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -25,7 +24,6 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service @Service
@Component
public class UsersServiceImpl implements UsersService { public class UsersServiceImpl implements UsersService {
@Resource @Resource
private UsersRepository usersRepository; private UsersRepository usersRepository;
@ -39,8 +37,9 @@ public class UsersServiceImpl implements UsersService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//添加用户时同步添加密码 //添加用户时同步添加密码
public Users addUser(Users user, UsersPassword userPassword) { public Users addUser(Users user, String password) {
Users u = usersRepository.save(user); Users u = usersRepository.save(user);
UsersPassword userPassword = new UsersPassword(u, password);
usersPasswordRepository.save(userPassword); usersPasswordRepository.save(userPassword);
return u; return u;
} }
@ -49,8 +48,8 @@ public class UsersServiceImpl implements UsersService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码 //删除用户时同步删除密码
public void deleteUserById(Long userId) { public void deleteUserById(Long userId) {
usersPasswordRepository.deleteByUserId(userId);
usersRepository.deleteById(userId); usersRepository.deleteById(userId);
usersPasswordRepository.deleteByUsersId(userId);
} }
@Override @Override
@ -64,7 +63,7 @@ public class UsersServiceImpl implements UsersService {
//获取用户ID //获取用户ID
userId = usersRepository.findByName(name).get().getId(); userId = usersRepository.findByName(name).get().getId();
//删除密码 //删除密码
usersPasswordRepository.deleteByUsersId(userId); usersPasswordRepository.deleteByUserId(userId);
} }
} }
@ -109,7 +108,7 @@ public class UsersServiceImpl implements UsersService {
throw new UsernameNotFoundException("用户名不存在"); throw new UsernameNotFoundException("用户名不存在");
} }
// 取出密码 // 取出密码
String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword(); String password = usersPasswordRepository.findByUserId(user.getId()).getPassword();
// 用户角色 // 用户角色
Roles role = user.getRoles(); Roles role = user.getRoles();