刘力
3 years ago
7 changed files with 275 additions and 14 deletions
-
18common/src/main/java/com/canvas/web/annotation/DataPermission.java
-
15common/src/main/java/com/canvas/web/utils/EntityNotFoundException.java
-
190common/src/main/java/com/canvas/web/utils/QueryHelp.java
-
25common/src/main/java/com/canvas/web/utils/ValidationUtil.java
-
12system/src/main/java/com/canvas/web/modules/security/controller/AuthorizationController.java
-
6system/src/main/java/com/canvas/web/modules/system/service/dto/UserDto.java
-
23system/src/main/java/com/canvas/web/modules/system/service/impl/UserServiceImpl.java
@ -0,0 +1,18 @@ |
|||
package com.canvas.web.annotation; |
|||
|
|||
|
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
|
|||
@Target(ElementType.TYPE) |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
public @interface DataPermission { |
|||
|
|||
//Entity 中的字段名称 |
|||
String fieldName() default ""; |
|||
|
|||
//Entity 中与部门关联的字段名称 |
|||
String joinName() default ""; |
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.canvas.web.utils; |
|||
|
|||
import org.springframework.util.StringUtils; |
|||
|
|||
public class EntityNotFoundException extends RuntimeException{ |
|||
|
|||
public EntityNotFoundException(Class clazz, String field, String val) { |
|||
super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), field, val)); |
|||
} |
|||
|
|||
private static String generateMessage(String entity, String field, String val) { |
|||
return StringUtils.capitalize(entity) |
|||
+ " with " + field + " "+ val + " does not exist"; |
|||
} |
|||
} |
@ -0,0 +1,190 @@ |
|||
package com.canvas.web.utils; |
|||
|
|||
import cn.hutool.core.collection.CollUtil; |
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import com.canvas.web.annotation.DataPermission; |
|||
import com.canvas.web.annotation.Query; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import javax.persistence.criteria.*; |
|||
import java.lang.reflect.Field; |
|||
import java.util.ArrayList; |
|||
import java.util.Arrays; |
|||
import java.util.Collection; |
|||
import java.util.List; |
|||
|
|||
@Slf4j |
|||
public class QueryHelp { |
|||
|
|||
public static <R,Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) { |
|||
|
|||
List<Predicate> list = new ArrayList<>(); |
|||
if (query == null) { |
|||
return cb.and(list.toArray(new Predicate[0])); |
|||
} |
|||
//数据权限验证 |
|||
DataPermission permission = query.getClass().getAnnotation(DataPermission.class); |
|||
//获取数据权限 |
|||
if (permission != null) { |
|||
// 获取数据权限 |
|||
List<Long> dataScopes = SecurityUtils.getCurrentUserDataScope(); |
|||
if (CollectionUtil.isNotEmpty(dataScopes)) { |
|||
if (org.apache.commons.lang3.StringUtils.isNotBlank(permission.joinName()) && org.apache.commons.lang3.StringUtils.isNotBlank(permission.fieldName())) { |
|||
Join join = root.join(permission.joinName(), JoinType.LEFT); |
|||
list.add(getExpression(permission.fieldName(), join, root).in(dataScopes)); |
|||
} else if (org.apache.commons.lang3.StringUtils.isBlank(permission.joinName()) && StringUtils.isNotBlank(permission.fieldName())) { |
|||
list.add(getExpression(permission.fieldName(), null, root).in(dataScopes)); |
|||
} |
|||
} |
|||
} |
|||
try { |
|||
List<Field> fields = getAllFields(query.getClass(), new ArrayList<>()); |
|||
for (Field field : fields) { |
|||
boolean accessible = field.isAccessible(); |
|||
// 设置对象的访问权限,保证对private的属性的访 |
|||
field.setAccessible(true); |
|||
Query q = field.getAnnotation(Query.class); |
|||
if (q != null) { |
|||
String propName = q.propName(); |
|||
String joinName = q.joinName(); |
|||
String blurry = q.blurry(); |
|||
String attributeName = isBlank(propName) ? field.getName() : propName; |
|||
Class<?> fieldType = field.getType(); |
|||
Object val = field.get(query); |
|||
if (ObjectUtil.isNull(val) || "".equals(val)) { |
|||
continue; |
|||
} |
|||
Join join = null; |
|||
// 模糊多字段 |
|||
if (ObjectUtil.isNotEmpty(blurry)) { |
|||
String[] blurrys = blurry.split(","); |
|||
List<Predicate> orPredicate = new ArrayList<>(); |
|||
for (String s : blurrys) { |
|||
orPredicate.add(cb.like(root.get(s) |
|||
.as(String.class), "%" + val.toString() + "%")); |
|||
} |
|||
Predicate[] p = new Predicate[orPredicate.size()]; |
|||
list.add(cb.or(orPredicate.toArray(p))); |
|||
continue; |
|||
} |
|||
if (ObjectUtil.isNotEmpty(joinName)) { |
|||
String[] joinNames = joinName.split(">"); |
|||
for (String name : joinNames) { |
|||
switch (q.join()) { |
|||
case LEFT: |
|||
if(ObjectUtil.isNotNull(join) && ObjectUtil.isNotNull(val)){ |
|||
join = join.join(name, JoinType.LEFT); |
|||
} else { |
|||
join = root.join(name, JoinType.LEFT); |
|||
} |
|||
break; |
|||
case RIGHT: |
|||
if(ObjectUtil.isNotNull(join) && ObjectUtil.isNotNull(val)){ |
|||
join = join.join(name, JoinType.RIGHT); |
|||
} else { |
|||
join = root.join(name, JoinType.RIGHT); |
|||
} |
|||
break; |
|||
case INNER: |
|||
if(ObjectUtil.isNotNull(join) && ObjectUtil.isNotNull(val)){ |
|||
join = join.join(name, JoinType.INNER); |
|||
} else { |
|||
join = root.join(name, JoinType.INNER); |
|||
} |
|||
break; |
|||
default: break; |
|||
} |
|||
} |
|||
} |
|||
switch (q.type()) { |
|||
case EQUAL: |
|||
list.add(cb.equal(getExpression(attributeName,join,root) |
|||
.as((Class<? extends Comparable>) fieldType),val)); |
|||
break; |
|||
case GREATER_THAN: |
|||
list.add(cb.greaterThanOrEqualTo(getExpression(attributeName,join,root) |
|||
.as((Class<? extends Comparable>) fieldType), (Comparable) val)); |
|||
break; |
|||
case LESS_THAN: |
|||
list.add(cb.lessThanOrEqualTo(getExpression(attributeName,join,root) |
|||
.as((Class<? extends Comparable>) fieldType), (Comparable) val)); |
|||
break; |
|||
case LESS_THAN_NQ: |
|||
list.add(cb.lessThan(getExpression(attributeName,join,root) |
|||
.as((Class<? extends Comparable>) fieldType), (Comparable) val)); |
|||
break; |
|||
case INNER_LIKE: |
|||
list.add(cb.like(getExpression(attributeName,join,root) |
|||
.as(String.class), "%" + val.toString() + "%")); |
|||
break; |
|||
case LEFT_LIKE: |
|||
list.add(cb.like(getExpression(attributeName,join,root) |
|||
.as(String.class), "%" + val.toString())); |
|||
break; |
|||
case RIGHT_LIKE: |
|||
list.add(cb.like(getExpression(attributeName,join,root) |
|||
.as(String.class), val.toString() + "%")); |
|||
break; |
|||
case IN: |
|||
if (CollUtil.isNotEmpty((Collection<Long>)val)) { |
|||
list.add(getExpression(attributeName,join,root).in((Collection<Long>) val)); |
|||
} |
|||
break; |
|||
case NOT_EQUAL: |
|||
list.add(cb.notEqual(getExpression(attributeName,join,root), val)); |
|||
break; |
|||
case NOT_NULL: |
|||
list.add(cb.isNotNull(getExpression(attributeName,join,root))); |
|||
break; |
|||
case IS_NULL: |
|||
list.add(cb.isNull(getExpression(attributeName,join,root))); |
|||
break; |
|||
case BETWEEN: |
|||
List<Object> between = new ArrayList<>((List<Object>)val); |
|||
list.add(cb.between(getExpression(attributeName, join, root).as((Class<? extends Comparable>) between.get(0).getClass()), |
|||
(Comparable) between.get(0), (Comparable) between.get(1))); |
|||
break; |
|||
default: break; |
|||
} |
|||
} |
|||
field.setAccessible(accessible); |
|||
} |
|||
} catch (Exception e) { |
|||
log.error(e.getMessage(), e); |
|||
} |
|||
int size = list.size(); |
|||
return cb.and(list.toArray(new Predicate[size])); |
|||
} |
|||
|
|||
|
|||
private static <T, R> Expression<T> getExpression(String attributeName, Join join, Root<R> root) { |
|||
if (ObjectUtil.isNotEmpty(join)) { |
|||
return join.get(attributeName); |
|||
} else { |
|||
return root.get(attributeName); |
|||
} |
|||
} |
|||
|
|||
public static List<Field> getAllFields(Class clazz, List<Field> fields) { |
|||
if (clazz != null) { |
|||
fields.addAll(Arrays.asList(clazz.getDeclaredFields())); |
|||
getAllFields(clazz.getSuperclass(), fields); |
|||
} |
|||
return fields; |
|||
} |
|||
|
|||
private static boolean isBlank(final CharSequence cs) { |
|||
int strLen; |
|||
if (cs == null || (strLen = cs.length()) == 0) { |
|||
return true; |
|||
} |
|||
for (int i = 0; i < strLen; i++) { |
|||
if (!Character.isWhitespace(cs.charAt(i))) { |
|||
return false; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
package com.canvas.web.utils; |
|||
|
|||
import cn.hutool.core.util.ObjectUtil; |
|||
import com.canvas.web.exception.BaseException; |
|||
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator; |
|||
|
|||
public class ValidationUtil { |
|||
|
|||
/** |
|||
* @title: 验证空 |
|||
* @Author: Liu_Lee |
|||
* @Date: 15:17 2020/12/17 |
|||
* @Param: |
|||
* @return: |
|||
**/ |
|||
public static void isNull(Object obj, String entity, String paramenter, Object value) { |
|||
if (ObjectUtil.isNull(obj)) { |
|||
String msg = entity + "不存在:" + paramenter + "is" + value; |
|||
throw new BaseException(msg); |
|||
} |
|||
} |
|||
|
|||
//验证是否为邮箱 |
|||
public static boolean isEmail(String email){return new EmailValidator().isValid(email,null);} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue