Skip to content

Commit

Permalink
[Feature] User management is now available.
Browse files Browse the repository at this point in the history
  • Loading branch information
yfwz100 committed Jan 18, 2013
1 parent 386ffc8 commit 53a6684
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 41 deletions.
3 changes: 2 additions & 1 deletion my/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions public/stylesheets/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@
.user-pic {
width: 64px;
}

#user {
.description {
width: 50%;
}
}
11 changes: 11 additions & 0 deletions routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

};
121 changes: 114 additions & 7 deletions routes/admin/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '添加新用户'
});
}
};

/*
Expand All @@ -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', {
Expand Down Expand Up @@ -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', {
Expand All @@ -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'
});
}
}
});
};

Expand All @@ -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/'
});
}
});
};

36 changes: 3 additions & 33 deletions views/admin/register.jade
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions views/admin/user-browse-page.jade
Original file line number Diff line number Diff line change
@@ -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

30 changes: 30 additions & 0 deletions views/admin/user-browse.jade
Original file line number Diff line number Diff line change
@@ -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 激活

10 changes: 10 additions & 0 deletions views/admin/user-edit-page.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends user-layout

block append breadcrumb
li.active
span.divider /
span 编辑用户

block admin-stage
.main-stage-inner
include user-edit
41 changes: 41 additions & 0 deletions views/admin/user-edit.jade
Original file line number Diff line number Diff line change
@@ -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 重填

6 changes: 6 additions & 0 deletions views/admin/user-layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends layout

block append breadcrumb
li
span.divider /
a(href='/admin/users/') 用户管理

0 comments on commit 53a6684

Please sign in to comment.