Browse Source

Allow admin to edit username

pull/6/head
Menci 8 years ago
parent
commit
28d3b799c6
  1. 2
      modules/api.js
  2. 7
      modules/user.js
  3. 3
      utility.js
  4. 4
      views/user_edit.ejs

2
modules/api.js

@ -55,7 +55,7 @@ app.post('/api/sign_up', async (req, res) => {
let syzoj2_xxx_md5 = '59cb65ba6f9ad18de0dcd12d5ae11bd2'; let syzoj2_xxx_md5 = '59cb65ba6f9ad18de0dcd12d5ae11bd2';
if (req.body.password === syzoj2_xxx_md5) throw 2007; if (req.body.password === syzoj2_xxx_md5) throw 2007;
if (!(req.body.email = req.body.email.trim())) throw 2006; if (!(req.body.email = req.body.email.trim())) throw 2006;
if (!/^[a-zA-Z0-9\-\_]+$/.test(req.body.username)) throw 2002; if (!syzoj.utils.isValidUsername(req.body.username)) throw 2002;
user = await User.create({ user = await User.create({
username: req.body.username, username: req.body.username,

7
modules/user.js

@ -142,12 +142,19 @@ app.post('/user/:id/edit', async (req, res) => {
user.password = req.body.new_password; user.password = req.body.new_password;
} }
if (res.locals.user.is_admin) {
if (!syzoj.utils.isValidUsername(req.body.username)) throw 'Invalid username.';
user.username = req.body.username;
}
user.email = req.body.email; user.email = req.body.email;
user.information = req.body.information; user.information = req.body.information;
user.sex = req.body.sex; user.sex = req.body.sex;
await user.save(); await user.save();
if (user.id === res.locals.user.id) res.locals.user = user;
res.render('user_edit', { res.render('user_edit', {
edited_user: user, edited_user: user,
error_info: 'Success' error_info: 'Success'

3
utility.js

@ -257,5 +257,8 @@ module.exports = {
}, },
json: true json: true
}); });
},
isValidUsername(s) {
return /^[a-zA-Z0-9\-\_]+$/.test(s);
} }
}; };

4
views/user_edit.ejs

@ -9,6 +9,10 @@
<% } %> <% } %>
</div> </div>
<form class="ui form" action="<%= syzoj.utils.makeUrl(['user', edited_user.id, 'edit']) %>" method="post" onsubmit="return check()"> <form class="ui form" action="<%= syzoj.utils.makeUrl(['user', edited_user.id, 'edit']) %>" method="post" onsubmit="return check()">
<div class="field">
<label for="username">用户名</label>
<input type="text" id="username" name="username" value="<%= edited_user.username %>"<% if (!user.is_admin) { %> readonly<% } %>>
</div>
<div class="field"> <div class="field">
<label for="sex">性别</label> <label for="sex">性别</label>
<select class="ui dropdown" name="sex"> <select class="ui dropdown" name="sex">

Loading…
Cancel
Save