From 2a457d6027b4db3082a3b05b13c4d05ac6923ba1 Mon Sep 17 00:00:00 2001
From: life <lifephp@gmail.com>
Date: Sun, 9 Nov 2014 16:54:56 +0800
Subject: [PATCH] reset password

---
 app/controllers/admin/AdminUserController.go | 15 +++---
 app/service/UserService.go                   | 30 +++++------
 app/views/admin/user/add.html                |  2 +-
 app/views/admin/user/list.html               | 56 ++++++++------------
 app/views/admin/user/reset_pwd.html          | 52 ++++++++++++++++++
 5 files changed, 98 insertions(+), 57 deletions(-)
 create mode 100644 app/views/admin/user/reset_pwd.html

diff --git a/app/controllers/admin/AdminUserController.go b/app/controllers/admin/AdminUserController.go
index 6f70079..e44eb4f 100644
--- a/app/controllers/admin/AdminUserController.go
+++ b/app/controllers/admin/AdminUserController.go
@@ -3,7 +3,7 @@ package admin
 import (
 	"github.com/revel/revel"
 	. "github.com/leanote/leanote/app/lea"
-	"time"
+//	"time"
 	"github.com/leanote/leanote/app/info"
 )
 
@@ -50,16 +50,17 @@ func (c AdminUser) Register(email, pwd string) revel.Result {
 }
 
 // 修改帐户
-func (c AdminUser) UpdateAccount(userId string) revel.Result {
+func (c AdminUser) ResetPwd(userId string) revel.Result {
 	userInfo := userService.GetUserInfo(userId)
 	c.RenderArgs["userInfo"] = userInfo
-	return c.RenderTemplate("admin/user/update_account.html");
+	return c.RenderTemplate("admin/user/reset_pwd.html");
 }
 
-func (c AdminUser) DoUpdateAccount(userId, accountType string, accountStartTime, accountEndTime string, maxImageNum, maxImageSize, maxAttachNum, maxAttachSize, maxPerAttachSize int) revel.Result {
+func (c AdminUser) DoResetPwd(userId, pwd string) revel.Result {
 	re := info.NewRe();
-	s, _ := time.Parse("2006-01-02 15:04:02", accountStartTime)
-	e, _ := time.Parse("2006-01-02 15:04:02", accountEndTime)
-	re.Ok = userService.UpdateAccount(userId, accountType, s, e, maxImageNum, maxImageSize, maxAttachNum, maxAttachSize, maxPerAttachSize )
+	if re.Ok, re.Msg = Vd("password", pwd); !re.Ok {
+		return c.RenderRe(re);
+	}
+	re.Ok, re.Msg = userService.ResetPwd(c.GetUserId(), userId, pwd)
 	return c.RenderRe(re)
 }
\ No newline at end of file
diff --git a/app/service/UserService.go b/app/service/UserService.go
index 3bce643..8b0ac4d 100644
--- a/app/service/UserService.go
+++ b/app/service/UserService.go
@@ -254,26 +254,22 @@ func (this *UserService) UpdatePwd(userId, oldPwd, pwd string) (bool, string) {
 	return ok, ""
 }
 
+// 管理员重置密码
+func (this *UserService) ResetPwd(adminUserId, userId, pwd string) (ok bool, msg string) {
+	adminInfo := this.GetUserInfoByAny(adminUsername)
+	if adminInfo.UserId.Hex() != adminUserId {
+		return
+	}
+	ok = db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", Md5(pwd))
+	return
+}
+
 // 修改主题
 func (this *UserService) UpdateTheme(userId, theme string) (bool) {
 	ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Theme", theme)
 	return ok
 }
 
-// 帐户类型设置
-func (this *UserService) UpdateAccount(userId, accountType string, accountStartTime, accountEndTime time.Time, 
-	maxImageNum, maxImageSize, maxAttachNum, maxAttachSize, maxPerAttachSize int) bool {
-	return db.UpdateByQI(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, info.UserAccount{
-			AccountType: accountType,
-			AccountStartTime: accountStartTime,
-			AccountEndTime: accountEndTime,
-			MaxImageNum: maxImageNum,
-			MaxImageSize: maxImageSize,
-			MaxAttachNum: maxAttachNum,
-			MaxAttachSize: maxAttachSize,
-			MaxPerAttachSize: maxPerAttachSize,
-		})
-}
 
 //---------------
 // 修改email
@@ -366,7 +362,11 @@ func (this *UserService) ListUsers(pageNumber, pageSize int, sortField string, i
 	skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
 	query := bson.M{}
 	if email != "" {
-		query["Email"] = bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}
+		orQ := []bson.M{
+			bson.M{"Email": bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}},
+			bson.M{"Username": bson.M{"$regex": bson.RegEx{".*?" + email + ".*", "i"}}},
+		}
+		query["$or"] = orQ
 	}
 	q := db.Users.Find(query);
 	// 总记录数
diff --git a/app/views/admin/user/add.html b/app/views/admin/user/add.html
index c7cfac3..9ef59ef 100644
--- a/app/views/admin/user/add.html
+++ b/app/views/admin/user/add.html
@@ -42,7 +42,7 @@ $(function() {
 			var t = this;
 			if($("#add_user_form").valid()) {
 				$(t).button('loading');
-				ajaxPost("/auth/doRegister", getFormJsonData("add_user_form"), function(ret){
+				ajaxPost("/adminUser/register", getFormJsonData("add_user_form"), function(ret){
 					$(t).button('reset')
 					if(!ret.Ok) {
 						art.alert(ret.Msg);
diff --git a/app/views/admin/user/list.html b/app/views/admin/user/list.html
index aee5a62..3c5e2ff 100644
--- a/app/views/admin/user/list.html
+++ b/app/views/admin/user/list.html
@@ -4,16 +4,8 @@
 <section class="panel panel-default">
 	<div class="row wrapper">
 		<div class="col-sm-5 m-b-xs">
-			<select class="input-sm form-control input-s-sm inline v-middle">
-				<option value="">
-					Bulk action
-				</option>
-				<option value="1">
-					Send Email
-				</option>
-			</select>
-			<button class="btn btn-sm btn-default bulk-btn">
-				Apply
+			<button class="btn btn-sm btn-default bulk-email">
+				Send Email
 			</button>
 		</div>
 		<div class="col-sm-4 m-b-xs">
@@ -21,7 +13,7 @@
 		</div>
 		<div class="col-sm-3">
 			<div class="input-group search-group">
-				<input type="text" class="input-sm form-control" placeholder="Email" id="keywords" value="{{.keywords}}" />
+				<input type="text" class="input-sm form-control" placeholder="Username or Email" id="keywords" value="{{.keywords}}" />
 				<span class="input-group-btn">
 					<button class="btn btn-sm btn-default" type="button" data-url="/adminUser/index">Search</button>
 				</span>
@@ -99,7 +91,8 @@
 						{{.CreatedTime|datetime}}
 					</td>
 					<td>
-						<a href="#" class="btn btn-default send-email" data-email="{{.Email}}">Send Email</a>
+						<a href="#" class="btn btn-sm btn-default reset-pwd" data-id="{{.UserId.Hex}}">Reset Password</a>
+						<a href="#" class="btn btn-sm btn-default send-email" data-email="{{.Email}}">Send Email</a>
 					</td>
 				</tr>
 				{{end}}
@@ -109,16 +102,8 @@
 	<footer class="panel-footer">
 		<div class="row">
 			<div class="col-sm-4 hidden-xs">
-				<select class="input-sm form-control input-s-sm inline v-middle">
-					<option value="">
-						Bulk action
-					</option>
-					<option value="1">
-						Send Email
-					</option>
-				</select>
-				<button class="btn btn-sm btn-default bulk-btn">
-					Apply
+				<button class="btn btn-sm btn-default bulk-email">
+					Send Email
 				</button>
 			</div>
 			
@@ -137,19 +122,22 @@ $(function() {
 	$(".send-email").click(function() {
 		openSendEmailDialog($(this).data("email"));
 	});
-	$(".bulk-btn").click(function() {
-		// email
-		if($(this).prev().val() == "1") {
-			var emails = [];
-			$(".ck:checked").each(function() {
-				emails.push($(this).data("email"));
-			});
-			if(emails.length == 0) {
-				art.alert("No user");
-				return;
-			}
-			openSendEmailDialog(emails.join(","));
+	$(".bulk-email").click(function() {
+		var emails = [];
+		$(".ck:checked").each(function() {
+			emails.push($(this).data("email"));
+		});
+		if(emails.length == 0) {
+			art.alert("No user");
+			return;
 		}
+		openSendEmailDialog(emails.join(","));
+	});
+	
+	// reset password
+	$(".reset-pwd").click(function() {
+		var id = $(this).data("id");
+		openDialog({width: 500,  url: "/adminUser/resetPwd?userId=" + id, title: "Reset Password"});
 	});
 });
 </script>
diff --git a/app/views/admin/user/reset_pwd.html b/app/views/admin/user/reset_pwd.html
new file mode 100644
index 0000000..da2b206
--- /dev/null
+++ b/app/views/admin/user/reset_pwd.html
@@ -0,0 +1,52 @@
+<div class="row" style="width: 500px;">
+<div class="col-sm-12">
+	<form id="dataForm">
+		<section class="panel panel-default">
+			<header class="panel-heading font-bold">Email</header>
+			<div class="panel-body">
+				<div class="form-group pull-in clearfix">
+					<div class="col-sm-12">
+						<label>New password</label>
+						<input id="pwd" type="password" class="form-control" data-rule-required="true" id="pwd" name="pwd" data-rule-minlength="6">
+					</div>
+				</div>
+				<div class="form-group pull-in clearfix">
+					<div class="col-sm-12">
+						<label>Confirm password</label>
+						<input type="password" class="form-control parsley-validated" data-rule-equalto="#pwd" data-rule-required="true" name="password2">
+					</div>
+				</div>
+			</div>
+			
+			<footer class="panel-footer text-right bg-light lter">
+				<button type="submit" id="resetPwdBtn" class="btn btn-success btn-s-xs">Submit</button>
+			</footer>
+		</section>
+	</form>
+</div>
+
+</div>
+
+<script src="/public/admin/js/jquery-validation-1.13.0/jquery.validate.js"></script>
+<script>
+$(function() {
+	var userId = "{{.userInfo.UserId.Hex}}";
+	init_validator("#dataForm");
+	$("#resetPwdBtn").click(function(e){
+		e.preventDefault();
+		var t = this;
+		if($("#dataForm").valid()) {
+			$(t).button('loading');
+			ajaxPost("/adminUser/doResetPwd", {userId: userId, pwd: $("#pwd").val()}, function(ret){
+				$(t).button('reset')
+				if(!ret.Ok) {
+					art.alert(ret.Msg);
+				} else {
+					art.tips("Success");
+					closeDialog();
+				}
+			});
+		}
+	});
+});
+</script>
\ No newline at end of file