Commit 5cbf0593 authored by ma's avatar ma

完善分配权限接口

parent 8f386c27
...@@ -301,7 +301,7 @@ public class UserController { ...@@ -301,7 +301,7 @@ public class UserController {
} }
/** /**
* todo 待完善 为用户批量分配权限 * 为用户批量分配权限
* *
* @param assignUserPermissionDTO * @param assignUserPermissionDTO
* @return * @return
......
...@@ -17,4 +17,6 @@ public interface UserPermissionMapper extends BaseMapper<UserPermission> { ...@@ -17,4 +17,6 @@ public interface UserPermissionMapper extends BaseMapper<UserPermission> {
int saveBatch(List<Integer> userPermissions, int userId); int saveBatch(List<Integer> userPermissions, int userId);
void deleteUserPermission(int parseInt); void deleteUserPermission(int parseInt);
int delBatch(List<Integer> permissionDelList, Integer userId);
} }
package iot.sixiang.license.service.impl; package iot.sixiang.license.service.impl;
import iot.sixiang.license.consts.ResultCode; import iot.sixiang.license.consts.ResultCode;
import iot.sixiang.license.entity.Permission;
import iot.sixiang.license.entity.UserPermission; import iot.sixiang.license.entity.UserPermission;
import iot.sixiang.license.handler.IotLicenseException; import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.PermissionMapper;
import iot.sixiang.license.mapper.UserPermissionMapper; import iot.sixiang.license.mapper.UserPermissionMapper;
import iot.sixiang.license.model.dto.AssignUserPermissionDTO; import iot.sixiang.license.model.dto.AssignUserPermissionDTO;
import iot.sixiang.license.service.PermissionService;
import iot.sixiang.license.service.UserPermissionService; import iot.sixiang.license.service.UserPermissionService;
import iot.sixiang.license.util.CommonUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* Created by m33 * Created by m33
...@@ -39,7 +38,21 @@ public class UserPermissionServiceImpl implements UserPermissionService { ...@@ -39,7 +38,21 @@ public class UserPermissionServiceImpl implements UserPermissionService {
if (userPermissions.isEmpty() || userId <= 0) { if (userPermissions.isEmpty() || userId <= 0) {
throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg()); throw new IotLicenseException(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg());
} }
return userPermissionMapper.saveBatch(userPermissions, userId) > 0; List<UserPermission> permissionById = userPermissionMapper.getPermissionById(userId);
// 筛选出已有的权限
List<Integer> originPermissions = permissionById.stream().map(UserPermission::getPermission).collect(Collectors.toList());
// 新增权限处理,找出 userPermissions 中新增的权限并添加到用户权限表中
List<Integer> permissionAddList = CommonUtil.getDiffList(userPermissions, originPermissions);
int res = 0;
if (!permissionAddList.isEmpty()) {
res = userPermissionMapper.saveBatch(permissionAddList, userId);
}
// 删除权限处理,找出 originPermissions 中多出的权限并从用户权限表中删除
List<Integer> permissionDelList = CommonUtil.getDiffList(originPermissions, userPermissions);
if (!permissionDelList.isEmpty()) {
res += userPermissionMapper.delBatch(permissionDelList, userId);
}
return res >= 0;
} }
@Override @Override
......
...@@ -17,6 +17,7 @@ import java.text.SimpleDateFormat; ...@@ -17,6 +17,7 @@ import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public class CommonUtil { public class CommonUtil {
...@@ -39,6 +40,7 @@ public class CommonUtil { ...@@ -39,6 +40,7 @@ public class CommonUtil {
/** /**
* date 转 字符串 * date 转 字符串
*
* @param date * @param date
* @param pattern * @param pattern
* @return * @return
...@@ -344,6 +346,13 @@ public class CommonUtil { ...@@ -344,6 +346,13 @@ public class CommonUtil {
return ""; return "";
} }
/**
* 检查是否有某个权限
*
* @param userPermissions
* @param permission
* @return
*/
public static boolean checkPermission(List<UserPermission> userPermissions, int permission) { public static boolean checkPermission(List<UserPermission> userPermissions, int permission) {
boolean res = false; boolean res = false;
for (UserPermission userPermission : userPermissions) { for (UserPermission userPermission : userPermissions) {
...@@ -354,4 +363,27 @@ public class CommonUtil { ...@@ -354,4 +363,27 @@ public class CommonUtil {
} }
return res; return res;
} }
/**
* 差集计算
*
* @param A 集合A
* @param B 集合B
* @return 筛选出【集合A中存在且集合B中不存在】的元素集合
*/
public static <T> List<T> getDiffList(List<T> A, List<T> B) {
if (A == null) {
return Collections.emptyList();
}
if (B == null) {
return A;
}
return A.stream().filter(a -> {
if (a == null) {
return false;
}
return B.stream().noneMatch(b -> b != null && b.equals(a));
}
).collect(Collectors.toList());
}
} }
...@@ -10,6 +10,12 @@ ...@@ -10,6 +10,12 @@
<delete id="deleteUserPermission"> <delete id="deleteUserPermission">
delete from user_permission where user = #{userId} delete from user_permission where user = #{userId}
</delete> </delete>
<delete id="delBatch">
delete from user_permission where user = #{userId} and permission in
<foreach collection="permissionDelList" open="(" separator="," close=")" item="permission">
#{permission}
</foreach>
</delete>
<select id="getPermissionById" resultType="iot.sixiang.license.entity.UserPermission"> <select id="getPermissionById" resultType="iot.sixiang.license.entity.UserPermission">
SELECT * FROM `user_permission` WHERE user = #{userId} and deleted = 0 SELECT * FROM `user_permission` WHERE user = #{userId} and deleted = 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment