master
parent
6eb7968531
commit
0b68a95534
@ -0,0 +1,247 @@
|
||||
package com.macro.mall.service.impl;
|
||||
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.macro.mall.dao.UmsAdminPermissionRelationDao;
|
||||
import com.macro.mall.dao.UmsAdminRoleRelationDao;
|
||||
import com.macro.mall.dto.UmsAdminParam;
|
||||
import com.macro.mall.mapper.UmsAdminLoginLogMapper;
|
||||
import com.macro.mall.mapper.UmsAdminMapper;
|
||||
import com.macro.mall.mapper.UmsAdminPermissionRelationMapper;
|
||||
import com.macro.mall.mapper.UmsAdminRoleRelationMapper;
|
||||
import com.macro.mall.model.*;
|
||||
import com.macro.mall.service.UmsAdminService;
|
||||
import com.macro.mall.util.JwtTokenUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.authentication.encoding.PasswordEncoder;
|
||||
import org.springframework.security.core.Authentication;
|
||||
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.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
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.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* UmsAdminService实现类
|
||||
*/
|
||||
@Service
|
||||
public class UmsAdminServiceImpl implements UmsAdminService {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
|
||||
@Autowired
|
||||
private AuthenticationManager authenticationManager;
|
||||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
@Autowired
|
||||
private JwtTokenUtil jwtTokenUtil;
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
@Value("${jwt.tokenHead}")
|
||||
private String tokenHead;
|
||||
@Autowired
|
||||
private UmsAdminMapper adminMapper;
|
||||
@Autowired
|
||||
private UmsAdminRoleRelationMapper adminRoleRelationMapper;
|
||||
@Autowired
|
||||
private UmsAdminRoleRelationDao adminRoleRelationDao;
|
||||
@Autowired
|
||||
private UmsAdminPermissionRelationMapper adminPermissionRelationMapper;
|
||||
@Autowired
|
||||
private UmsAdminPermissionRelationDao adminPermissionRelationDao;
|
||||
@Autowired
|
||||
private UmsAdminLoginLogMapper loginLogMapper;
|
||||
|
||||
@Override
|
||||
public UmsAdmin getAdminByUsername(String username) {
|
||||
UmsAdminExample example = new UmsAdminExample();
|
||||
example.createCriteria().andUsernameEqualTo(username);
|
||||
List<UmsAdmin> adminList = adminMapper.selectByExample(example);
|
||||
if (adminList != null && adminList.size() > 0) {
|
||||
return adminList.get(0);
|
||||
}
|
||||
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 md5Password = passwordEncoder.encodePassword(umsAdmin.getPassword(), null);
|
||||
umsAdmin.setPassword(md5Password);
|
||||
adminMapper.insert(umsAdmin);
|
||||
return umsAdmin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String login(String username, String password) {
|
||||
String token = null;
|
||||
//密码需要客户端加密后传递
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, passwordEncoder.encodePassword(password, null));
|
||||
try {
|
||||
Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
||||
token = jwtTokenUtil.generateToken(userDetails);
|
||||
updateLoginTimeByUsername(username);
|
||||
insertLoginLog(username);
|
||||
} catch (AuthenticationException e) {
|
||||
LOGGER.warn("登录异常:{}", e.getMessage());
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加登录记录
|
||||
* @param username 用户名
|
||||
*/
|
||||
private void insertLoginLog(String username) {
|
||||
UmsAdmin admin = getAdminByUsername(username);
|
||||
UmsAdminLoginLog loginLog = new UmsAdminLoginLog();
|
||||
loginLog.setAdminId(admin.getId());
|
||||
loginLog.setCreateTime(new Date());
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
loginLog.setIp(request.getRemoteAddr());
|
||||
loginLogMapper.insert(loginLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名修改登录时间
|
||||
*/
|
||||
private void updateLoginTimeByUsername(String username) {
|
||||
UmsAdmin record = new UmsAdmin();
|
||||
record.setLoginTime(new Date());
|
||||
UmsAdminExample example = new UmsAdminExample();
|
||||
example.createCriteria().andUsernameEqualTo(username);
|
||||
adminMapper.updateByExampleSelective(record, example);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String refreshToken(String oldToken) {
|
||||
String token = oldToken.substring(tokenHead.length());
|
||||
if (jwtTokenUtil.canRefresh(token)) {
|
||||
return jwtTokenUtil.refreshToken(token);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UmsAdmin getItem(Long id) {
|
||||
return adminMapper.selectByPrimaryKey(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsAdmin> list(String name, Integer pageSize, Integer pageNum) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
UmsAdminExample example = new UmsAdminExample();
|
||||
UmsAdminExample.Criteria criteria = example.createCriteria();
|
||||
if (!StringUtils.isEmpty(name)) {
|
||||
criteria.andUsernameLike("%" + name + "%");
|
||||
example.or(example.createCriteria().andNickNameLike("%" + name + "%"));
|
||||
}
|
||||
return adminMapper.selectByExample(example);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(Long id, UmsAdmin admin) {
|
||||
admin.setId(id);
|
||||
return adminMapper.updateByPrimaryKey(admin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(Long id) {
|
||||
return adminMapper.deleteByPrimaryKey(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateRole(Long adminId, List<Long> roleIds) {
|
||||
int count = roleIds == null ? 0 : roleIds.size();
|
||||
//先删除原来的关系
|
||||
UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();
|
||||
adminRoleRelationExample.createCriteria().andAdminIdEqualTo(adminId);
|
||||
adminRoleRelationMapper.deleteByExample(adminRoleRelationExample);
|
||||
//建立新关系
|
||||
if (!CollectionUtils.isEmpty(roleIds)) {
|
||||
List<UmsAdminRoleRelation> list = new ArrayList<>();
|
||||
for (Long roleId : roleIds) {
|
||||
UmsAdminRoleRelation roleRelation = new UmsAdminRoleRelation();
|
||||
roleRelation.setAdminId(adminId);
|
||||
roleRelation.setRoleId(roleId);
|
||||
list.add(roleRelation);
|
||||
}
|
||||
adminRoleRelationDao.insertList(list);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsRole> getRoleList(Long adminId) {
|
||||
return adminRoleRelationDao.getRoleList(adminId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updatePermission(Long adminId, List<Long> permissionIds) {
|
||||
//删除原所有权限关系
|
||||
UmsAdminPermissionRelationExample relationExample = new UmsAdminPermissionRelationExample();
|
||||
relationExample.createCriteria().andAdminIdEqualTo(adminId);
|
||||
adminPermissionRelationMapper.deleteByExample(relationExample);
|
||||
//获取用户所有角色权限
|
||||
List<UmsPermission> permissionList = adminRoleRelationDao.getRolePermissionList(adminId);
|
||||
List<Long> rolePermissionList = permissionList.stream().map(UmsPermission::getId).collect(Collectors.toList());
|
||||
if (!CollectionUtils.isEmpty(permissionIds)) {
|
||||
List<UmsAdminPermissionRelation> relationList = new ArrayList<>();
|
||||
//筛选出+权限
|
||||
List<Long> addPermissionIdList = permissionIds.stream().filter(permissionId -> !rolePermissionList.contains(permissionId)).collect(Collectors.toList());
|
||||
//筛选出-权限
|
||||
List<Long> subPermissionIdList = rolePermissionList.stream().filter(permissionId -> !permissionIds.contains(permissionId)).collect(Collectors.toList());
|
||||
//插入+-权限关系
|
||||
relationList.addAll(convert(adminId,1,addPermissionIdList));
|
||||
relationList.addAll(convert(adminId,-1,subPermissionIdList));
|
||||
return adminPermissionRelationDao.insertList(relationList);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将+-权限关系转化为对象
|
||||
*/
|
||||
private List<UmsAdminPermissionRelation> convert(Long adminId,Integer type,List<Long> permissionIdList) {
|
||||
List<UmsAdminPermissionRelation> relationList = permissionIdList.stream().map(permissionId -> {
|
||||
UmsAdminPermissionRelation relation = new UmsAdminPermissionRelation();
|
||||
relation.setAdminId(adminId);
|
||||
relation.setType(type);
|
||||
relation.setPermissionId(permissionId);
|
||||
return relation;
|
||||
}).collect(Collectors.toList());
|
||||
return relationList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsPermission> getPermissionList(Long adminId) {
|
||||
return adminRoleRelationDao.getPermissionList(adminId);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.macro.mall.service.impl;
|
||||
|
||||
import com.macro.mall.mapper.UmsMemberLevelMapper;
|
||||
import com.macro.mall.model.UmsMemberLevel;
|
||||
import com.macro.mall.model.UmsMemberLevelExample;
|
||||
import com.macro.mall.service.UmsMemberLevelService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* »áÔ±µÈ¼¶¹ÜÀíServiceʵÏÖÀà
|
||||
*/
|
||||
@Service
|
||||
public class UmsMemberLevelServiceImpl implements UmsMemberLevelService{
|
||||
@Autowired
|
||||
private UmsMemberLevelMapper memberLevelMapper;
|
||||
@Override
|
||||
public List<UmsMemberLevel> list(Integer defaultStatus) {
|
||||
UmsMemberLevelExample example = new UmsMemberLevelExample();
|
||||
example.createCriteria().andDefaultStatusEqualTo(defaultStatus);
|
||||
return memberLevelMapper.selectByExample(example);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.macro.mall.service.impl;
|
||||
|
||||
import com.macro.mall.dto.UmsPermissionNode;
|
||||
import com.macro.mall.mapper.UmsPermissionMapper;
|
||||
import com.macro.mall.model.UmsPermission;
|
||||
import com.macro.mall.model.UmsPermissionExample;
|
||||
import com.macro.mall.service.UmsPermissionService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 后台用户权限管理Service实现类
|
||||
*/
|
||||
@Service
|
||||
public class UmsPermissionServiceImpl implements UmsPermissionService {
|
||||
@Autowired
|
||||
private UmsPermissionMapper permissionMapper;
|
||||
|
||||
@Override
|
||||
public int create(UmsPermission permission) {
|
||||
permission.setStatus(1);
|
||||
permission.setCreateTime(new Date());
|
||||
permission.setSort(0);
|
||||
return permissionMapper.insert(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(Long id, UmsPermission permission) {
|
||||
permission.setId(id);
|
||||
return permissionMapper.updateByPrimaryKey(permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(List<Long> ids) {
|
||||
UmsPermissionExample example = new UmsPermissionExample();
|
||||
example.createCriteria().andIdIn(ids);
|
||||
return permissionMapper.deleteByExample(example);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsPermissionNode> treeList() {
|
||||
List<UmsPermission> permissionList = permissionMapper.selectByExample(new UmsPermissionExample());
|
||||
List<UmsPermissionNode> result = permissionList.stream()
|
||||
.filter(permission -> permission.getPid().equals(0L))
|
||||
.map(permission -> covert(permission,permissionList)).collect(Collectors.toList());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsPermission> list() {
|
||||
return permissionMapper.selectByExample(new UmsPermissionExample());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将权限转换为带有子级的权限对象
|
||||
* 当找不到子级权限的时候map操作不会再递归调用covert
|
||||
*/
|
||||
private UmsPermissionNode covert(UmsPermission permission,List<UmsPermission> permissionList){
|
||||
UmsPermissionNode node = new UmsPermissionNode();
|
||||
BeanUtils.copyProperties(permission,node);
|
||||
List<UmsPermissionNode> children = permissionList.stream()
|
||||
.filter(subPermission -> subPermission.getPid().equals(permission.getId()))
|
||||
.map(subPermission -> covert(subPermission,permissionList)).collect(Collectors.toList());
|
||||
node.setChildren(children);
|
||||
return node;
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.macro.mall.service.impl;
|
||||
|
||||
import com.macro.mall.dao.UmsRolePermissionRelationDao;
|
||||
import com.macro.mall.mapper.UmsRoleMapper;
|
||||
import com.macro.mall.mapper.UmsRolePermissionRelationMapper;
|
||||
import com.macro.mall.model.*;
|
||||
import com.macro.mall.service.UmsRoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台角色管理Service实现类
|
||||
*/
|
||||
@Service
|
||||
public class UmsRoleServiceImpl implements UmsRoleService {
|
||||
@Autowired
|
||||
private UmsRoleMapper roleMapper;
|
||||
@Autowired
|
||||
private UmsRolePermissionRelationMapper rolePermissionRelationMapper;
|
||||
@Autowired
|
||||
private UmsRolePermissionRelationDao rolePermissionRelationDao;
|
||||
@Override
|
||||
public int create(UmsRole role) {
|
||||
role.setCreateTime(new Date());
|
||||
role.setStatus(1);
|
||||
role.setAdminCount(0);
|
||||
role.setSort(0);
|
||||
return roleMapper.insert(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(Long id, UmsRole role) {
|
||||
role.setId(id);
|
||||
return roleMapper.updateByPrimaryKey(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(List<Long> ids) {
|
||||
UmsRoleExample example = new UmsRoleExample();
|
||||
example.createCriteria().andIdIn(ids);
|
||||
return roleMapper.deleteByExample(example);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsPermission> getPermissionList(Long roleId) {
|
||||
return rolePermissionRelationDao.getPermissionList(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updatePermission(Long roleId, List<Long> permissionIds) {
|
||||
//先删除原有关系
|
||||
UmsRolePermissionRelationExample example=new UmsRolePermissionRelationExample();
|
||||
example.createCriteria().andRoleIdEqualTo(roleId);
|
||||
rolePermissionRelationMapper.deleteByExample(example);
|
||||
//批量插入新关系
|
||||
List<UmsRolePermissionRelation> relationList = new ArrayList<>();
|
||||
for (Long permissionId : permissionIds) {
|
||||
UmsRolePermissionRelation relation = new UmsRolePermissionRelation();
|
||||
relation.setRoleId(roleId);
|
||||
relation.setPermissionId(permissionId);
|
||||
relationList.add(relation);
|
||||
}
|
||||
return rolePermissionRelationDao.insertList(relationList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UmsRole> list() {
|
||||
return roleMapper.selectByExample(new UmsRoleExample());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue