|
@@ -0,0 +1,203 @@
|
|
|
|
|
+package cn.tr.module.phototherapy.web.auth;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.tr.core.utils.PswUtils;
|
|
|
|
|
+import cn.tr.module.common.menus.*;
|
|
|
|
|
+import cn.tr.module.sys.exception.CustomException;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.bean.LoginUser;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.enums.GrantTypeEnum;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.granter.IAccountOperator;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.utils.SecurityUtil;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.vo.AccountInfoVO;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.vo.RoleInfoVO;
|
|
|
|
|
+import cn.tr.module.sys.oauth2.vo.UserInfoVO;
|
|
|
|
|
+import cn.tr.module.sys.user.constant.MenuConstants;
|
|
|
|
|
+import cn.tr.module.sys.user.po.SysMenuPO;
|
|
|
|
|
+import cn.tr.module.sys.user.po.SysRolePO;
|
|
|
|
|
+import cn.tr.module.sys.user.po.SysUserPO;
|
|
|
|
|
+import cn.tr.module.sys.user.service.ISysMenuService;
|
|
|
|
|
+import cn.tr.module.sys.user.service.ISysRoleService;
|
|
|
|
|
+import cn.tr.module.sys.user.service.impl.ISysUserService;
|
|
|
|
|
+import cn.tr.module.sys.user.vo.RouteItemVO;
|
|
|
|
|
+import cn.tr.module.sys.user.vo.RouteMetoVO;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author lifang
|
|
|
|
|
+ * @version 1.0.0
|
|
|
|
|
+ * @ClassName DefaultAccountOperator.java
|
|
|
|
|
+ * @Description 默认账户操作
|
|
|
|
|
+ * @createTime 2022年08月01日 15:40:00
|
|
|
|
|
+ */
|
|
|
|
|
+@Component
|
|
|
|
|
+@AllArgsConstructor
|
|
|
|
|
+public class WebAccountOperator implements IAccountOperator {
|
|
|
|
|
+ private final ISysMenuService sysMenuService;
|
|
|
|
|
+
|
|
|
|
|
+ private final ISysUserService sysUserService;
|
|
|
|
|
+
|
|
|
|
|
+ private final ISysRoleService sysRoleService;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean matchGrantType(GrantTypeEnum type) {
|
|
|
|
|
+ return GrantTypeEnum.APPKEY_APPSECRET.equals(type)
|
|
|
|
|
+ ||GrantTypeEnum.WEB_USERNAME_PASSWORD.equals(type);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public UserInfoVO getUserInfo() {
|
|
|
|
|
+ UserInfoVO userInfoVO = new UserInfoVO();
|
|
|
|
|
+ LoginUser loginUser = SecurityUtil.getLoginUser();
|
|
|
|
|
+ if(GrantTypeEnum.WEB_USERNAME_PASSWORD.equals(loginUser.getGrantType())){
|
|
|
|
|
+ SysUserPO sysUser = sysUserService.getById(SecurityUtil.getStpLogic().getLoginIdAsLong());
|
|
|
|
|
+ // 查询角色列表
|
|
|
|
|
+ List<SysRolePO> sysRoleList = sysRoleService.listSysRoleByUserId(sysUser.getId());
|
|
|
|
|
+ List<RoleInfoVO> roleInfoVOList = sysRoleList.stream().map(item -> {
|
|
|
|
|
+ RoleInfoVO roleInfoVO = new RoleInfoVO();
|
|
|
|
|
+ roleInfoVO.setRoleName(item.getRoleName());
|
|
|
|
|
+ roleInfoVO.setValue(item.getRoleCode());
|
|
|
|
|
+ return roleInfoVO;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ userInfoVO.setUserId(sysUser.getId().toString());
|
|
|
|
|
+ userInfoVO.setUsername(sysUser.getAccount());
|
|
|
|
|
+ String realname=StrUtil.isNotEmpty(sysUser.getRealname())?sysUser.getRealname():sysUser.getNickname();
|
|
|
|
|
+ userInfoVO.setRealName(realname);
|
|
|
|
|
+ userInfoVO.setNickname(sysUser.getNickname());
|
|
|
|
|
+ userInfoVO.setAvatar(sysUser.getAvatar());
|
|
|
|
|
+ userInfoVO.setDesc(sysUser.getRemarks());
|
|
|
|
|
+ userInfoVO.setRoles(roleInfoVOList);
|
|
|
|
|
+ userInfoVO.setIsSys(sysUser.getIsSys());
|
|
|
|
|
+ userInfoVO.setTenantName(sysUser.getTenantName());
|
|
|
|
|
+ userInfoVO.setPswNeedReset(sysUser.getPswNeedReset());
|
|
|
|
|
+ }
|
|
|
|
|
+ userInfoVO.setTenantId(String.valueOf(loginUser.getTenantId()));
|
|
|
|
|
+ return userInfoVO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Set<String> getPermCode() {
|
|
|
|
|
+ return SecurityUtil.getLoginUser().getPermissions();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<RouteItemVO> getMenuList() {
|
|
|
|
|
+ List<SysMenuPO> sysMenuList;
|
|
|
|
|
+ LoginUser<Long> loginUser = SecurityUtil.getLoginUser();
|
|
|
|
|
+ if (SecurityUtil.isSuperAdmin()) {
|
|
|
|
|
+ LambdaQueryWrapper<SysMenuPO> queryWrapper = Wrappers.lambdaQuery();
|
|
|
|
|
+ queryWrapper.eq(SysMenuPO::getStatus,String.valueOf(StatusEnum.YES.getValue()));
|
|
|
|
|
+ queryWrapper.in(SysMenuPO::getMenuType, MenuTypeEnum.DIR.getCode(), MenuTypeEnum.MENU.getCode());
|
|
|
|
|
+ queryWrapper.eq(!loginUser.isSys(),SysMenuPO::getTenantMenu,true);
|
|
|
|
|
+ sysMenuList = sysMenuService.list(queryWrapper);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ sysMenuList = sysMenuService.listGrantMenuByUserId(loginUser.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ List<RouteItemVO> routeItemVOList = sysMenuList.stream()
|
|
|
|
|
+ .filter(item -> item.getParentId().intValue() == 0)
|
|
|
|
|
+ .sorted(Comparator.comparing(SysMenuPO::getSort))
|
|
|
|
|
+ .map(item -> {
|
|
|
|
|
+ RouteItemVO node = convertToRoute(item);
|
|
|
|
|
+ node.setChildren(getChildrenList(item, sysMenuList));
|
|
|
|
|
+ return node;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ return routeItemVOList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AccountInfoVO getAccountInfo() {
|
|
|
|
|
+ LoginUser<Long> loginUser = SecurityUtil.getLoginUser();
|
|
|
|
|
+ SysUserPO sysUser = sysUserService.getById(loginUser.getId());
|
|
|
|
|
+ AccountInfoVO accountInfoVO = BeanUtil.copyProperties(sysUser, AccountInfoVO.class);
|
|
|
|
|
+ accountInfoVO.setRealName(sysUser.getRealname());
|
|
|
|
|
+ return accountInfoVO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void saveAccountInfo(AccountInfoVO req) {
|
|
|
|
|
+ LoginUser<Long> loginUser = SecurityUtil.getLoginUser();
|
|
|
|
|
+ SysUserPO entity = BeanUtil.copyProperties(req, SysUserPO.class);
|
|
|
|
|
+ entity.setId(loginUser.getId());
|
|
|
|
|
+ entity.setNickname(req.getNickname());
|
|
|
|
|
+ entity.setRealname(req.getRealName());
|
|
|
|
|
+ sysUserService.updateById(entity);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean updatePass(String oldPass, String newPass) {
|
|
|
|
|
+ // 密码解密
|
|
|
|
|
+ Long userId = Long.valueOf(String.valueOf(SecurityUtil.getId()));
|
|
|
|
|
+ SysUserPO sysUser = sysUserService.getById(userId);
|
|
|
|
|
+ if (!SecurityUtil.matchesPassword(oldPass, sysUser.getPassword())) {
|
|
|
|
|
+ throw new CustomException("旧密码不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!PswUtils.validatePsw(newPass)) {
|
|
|
|
|
+ throw new CustomException("密码由8~16位大小写字母、数字及特殊字符等混合、随机组成(至少包括数字、小写字母、大写字母和特殊符号中的三种)");
|
|
|
|
|
+ }
|
|
|
|
|
+ boolean validatePsw = PswUtils.validatePsw(newPass);
|
|
|
|
|
+ String encryptPassword = SecurityUtil.encryptPassword(newPass);
|
|
|
|
|
+ return sysUserService
|
|
|
|
|
+ .update(new UpdateWrapper<SysUserPO>()
|
|
|
|
|
+ .lambda().eq(SysUserPO::getId,userId)
|
|
|
|
|
+ .set(SysUserPO::getPassword,encryptPassword)
|
|
|
|
|
+ .set(SysUserPO::getLastModifyPswTime,new Date())
|
|
|
|
|
+ .set(SysUserPO::getPswNeedReset,!validatePsw));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<RouteItemVO> getChildrenList(SysMenuPO root, List<SysMenuPO> list) {
|
|
|
|
|
+ List<RouteItemVO> childrenList = list.stream()
|
|
|
|
|
+ .filter(item -> item.getParentId().equals(root.getId()))
|
|
|
|
|
+ .sorted(Comparator.comparing(SysMenuPO::getSort))
|
|
|
|
|
+ .map(item -> {
|
|
|
|
|
+ RouteItemVO node = convertToRoute(item);
|
|
|
|
|
+ node.setChildren(getChildrenList(item, list));
|
|
|
|
|
+ return node;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ return childrenList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private RouteItemVO convertToRoute(SysMenuPO item) {
|
|
|
|
|
+ RouteItemVO node = new RouteItemVO();
|
|
|
|
|
+ node.setName(StrUtil.upperFirst(item.getRoutePath()));
|
|
|
|
|
+ node.setPath(item.getRoutePath());
|
|
|
|
|
+ node.setComponent(item.getComponent());
|
|
|
|
|
+ // 一级目录
|
|
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuTypeEnum.DIR.getCode()) && item.getParentId().intValue() == 0) {
|
|
|
|
|
+ node.setPath("/" + item.getRoutePath());
|
|
|
|
|
+ node.setComponent(MenuConstants.LAYOUT);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 外部链接
|
|
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuTypeEnum.MENU.getCode()) && Objects.equals(item.getLinkExternal(), LinkExternalEnum.YES.getCode())) {
|
|
|
|
|
+ node.setComponent(MenuConstants.IFRAME);
|
|
|
|
|
+ }
|
|
|
|
|
+ RouteMetoVO routeMetoVO = new RouteMetoVO();
|
|
|
|
|
+ routeMetoVO.setTitle(item.getMenuName());
|
|
|
|
|
+ routeMetoVO.setIcon(item.getIcon());
|
|
|
|
|
+ routeMetoVO.setHideMenu(StrUtil.isNotBlank(item.getVisible()) && item.getVisible().equals(VisibleEnum.HIDE.getCode()));
|
|
|
|
|
+ // 菜单
|
|
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuTypeEnum.MENU.getCode())) {
|
|
|
|
|
+ routeMetoVO.setIgnoreKeepAlive(item.getKeepalive().equals(KeepaliveEnum.YES.getCode()));
|
|
|
|
|
+ }
|
|
|
|
|
+ // 外部链接
|
|
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuTypeEnum.MENU.getCode()) && Objects.equals(item.getLinkExternal(), LinkExternalEnum.YES.getCode())) {
|
|
|
|
|
+ // 内嵌
|
|
|
|
|
+ if (Objects.equals(item.getFrame(), FrameEnum.YES.getCode())) {
|
|
|
|
|
+ routeMetoVO.setFrameSrc(item.getLinkUrl());
|
|
|
|
|
+ }
|
|
|
|
|
+ // 外嵌
|
|
|
|
|
+ if (item.getFrame().equals(FrameEnum.NO.getCode())) {
|
|
|
|
|
+ node.setPath(item.getLinkUrl());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ node.setMeta(routeMetoVO);
|
|
|
|
|
+ return node;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|