|
@@ -1,31 +1,26 @@
|
|
|
package com.yonge.cooleshow.admin.service.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
-import com.yonge.cooleshow.admin.bo.AdminUserDetails;
|
|
|
-import com.yonge.cooleshow.mall.common.exception.Asserts;
|
|
|
-import com.yonge.cooleshow.mall.common.util.RequestUtil;
|
|
|
import com.yonge.cooleshow.admin.dao.UmsAdminRoleRelationDao;
|
|
|
-import com.yonge.cooleshow.admin.dto.UmsAdminParam;
|
|
|
-import com.yonge.cooleshow.admin.dto.UpdateAdminPasswordParam;
|
|
|
+import com.yonge.cooleshow.admin.service.UmsAdminCacheService;
|
|
|
+import com.yonge.cooleshow.admin.service.UmsAdminService;
|
|
|
+import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
|
|
|
+import com.yonge.cooleshow.auth.api.dto.SysUserQueryInfo;
|
|
|
+import com.yonge.cooleshow.auth.api.entity.SysUser;
|
|
|
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
|
|
|
+import com.yonge.cooleshow.common.page.PageInfo;
|
|
|
+import com.yonge.cooleshow.mall.common.util.RequestUtil;
|
|
|
+import com.yonge.cooleshow.mbg.mapper.SysConfigMapper;
|
|
|
import com.yonge.cooleshow.mbg.mapper.UmsAdminLoginLogMapper;
|
|
|
import com.yonge.cooleshow.mbg.mapper.UmsAdminMapper;
|
|
|
import com.yonge.cooleshow.mbg.mapper.UmsAdminRoleRelationMapper;
|
|
|
import com.yonge.cooleshow.mbg.model.*;
|
|
|
-import com.yonge.cooleshow.mall.security.util.JwtTokenUtil;
|
|
|
-import com.yonge.cooleshow.admin.service.UmsAdminCacheService;
|
|
|
-import com.yonge.cooleshow.admin.service.UmsAdminService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
-import org.springframework.security.core.AuthenticationException;
|
|
|
-import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
-import org.springframework.security.core.userdetails.UserDetails;
|
|
|
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
@@ -33,9 +28,10 @@ import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 后台用户管理Service实现类
|
|
@@ -45,9 +41,7 @@ import java.util.List;
|
|
|
public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
|
|
|
@Autowired
|
|
|
- private JwtTokenUtil jwtTokenUtil;
|
|
|
- @Autowired
|
|
|
- private PasswordEncoder passwordEncoder;
|
|
|
+ private SysUserFeignService sysUserFeignService;
|
|
|
@Autowired
|
|
|
private UmsAdminMapper adminMapper;
|
|
|
@Autowired
|
|
@@ -59,6 +53,9 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
@Autowired
|
|
|
private UmsAdminCacheService adminCacheService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysConfigMapper configMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public UmsAdmin getAdminByUsername(String username) {
|
|
|
UmsAdmin admin = adminCacheService.getAdmin(username);
|
|
@@ -74,48 +71,6 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public UmsAdmin register(UmsAdminParam umsAdminParam) {
|
|
|
- UmsAdmin umsAdmin = new UmsAdmin();
|
|
|
- BeanUtils.copyProperties(umsAdminParam, umsAdmin);
|
|
|
- umsAdmin.setCreateTime(new Date());
|
|
|
- umsAdmin.setStatus(1);
|
|
|
- //查询是否有相同用户名的用户
|
|
|
- UmsAdminExample example = new UmsAdminExample();
|
|
|
- example.createCriteria().andUsernameEqualTo(umsAdmin.getUsername());
|
|
|
- List<UmsAdmin> umsAdminList = adminMapper.selectByExample(example);
|
|
|
- if (umsAdminList.size() > 0) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- //将密码进行加密操作
|
|
|
- String encodePassword = passwordEncoder.encode(umsAdmin.getPassword());
|
|
|
- umsAdmin.setPassword(encodePassword);
|
|
|
- adminMapper.insert(umsAdmin);
|
|
|
- return umsAdmin;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String login(String username, String password) {
|
|
|
- String token = null;
|
|
|
- //密码需要客户端加密后传递
|
|
|
- try {
|
|
|
- UserDetails userDetails = loadUserByUsername(username);
|
|
|
- // if(!passwordEncoder.matches(password,userDetails.getPassword())){
|
|
|
- // Asserts.fail("密码不正确");
|
|
|
- // }
|
|
|
- if(!userDetails.isEnabled()){
|
|
|
- Asserts.fail("帐号已被禁用");
|
|
|
- }
|
|
|
- UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
|
|
- SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
- token = jwtTokenUtil.generateToken(userDetails);
|
|
|
-// updateLoginTimeByUsername(username);
|
|
|
- insertLoginLog(username);
|
|
|
- } catch (AuthenticationException e) {
|
|
|
- LOGGER.warn("登录异常:{}", e.getMessage());
|
|
|
- }
|
|
|
- return token;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 添加登录记录
|
|
@@ -144,10 +99,6 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
adminMapper.updateByExampleSelective(record, example);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public String refreshToken(String oldToken) {
|
|
|
- return jwtTokenUtil.refreshHeadToken(oldToken);
|
|
|
- }
|
|
|
|
|
|
@Override
|
|
|
public UmsAdmin getItem(Long id) {
|
|
@@ -178,7 +129,7 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
if(StrUtil.isEmpty(admin.getPassword())){
|
|
|
admin.setPassword(null);
|
|
|
}else{
|
|
|
- admin.setPassword(passwordEncoder.encode(admin.getPassword()));
|
|
|
+ admin.setPassword(null);
|
|
|
}
|
|
|
}
|
|
|
int count = adminMapper.updateByPrimaryKeySelective(admin);
|
|
@@ -234,39 +185,7 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
return resourceList;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public int updatePassword(UpdateAdminPasswordParam param) {
|
|
|
- if(StrUtil.isEmpty(param.getUsername())
|
|
|
- ||StrUtil.isEmpty(param.getOldPassword())
|
|
|
- ||StrUtil.isEmpty(param.getNewPassword())){
|
|
|
- return -1;
|
|
|
- }
|
|
|
- UmsAdminExample example = new UmsAdminExample();
|
|
|
- example.createCriteria().andUsernameEqualTo(param.getUsername());
|
|
|
- List<UmsAdmin> adminList = adminMapper.selectByExample(example);
|
|
|
- if(CollUtil.isEmpty(adminList)){
|
|
|
- return -2;
|
|
|
- }
|
|
|
- UmsAdmin umsAdmin = adminList.get(0);
|
|
|
- if(!passwordEncoder.matches(param.getOldPassword(),umsAdmin.getPassword())){
|
|
|
- return -3;
|
|
|
- }
|
|
|
- umsAdmin.setPassword(passwordEncoder.encode(param.getNewPassword()));
|
|
|
- adminMapper.updateByPrimaryKey(umsAdmin);
|
|
|
- adminCacheService.delAdmin(umsAdmin.getId());
|
|
|
- return 1;
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public UserDetails loadUserByUsername(String username){
|
|
|
- //获取用户信息
|
|
|
- UmsAdmin admin = getAdminByUsername(username);
|
|
|
- if (admin != null) {
|
|
|
- List<UmsResource> resourceList = getResourceList(admin.getId());
|
|
|
- return new AdminUserDetails(admin,resourceList);
|
|
|
- }
|
|
|
- throw new UsernameNotFoundException("用户名或密码错误");
|
|
|
- }
|
|
|
|
|
|
@Override
|
|
|
public UmsAdmin getAdminById(Long id) {
|
|
@@ -280,4 +199,77 @@ public class UmsAdminServiceImpl implements UmsAdminService {
|
|
|
roleIds.add(1L);
|
|
|
return updateRole(admin.getId(),roleIds) > 0;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean addRootAdmin(List<UmsAdmin> adminList) {
|
|
|
+ adminMapper.saveRootAdminList(adminList);
|
|
|
+ return addAdminRootRole(adminList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveAdmin() {
|
|
|
+ // 调用远程,保存用户
|
|
|
+ SysUserQueryInfo sysUserQueryInfo = new SysUserQueryInfo();
|
|
|
+ String now = LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ String syncTime = configMapper.getConfig(SysConfig.SYNC_TIME);
|
|
|
+ sysUserQueryInfo.setUserType("SYSTEM");
|
|
|
+ sysUserQueryInfo.setCreateEndDate(now);
|
|
|
+ sysUserQueryInfo.setCreateStartDate(syncTime);
|
|
|
+ sysUserQueryInfo.setPage(1);
|
|
|
+ sysUserQueryInfo.setRows(9999);
|
|
|
+ HttpResponseResult<PageInfo<SysUser>> result = sysUserFeignService.page(sysUserQueryInfo);
|
|
|
+
|
|
|
+ if (result.getStatus()) {
|
|
|
+ List<SysUser> rows = result.getData().getRows();
|
|
|
+ List<UmsAdmin> adminList = new ArrayList<>();
|
|
|
+ for (SysUser row : rows) {
|
|
|
+ UmsAdmin admin = new UmsAdmin();
|
|
|
+ admin.setId(row.getId());
|
|
|
+ admin.setCreateTime(row.getCreateTime());
|
|
|
+ admin.setUsername(row.getUsername());
|
|
|
+ admin.setPassword(row.getPassword());
|
|
|
+ admin.setStatus(1);
|
|
|
+ admin.setIcon(row.getAvatar());
|
|
|
+ adminList.add(admin);
|
|
|
+ }
|
|
|
+ addRootAdmin(adminList);
|
|
|
+ }
|
|
|
+ configMapper.setConfig(SysConfig.SYNC_TIME,now);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加默认权限
|
|
|
+ *
|
|
|
+ * @param adminList
|
|
|
+ */
|
|
|
+ private boolean addAdminRootRole(List<UmsAdmin> adminList) {
|
|
|
+ List<Long> adminIdList = adminList.stream().map(UmsAdmin::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 已经设置里权限的不做处理
|
|
|
+ UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();
|
|
|
+ adminRoleRelationExample.createCriteria().andAdminIdIn(adminIdList);
|
|
|
+ List<UmsAdminRoleRelation> umsAdminRoleRelations = adminRoleRelationMapper.selectByExample(
|
|
|
+ adminRoleRelationExample);
|
|
|
+ Set<Long> hasRoleAdminIdList = new HashSet<>();
|
|
|
+ if (CollUtil.isNotEmpty(umsAdminRoleRelations)) {
|
|
|
+ hasRoleAdminIdList = umsAdminRoleRelations.stream()
|
|
|
+ .map(UmsAdminRoleRelation::getAdminId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+ Set<Long> finalHasRoleAdminIdList = hasRoleAdminIdList;
|
|
|
+ adminIdList = adminIdList.stream()
|
|
|
+ .filter(id -> !finalHasRoleAdminIdList.contains(id))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 设置权限
|
|
|
+ List<UmsAdminRoleRelation> list = new ArrayList<>();
|
|
|
+ for (Long adminId : adminIdList) {
|
|
|
+ UmsAdminRoleRelation roleRelation = new UmsAdminRoleRelation();
|
|
|
+ roleRelation.setAdminId(adminId);
|
|
|
+ roleRelation.setRoleId(1L);
|
|
|
+ list.add(roleRelation);
|
|
|
+ }
|
|
|
+ return adminRoleRelationDao.insertList(list) > 0;
|
|
|
+
|
|
|
+ }
|
|
|
}
|