Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit 7f825ef

Browse files
committed
实现权限记录的删改查
1 parent 212d392 commit 7f825ef

File tree

13 files changed

+515
-81
lines changed

13 files changed

+515
-81
lines changed

mysql/efo.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
179179
INSERT INTO user(username,real_name,email,password,permission,is_deletable,is_updatable) VALUES("system","系统","[email protected]",sha2("123456",256),3,1,1);
180180

181181
#请确保数据库中始终有“未分类”这个分类,否则系统运行时有可能出错
182-
INSERT INTO category (name) VALUES("未分类");
182+
INSERT INTO category(name) VALUES("未分类");
183183

184184
DROP USER IF EXISTS 'zhazhapan'@'localhost';
185185

src/main/java/com/zhazhapan/efo/dao/AuthDAO.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
@Repository
1616
public interface AuthDAO {
1717

18+
/**
19+
* 批量删除权限记录
20+
*
21+
* @param ids 权限编号集
22+
*
23+
* @return 是否删除成功
24+
*/
25+
@DeleteProvider(type = AuthSqlProvider.class, method = "batchDelete")
26+
boolean batchDelete(@Param("ids") String ids);
27+
1828
/**
1929
* 添加一条权限记录
2030
*
@@ -61,23 +71,26 @@ public interface AuthDAO {
6171
* @param isVisible 可查权限
6272
* @param isDeletable 删除权限
6373
* @param isUpdatable 更新权限
74+
*
75+
* @return 是否更新成功
6476
*/
6577
@UpdateProvider(type = AuthSqlProvider.class, method = "updateAuthById")
66-
void updateAuthById(@Param("id") int id, @Param("isDownloadable") int isDownloadable, @Param("isUploadable") int
67-
isUploadable, @Param("isVisible") int isVisible, @Param("isDeletable") int isDeletable, @Param
68-
("isUpdatable") int isUpdatable);
78+
boolean updateAuthById(@Param("id") long id, @Param("isDownloadable") int isDownloadable, @Param("isUploadable")
79+
int isUploadable, @Param("isDeletable") int isDeletable, @Param("isUpdatable") int isUpdatable, @Param
80+
("isVisible") int isVisible);
6981

7082
/**
7183
* 获取权限记录
7284
*
7385
* @param id 编号,值小于等于0时不作为条件
7486
* @param userId 用户编号,值小于等于0时不作为条件
7587
* @param fileId 文件编号,值小于等于0时不作为条件
88+
* @param fileName 模糊搜索文件名(当参数不为空时)
7689
* @param offset 偏移
7790
*
7891
* @return {@link List}
7992
*/
8093
@SelectProvider(type = AuthSqlProvider.class, method = "getAuthBy")
8194
List<AuthRecord> getAuthBy(@Param("id") long id, @Param("userId") int userId, @Param("fileId") long fileId,
82-
@Param("offset") int offset);
95+
@Param("fileName") String fileName, @Param("offset") int offset);
8396
}

src/main/java/com/zhazhapan/efo/dao/sqlprovider/AuthSqlProvider.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.zhazhapan.efo.EfoApplication;
44
import com.zhazhapan.efo.modules.constant.ConfigConsts;
5+
import com.zhazhapan.util.Checker;
56
import org.apache.ibatis.annotations.Param;
67
import org.apache.ibatis.jdbc.SQL;
78

@@ -15,7 +16,13 @@ public String updateAuthById() {
1516
return CommonSqlProvider.updateAuthById("auth");
1617
}
1718

18-
public String getAuthBy(@Param("id") long id, @Param("userId") int userId, @Param("fileId") long fileId) {
19+
public String batchDelete(@Param("ids") String ids) {
20+
return "delete from auth where id in " + (ids.startsWith("(") ? "" : "(") + ids + (ids.endsWith(")") ? "" :
21+
")");
22+
}
23+
24+
public String getAuthBy(@Param("id") long id, @Param("userId") int userId, @Param("fileId") long fileId, @Param
25+
("fileName") String fileName) {
1926
String sql = new SQL() {{
2027
SELECT("a.id,a.user_id,a.file_id,u.username,f.name file_name,f.local_url,a.is_downloadable,a" + "" + "" +
2128
".is_uploadable,a.is_deletable,a.is_updatable,a.is_visible,a.create_time");
@@ -30,6 +37,8 @@ public String getAuthBy(@Param("id") long id, @Param("userId") int userId, @Para
3037
}
3138
if (fileId > 0) {
3239
WHERE("f.id=#{fileId}");
40+
} else if (Checker.isNotEmpty(fileName)) {
41+
WHERE("f.local_url like '%" + fileName + "%'");
3342
}
3443
ORDER_BY("a." + EfoApplication.settings.getStringUseEval(ConfigConsts.AUTH_ORDER_BY_OF_SETTINGS));
3544
}}.toString();

src/main/java/com/zhazhapan/efo/dao/sqlprovider/DownloadedSqlProvider.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ public String getDownloadBy(@Param("userId") int userId, @Param("fileId") long f
3434
}
3535
if (fileId > 0) {
3636
WHERE("d.file_id=#{fileId}");
37+
} else if (Checker.isNotEmpty(fileName)) {
38+
WHERE("f.local_url like '%" + fileName + "%'");
3739
}
3840
if (categoryId > 0) {
3941
WHERE("c.id=#{categoryId}");
4042
}
41-
if (Checker.isNotEmpty(fileName)) {
42-
WHERE("f.local_url like '%" + fileName + "%'");
43-
}
4443
ORDER_BY("d." + EfoApplication.settings.getStringUseEval(ConfigConsts.DOWNLOAD_ORDER_BY_OF_SETTINGS));
4544
}}.toString();
4645
int size = EfoApplication.settings.getIntegerUseEval(ConfigConsts.DOWNLOAD_PAGE_SIZE_OF_SETTINGS);

src/main/java/com/zhazhapan/efo/dao/sqlprovider/FileSqlProvider.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ public String getBasicBy(@Param("userId") int userId, @Param("fileId") long file
4040
}
4141
if (fileId > 0) {
4242
WHERE("f.id=#{fileId}");
43+
} else if (Checker.isNotEmpty(fileName)) {
44+
WHERE("f.local_url like '%" + fileName + "%'");
4345
}
4446
if (categoryId > 0) {
4547
WHERE("c.id=#{categoryId}");
4648
}
47-
if (Checker.isNotEmpty(fileName)) {
48-
WHERE("f.local_url like '%" + fileName + "%'");
49-
}
5049
ORDER_BY("f." + EfoApplication.settings.getStringUseEval(ConfigConsts.FILE_ORDER_BY_OF_SETTING));
5150
}}.toString();
5251
int size = EfoApplication.settings.getIntegerUseEval(ConfigConsts.FILE_PAGE_SIZE_OF_SETTING);

src/main/java/com/zhazhapan/efo/dao/sqlprovider/UploadedSqlProvider.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ public String getDownloadBy(@Param("userId") int userId, @Param("fileId") long f
3333
}
3434
if (fileId > 0) {
3535
WHERE("f.id=#{fileId}");
36+
} else if (Checker.isNotEmpty(fileName)) {
37+
WHERE("f.local_url like '%" + fileName + "%'");
3638
}
3739
if (categoryId > 0) {
3840
WHERE("c.id=#{categoryId}");
3941
}
40-
if (Checker.isNotEmpty(fileName)) {
41-
WHERE("f.local_url like '%" + fileName + "%'");
42-
}
4342
ORDER_BY("f." + EfoApplication.settings.getStringUseEval(ConfigConsts.FILE_ORDER_BY_OF_SETTING));
4443
}}.toString();
4544
int size = EfoApplication.settings.getIntegerUseEval(ConfigConsts.FILE_PAGE_SIZE_OF_SETTING);

src/main/java/com/zhazhapan/efo/service/IAuthService.java

+32
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@
33
import com.zhazhapan.efo.entity.Auth;
44
import com.zhazhapan.efo.model.AuthRecord;
55

6+
import java.util.List;
7+
68
/**
79
* @author pantao
810
* @since 2018/2/1
911
*/
1012
public interface IAuthService {
1113

14+
/**
15+
* 批量删除权限记录
16+
*
17+
* @param ids 权限编号集
18+
*
19+
* @return 是否删除成功
20+
*/
21+
boolean batchDelete(String ids);
22+
23+
/**
24+
* 更新权限
25+
*
26+
* @param id 权限编号
27+
* @param auths 权限
28+
*
29+
* @return 是否更新成功
30+
*/
31+
boolean updateAuth(long id, String auths);
32+
33+
/**
34+
* 获取权限表数据
35+
*
36+
* @param usernameOrEmail 用户名或邮箱
37+
* @param fileName 文件名
38+
* @param offset 偏移
39+
*
40+
* @return {@link List}
41+
*/
42+
List<AuthRecord> getAuth(String usernameOrEmail, String fileName, int offset);
43+
1244
/**
1345
* 获取一个权限
1446
*

src/main/java/com/zhazhapan/efo/service/impl/AuthServiceImpl.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.zhazhapan.efo.model.AuthRecord;
77
import com.zhazhapan.efo.modules.constant.ConfigConsts;
88
import com.zhazhapan.efo.service.IAuthService;
9+
import com.zhazhapan.efo.util.BeanUtils;
10+
import com.zhazhapan.efo.util.ServiceUtils;
911
import com.zhazhapan.modules.constant.ValueConsts;
1012
import com.zhazhapan.util.Checker;
1113
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,9 +27,28 @@ public class AuthServiceImpl implements IAuthService {
2527
@Autowired
2628
public AuthServiceImpl(AuthDAO authDAO) {this.authDAO = authDAO;}
2729

30+
@Override
31+
public boolean batchDelete(String ids) {
32+
return Checker.isNotEmpty(ids) && authDAO.batchDelete(ids);
33+
}
34+
35+
@Override
36+
public boolean updateAuth(long id, String auths) {
37+
int[] auth = BeanUtils.getAuth(auths);
38+
return authDAO.updateAuthById(id, auth[0], auth[1], auth[2], auth[3], auth[4]);
39+
}
40+
41+
@Override
42+
public List<AuthRecord> getAuth(String usernameOrEmail, String fileName, int offset) {
43+
long fileId = ServiceUtils.getFileId(fileName);
44+
int userId = ServiceUtils.getUserId(usernameOrEmail);
45+
return authDAO.getAuthBy(ValueConsts.ZERO_INT, userId, fileId, fileName, offset);
46+
}
47+
2848
@Override
2949
public AuthRecord getByFileIdAndUserId(long fileId, int userId) {
30-
List<AuthRecord> authRecords = authDAO.getAuthBy(ValueConsts.ZERO_INT, userId, fileId, ValueConsts.ZERO_INT);
50+
List<AuthRecord> authRecords = authDAO.getAuthBy(ValueConsts.ZERO_INT, userId, fileId, ValueConsts
51+
.EMPTY_STRING, ValueConsts.ZERO_INT);
3152
if (Checker.isNotEmpty(authRecords)) {
3253
return authRecords.get(0);
3354
}

src/main/java/com/zhazhapan/efo/util/BeanUtils.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.alibaba.fastjson.JSONObject;
44
import com.zhazhapan.modules.constant.ValueConsts;
5+
import com.zhazhapan.util.Checker;
56
import com.zhazhapan.util.Formatter;
67
import com.zhazhapan.util.enums.FieldModifier;
78
import org.slf4j.Logger;
@@ -30,10 +31,12 @@ private BeanUtils() {}
3031
*/
3132
public static int[] getAuth(String auth) {
3233
int[] a = new int[5];
33-
String[] u = auth.split(ValueConsts.COMMA_SIGN);
34-
int len = Math.min(a.length, u.length);
35-
for (int i = 0; i < len; i++) {
36-
a[i] = Formatter.stringToInt(u[i]);
34+
if (Checker.isNotEmpty(auth)) {
35+
String[] u = auth.split(ValueConsts.COMMA_SIGN);
36+
int len = Math.min(a.length, u.length);
37+
for (int i = 0; i < len; i++) {
38+
a[i] = Formatter.stringToInt(u[i]);
39+
}
3740
}
3841
return a;
3942
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.zhazhapan.efo.web.controller;
2+
3+
import com.zhazhapan.efo.annotation.AuthInterceptor;
4+
import com.zhazhapan.efo.enums.InterceptorLevel;
5+
import com.zhazhapan.efo.service.IAuthService;
6+
import com.zhazhapan.efo.util.ControllerUtils;
7+
import com.zhazhapan.util.Formatter;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
/**
15+
* @author pantao
16+
* @since 2018/3/8
17+
*/
18+
@RestController
19+
@RequestMapping("/auth")
20+
public class AuthController {
21+
22+
private final IAuthService authService;
23+
24+
@Autowired
25+
public AuthController(IAuthService authService) {this.authService = authService;}
26+
27+
@AuthInterceptor(InterceptorLevel.ADMIN)
28+
@RequestMapping(value = "/all", method = RequestMethod.GET)
29+
public String getAuth(String user, String file, int offset) {
30+
return Formatter.listToJson(authService.getAuth(user, file, offset));
31+
}
32+
33+
@AuthInterceptor(InterceptorLevel.ADMIN)
34+
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
35+
public String updateAuth(@PathVariable("id") long id, String auth) {
36+
return ControllerUtils.getResponse(authService.updateAuth(id, auth));
37+
}
38+
39+
@AuthInterceptor(InterceptorLevel.ADMIN)
40+
@RequestMapping(value = "/batch/{ids}", method = RequestMethod.DELETE)
41+
public String batchDelete(@PathVariable("ids") String ids) {
42+
return ControllerUtils.getResponse(authService.batchDelete(ids));
43+
}
44+
}

0 commit comments

Comments
 (0)