Browse Source

添加权限控制

lgl 1 year ago
parent
commit
3cac1880e4

+ 5 - 1
pom.xml

@@ -347,7 +347,11 @@
347 347
             <version>1.18.12</version>
348 348
             <scope>provided</scope>
349 349
         </dependency>
350
-
350
+        <dependency>
351
+            <groupId>cn.hutool</groupId>
352
+            <artifactId>hutool-all</artifactId>
353
+            <version>5.8.4</version>
354
+        </dependency>
351 355
     </dependencies>
352 356
 
353 357
 

+ 93 - 0
src/main/java/com/liang/common/advice/DataPermissionsAdvice.java

@@ -0,0 +1,93 @@
1
+package com.liang.common.advice;
2
+
3
+
4
+import cn.hutool.core.util.StrUtil;
5
+import com.liang.common.annotation.DataPermissions;
6
+import com.liang.common.base.BaseResult;
7
+import com.liang.common.enums.Logical;
8
+import com.liang.common.enums.ResultCode;
9
+import com.liang.common.enums.Validation;
10
+import com.liang.common.utils.SpringUtil;
11
+import com.liang.common.utils.Tools;
12
+import org.aspectj.lang.ProceedingJoinPoint;
13
+import org.aspectj.lang.annotation.Around;
14
+import org.aspectj.lang.annotation.Aspect;
15
+import org.aspectj.lang.reflect.MethodSignature;
16
+import org.springframework.stereotype.Component;
17
+
18
+import java.util.ArrayList;
19
+import java.util.Arrays;
20
+import java.util.List;
21
+
22
+/**
23
+ * 数据权限处理
24
+ *
25
+ * @author
26
+ */
27
+@Aspect
28
+@Component
29
+public class DataPermissionsAdvice {
30
+
31
+    /**
32
+     * 功能描述:
33
+     */
34
+    @Around(value = "@annotation(com.liang.common.annotation.DataPermissions)")
35
+    public Object validationPermissions(ProceedingJoinPoint pjp) throws Throwable {
36
+
37
+        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
38
+        DataPermissions dataPermissions = methodSignature.getMethod().getAnnotation(DataPermissions.class);
39
+
40
+        Validation validation;//
41
+        try {
42
+            validation = validation(dataPermissions, pjp.getArgs());
43
+        } catch (Exception e) {
44
+            //发现异常认为权限不足
45
+            validation = Validation.DENIED;
46
+        }
47
+
48
+        //校验成功执行方法,失败方法就不用执行了
49
+        if (Validation.ACCESS == validation) {
50
+            return pjp.proceed();
51
+        } else {
52
+            //返回失败的信息
53
+            return BaseResult.failure(ResultCode.FAIL);
54
+        }
55
+    }
56
+
57
+    private Validation validation(DataPermissions dataPermissions, Object[] args) {
58
+        String[] permissionsArr = dataPermissions.value();
59
+        if (Tools.isEmpty(permissionsArr)) {
60
+            return Validation.ACCESS;
61
+        }
62
+
63
+        //获取用户权限
64
+        List<String> permissionList = getPermissionList(args);
65
+        if (Tools.isEmpty(permissionList)) {
66
+            return Validation.DENIED;
67
+        }
68
+
69
+        Logical logical = dataPermissions.logical();
70
+        //有一个不满足 认为无权限
71
+        if (Logical.AND == logical) {
72
+            return Arrays.stream(permissionsArr).anyMatch(permission -> !permissionList.contains(permission)) ? Validation.DENIED : Validation.ACCESS;
73
+        }
74
+        //只要有一个满足 认为有权限
75
+        if (Logical.OR == logical) {
76
+            return Arrays.stream(permissionsArr).anyMatch(permissionList::contains) ? Validation.ACCESS : Validation.DENIED;
77
+        }
78
+        return Validation.DENIED;
79
+    }
80
+
81
+
82
+    private List<String> getPermissionList(Object[] args) {
83
+        List<String> permissionList = new ArrayList<>();
84
+//        PermissionDao permissionsDao = SpringUtil.getBean(PermissionDao.class);
85
+//        User user = UserUtil.getCurrentUserFromRedis();
86
+//
87
+//
88
+//        permissionList = permissionsDao.listPermissionByUserId(user.getId());
89
+
90
+
91
+        return permissionList;
92
+    }
93
+}

+ 24 - 0
src/main/java/com/liang/common/annotation/DataPermissions.java

@@ -0,0 +1,24 @@
1
+package com.liang.common.annotation;
2
+
3
+
4
+
5
+import com.liang.common.enums.Logical;
6
+
7
+import java.lang.annotation.ElementType;
8
+import java.lang.annotation.Retention;
9
+import java.lang.annotation.RetentionPolicy;
10
+import java.lang.annotation.Target;
11
+
12
+/**
13
+ * 数据权限
14
+ *
15
+ * @author tangfei
16
+ *
17
+ */
18
+@Target({ ElementType.PARAMETER,ElementType.METHOD })
19
+@Retention(RetentionPolicy.RUNTIME)
20
+public @interface DataPermissions {
21
+	String[] value() default {};//权限标识
22
+	String type() default "";//方法分类
23
+	Logical logical() default Logical.AND;
24
+}

+ 4 - 0
src/main/java/com/liang/common/base/BaseResult.java

@@ -126,6 +126,10 @@ public class BaseResult implements Serializable {
126 126
         return new BaseResult(code.code(), message, NOOP);
127 127
     }
128 128
 
129
+    public static BaseResult failure(ResultCode result) {
130
+        return new BaseResult(result.code(), result.msg(), NOOP);
131
+    }
132
+
129 133
     /**
130 134
      * 处理失败
131 135
      *

+ 5 - 0
src/main/java/com/liang/common/enums/Logical.java

@@ -0,0 +1,5 @@
1
+package com.liang.common.enums;
2
+
3
+public enum Logical {
4
+    AND, OR
5
+}

+ 5 - 0
src/main/java/com/liang/common/enums/Validation.java

@@ -0,0 +1,5 @@
1
+package com.liang.common.enums;
2
+
3
+public enum Validation {
4
+     DENIED, ACCESS
5
+}

+ 395 - 0
src/main/java/com/liang/common/utils/Tools.java

@@ -0,0 +1,395 @@
1
+package com.liang.common.utils;
2
+
3
+import cn.hutool.core.date.DateTime;
4
+import cn.hutool.core.date.DateUtil;
5
+import cn.hutool.core.exceptions.ValidateException;
6
+import cn.hutool.core.lang.Validator;
7
+import cn.hutool.core.util.StrUtil;
8
+import cn.hutool.http.HttpException;
9
+import cn.hutool.http.HttpRequest;
10
+import cn.hutool.json.JSONObject;
11
+import cn.hutool.json.JSONUtil;
12
+import lombok.extern.slf4j.Slf4j;
13
+
14
+import java.io.*;
15
+import java.security.NoSuchAlgorithmException;
16
+import java.security.SecureRandom;
17
+import java.text.ParseException;
18
+import java.text.SimpleDateFormat;
19
+import java.util.*;
20
+import java.util.stream.Collectors;
21
+
22
+/**
23
+ * 说明:常用工具
24
+ */
25
+@Slf4j
26
+public class Tools {
27
+
28
+
29
+    private static Random rand;  // SecureRandom is preferred to Random
30
+
31
+    static {
32
+        try {
33
+            rand = SecureRandom.getInstanceStrong();
34
+        } catch (NoSuchAlgorithmException e) {
35
+            log.error("init error", e);
36
+        }
37
+
38
+    }
39
+
40
+
41
+    private Tools() {
42
+    }
43
+
44
+    /**
45
+     * 随机生成六位数验证码
46
+     *
47
+     * @return
48
+     */
49
+    public static int getRandomNum() {
50
+//        Random r = new Random();
51
+
52
+        return rand.nextInt(900000);//(Math.random()*(999999-100000)+100000)
53
+    }
54
+
55
+    /**
56
+     * 随机生成四位数验证码
57
+     *
58
+     * @return
59
+     */
60
+    public static int getRandomNum4() {
61
+//        Random r = new Random();
62
+        return rand.nextInt(9000) + 1000;
63
+    }
64
+
65
+
66
+    /**
67
+     * 随机生成四位数验证码
68
+     *
69
+     * @return
70
+     */
71
+    public static int getRandomNumLen(int bound) {
72
+//        Random r = new Random();
73
+        return rand.nextInt(bound);
74
+    }
75
+
76
+    public static Random getRandom() {
77
+        return rand;
78
+    }
79
+
80
+    /**
81
+     * 往文件里的内容
82
+     *
83
+     * @param fileP   文件路径
84
+     * @param content 写入的内容
85
+     */
86
+    public static void writeFile(String fileP, String content) throws IOException {
87
+        String filePath = (Thread.currentThread().getContextClassLoader().getResource("")) + "../../";    //项目路径
88
+        filePath = filePath.replace("file:/", "");
89
+        filePath = filePath.replace("%20", " ");
90
+        filePath = filePath.trim() + fileP.trim();
91
+        if (filePath.indexOf(":") != 1) {
92
+            filePath = File.separator + filePath;
93
+        }
94
+        try (OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
95
+             BufferedWriter writer = new BufferedWriter(write);) {
96
+            writer.write(content);
97
+        } catch (IOException e) {
98
+            throw e;
99
+        }
100
+    }
101
+
102
+    public static String getString(Map map, String key) {
103
+        if (map == null || key == null) {
104
+            return "";
105
+        }
106
+        Object v = map.get(key);
107
+        return v == null ? "" : v.toString();
108
+    }
109
+
110
+    /**
111
+     * 检测字符串是否不为空(null,"","null")
112
+     *
113
+     * @param s
114
+     * @return 不为空则返回true,否则返回false
115
+     */
116
+    public static boolean notEmpty(String s) {
117
+        return !isEmpty(s);
118
+    }
119
+
120
+
121
+    /**
122
+     * 集合是否为非空
123
+     *
124
+     * @param collection 集合
125
+     * @return 是否为非空
126
+     */
127
+    public static boolean notEmpty(Collection<?> collection) {
128
+        return !isEmpty(collection);
129
+    }
130
+
131
+
132
+    /**
133
+     * 检测字符串是否不为空(null,"")
134
+     *
135
+     * @param obj
136
+     * @return 不为空则返回true,否则返回false
137
+     */
138
+    public static boolean notEmpty(Object obj) {
139
+        return !isEmpty(obj);
140
+    }
141
+
142
+
143
+    /**
144
+     * 数组是否为非空
145
+     *
146
+     * @param <T>   数组元素类型
147
+     * @param array 数组
148
+     * @return 是否为非空
149
+     */
150
+    public static <T> boolean notEmpty(T[] array) {
151
+        return !isEmpty(array);
152
+    }
153
+
154
+    /**
155
+     * 检测字符串是否为空(null,"","null")
156
+     *
157
+     * @param s
158
+     * @return 为空则返回true,不否则返回false
159
+     */
160
+    public static boolean isEmpty(String s) {
161
+        return s == null || "".equals(s) || "null".equals(s);
162
+    }
163
+
164
+
165
+    /**
166
+     * 集合是否为空
167
+     *
168
+     * @param collection 集合
169
+     * @return 是否为空
170
+     */
171
+    public static boolean isEmpty(Collection<?> collection) {
172
+        return collection == null || collection.isEmpty();
173
+    }
174
+
175
+    /**
176
+     * 检测字符串是否为空(null,"","null")
177
+     *
178
+     * @param obj
179
+     * @return 为空则返回true,不否则返回false
180
+     */
181
+    public static boolean isEmpty(Object obj) {
182
+        if (obj instanceof String) {
183
+            return isEmpty((String) obj);//TODO
184
+        } else {
185
+            return null == obj;
186
+        }
187
+    }
188
+
189
+    /**
190
+     * 数组是否为空
191
+     *
192
+     * @param <T>   数组元素类型
193
+     * @param array 数组
194
+     * @return 是否为空
195
+     */
196
+    public static <T> boolean isEmpty(T[] array) {
197
+        return array == null || array.length == 0;
198
+    }
199
+
200
+    /**
201
+     * 字符串转换为字符串数组
202
+     *
203
+     * @param str        字符串
204
+     * @param splitRegex 分隔符
205
+     * @return
206
+     */
207
+    public static String[] str2StrArray(String str, String splitRegex) {
208
+        if (isEmpty(str)) {
209
+            return null;
210
+        }
211
+        return str.split(splitRegex);
212
+    }
213
+
214
+    /**
215
+     * 用默认的分隔符(,)将字符串转换为字符串数组
216
+     *
217
+     * @param str 字符串
218
+     * @return
219
+     */
220
+    public static String[] str2StrArray(String str) {
221
+        return str2StrArray(str, ",\\s*");
222
+    }
223
+
224
+    /**
225
+     * 按照yyyy-MM-dd HH:mm:ss的格式,日期转字符串
226
+     *
227
+     * @param date
228
+     * @return yyyy-MM-dd HH:mm:ss
229
+     */
230
+    public static String date2Str(Date date) {
231
+        return date2Str(date, "yyyy-MM-dd HH:mm:ss");
232
+    }
233
+
234
+    /**
235
+     * 按照yyyy-MM-dd HH:mm:ss的格式,字符串转日期
236
+     *
237
+     * @param date
238
+     * @return
239
+     */
240
+    public static Date str2Date(String date) {
241
+        if (notEmpty(date)) {
242
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
243
+            try {
244
+                return sdf.parse(date);
245
+            } catch (ParseException e) {
246
+                log.error("", e);
247
+            }
248
+            return new Date();
249
+        } else {
250
+            return null;
251
+        }
252
+    }
253
+
254
+    /**
255
+     * 按照参数format的格式,日期转字符串
256
+     *
257
+     * @param date
258
+     * @param format
259
+     * @return
260
+     */
261
+    public static String date2Str(Date date, String format) {
262
+        if (date != null) {
263
+            SimpleDateFormat sdf = new SimpleDateFormat(format);
264
+            return sdf.format(date);
265
+        } else {
266
+            return "";
267
+        }
268
+    }
269
+
270
+    /**
271
+     * 把时间根据时、分、秒转换为时间段
272
+     *
273
+     * @param StrDate
274
+     */
275
+    public static String getTimes(String StrDate) {
276
+        String resultTimes = "";
277
+
278
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
279
+        java.util.Date now;
280
+
281
+        try {
282
+            now = new Date();
283
+            java.util.Date date = df.parse(StrDate);
284
+            long times = now.getTime() - date.getTime();
285
+            long day = times / (24 * 60 * 60 * 1000);
286
+            long hour = (times / (60 * 60 * 1000) - day * 24);
287
+            long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
288
+            long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
289
+
290
+            StringBuffer sb = new StringBuffer();
291
+            //sb.append("发表于:");
292
+            if (hour > 0) {
293
+                sb.append(hour + "小时前");
294
+            } else if (min > 0) {
295
+                sb.append(min + "分钟前");
296
+            } else {
297
+                sb.append(sec + "秒前");
298
+            }
299
+
300
+            resultTimes = sb.toString();
301
+        } catch (ParseException e) {
302
+            log.error("", e);
303
+        }
304
+
305
+        return resultTimes;
306
+    }
307
+
308
+
309
+    /**
310
+     * 读取txt里的全部内容
311
+     *
312
+     * @param fileP    文件路径
313
+     * @param encoding 编码
314
+     * @return
315
+     */
316
+    public static String readTxtFileAll(String fileP, String encoding) throws IOException {
317
+        StringBuffer fileContent = new StringBuffer();
318
+        try {
319
+            String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../";    //项目路径
320
+            filePath = filePath.replaceAll("file:/", "");
321
+            filePath = filePath.replaceAll("%20", " ");
322
+            filePath = filePath.trim() + fileP.trim();
323
+            if (filePath.indexOf(":") != 1) {
324
+                filePath = File.separator + filePath;
325
+            }
326
+            File file = new File(filePath);
327
+            if (file.isFile() && file.exists()) {        // 判断文件是否存在
328
+                try (InputStreamReader read = new InputStreamReader(
329
+                        new FileInputStream(file), encoding);    // 考虑到编码格式
330
+                     BufferedReader bufferedReader = new BufferedReader(read);) {
331
+                    String lineTxt;
332
+                    while ((lineTxt = bufferedReader.readLine()) != null) {
333
+                        fileContent.append(lineTxt);
334
+                        fileContent.append("\n");
335
+                    }
336
+                }
337
+            } else {
338
+                throw new FileNotFoundException("找不到指定的文件,查看此路径是否正确:" + filePath);
339
+            }
340
+        } catch (IOException e) {
341
+            throw new IOException("读取文件内容出错" + e.getMessage());
342
+        }
343
+        return fileContent.toString();
344
+    }
345
+
346
+
347
+    public static boolean isEmail(String email) {
348
+        return Validator.isMatchRegex("^[A-Za-z0-9+_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$", email);
349
+    }
350
+
351
+    public static boolean isMobile(String phone) {
352
+        return Validator.isMobile(phone);
353
+    }
354
+
355
+
356
+    /**
357
+     * 获取周末  月从0开始
358
+     *
359
+     * @param year
360
+     * @param mouth
361
+     * @return
362
+     */
363
+    public static Set<String> getMonthWekDay(int year, int mouth) {
364
+        Set<String> dateList = new HashSet<>();
365
+        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
366
+        Calendar calendar = new GregorianCalendar(year, mouth, 1);
367
+        Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
368
+        endCalendar.add(Calendar.MONTH, 1);
369
+        while (true) {
370
+            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
371
+            if (weekday == 1 || weekday == 7) {
372
+                dateList.add(simdf.format(calendar.getTime()));
373
+            }
374
+            calendar.add(Calendar.DATE, 1);
375
+            if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
376
+                break;
377
+            }
378
+        }
379
+        return dateList;
380
+    }
381
+
382
+
383
+    /**
384
+     * @param obj 实体
385
+     * @return 全部不为空并且全部不为null, 则返回true;否则返回false
386
+     */
387
+    public static boolean allNotEmpty(Object... obj) {
388
+        for (Object o : obj) {
389
+            if (Tools.isEmpty(o)) {
390
+                return false;
391
+            }
392
+        }
393
+        return true;
394
+    }
395
+}

+ 0 - 2
src/main/resources/templates/prjproject/list_myproject.html

@@ -245,8 +245,6 @@
245 245
                     Message.warning("请选择要编辑的任务书!", 1500);
246 246
                 } else if (data.length > 1) {
247 247
                     Message.warning("请选择一条记录进行编辑操作!", 1500);
248
-                } else if (data[0].spzt == "审批结束") {
249
-                    Message.warning("该任务书已审批结束,不能修改!", 1500);
250 248
                 } else if (data[0].spzt != null && data[0].spzt.indexOf("未提交") < 0) {
251 249
                     Message.warning("该任务书审批中,不能修改!", 2000);
252 250
                 } else {