Skip to content

Commit 79b26a7

Browse files
authored
feat: add blockUser middleware (#86)
* feat: add blockUser middleware * add test case for block user
1 parent e91f363 commit 79b26a7

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

app/controller/user.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ class UserController extends Controller {
188188
}
189189

190190
async setting() {
191-
const { ctx, ctx: { request: req }, service } = this;
191+
const { ctx, service } = this;
192192
// 显示出错或成功信息
193193
async function showMessage(msg, data, isSuccess) {
194-
data = data || req.body;
194+
data = data || ctx.request.body;
195195
const user = {
196196
loginname: data.loginname,
197197
email: data.email,
@@ -201,16 +201,19 @@ class UserController extends Controller {
201201
weibo: data.weibo,
202202
accessToken: data.accessToken,
203203
};
204+
204205
if (isSuccess) {
205206
user.success = msg;
206207
} else {
207208
user.error = msg;
208209
}
210+
209211
return await ctx.render('user/setting', { user });
210212
}
211213

212214
// post
213-
const { body, body: { action } } = req;
215+
const { body } = ctx.request;
216+
const action = body.action;
214217
if (action === 'change_setting') {
215218
const url = validator.trim(body.url);
216219
const location = validator.trim(body.location);
@@ -227,8 +230,8 @@ class UserController extends Controller {
227230
}
228231

229232
if (action === 'change_password') {
230-
const oldPass = validator.trim(req.body.old_pass);
231-
const newPass = validator.trim(req.body.new_pass);
233+
const oldPass = validator.trim(body.old_pass);
234+
const newPass = validator.trim(body.new_pass);
232235
if (!oldPass || !newPass) {
233236
return showMessage('旧密码或新密码不得为空');
234237
}
@@ -247,10 +250,13 @@ class UserController extends Controller {
247250
}
248251

249252
async toggleStar() {
250-
const { ctx, ctx: { request: req }, service } = this;
251-
const { body } = req;
252-
const user_id = body.user_id;
253+
const { ctx, service } = this;
254+
const user_id = ctx.request.body.user_id;
253255
const user = await service.user.getUserById(user_id);
256+
if (!user) {
257+
ctx.body = { status: 'failed', message: '用户不存在' };
258+
return;
259+
}
254260

255261
user.is_star = !user.is_star;
256262
await user.save();
@@ -259,8 +265,8 @@ class UserController extends Controller {
259265
}
260266

261267
async block() {
262-
const { ctx, ctx: { request: req }, service } = this;
263-
const { body: { action } } = req;
268+
const { ctx, service } = this;
269+
const action = ctx.request.body.action;
264270
const loginname = ctx.params.name;
265271
const user = await service.user.getUserByLoginName(loginname);
266272

app/middleware/block_user.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
module.exports = () => {
4+
5+
return async function blockUser(ctx, next) {
6+
if (ctx.path === '/signout') {
7+
await next();
8+
return;
9+
}
10+
11+
if (ctx.user && ctx.user.is_block && ctx.method !== 'GET') {
12+
ctx.status = 403;
13+
ctx.body = '您已被管理员屏蔽了。有疑问请联系 @alsotang。';
14+
return;
15+
}
16+
17+
await next();
18+
};
19+
};

app/router.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ module.exports = app => {
2121

2222
// sign controller
2323
if (config.allow_sign_up) {
24-
router.get('/signup', sign.showSignup); // 跳转到注册页面
25-
router.post('/signup', sign.signup); // 提交注册信息
24+
// 跳转到注册页面
25+
router.get('/signup', sign.showSignup);
26+
// 提交注册信息
27+
router.post('/signup', sign.signup);
2628
} else {
2729
// 进行github验证
28-
router.get('/signup', async function() {
29-
this.ctx.redirect('/auth/github');
30-
});
30+
router.redirect('/singup', '/auth/github');
3131
}
3232

3333
const localStrategy = app.passport.authenticate('local', {

config/config.default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = appInfo => {
2121
config.session_secret = 'node_club_secret'; // 务必修改
2222

2323
// add your config here
24-
config.middleware = [ 'locals', 'authUser', 'errorPage' ];
24+
config.middleware = [ 'locals', 'authUser', 'blockUser', 'errorPage' ];
2525

2626
config.authUser = {
2727
enable: true,

test/app/controller/user.test.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('test/app/controller/user.test.js', () => {
146146
assert(/<strong>([\S\s]+)<\/strong>/g.exec(text)[1] === '需要管理员权限。');
147147
}
148148

149-
async function handleAdminPost(url, body, cb) {
149+
async function handleAdminPost(url, body) {
150150
// const adminName = Object.keys(app.config.admins)[0];
151151
// let admin = await ctx.service.user.getUserByLoginName(adminName);
152152
// if (!admin) {
@@ -162,8 +162,7 @@ describe('test/app/controller/user.test.js', () => {
162162
.send(body);
163163
assert(res.status === 200);
164164
assert(res.body.status === 'success');
165-
const updatedUser = await ctx.service.user.getUserById(user._id);
166-
cb(updatedUser);
165+
return await ctx.service.user.getUserById(user._id);
167166
}
168167

169168
it('should POST /passport/local set cookies', async () => {
@@ -217,42 +216,41 @@ describe('test/app/controller/user.test.js', () => {
217216

218217

219218
it('should POST /user/set_star ok', async () => {
220-
await handleAdminPost('/user/set_star', { user_id: user._id }, user => {
221-
assert(user.is_star === true);
222-
});
219+
const result = await handleAdminPost('/user/set_star', { user_id: user._id });
220+
assert(result.is_star === true);
223221
});
224222

225223
it('should POST /user/cancel_star ok', async () => {
226-
await handleAdminPost('/user/cancel_star', { user_id: user._id }, user => {
227-
assert(user.is_star === false);
228-
});
224+
const result = await handleAdminPost('/user/cancel_star', { user_id: user._id });
225+
assert(result.is_star === false);
229226
});
230227

231228
it('should POST /user/:name/block no_admin reject', async () => {
232229
await handleUserPost(`/user/${user.loginname}/block`);
233230
});
234231

235232
it('should POST /user/:name/block set block ok', async () => {
236-
await handleAdminPost(`/user/${user.loginname}/block`, { action: 'set_block' }, user => {
237-
assert(user.is_block === true);
233+
const result = await handleAdminPost(`/user/${user.loginname}/block`, {
234+
action: 'set_block',
238235
});
236+
assert(result.is_block === true);
239237
});
240238

241239
it('should POST /user/:name/block cancel block ok', async () => {
242-
await handleAdminPost(`/user/${user.loginname}/block`, { action: 'cancel_block' }, user => {
243-
assert(user.is_block === false);
240+
const result = await handleAdminPost(`/user/${user.loginname}/block`, {
241+
action: 'cancel_block',
244242
});
243+
assert(result.is_block === false);
245244
});
246245

247246
it('should POST /user/:name/delete_all no_admin reject', async () => {
248247
await handleUserPost(`/user/${user.loginname}/delete_all`);
249248
});
250249

251250
it('should POST /user/:name/delete_all ok', async () => {
252-
await handleAdminPost(`/user/${user.loginname}/delete_all`, {}, user => {
253-
assert(user);
254-
// TODO: Check topics and replies by service method.
255-
});
251+
const result = await handleAdminPost(`/user/${user.loginname}/delete_all`, {});
252+
assert(result);
253+
// TODO: Check topics and replies by service method.
256254
});
257255
});
258256

0 commit comments

Comments
 (0)