forked from free-lancers/electricity_bill_calc_service
feat(user):基本完成用户重设密码,待测。
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/utils"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -92,3 +94,64 @@ func (_UserService) ProcessManagementUserLogin(username, password string) (*mode
|
||||
cache.CacheSession(session)
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (_UserService) InvalidUserPassword(uid string) (string, error) {
|
||||
user, err := repository.UserRepo.FindUserByID(uid)
|
||||
if user == nil && err != nil {
|
||||
return "", exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
verifyCode := utils.RandStr(10)
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(verifyCode))
|
||||
user.Password = string(hash.Sum(nil))
|
||||
user.ResetNeeded = true
|
||||
affected, err := global.DBConn.ID(uid).Cols("password", "reset_needed").Update(user)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if affected > 0 {
|
||||
// ! 同一个用户在缓存中有两个键。
|
||||
cache.AbolishCacheData("user", user.Id)
|
||||
cache.AbolishCacheData("user", user.Username)
|
||||
return verifyCode, nil
|
||||
} else {
|
||||
return "", exceptions.NewUnsuccessfulOperationError()
|
||||
}
|
||||
}
|
||||
|
||||
func (_UserService) VerifyUserPassword(username, verifyCode string) (bool, error) {
|
||||
user, err := repository.UserRepo.FindUserByUsername(username)
|
||||
if user == nil || err != nil {
|
||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(verifyCode))
|
||||
hashedVerifyCode := string(hash.Sum(nil))
|
||||
if hashedVerifyCode != user.Password {
|
||||
return false, nil
|
||||
} else {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (_UserService) ResetUserPassword(username, password string) (bool, error) {
|
||||
user, err := repository.UserRepo.FindUserByUsername(username)
|
||||
if user == nil || err != nil {
|
||||
return false, exceptions.NewNotFoundError("指定的用户不存在。")
|
||||
}
|
||||
hash := sha512.New512_256()
|
||||
hash.Write([]byte(password))
|
||||
user.Password = string(hash.Sum(nil))
|
||||
user.ResetNeeded = false
|
||||
affected, err := global.DBConn.ID(user.Id).Cols("password", "reset_needed").Update(user)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if affected > 0 {
|
||||
cache.AbolishCacheData("user", user.Id)
|
||||
cache.AbolishCacheData("user", user.Username)
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user