-
Notifications
You must be signed in to change notification settings - Fork 66
/
account.go
159 lines (134 loc) · 4.13 KB
/
account.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
150
151
152
153
154
155
156
157
158
159
package service
import (
"errors"
v1 "github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/pkg/config"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
commService "github.com/aaronchen2k/deeptest/internal/pkg/service"
"github.com/aaronchen2k/deeptest/internal/server/modules/model"
"github.com/aaronchen2k/deeptest/internal/server/modules/repo"
_domain "github.com/aaronchen2k/deeptest/pkg/domain"
_i118Utils "github.com/aaronchen2k/deeptest/pkg/lib/i118"
logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
_mailUtils "github.com/aaronchen2k/deeptest/pkg/lib/mail"
"github.com/snowlyg/multi"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"strconv"
"time"
)
var (
ErrUserNameOrPassword = errors.New("用户名或密码错误")
)
type AccountService struct {
UserRepo *repo.UserRepo `inject:""`
LdapService *commService.LdapService `inject:""`
}
// Login 登录
func (s *AccountService) Login(tenantId consts.TenantId, req v1.LoginReq) (ret v1.LoginResp, err error) {
var Id uint
var userBase v1.UserBase
var user model.SysUser
user, _ = s.UserRepo.GetByUserName(tenantId, req.Username)
if config.CONFIG.Ldap && req.Username != "admin" && !user.Type {
userBase, err = s.LdapService.LdapUserInfo(req)
if err != nil {
return
}
Id, err = s.UserRepo.UpdateByLdapInfo(tenantId, userBase)
} else {
Id, err = s.UserLogin(tenantId, req)
}
if err != nil {
return
}
claims := &multi.CustomClaims{
ID: strconv.FormatUint(uint64(Id), 10),
Username: req.Username,
AuthorityId: string(tenantId),
AuthorityType: multi.AdminAuthority,
LoginType: multi.LoginTypeApp,
AuthType: multi.AuthPwd,
CreationDate: time.Now().Local().Unix(),
ExpiresIn: multi.RedisSessionTimeoutWeb.Milliseconds(),
}
ret.Token, _, err = multi.AuthDriver.GenerateToken(claims)
if err != nil {
return
}
return
}
func (s *AccountService) UserLogin(tenantId consts.TenantId, req v1.LoginReq) (userId uint, err error) {
user, err := s.UserRepo.FindPasswordByUserName(tenantId, req.Username)
if err != nil {
user, err = s.UserRepo.FindPasswordByEmail(tenantId, req.Username)
if err != nil {
return
}
}
userId = user.Id
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password))
if err != nil {
logUtils.Errorf("用户名或密码错误", zap.String("密码:", req.Password), zap.String("hash:", user.Password), zap.String("bcrypt.CompareHashAndPassword()", err.Error()))
err = ErrUserNameOrPassword
return
}
return
}
func (s *AccountService) Register(tenantId consts.TenantId, req v1.RegisterReq) (err _domain.BizErr) {
err = _domain.NoErr
if req.Password != req.Confirm {
err = _domain.ErrPasswordMustBeSame
return
}
po, _ := s.UserRepo.FindByUserName(tenantId, req.Username)
if po.Id > 0 {
err = _domain.ErrUsernameExist
return
}
user := model.SysUser{
UserBase: v1.UserBase{
Username: req.Username,
Email: req.Email,
},
Password: req.Password,
}
s.UserRepo.Register(tenantId, &user)
//mp := map[string]string{
// "name": user.Name,
// "sys": consts.Sys,
// "url": consts.Url,
//}
//_mailUtils.Send(user.Email, _i118Utils.Sprintf("register_success"), "register-success", mp)
return
}
func (s *AccountService) ForgotPassword(tenantId consts.TenantId, usernameOrPassword string) (err error) {
user, err := s.UserRepo.GetByUsernameOrPassword(tenantId, usernameOrPassword)
vcode, err := s.UserRepo.GenAndUpdateVcode(tenantId, user.ID)
url := consts.Url
if !consts.IsRelease {
url = consts.UrlDev
}
settings := map[string]string{
"name": user.Username,
"url": url,
"vcode": vcode,
}
_mailUtils.Send(user.Email, _i118Utils.Sprintf("reset_password"), "reset-password", settings)
return
}
func (s *AccountService) ResetPassword(tenantId consts.TenantId, req v1.ResetPasswordReq) (err error) {
user, err := s.UserRepo.FindByUserNameAndVcode(tenantId, req.Username, req.Vcode)
if err != nil {
return
}
err = s.UserRepo.UpdatePassword(tenantId, req.Password, user.ID)
if err != nil {
return
}
err = s.UserRepo.ClearVcode(tenantId, user.ID)
if err != nil {
return
}
return
}