Skip to content

Commit 292eba1

Browse files
committed
后台管理员操作权限控制
1 parent 7d154d9 commit 292eba1

4 files changed

Lines changed: 112 additions & 6 deletions

File tree

admin/src/main/java/info/xiaomo/admin/controller/AdminUserController.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import info.xiaomo.core.service.AdminUserService;
77
import info.xiaomo.core.untils.MD5Util;
88
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.beans.factory.annotation.Value;
109
import org.springframework.data.domain.Page;
1110
import org.springframework.data.domain.PageRequest;
1211
import org.springframework.web.bind.annotation.RequestMapping;
@@ -69,12 +68,32 @@ public Map<String, Object> login(@RequestParam String userName, @RequestParam St
6968
return result;
7069
}
7170

71+
/**
72+
* 添加用户
73+
*
74+
* @param operator
75+
* @param userName
76+
* @param password
77+
* @param authLevel
78+
* @return
79+
*/
7280
@RequestMapping(value = "add", method = RequestMethod.POST)
73-
public HashMap<String, Object> register(
81+
public HashMap<String, Object> add(
82+
@RequestParam String operator,
7483
@RequestParam String userName,
7584
@RequestParam String password,
7685
@RequestParam int authLevel
7786
) {
87+
AdminModel operatorModel = service.findAdminUserByUserName(operator);
88+
if (operator == null) {
89+
result.put(code, notFound);
90+
return result;
91+
}
92+
if (operatorModel.getAuthLevel() <= 0) {
93+
result.put(code, authError);
94+
return result;
95+
}
96+
7897
AdminModel adminModel = service.findAdminUserByUserName(userName);
7998
if (adminModel != null) {
8099
result.put(code, error);
@@ -108,15 +127,24 @@ public HashMap<String, Object> findUserById(@RequestParam("id") Long id) {
108127

109128

110129
@RequestMapping(value = "findAll", method = RequestMethod.GET)
111-
public HashMap<String, Object> getAll(@RequestParam(value = "start",defaultValue = "1") int start, @RequestParam(value = "pageSize", defaultValue ="10") int page) {
130+
public HashMap<String, Object> getAll(@RequestParam(value = "start", defaultValue = "1") int start, @RequestParam(value = "pageSize", defaultValue = "10") int page) {
112131
Page<AdminModel> pages = service.getAdminUsers(new PageRequest(start - 1, page));
113132
result.put(code, success);
114133
result.put(adminUsers, pages);
115134
return result;
116135
}
117136

118137
@RequestMapping(value = "deleteById", method = RequestMethod.GET)
119-
public HashMap<String, Object> deleteUserById(@RequestParam("id") Long id) throws UserNotFoundException {
138+
public HashMap<String, Object> deleteUserById(@RequestParam("id") Long id, @RequestParam String operator) throws UserNotFoundException {
139+
AdminModel operatorModel = service.findAdminUserByUserName(operator);
140+
if (operator == null) {
141+
result.put(code, notFound);
142+
return result;
143+
}
144+
if (operatorModel.getAuthLevel() <= 0) {
145+
result.put(code, authError);
146+
return result;
147+
}
120148
AdminModel adminModel = service.deleteAdminUserById(id);
121149
if (adminModel == null) {
122150
result.put(code, notFound);
@@ -129,10 +157,20 @@ public HashMap<String, Object> deleteUserById(@RequestParam("id") Long id) throw
129157

130158
@RequestMapping(value = "update", method = RequestMethod.POST)
131159
public HashMap<String, Object> update(
160+
@RequestParam("operator") String operator,
132161
@RequestParam("userName") String userName,
133162
@RequestParam("password") String password,
134163
@RequestParam("authLevel") int authLevel
135164
) throws UserNotFoundException {
165+
AdminModel operatorModel = service.findAdminUserByUserName(operator);
166+
if (operator == null) {
167+
result.put(code, notFound);
168+
return result;
169+
}
170+
if (operatorModel.getAuthLevel() <= 0) {
171+
result.put(code, authError);
172+
return result;
173+
}
136174
AdminModel adminModel = service.findAdminUserByUserName(userName);
137175
if (adminModel == null) {
138176
result.put(code, notFound);
@@ -148,7 +186,16 @@ public HashMap<String, Object> update(
148186
}
149187

150188
@RequestMapping(value = "forbid", method = RequestMethod.GET)
151-
public HashMap<String, Object> forbid(@RequestParam("id") Long id) throws UserNotFoundException {
189+
public HashMap<String, Object> forbid(@RequestParam("id") Long id, @RequestParam("operator") String operator) throws UserNotFoundException {
190+
AdminModel operatorModel = service.findAdminUserByUserName(operator);
191+
if (operator == null) {
192+
result.put(code, notFound);
193+
return result;
194+
}
195+
if (operatorModel.getAuthLevel() <= 0) {
196+
result.put(code, authError);
197+
return result;
198+
}
152199
AdminModel model = service.findAdminUserById(id);
153200
if (model == null) {
154201
result.put(code, notFound);

core/src/main/java/info/xiaomo/core/constant/Code.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public abstract class Code extends Module {
4747
/**
4848
* 图片格式不对
4949
*/
50-
protected static final int notImg = 205;
50+
protected static final int notImg = 206;
51+
52+
/**
53+
* 权限不够
54+
*/
55+
protected static final int authError = 207;
5156

5257
/**
5358
* 找不到

core/src/main/java/info/xiaomo/core/model/AdminModel.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class AdminModel extends BaseModel implements Serializable {
4646
*/
4747
private int authLevel;
4848

49+
/**
50+
* 操作员(谁加的这个帐号)
51+
*/
52+
private String operator;
53+
4954
public String getUserName() {
5055
return userName;
5156
}
@@ -77,4 +82,12 @@ public int getStatus() {
7782
public void setStatus(int status) {
7883
this.status = status;
7984
}
85+
86+
public String getOperator() {
87+
return operator;
88+
}
89+
90+
public void setOperator(String operator) {
91+
this.operator = operator;
92+
}
8093
}

项目文档/返回码对照表

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 成功
3+
*/
4+
success = 200;
5+
/**
6+
* 己激活
7+
*/
8+
activated = 201;
9+
/**
10+
* 己过期
11+
*/
12+
expired = 202;
13+
/**
14+
* 未激活
15+
*/
16+
notActivated = 203;
17+
/**
18+
* 重复
19+
*/
20+
repeat = 204;
21+
22+
/**
23+
* 出错
24+
*/
25+
error = 205;
26+
27+
/**
28+
* 图片格式不对
29+
*/
30+
notImg = 206;
31+
32+
/**
33+
* 权限不够
34+
*/
35+
authError = 207;
36+
37+
/**
38+
* 找不到
39+
*/
40+
notFound = 404;
41+

0 commit comments

Comments
 (0)