Files
leanote/app/service/UserService.go

497 lines
14 KiB
Go
Raw Normal View History

2014-05-07 13:06:24 +08:00
package service
import (
"github.com/leanote/leanote/app/db"
2015-11-13 17:58:41 +08:00
"github.com/leanote/leanote/app/info"
2014-05-07 13:06:24 +08:00
. "github.com/leanote/leanote/app/lea"
"gopkg.in/mgo.v2/bson"
2014-05-07 13:06:24 +08:00
"strings"
2015-11-13 17:58:41 +08:00
"time"
2014-05-07 13:06:24 +08:00
)
type UserService struct {
}
2015-03-31 14:27:26 +08:00
// 自增Usn
// 每次notebook,note添加, 修改, 删除, 都要修改
func (this *UserService) IncrUsn(userId string) int {
user := info.User{}
query := bson.M{"_id": bson.ObjectIdHex(userId)}
db.GetByQWithFields(db.Users, query, []string{"Usn"}, &user)
usn := user.Usn
usn += 1
Log("inc Usn")
db.UpdateByQField(db.Users, query, "Usn", usn)
return usn
2015-11-13 17:58:41 +08:00
// return db.Update(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, bson.M{"$inc": bson.M{"ReadNum": 1}})
2015-03-31 14:27:26 +08:00
}
func (this *UserService) GetUsn(userId string) int {
user := info.User{}
query := bson.M{"_id": bson.ObjectIdHex(userId)}
db.GetByQWithFields(db.Users, query, []string{"Usn"}, &user)
return user.Usn
}
2014-05-07 13:06:24 +08:00
// 添加用户
func (this *UserService) AddUser(user info.User) bool {
if user.UserId == "" {
user.UserId = bson.NewObjectId()
}
user.CreatedTime = time.Now()
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
if user.Email != "" {
user.Email = strings.ToLower(user.Email)
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 发送验证邮箱
go func() {
2014-10-22 16:20:45 +08:00
emailService.RegisterSendActiveEmail(user, user.Email)
// 发送给我 life@leanote.com
2015-11-28 14:35:46 +08:00
// emailService.SendEmail("life@leanote.com", "新增用户", "{header}用户名"+user.Email+"{footer}")
2015-11-13 17:58:41 +08:00
}()
2014-05-07 13:06:24 +08:00
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
return db.Insert(db.Users, user)
}
// 通过email得到userId
func (this *UserService) GetUserId(email string) string {
email = strings.ToLower(email)
user := info.User{}
db.GetByQ(db.Users, bson.M{"Email": email}, &user)
return user.UserId.Hex()
}
2014-11-12 17:32:03 +08:00
// 得到用户名
func (this *UserService) GetUsername(userId string) string {
user := info.User{}
db.GetByQWithFields(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, []string{"Username"}, &user)
return user.Username
}
// 得到用户名
func (this *UserService) GetUsernameById(userId bson.ObjectId) string {
user := info.User{}
db.GetByQWithFields(db.Users, bson.M{"_id": userId}, []string{"Username"}, &user)
return user.Username
}
2014-05-07 13:06:24 +08:00
// 是否存在该用户 email
func (this *UserService) IsExistsUser(email string) bool {
if this.GetUserId(email) == "" {
return false
}
return true
}
// 是否存在该用户 username
func (this *UserService) IsExistsUserByUsername(username string) bool {
return db.Count(db.Users, bson.M{"Username": username}) >= 1
}
// 得到用户信息, userId, username, email
func (this *UserService) GetUserInfoByAny(idEmailUsername string) info.User {
if IsObjectId(idEmailUsername) {
return this.GetUserInfo(idEmailUsername)
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
if strings.Contains(idEmailUsername, "@") {
return this.GetUserInfoByEmail(idEmailUsername)
}
2015-11-13 17:58:41 +08:00
2014-09-15 19:37:24 +08:00
// username
2014-05-07 13:06:24 +08:00
return this.GetUserInfoByUsername(idEmailUsername)
}
2014-10-22 16:20:45 +08:00
func (this *UserService) setUserLogo(user *info.User) {
// Logo路径问题, 有些有http: 有些没有
if user.Logo == "" {
user.Logo = "images/blog/default_avatar.png"
}
if user.Logo != "" && !strings.HasPrefix(user.Logo, "http") {
user.Logo = strings.Trim(user.Logo, "/")
user.Logo = "/" + user.Logo
2015-11-13 17:58:41 +08:00
}
2014-10-22 16:20:45 +08:00
}
// 仅得到用户
func (this *UserService) GetUser(userId string) info.User {
user := info.User{}
db.Get(db.Users, userId, &user)
return user
}
2014-05-07 13:06:24 +08:00
// 得到用户信息 userId
func (this *UserService) GetUserInfo(userId string) info.User {
user := info.User{}
db.Get(db.Users, userId, &user)
2014-10-22 16:20:45 +08:00
// Logo路径问题, 有些有http: 有些没有
this.setUserLogo(&user)
2014-05-07 13:06:24 +08:00
return user
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 得到用户信息 email
func (this *UserService) GetUserInfoByEmail(email string) info.User {
user := info.User{}
db.GetByQ(db.Users, bson.M{"Email": email}, &user)
2015-03-31 14:27:26 +08:00
// Logo路径问题, 有些有http: 有些没有
this.setUserLogo(&user)
2014-05-07 13:06:24 +08:00
return user
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 得到用户信息 username
func (this *UserService) GetUserInfoByUsername(username string) info.User {
user := info.User{}
2014-09-15 19:37:24 +08:00
username = strings.ToLower(username)
2014-05-07 13:06:24 +08:00
db.GetByQ(db.Users, bson.M{"Username": username}, &user)
2015-03-31 14:27:26 +08:00
// Logo路径问题, 有些有http: 有些没有
this.setUserLogo(&user)
2014-05-07 13:06:24 +08:00
return user
}
func (this *UserService) GetUserInfoByThirdUserId(thirdUserId string) info.User {
user := info.User{}
db.GetByQ(db.Users, bson.M{"ThirdUserId": thirdUserId}, &user)
this.setUserLogo(&user)
2014-05-07 13:06:24 +08:00
return user
}
func (this *UserService) ListUserInfosByUserIds(userIds []bson.ObjectId) []info.User {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
return users
}
2014-10-22 16:20:45 +08:00
func (this *UserService) ListUserInfosByEmails(emails []string) []info.User {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"Email": bson.M{"$in": emails}}, &users)
return users
}
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
// 用户信息即可
func (this *UserService) MapUserInfoByUserIds(userIds []bson.ObjectId) map[bson.ObjectId]info.User {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
2015-11-13 17:58:41 +08:00
userMap := make(map[bson.ObjectId]info.User, len(users))
for _, user := range users {
2014-10-22 16:20:45 +08:00
this.setUserLogo(&user)
userMap[user.UserId] = user
}
return userMap
}
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
// 用户信息和博客设置信息
func (this *UserService) MapUserInfoAndBlogInfosByUserIds(userIds []bson.ObjectId) map[bson.ObjectId]info.User {
return this.MapUserInfoByUserIds(userIds)
}
2014-05-07 13:06:24 +08:00
2014-11-09 16:24:19 +08:00
// 返回info.UserAndBlog
func (this *UserService) MapUserAndBlogByUserIds(userIds []bson.ObjectId) map[string]info.UserAndBlog {
users := []info.User{}
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
userBlogs := []info.UserBlog{}
db.ListByQ(db.UserBlogs, bson.M{"_id": bson.M{"$in": userIds}}, &userBlogs)
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
userBlogMap := make(map[bson.ObjectId]info.UserBlog, len(userBlogs))
for _, user := range userBlogs {
userBlogMap[user.UserId] = user
}
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
userAndBlogMap := make(map[string]info.UserAndBlog, len(users))
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
for _, user := range users {
this.setUserLogo(&user)
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
userBlog, ok := userBlogMap[user.UserId]
if !ok {
continue
}
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
userAndBlogMap[user.UserId.Hex()] = info.UserAndBlog{
2015-11-13 17:58:41 +08:00
UserId: user.UserId,
Username: user.Username,
Email: user.Email,
Logo: user.Logo,
2014-11-09 16:24:19 +08:00
BlogTitle: userBlog.Title,
2015-11-13 17:58:41 +08:00
BlogLogo: userBlog.Logo,
BlogUrl: blogService.GetUserBlogUrl(&userBlog, user.Username),
2014-11-09 16:24:19 +08:00
}
}
return userAndBlogMap
}
// 得到用户信息+博客主页
func (this *UserService) GetUserAndBlogUrl(userId string) info.UserAndBlogUrl {
user := this.GetUserInfo(userId)
userBlog := blogService.GetUserBlog(userId)
2015-11-13 17:58:41 +08:00
blogUrls := blogService.GetBlogUrls(&userBlog, &user)
2015-11-13 17:58:41 +08:00
return info.UserAndBlogUrl{
2015-11-13 17:58:41 +08:00
User: user,
BlogUrl: blogUrls.IndexUrl,
PostUrl: blogUrls.PostUrl,
}
}
2014-11-09 16:24:19 +08:00
// 得到userAndBlog公开信息
func (this *UserService) GetUserAndBlog(userId string) info.UserAndBlog {
user := this.GetUserInfo(userId)
userBlog := blogService.GetUserBlog(userId)
return info.UserAndBlog{
UserId: user.UserId,
Username: user.Username,
Email: user.Email,
Logo: user.Logo,
2014-11-09 16:24:19 +08:00
BlogTitle: userBlog.Title,
BlogLogo: userBlog.Logo,
BlogUrl: blogService.GetUserBlogUrl(&userBlog, user.Username),
BlogUrls: blogService.GetBlogUrls(&userBlog, &user),
2014-11-09 16:24:19 +08:00
}
}
2014-05-07 13:06:24 +08:00
// 通过ids得到users, 按id的顺序组织users
func (this *UserService) GetUserInfosOrderBySeq(userIds []bson.ObjectId) []info.User {
users := []info.User{}
2015-11-13 17:58:41 +08:00
db.ListByQ(db.Users, bson.M{"_id": bson.M{"$in": userIds}}, &users)
2014-05-07 13:06:24 +08:00
usersMap := map[bson.ObjectId]info.User{}
for _, user := range users {
usersMap[user.UserId] = user
}
2015-11-13 17:58:41 +08:00
2014-11-09 16:24:19 +08:00
hasAppend := map[bson.ObjectId]bool{} // 为了防止userIds有重复的
2015-11-13 17:58:41 +08:00
users2 := []info.User{}
2014-05-07 13:06:24 +08:00
for _, userId := range userIds {
2014-11-09 16:24:19 +08:00
if user, ok := usersMap[userId]; ok && !hasAppend[userId] {
hasAppend[userId] = true
2014-05-07 13:06:24 +08:00
users2 = append(users2, user)
}
}
return users2
}
2015-09-06 23:16:56 +08:00
// 使用email(username), 得到用户信息
func (this *UserService) GetUserInfoByName(emailOrUsername string) info.User {
emailOrUsername = strings.ToLower(emailOrUsername)
2015-11-13 17:58:41 +08:00
2015-09-06 23:16:56 +08:00
user := info.User{}
if strings.Contains(emailOrUsername, "@") {
db.GetByQ(db.Users, bson.M{"Email": emailOrUsername}, &user)
} else {
db.GetByQ(db.Users, bson.M{"Username": emailOrUsername}, &user)
}
this.setUserLogo(&user)
return user
}
2014-05-07 13:06:24 +08:00
// 更新username
func (this *UserService) UpdateUsername(userId, username string) (bool, string) {
if userId == "" || username == "" || username == "admin" { // admin用户是内置的, 不能设置
2014-11-09 16:24:19 +08:00
return false, "usernameIsExisted"
2014-05-07 13:06:24 +08:00
}
usernameRaw := username // 原先的, 可能是同一个, 但有大小写
username = strings.ToLower(username)
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 先判断是否存在
userIdO := bson.ObjectIdHex(userId)
if db.Has(db.Users, bson.M{"Username": username, "_id": bson.M{"$ne": userIdO}}) {
2014-11-09 16:24:19 +08:00
return false, "usernameIsExisted"
2014-05-07 13:06:24 +08:00
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
ok := db.UpdateByQMap(db.Users, bson.M{"_id": userIdO}, bson.M{"Username": username, "UsernameRaw": usernameRaw})
return ok, ""
}
2014-10-22 16:20:45 +08:00
// 修改头像
2015-11-13 17:58:41 +08:00
func (this *UserService) UpdateAvatar(userId, avatarPath string) bool {
2014-10-22 16:20:45 +08:00
userIdO := bson.ObjectIdHex(userId)
return db.UpdateByQField(db.Users, bson.M{"_id": userIdO}, "Logo", avatarPath)
}
2014-05-07 13:06:24 +08:00
//----------------------
// 已经登录了的用户修改密码
func (this *UserService) UpdatePwd(userId, oldPwd, pwd string) (bool, string) {
userInfo := this.GetUserInfo(userId)
if !ComparePwd(oldPwd, userInfo.Pwd) {
2014-11-09 16:24:19 +08:00
return false, "oldPasswordError"
2014-05-07 13:06:24 +08:00
}
passwd := GenPwd(pwd)
if passwd == "" {
return false, "GenerateHash error"
}
ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", passwd)
2014-05-07 13:06:24 +08:00
return ok, ""
}
2014-11-09 16:54:56 +08:00
// 管理员重置密码
func (this *UserService) ResetPwd(adminUserId, userId, pwd string) (ok bool, msg string) {
if configService.GetAdminUserId() != adminUserId {
2014-11-09 16:54:56 +08:00
return
}
2015-11-13 17:58:41 +08:00
passwd := GenPwd(pwd)
if passwd == "" {
return false, "GenerateHash error"
}
ok = db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Pwd", passwd)
2014-11-09 16:54:56 +08:00
return
}
2014-05-07 13:06:24 +08:00
// 修改主题
2015-11-13 17:58:41 +08:00
func (this *UserService) UpdateTheme(userId, theme string) bool {
2014-05-07 13:06:24 +08:00
ok := db.UpdateByQField(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, "Theme", theme)
return ok
}
2014-11-12 17:32:03 +08:00
// 帐户类型设置
2015-11-13 17:58:41 +08:00
func (this *UserService) UpdateAccount(userId, accountType string, accountStartTime, accountEndTime time.Time,
2014-11-12 17:32:03 +08:00
maxImageNum, maxImageSize, maxAttachNum, maxAttachSize, maxPerAttachSize int) bool {
return db.UpdateByQI(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, info.UserAccount{
2015-11-13 17:58:41 +08:00
AccountType: accountType,
AccountStartTime: accountStartTime,
AccountEndTime: accountEndTime,
MaxImageNum: maxImageNum,
MaxImageSize: maxImageSize,
MaxAttachNum: maxAttachNum,
MaxAttachSize: maxAttachSize,
MaxPerAttachSize: maxPerAttachSize,
})
2014-11-12 17:32:03 +08:00
}
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
//---------------
// 修改email
// 注册后验证邮箱
func (this *UserService) ActiveEmail(token string) (ok bool, msg, email string) {
tokenInfo := info.Token{}
if ok, msg, tokenInfo = tokenService.VerifyToken(token, info.TokenActiveEmail); ok {
// 修改之后的邮箱
email = tokenInfo.Email
userInfo := this.GetUserInfoByEmail(email)
if userInfo.UserId == "" {
ok = false
msg = "不存在该用户"
2015-11-13 17:58:41 +08:00
return
2014-05-07 13:06:24 +08:00
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 修改之, 并将verified = true
ok = db.UpdateByQMap(db.Users, bson.M{"_id": userInfo.UserId}, bson.M{"Verified": true})
return
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
ok = false
msg = "该链接已过期"
return
}
// 修改邮箱
// 在此之前, 验证token是否过期
// 验证email是否有人注册了
func (this *UserService) UpdateEmail(token string) (ok bool, msg, email string) {
tokenInfo := info.Token{}
if ok, msg, tokenInfo = tokenService.VerifyToken(token, info.TokenUpdateEmail); ok {
// 修改之后的邮箱
2015-10-10 14:32:30 +08:00
email = strings.ToLower(tokenInfo.Email)
2014-05-07 13:06:24 +08:00
// 先验证该email是否被注册了
if userService.IsExistsUser(email) {
ok = false
msg = "该邮箱已注册"
return
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 修改之, 并将verified = true
ok = db.UpdateByQMap(db.Users, bson.M{"_id": tokenInfo.UserId}, bson.M{"Email": email, "Verified": true})
return
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
ok = false
msg = "该链接已过期"
return
}
//------------
// 偏好设置
// 宽度
2015-11-13 17:58:41 +08:00
func (this *UserService) UpdateColumnWidth(userId string, notebookWidth, noteListWidth, mdEditorWidth int) bool {
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "MdEditorWidth": mdEditorWidth})
2014-05-07 13:06:24 +08:00
}
2015-11-13 17:58:41 +08:00
2014-05-07 13:06:24 +08:00
// 左侧是否隐藏
2015-11-13 17:58:41 +08:00
func (this *UserService) UpdateLeftIsMin(userId string, leftIsMin bool) bool {
2014-05-07 13:06:24 +08:00
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"LeftIsMin": leftIsMin})
}
//-------------
// user admin
func (this *UserService) ListUsers(pageNumber, pageSize int, sortField string, isAsc bool, email string) (page info.Page, users []info.User) {
users = []info.User{}
skipNum, sortFieldR := parsePageAndSort(pageNumber, pageSize, sortField, isAsc)
query := bson.M{}
if email != "" {
2014-11-09 16:54:56 +08:00
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
}
2015-11-13 17:58:41 +08:00
q := db.Users.Find(query)
// 总记录数
count, _ := q.Count()
// 列表
q.Sort(sortFieldR).
Skip(skipNum).
Limit(pageSize).
All(&users)
page = info.NewPage(pageNumber, pageSize, count, nil)
return
2014-10-22 16:20:45 +08:00
}
func (this *UserService) GetAllUserByFilter(userFilterEmail, userFilterWhiteList, userFilterBlackList string, verified bool) []info.User {
query := bson.M{}
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
if verified {
query["Verified"] = true
}
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
orQ := []bson.M{}
if userFilterEmail != "" {
2015-11-13 17:58:41 +08:00
orQ = append(orQ, bson.M{"Email": bson.M{"$regex": bson.RegEx{".*?" + userFilterEmail + ".*", "i"}}},
2014-10-22 16:20:45 +08:00
bson.M{"Username": bson.M{"$regex": bson.RegEx{".*?" + userFilterEmail + ".*", "i"}}},
)
}
2015-11-13 17:58:41 +08:00
if userFilterWhiteList != "" {
2014-10-22 16:20:45 +08:00
userFilterWhiteList = strings.Replace(userFilterWhiteList, "\r", "", -1)
2015-11-13 17:58:41 +08:00
emails := strings.Split(userFilterWhiteList, "\n")
2014-10-22 16:20:45 +08:00
orQ = append(orQ, bson.M{"Email": bson.M{"$in": emails}})
}
if len(orQ) > 0 {
query["$or"] = orQ
}
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
emailQ := bson.M{}
2015-11-13 17:58:41 +08:00
if userFilterBlackList != "" {
2014-10-22 16:20:45 +08:00
userFilterWhiteList = strings.Replace(userFilterBlackList, "\r", "", -1)
2015-11-13 17:58:41 +08:00
bEmails := strings.Split(userFilterBlackList, "\n")
2014-10-22 16:20:45 +08:00
emailQ["$nin"] = bEmails
query["Email"] = emailQ
}
LogJ(query)
users := []info.User{}
2015-11-13 17:58:41 +08:00
q := db.Users.Find(query)
2014-10-22 16:20:45 +08:00
q.All(&users)
Log(len(users))
2015-11-13 17:58:41 +08:00
2014-10-22 16:20:45 +08:00
return users
}
2015-11-13 17:58:41 +08:00
// 统计
2014-10-22 16:20:45 +08:00
func (this *UserService) CountUser() int {
return db.Count(db.Users, bson.M{})
}