-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
profile.go
149 lines (135 loc) · 4.58 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package user
import (
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v3/application/handler"
"github.com/admpub/nging/v3/application/handler/term"
"github.com/admpub/nging/v3/application/library/codec"
"github.com/admpub/nging/v3/application/library/filemanager"
"github.com/admpub/nging/v3/application/model"
)
func Edit(ctx echo.Context) error {
var err error
user := handler.User(ctx)
if user == nil {
return ctx.E(`登录信息获取失败,请重新登录`)
}
m := model.NewUser(ctx)
err = m.Get(nil, `id`, user.Id)
if err != nil {
return err
}
needCheckU2F := m.NeedCheckU2F(user.Id)
if ctx.IsPost() {
email := strings.TrimSpace(ctx.Form(`email`))
mobile := strings.TrimSpace(ctx.Form(`mobile`))
modifyPass := ctx.Form(`modifyPass`) == `1`
//新密码
newPass := strings.TrimSpace(ctx.Form(`newPass`))
confirmPass := strings.TrimSpace(ctx.Form(`confirmPass`))
//旧密码
passwd := strings.TrimSpace(ctx.Form(`pass`))
passwd, err = codec.DefaultSM2DecryptHex(passwd)
if err != nil {
return ctx.NewError(code.InvalidParameter, ctx.T(`旧密码解密失败: %v`, err)).SetZone(`pass`)
}
if modifyPass {
newPass, err = codec.DefaultSM2DecryptHex(newPass)
if err != nil {
return ctx.NewError(code.InvalidParameter, ctx.T(`新密码解密失败: %v`, err)).SetZone(`newPass`)
}
confirmPass, err = codec.DefaultSM2DecryptHex(confirmPass)
if err != nil {
return ctx.NewError(code.InvalidParameter, ctx.T(`您输入的确认密码解密失败: %v`, err)).SetZone(`confirmPass`)
}
}
gender := strings.TrimSpace(ctx.Form(`gender`))
if len(email) == 0 {
err = ctx.E(`Email不能为空`)
} else if modifyPass && len(newPass) < 8 {
err = ctx.E(`新密码不能少于8个字符`)
} else if modifyPass && newPass != confirmPass {
err = ctx.E(`新密码与确认新密码不一致`)
} else if !ctx.Validate(`email`, email, `email`).Ok() {
err = ctx.E(`Email地址"%s"格式不正确`, email)
} else if len(mobile) > 0 && !ctx.Validate(`mobile`, mobile, `mobile`).Ok() {
err = ctx.E(`手机号"%s"格式不正确`, mobile)
} else if m.NgingUser.Password != com.MakePassword(passwd, m.NgingUser.Salt) {
err = ctx.E(`旧密码输入不正确`)
} else if needCheckU2F {
//两步验证码
err = GAuthVerify(ctx, `u2fCode`)
}
if err == nil && !ctx.Validate(`email`, email, `email`).Ok() {
err = ctx.E(`Email地址格式不正确`)
}
if err == nil {
set := map[string]interface{}{
`email`: email,
`mobile`: mobile,
`avatar`: ctx.Form(`avatar`),
`gender`: gender,
}
if modifyPass {
set[`password`] = com.MakePassword(newPass, m.NgingUser.Salt)
}
err = m.SetFields(nil, set, `id`, user.Id)
}
if err == nil {
handler.SendOk(ctx, ctx.T(`修改成功`))
m.Get(nil, `id`, user.Id)
m.SetSession()
return ctx.Redirect(handler.URLFor(`/user/edit`))
}
}
ctx.Set(`needCheckU2F`, needCheckU2F)
return ctx.Render(`user/edit`, handler.Err(ctx, err))
}
func AutoCompletePath(ctx echo.Context) error {
sshAccountID := ctx.Formx(`sshAccountId`).Uint()
if sshAccountID > 0 {
check, _ := ctx.Funcs()[`CheckPerm`].(func(string) error)
data := ctx.Data()
if check == nil {
data.SetData([]string{})
return ctx.JSON(data)
}
if err := check(`manager/command_add`); err != nil {
return err
}
if err := check(`manager/command_edit`); err != nil {
return err
}
return term.SftpSearch(ctx, sshAccountID)
}
data := ctx.Data()
prefix := ctx.Form(`query`)
size := ctx.Formx(`size`, `10`).Int()
var paths []string
switch ctx.Form(`type`) {
case `dir`:
paths = filemanager.SearchDir(prefix, size)
case `file`:
paths = filemanager.SearchFile(prefix, size)
default:
paths = filemanager.Search(prefix, size)
}
data.SetData(paths)
return ctx.JSON(data)
}