diff --git a/my/model/user.js b/my/model/user.js index a444b35..7899ff7 100644 --- a/my/model/user.js +++ b/my/model/user.js @@ -5,7 +5,8 @@ var userSchema = new mongoose.Schema({ name: String, password: String, pic: String, - description: String + description: String, + verified: Boolean }); userSchema.statics.get = function get(username, callback) { diff --git a/public/stylesheets/admin.less b/public/stylesheets/admin.less index 4006628..b47589b 100644 --- a/public/stylesheets/admin.less +++ b/public/stylesheets/admin.less @@ -38,3 +38,9 @@ .user-pic { width: 64px; } + +#user { + .description { + width: 50%; + } +} diff --git a/routes/admin/index.js b/routes/admin/index.js index 463e5da..fd5a612 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -105,6 +105,17 @@ module.exports = { 'view/': { } + }, + + 'users/': { + all: user.checkLogin, + get: user.browse, + + 'edit/:id': { + all: user.checkLogin, + get: user.updateView, + post: user.update + } } }; diff --git a/routes/admin/user.js b/routes/admin/user.js index 461fafe..ed567c0 100644 --- a/routes/admin/user.js +++ b/routes/admin/user.js @@ -22,9 +22,15 @@ exports.checkLogin = function checkLogin(req, res, next) { * Render the register page. */ exports.registerView = function registerView(req, res) { - res.render('admin/register', { - title: '注册' - }); + if (! req.session.user) { + res.render('admin/register', { + title: '注册' + }); + } else { + res.render('admin/user-edit-page', { + title: '添加新用户' + }); + } }; /* @@ -36,12 +42,13 @@ exports.register = function register(req, res) { * First check the parameters: user[name], user[pass], user[rpass] */ var user = new User(req.body.user); + user.verified = false; user.save(function(err) { if (!err) { res.render('done', { title: '完成', link: '/admin/login', - message: '已经成功登陆。' + message: '成功注册!请等候审核。' }); } else { res.render('error', { @@ -80,7 +87,7 @@ exports.login = function login(req, res) { var username = req.body.user.name; var password = req.body.user.pass; User.get(username, function(err, user) { - if (user && username == user.name && password == user.password) { + if (user && username == user.name && password == user.password && user.verified) { req.session.user = user.toJSON(); req.session.user.loginDate = new Date; res.render('done', { @@ -89,12 +96,16 @@ exports.login = function login(req, res) { link: '/admin/' }); } else { + var message = '用户名或密码错误!'; + if (! user.verified) { + message = '用户尚未激活。'; + } res.render('error', { title: '错误!', - message: '用户名或密码错误。', + message: message, link: '/admin/register' }); - } + } }); }; @@ -109,3 +120,99 @@ exports.logout = function logout(req, res) { message: '已经安全退出本系统。' }); }; + +exports.browse = function browse(req, res) { + User.find({}, function(err, users) { + if (!err) { + res.render('admin/user-browse-page', { + title: '用户管理', + users: users + }); + } else { + res.render('error', { + message: err, + link: '/admin/' + }); + } + }); +}; + +exports.updateView = function updateView(req, res) { + var id = req.params.id; + User.findOne({ + _id: id + }, function(err, user) { + if (err) { + res.render('error', { + message: err, + link: '/admin/users/' + }); + } else { + res.render('admin/user-edit-page', { + actionUrl: '/admin/users/edit/' + id, + title: '正在编辑用户“' + user.name + '”', + user: user + }); + } + }); +}; + +exports.update = function update(req, res) { + var id = req.params.id; + var pass = req.body.user.pass; + var rpass = req.body.user.rpass; + var updated = { + name: req.body.user.name, + description: req.body.user.description + }; + if (pass == rpass) { + updated.pass = pass; + } + if (req.body.user.verified) { + updated.verified = req.body.user.verified; + } + + User.findOneAndUpdate({ + _id: id + }, updated, function(err, user) { + if (err) { + res.render('error', { + message: err, + link: '/admin/users/' + }); + } else { + if (id == req.session.user._id) { + req.session.user = user; + } + res.render('done', { + message: '完成编辑!', + link: '/admin/users/' + }); + } + }); +}; + +exports.verify = function verify(req, res) { + var id = req.params.id; + var updated = {}; + if (req.body.user.verified) { + updated.verified = req.body.user.verified; + } + + User.findOneAndUpdate({ + _id: id + }, updated, function(err, user) { + if (err) { + res.render('error', { + message: err, + link: '/admin/users/' + }); + } else { + res.render('done', { + message: '完成激活!', + link: '/admin/users/' + }); + } + }); +}; + diff --git a/views/admin/register.jade b/views/admin/register.jade index 65f5321..3a8c025 100644 --- a/views/admin/register.jade +++ b/views/admin/register.jade @@ -2,37 +2,7 @@ extends layout block main-stage .main-stage-inner - form(action='/admin/register', method='POST').form-horizontal - fieldset - legend 注册 - - .control-group - label(for='username').control-label 用户名 - .controls - input( - type='text' - name='user[name]' - placeholder='Email' - )#username - .control-group - label(for='password').control-label 密码 - .controls - input( - type='password' - name='user[pass]' - placeholder='Password' - )#password - .control-group - label(for='rpassword').control-label 重输密码 - .controls - input( - type='password' - name='user[rpass]' - placeholder='Re enter password' - )#rpassword - - .form-actions - button(type='submit').btn.btn-primary 提交 - | - button(type='reset').btn 重填 + .page-header + h1 注册 + include user-edit diff --git a/views/admin/user-browse-page.jade b/views/admin/user-browse-page.jade new file mode 100644 index 0000000..96d734d --- /dev/null +++ b/views/admin/user-browse-page.jade @@ -0,0 +1,10 @@ +extends user-layout + +block append breadcrumb + li + span.divider / + a(href='/admin/register'): i.icon-plus + +block admin-stage + include user-browse + diff --git a/views/admin/user-browse.jade b/views/admin/user-browse.jade new file mode 100644 index 0000000..ccb835c --- /dev/null +++ b/views/admin/user-browse.jade @@ -0,0 +1,30 @@ +.action-bar + a(href='/admin/register').btn.btn-success 注册新用户 + .clearfix + +table.table.table-hover#user + thead + tr + th 用户名 + th 描述 + th 激活状态 + th 动作 + + tbody + for user in users + tr + td.name= user.name + td.description= user.description ? user.description : '暂无描述。'; + td.user-state + if user.verified + span.label.label-success 已激活 + else + span.label.label-warning 未激活 + td + a(href='/admin/users/edit/'+user.id).btn.btn-mini 编辑 + | + a(href='/admin/users/del/').btn.btn-mini 删除 + unless user.verified + | + a(href='/admin/users/verify/').btn.btn-warning.btn-mini 激活 + diff --git a/views/admin/user-edit-page.jade b/views/admin/user-edit-page.jade new file mode 100644 index 0000000..ac0f89d --- /dev/null +++ b/views/admin/user-edit-page.jade @@ -0,0 +1,10 @@ +extends user-layout + +block append breadcrumb + li.active + span.divider / + span 编辑用户 + +block admin-stage + .main-stage-inner + include user-edit diff --git a/views/admin/user-edit.jade b/views/admin/user-edit.jade new file mode 100644 index 0000000..098718a --- /dev/null +++ b/views/admin/user-edit.jade @@ -0,0 +1,41 @@ +form(action=typeof actionUrl!='undefined'?actionUrl:'/admin/register', method='POST').form-horizontal + .control-group + label(for='username').control-label 用户名 + .controls + input( + type='text' + name='user[name]' + placeholder='Email' + value=typeof user != 'undefined'?user.name:'' + )#username.span6 + .control-group + label(for='password').control-label 密码 + .controls + input( + type='password' + name='user[pass]' + placeholder='Password' + )#password.span6 + .control-group + label(for='rpassword').control-label 重输密码 + .controls + input( + type='password' + name='user[rpass]' + placeholder='Re enter password' + )#rpassword.span6 + .control-group + label(for='description').control-label 描述 + .controls + textarea( + name='user[description]' + rows='5' + )#description.span6 + if typeof user != 'undefined' + | #{user.description} + + .form-actions + button(type='submit').btn.btn-primary 提交 + | + button(type='reset').btn 重填 + diff --git a/views/admin/user-layout.jade b/views/admin/user-layout.jade new file mode 100644 index 0000000..3e2ea2c --- /dev/null +++ b/views/admin/user-layout.jade @@ -0,0 +1,6 @@ +extends layout + +block append breadcrumb + li + span.divider / + a(href='/admin/users/') 用户管理