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

Commit 31abadb

Browse files
committed
完成管理员模块
1 parent 9916cc2 commit 31abadb

14 files changed

Lines changed: 653 additions & 23 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
@Repository
1616
public interface AuthDAO {
1717

18+
/**
19+
* 检测某个权限是否存在
20+
*
21+
* @param userId 用户编号
22+
* @param fileId 文件编号
23+
*
24+
* @return {@link Auth}
25+
*/
26+
@Select("select * from auth where user_id=#{userId} and file_id=#{fileId}")
27+
Auth exists(@Param("userId") int userId, @Param("fileId") long fileId);
28+
1829
/**
1930
* 批量删除权限记录
2031
*

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
@Repository
1515
public interface UserDAO {
1616

17+
/**
18+
* 更新用户权限
19+
*
20+
* @param id 用户编号
21+
* @param permission 权限
22+
*
23+
* @return 是否更新成功
24+
*/
25+
@Update("update user set permission=#{permission} where id=#{id}")
26+
boolean updatePermission(@Param("id") int id, @Param("permission") int permission);
27+
1728
/**
1829
* 用过用户名获取用户Id
1930
*
@@ -52,12 +63,14 @@ boolean updateBasicInfo(@Param("id") int id, @Param("avatar") String avatar, @Pa
5263
* 通过权限获取用户
5364
*
5465
* @param permission 权限
66+
* @param condition 条件
5567
* @param offset 偏移
5668
*
5769
* @return {@link List}
5870
*/
5971
@SelectProvider(type = UserSqlProvider.class, method = "getUserBy")
60-
List<User> getUserBy(@Param("permission") int permission, @Param("offset") int offset);
72+
List<User> getUserBy(@Param("permission") int permission, @Param("condition") String condition, @Param("offset")
73+
int offset);
6174

6275
/**
6376
* 用户登录
@@ -107,8 +120,8 @@ boolean updateBasicInfo(@Param("id") int id, @Param("avatar") String avatar, @Pa
107120
*/
108121
@UpdateProvider(type = UserSqlProvider.class, method = "updateAuthById")
109122
boolean updateAuthById(@Param("id") int id, @Param("isDownloadable") int isDownloadable, @Param("isUploadable")
110-
int isUploadable, @Param("isVisible") int isVisible, @Param("isDeletable") int isDeletable, @Param
111-
("isUpdatable") int isUpdatable);
123+
int isUploadable, @Param("isDeletable") int isDeletable, @Param("isUpdatable") int isUpdatable, @Param
124+
("isVisible") int isVisible);
112125

113126
/**
114127
* 通过编号哦更新密码

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.zhazhapan.efo.EfoApplication;
44
import com.zhazhapan.efo.modules.constant.ConfigConsts;
55
import com.zhazhapan.efo.modules.constant.DefaultValues;
6+
import com.zhazhapan.util.Checker;
67
import org.apache.ibatis.annotations.Param;
78
import org.apache.ibatis.jdbc.SQL;
89

@@ -16,16 +17,20 @@ public String updateAuthById() {
1617
return CommonSqlProvider.updateAuthById("user");
1718
}
1819

19-
public String getUserBy(@Param("permission") int permission) {
20+
public String getUserBy(@Param("permission") int permission, @Param("condition") String condition) {
2021
String sql = new SQL() {{
2122
SELECT("*");
2223
FROM("user");
2324
if (permission == DefaultValues.THREE_INT) {
24-
WHERE("permission!=3");
25+
WHERE("permission<3");
2526
} else if (permission == DefaultValues.TWO_INT) {
26-
WHERE("permission=2");
27+
WHERE("permission<2");
2728
} else {
28-
WHERE("permission=1");
29+
WHERE("permission<0");
30+
}
31+
if (Checker.isNotEmpty(condition)) {
32+
WHERE("username like '%" + condition + "%' or email like '%" + condition + "%' or real_name like '" +
33+
condition + "'");
2934
}
3035
ORDER_BY(EfoApplication.settings.getStringUseEval(ConfigConsts.USER_ORDER_BY_OF_SETTINGS));
3136
}}.toString();

src/main/java/com/zhazhapan/efo/entity/Auth.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public void setAuth(int isDownloadable, int isUploadable, int isDeletable, int i
5454
this.isVisible = isVisible;
5555
}
5656

57+
public void setAuth(int[] auth) {
58+
setAuth(auth[0], auth[1], auth[2], auth[3], auth[4]);
59+
}
60+
5761
@Override
5862
public String toString() {
5963
return BeanUtils.toPrettyJson(this);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
*/
1212
public interface IAuthService {
1313

14+
/**
15+
* 添加权限集
16+
*
17+
* @param files 文件
18+
* @param users 用户
19+
* @param auths 权限集
20+
*
21+
* @return 是否添加成功
22+
*/
23+
boolean addAuth(String files, String users, String auths);
24+
1425
/**
1526
* 批量删除权限记录
1627
*

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,55 @@
33
import com.zhazhapan.efo.entity.User;
44

55
import javax.servlet.http.HttpServletResponse;
6+
import java.util.List;
67

78
/**
89
* @author pantao
910
* @since 2018/1/22
1011
*/
1112
public interface IUserService {
1213

14+
/**
15+
* 更新用户权限
16+
*
17+
* @param id 用户编号
18+
* @param permission 权限
19+
*
20+
* @return 是否更新成功
21+
*/
22+
boolean updatePermission(int id, int permission);
23+
24+
/**
25+
* 重置用户密码
26+
*
27+
* @param id 用户编号
28+
* @param password 密码
29+
*
30+
* @return 是否重置成功
31+
*/
32+
boolean resetPassword(int id, String password);
33+
34+
/**
35+
* 更新用户权限
36+
*
37+
* @param id 编号
38+
* @param auths 操作文件的权限集
39+
*
40+
* @return 是否更新成功
41+
*/
42+
boolean updateFileAuth(int id, String auths);
43+
44+
/**
45+
* 获取用户
46+
*
47+
* @param permission 当前用户权限
48+
* @param condition 筛选条件
49+
* @param offset 偏移
50+
*
51+
* @return {@link List}
52+
*/
53+
List<User> getUser(int permission, String condition, int offset);
54+
1355
/**
1456
* 登录
1557
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.zhazhapan.efo.util.ServiceUtils;
1111
import com.zhazhapan.modules.constant.ValueConsts;
1212
import com.zhazhapan.util.Checker;
13+
import com.zhazhapan.util.Formatter;
1314
import org.springframework.beans.factory.annotation.Autowired;
1415
import org.springframework.stereotype.Service;
1516

@@ -27,6 +28,26 @@ public class AuthServiceImpl implements IAuthService {
2728
@Autowired
2829
public AuthServiceImpl(AuthDAO authDAO) {this.authDAO = authDAO;}
2930

31+
@Override
32+
public boolean addAuth(String files, String users, String auths) {
33+
if (Checker.isNotEmpty(files) && Checker.isNotEmpty(users) && Checker.isNotEmpty(auths)) {
34+
String[] file = files.split(ValueConsts.COMMA_SIGN);
35+
String[] user = users.split(ValueConsts.COMMA_SIGN);
36+
for (String f : file) {
37+
long fileId = Formatter.stringToLong(f);
38+
for (String u : user) {
39+
int userId = Formatter.stringToInt(u);
40+
if (Checker.isNull(authDAO.exists(userId, fileId))) {
41+
Auth auth = new Auth(userId, fileId);
42+
auth.setAuth(BeanUtils.getAuth(auths));
43+
authDAO.insertAuth(auth);
44+
}
45+
}
46+
}
47+
}
48+
return true;
49+
}
50+
3051
@Override
3152
public boolean batchDelete(String ids) {
3253
return Checker.isNotEmpty(ids) && authDAO.batchDelete(ids);

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import com.zhazhapan.efo.entity.User;
88
import com.zhazhapan.efo.modules.constant.ConfigConsts;
99
import com.zhazhapan.efo.service.IUserService;
10+
import com.zhazhapan.efo.util.BeanUtils;
1011
import com.zhazhapan.modules.constant.ValueConsts;
1112
import com.zhazhapan.util.Checker;
1213
import com.zhazhapan.util.DateUtils;
14+
import com.zhazhapan.util.MailSender;
15+
import org.apache.log4j.Logger;
1316
import org.springframework.beans.factory.annotation.Autowired;
1417
import org.springframework.stereotype.Service;
1518

1619
import javax.servlet.http.Cookie;
1720
import javax.servlet.http.HttpServletResponse;
21+
import java.util.List;
1822
import java.util.regex.Pattern;
1923

2024
import static com.zhazhapan.efo.EfoApplication.settings;
@@ -29,9 +33,41 @@ public class UserServiceImpl implements IUserService {
2933

3034
private final UserDAO userDAO;
3135

36+
private Logger logger = Logger.getLogger(UserServiceImpl.class);
37+
3238
@Autowired
3339
public UserServiceImpl(UserDAO userDAO) {this.userDAO = userDAO;}
3440

41+
@Override
42+
public boolean updatePermission(int id, int permission) {
43+
return userDAO.updatePermission(id, permission > 2 ? 2 : permission);
44+
}
45+
46+
@Override
47+
public boolean resetPassword(int id, String password) {
48+
boolean result = Checker.isNotEmpty(password) && userDAO.updatePasswordById(id, password);
49+
if (result) {
50+
removeTokenByValue(id);
51+
try {
52+
MailSender.sendMail(getUserById(id).getEmail(), "密码重置通知", "您的密码已被管理员重置为:" + password);
53+
} catch (Exception e) {
54+
logger.error(e.getMessage());
55+
}
56+
}
57+
return result;
58+
}
59+
60+
@Override
61+
public boolean updateFileAuth(int id, String auths) {
62+
int[] auth = BeanUtils.getAuth(auths);
63+
return userDAO.updateAuthById(id, auth[0], auth[1], auth[2], auth[3], auth[4]);
64+
}
65+
66+
@Override
67+
public List<User> getUser(int permission, String condition, int offset) {
68+
return userDAO.getUserBy(permission, condition, offset);
69+
}
70+
3571
@Override
3672
public User login(String loginName, String password, String token, HttpServletResponse response) {
3773
boolean allowLogin = settings.getBooleanUseEval(ConfigConsts.ALLOW_LOGIN_OF_SETTINGS);

src/main/java/com/zhazhapan/efo/web/controller/AuthController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public class AuthController {
2424
@Autowired
2525
public AuthController(IAuthService authService) {this.authService = authService;}
2626

27+
@AuthInterceptor(InterceptorLevel.ADMIN)
28+
@RequestMapping(value = "", method = RequestMethod.POST)
29+
public String add(String files, String users, String auths) {
30+
System.out.println("files: " + files + " users: " + users + " auths: " + auths);
31+
return ControllerUtils.getResponse(authService.addAuth(files, users, auths));
32+
}
33+
2734
@AuthInterceptor(InterceptorLevel.ADMIN)
2835
@RequestMapping(value = "/all", method = RequestMethod.GET)
2936
public String getAuth(String user, String file, int offset) {

src/main/java/com/zhazhapan/efo/web/controller/ConfigController.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.zhazhapan.efo.web.controller;
22

3+
import com.zhazhapan.efo.EfoApplication;
34
import com.zhazhapan.efo.annotation.AuthInterceptor;
5+
import com.zhazhapan.efo.entity.User;
46
import com.zhazhapan.efo.enums.InterceptorLevel;
57
import com.zhazhapan.efo.service.IConfigService;
8+
import com.zhazhapan.modules.constant.ValueConsts;
69
import org.springframework.beans.factory.annotation.Autowired;
710
import org.springframework.web.bind.annotation.RequestMapping;
811
import org.springframework.web.bind.annotation.RequestMethod;
912
import org.springframework.web.bind.annotation.RestController;
1013

14+
import javax.servlet.http.HttpServletRequest;
15+
1116
/**
1217
* @author pantao
1318
* @since 2018/1/22
@@ -18,8 +23,36 @@ public class ConfigController {
1823

1924
private final IConfigService configService;
2025

26+
private final HttpServletRequest request;
27+
2128
@Autowired
22-
public ConfigController(IConfigService configService) {this.configService = configService;}
29+
public ConfigController(IConfigService configService, HttpServletRequest request) {
30+
this.configService = configService;
31+
this.request = request;
32+
}
33+
34+
@AuthInterceptor(InterceptorLevel.ADMIN)
35+
@RequestMapping(value = "", method = RequestMethod.PUT)
36+
public String updateConfig(String config) {
37+
User user = (User) request.getSession().getAttribute(ValueConsts.USER_STRING);
38+
if (user.getPermission() > ValueConsts.TWO_INT) {
39+
EfoApplication.settings.setJsonObject(config);
40+
return "{\"message\":\"saved successfully\"}";
41+
} else {
42+
return "{\"message\":\"permission denied\"}";
43+
}
44+
}
45+
46+
@AuthInterceptor(InterceptorLevel.ADMIN)
47+
@RequestMapping(value = "/all", method = RequestMethod.GET)
48+
public String getAll() {
49+
User user = (User) request.getSession().getAttribute(ValueConsts.USER_STRING);
50+
if (user.getPermission() > ValueConsts.TWO_INT) {
51+
return EfoApplication.settings.toString();
52+
} else {
53+
return "{\"message\":\"permission denied\"}";
54+
}
55+
}
2356

2457
@AuthInterceptor(InterceptorLevel.NONE)
2558
@RequestMapping(value = "/global", method = RequestMethod.GET)

0 commit comments

Comments
 (0)