feat(role): 简化切换用户状态功能代码
This commit is contained in:
parent
c54fd77eda
commit
d706e56451
@ -1,6 +1,7 @@
|
|||||||
package com.zsc.edu.gateway.framework.security;
|
package com.zsc.edu.gateway.framework.security;
|
||||||
|
|
||||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
|
||||||
@ -20,6 +21,13 @@ public class SecurityUtil {
|
|||||||
return (UserDetailsImpl) authentication.getPrincipal();
|
return (UserDetailsImpl) authentication.getPrincipal();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setUserInfo(UserDetailsImpl user) {
|
||||||
|
// 重新加载用户信息并更新SecurityContext
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(
|
||||||
|
new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static Optional<String> getCurrentAuditor() {
|
public static Optional<String> getCurrentAuditor() {
|
||||||
try {
|
try {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
@ -91,7 +91,10 @@ public class UserController {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
@PatchMapping("self")
|
@PatchMapping("self")
|
||||||
public Boolean selfUpdate(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody UserSelfUpdateDto dto) {
|
public Boolean selfUpdate(
|
||||||
|
@AuthenticationPrincipal UserDetailsImpl userDetails,
|
||||||
|
@RequestBody UserSelfUpdateDto dto
|
||||||
|
) {
|
||||||
return service.selfUpdate(userDetails, dto);
|
return service.selfUpdate(userDetails, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +189,7 @@ public class UserController {
|
|||||||
* */
|
* */
|
||||||
@GetMapping("dept/{id}")
|
@GetMapping("dept/{id}")
|
||||||
public Collection<User> listByDept(@PathVariable("id") Long id) {
|
public Collection<User> listByDept(@PathVariable("id") Long id) {
|
||||||
return service.list(new QueryWrapper<User>().eq("dept_id", id));
|
return service.list(new LambdaQueryWrapper<User>().eq(User::getDeptId, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ package com.zsc.edu.gateway.modules.system.service.impl;
|
|||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.zsc.edu.gateway.exception.ConstraintException;
|
import com.zsc.edu.gateway.exception.ConstraintException;
|
||||||
|
import com.zsc.edu.gateway.framework.security.SecurityUtil;
|
||||||
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
|
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
|
||||||
import com.zsc.edu.gateway.modules.system.dto.UserCreateDto;
|
import com.zsc.edu.gateway.modules.system.dto.UserCreateDto;
|
||||||
import com.zsc.edu.gateway.modules.system.dto.UserSelfUpdateDto;
|
import com.zsc.edu.gateway.modules.system.dto.UserSelfUpdateDto;
|
||||||
@ -122,20 +123,13 @@ public class UserServiceImpl extends ServiceImpl<UserRepository, User> implement
|
|||||||
if (role == null) {
|
if (role == null) {
|
||||||
throw new ConstraintException("角色不存在");
|
throw new ConstraintException("角色不存在");
|
||||||
}
|
}
|
||||||
UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
UserDetailsImpl userDetails = SecurityUtil.getUserInfo();
|
||||||
User user = getById(userDetails.getId());
|
boolean updated = lambdaUpdate().eq(User::getId, userDetails.getId())
|
||||||
if (Objects.equals(user.getRoleId(), roleId)) {
|
.set(User::getRoleId, roleId)
|
||||||
throw new ConstraintException("不能切换到当前角色");
|
.update();
|
||||||
}
|
|
||||||
user.setRoleId(roleId);
|
|
||||||
boolean updated = updateById(user);
|
|
||||||
userDetails.setRole(role);
|
userDetails.setRole(role);
|
||||||
if (updated) {
|
if (updated) {
|
||||||
SecurityContextHolder.getContext().setAuthentication(
|
SecurityUtil.setUserInfo(userDetails);
|
||||||
new org.springframework.security.authentication.UsernamePasswordAuthenticationToken(
|
|
||||||
userDetails, userDetails.getPassword(), userDetails.getAuthorities()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return userDetails;
|
return userDetails;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user