mirror of
https://github.com/fumiama/paper-manager.git
synced 2026-06-10 10:50:23 +08:00
finish reset password
This commit is contained in:
@@ -106,7 +106,7 @@ func init() {
|
||||
type message struct {
|
||||
M string `json:"msg"`
|
||||
}
|
||||
writeresult(w, codeSuccess, &message{M: "成功, 请耐心等待通知"}, messageOk, typeSuccess)
|
||||
writeresult(w, codeSuccess, &message{M: "已上报, 请耐心等待通知"}, messageOk, typeSuccess)
|
||||
}}
|
||||
|
||||
apimap["/api/getUsersCount"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -212,6 +212,38 @@ func init() {
|
||||
writeresult(w, codeSuccess, &message{M: "成功"}, messageOk, typeSuccess)
|
||||
}}
|
||||
|
||||
apimap["/api/resetPassword"] = &apihandler{"POST", func(w http.ResponseWriter, r *http.Request) {
|
||||
type resetpwdbody struct {
|
||||
Username string `json:"username"`
|
||||
Mobile string `json:"mobile"`
|
||||
}
|
||||
if r.Header.Get("Authorization") != "" {
|
||||
writeresult(w, codeError, nil, errInvalidToken.Error(), typeError)
|
||||
return
|
||||
}
|
||||
var body resetpwdbody
|
||||
defer r.Body.Close()
|
||||
err := json.NewDecoder(r.Body).Decode(&body)
|
||||
if err != nil {
|
||||
writeresult(w, codeError, nil, err.Error(), typeError)
|
||||
return
|
||||
}
|
||||
ip := r.RemoteAddr
|
||||
i := strings.LastIndex(ip, ":")
|
||||
if i >= 0 {
|
||||
ip = ip[:i]
|
||||
}
|
||||
err = resetPassword(ip, body.Username, body.Mobile)
|
||||
if err != nil {
|
||||
writeresult(w, codeError, nil, err.Error(), typeError)
|
||||
return
|
||||
}
|
||||
type message struct {
|
||||
M string `json:"msg"`
|
||||
}
|
||||
writeresult(w, codeSuccess, &message{M: "已上报, 请耐心等待通知"}, messageOk, typeSuccess)
|
||||
}}
|
||||
|
||||
apimap["/api/getMessageList"] = &apihandler{"GET", func(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get("Authorization")
|
||||
ret, err := getMessageList(token)
|
||||
|
||||
@@ -67,6 +67,7 @@ var (
|
||||
ErrEmptyContact = errors.New("empty contact")
|
||||
ErrUsernameExists = errors.New("username exists")
|
||||
ErrInvalidName = errors.New("invalid name")
|
||||
ErrInvalidContact = errors.New("invalid contact")
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -367,8 +368,7 @@ func (u *UserDatabase) NotifyRegister(ip, name, cont, pswd string) error {
|
||||
}
|
||||
}
|
||||
|
||||
_, err := u.GetUserByName(name)
|
||||
if err == nil {
|
||||
if u.IsNameExists(name) {
|
||||
return ErrInvalidName
|
||||
}
|
||||
|
||||
@@ -396,6 +396,50 @@ func (u *UserDatabase) NotifyRegister(ip, name, cont, pswd string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NotifyResetPassword will send notification to all supers
|
||||
func (u *UserDatabase) NotifyResetPassword(ip, name, cont string) error {
|
||||
if name == "" {
|
||||
return ErrEmptyName
|
||||
}
|
||||
if cont == "" {
|
||||
return ErrEmptyContact
|
||||
}
|
||||
for _, c := range name {
|
||||
if !(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') {
|
||||
return ErrInvalidName
|
||||
}
|
||||
}
|
||||
|
||||
user, err := u.GetUserByName(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cont != user.Cont {
|
||||
return ErrInvalidContact
|
||||
}
|
||||
|
||||
tos, err := u.GetSuperIDs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m := Message{
|
||||
Date: time.Now().Unix(),
|
||||
Text: "收到来自 " + ip + ", 用户名 " + user.Name + " 的重置密码请求, 联系方式: " + user.Cont,
|
||||
Name: user.Name,
|
||||
}
|
||||
u.mu.Lock()
|
||||
defer u.mu.Unlock()
|
||||
for _, to := range tos {
|
||||
m.ToID = to
|
||||
err = u.db.InsertUnique(UserTableMessage, &m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// notifyUserAdded will send notification to all supers
|
||||
func (u *UserDatabase) notifyUserAdded(opname, name string) error {
|
||||
if opname == "" || name == "" {
|
||||
|
||||
@@ -11,13 +11,13 @@ import (
|
||||
var registerlimit = ttl.NewCache[string, bool](time.Minute * 10)
|
||||
|
||||
var (
|
||||
errRegisterTooFast = errors.New("register too fast")
|
||||
errInvalidIP = errors.New("invalid IP")
|
||||
errRequestTooFast = errors.New("request too fast")
|
||||
errInvalidIP = errors.New("invalid IP")
|
||||
)
|
||||
|
||||
func register(ip, name, mobile, npwd string) error {
|
||||
if registerlimit.Get(ip) {
|
||||
return errRegisterTooFast
|
||||
return errRequestTooFast
|
||||
}
|
||||
if ip == "" {
|
||||
return errInvalidIP
|
||||
|
||||
@@ -146,3 +146,14 @@ func setUserInfo(id int, nick, desc, avtr *string) error {
|
||||
}
|
||||
return global.UserDB.UpdateUserInfo(id, n, a, d)
|
||||
}
|
||||
|
||||
func resetPassword(ip, name, mobile string) error {
|
||||
if registerlimit.Get(ip) {
|
||||
return errRequestTooFast
|
||||
}
|
||||
if ip == "" {
|
||||
return errInvalidIP
|
||||
}
|
||||
registerlimit.Set(ip, true)
|
||||
return global.UserDB.NotifyResetPassword(ip, name, mobile)
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ export default [
|
||||
},
|
||||
},*/
|
||||
// mock reset password
|
||||
{
|
||||
/*{
|
||||
url: '/api/resetPassword',
|
||||
timeout: 200,
|
||||
method: 'post',
|
||||
@@ -101,7 +101,7 @@ export default [
|
||||
msg: '已将用户' + username + '电话' + mobile + '的重置请求上报, 请耐心等待!',
|
||||
})
|
||||
},
|
||||
},
|
||||
},*/
|
||||
// mock register
|
||||
/*{
|
||||
url: '/api/register',
|
||||
|
||||
@@ -17,7 +17,9 @@
|
||||
ghost
|
||||
color="success"
|
||||
v-if="
|
||||
item.type in [MessageTypeEnum.MessageRegister, MessageTypeEnum.MessageResetPassword]
|
||||
[MessageTypeEnum.MessageRegister, MessageTypeEnum.MessageResetPassword].includes(
|
||||
item.type,
|
||||
)
|
||||
"
|
||||
>接受</a-button
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user