/
b_login.go
258 lines (225 loc) · 6.73 KB
/
b_login.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package bll
import (
"context"
"net/http"
"sort"
"github.com/google/wire"
"github.com/ops-cn/admin/app/bll"
"github.com/ops-cn/admin/app/model"
"github.com/ops-cn/common/auth"
"github.com/ops-cn/common/errors"
"github.com/ops-cn/common/schema"
"github.com/ops-cn/common/thirdparty/captcha"
"github.com/ops-cn/common/util"
)
var _ bll.ILogin = (*Login)(nil)
// LoginSet 注入Login
var LoginSet = wire.NewSet(wire.Struct(new(Login), "*"), wire.Bind(new(bll.ILogin), new(*Login)))
// Login 登录管理
type Login struct {
Auth auth.Auther
UserModel model.IUser
UserRoleModel model.IUserRole
RoleModel model.IRole
RoleMenuModel model.IRoleMenu
MenuModel model.IMenu
MenuActionModel model.IMenuAction
}
// GetCaptcha 获取图形验证码信息
func (a *Login) GetCaptcha(ctx context.Context, length int) (*schema.LoginCaptcha, error) {
captchaID := captcha.NewLen(length)
item := &schema.LoginCaptcha{
CaptchaID: captchaID,
}
return item, nil
}
// ResCaptcha 生成并响应图形验证码
func (a *Login) ResCaptcha(ctx context.Context, w http.ResponseWriter, captchaID string, width, height int) error {
err := captcha.WriteImage(w, captchaID, width, height)
if err != nil {
if err == captcha.ErrNotFound {
return errors.ErrNotFound
}
return errors.WithStack(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.Header().Set("Content-Type", "image/png")
return nil
}
// Verify 登录验证
func (a *Login) Verify(ctx context.Context, userName, password string) (*schema.User, error) {
// 检查是否是超级用户
root := schema.GetRootUser()
if userName == root.UserName && root.Password == password {
return root, nil
}
result, err := a.UserModel.Query(ctx, schema.UserQueryParam{
UserName: userName,
})
if err != nil {
return nil, err
} else if len(result.Data) == 0 {
return nil, errors.ErrInvalidUserName
}
item := result.Data[0]
if item.Password != util.SHA1HashString(password) {
return nil, errors.ErrInvalidPassword
} else if item.Status != 1 {
return nil, errors.ErrUserDisable
}
return item, nil
}
// GenerateToken 生成令牌
func (a *Login) GenerateToken(ctx context.Context, userID string) (*schema.LoginTokenInfo, error) {
tokenInfo, err := a.Auth.GenerateToken(ctx, userID)
if err != nil {
return nil, errors.WithStack(err)
}
item := &schema.LoginTokenInfo{
AccessToken: tokenInfo.GetAccessToken(),
TokenType: tokenInfo.GetTokenType(),
ExpiresAt: tokenInfo.GetExpiresAt(),
}
return item, nil
}
// DestroyToken 销毁令牌
func (a *Login) DestroyToken(ctx context.Context, tokenString string) error {
err := a.Auth.DestroyToken(ctx, tokenString)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (a *Login) checkAndGetUser(ctx context.Context, userID string) (*schema.User, error) {
user, err := a.UserModel.Get(ctx, userID)
if err != nil {
return nil, err
} else if user == nil {
return nil, errors.ErrInvalidUser
} else if user.Status != 1 {
return nil, errors.ErrUserDisable
}
return user, nil
}
// GetLoginInfo 获取当前用户登录信息
func (a *Login) GetLoginInfo(ctx context.Context, userID string) (*schema.UserLoginInfo, error) {
if isRoot := schema.CheckIsRootUser(ctx, userID); isRoot {
root := schema.GetRootUser()
loginInfo := &schema.UserLoginInfo{
UserName: root.UserName,
RealName: root.RealName,
}
return loginInfo, nil
}
user, err := a.checkAndGetUser(ctx, userID)
if err != nil {
return nil, err
}
info := &schema.UserLoginInfo{
UserID: user.ID,
UserName: user.UserName,
RealName: user.RealName,
}
userRoleResult, err := a.UserRoleModel.Query(ctx, schema.UserRoleQueryParam{
UserID: userID,
})
if err != nil {
return nil, err
}
if roleIDs := userRoleResult.Data.ToRoleIDs(); len(roleIDs) > 0 {
roleResult, err := a.RoleModel.Query(ctx, schema.RoleQueryParam{
IDs: roleIDs,
Status: 1,
})
if err != nil {
return nil, err
}
info.Roles = roleResult.Data
}
return info, nil
}
// QueryUserMenuTree 查询当前用户的权限菜单树
func (a *Login) QueryUserMenuTree(ctx context.Context, userID string) (schema.MenuTrees, error) {
isRoot := schema.CheckIsRootUser(ctx, userID)
// 如果是root用户,则查询所有显示的菜单树
if isRoot {
result, err := a.MenuModel.Query(ctx, schema.MenuQueryParam{
Status: 1,
}, schema.MenuQueryOptions{
OrderFields: schema.NewOrderFields(schema.NewOrderField("sequence", schema.OrderByDESC)),
})
if err != nil {
return nil, err
}
menuActionResult, err := a.MenuActionModel.Query(ctx, schema.MenuActionQueryParam{})
if err != nil {
return nil, err
}
return result.Data.FillMenuAction(menuActionResult.Data.ToMenuIDMap()).ToTree(), nil
}
userRoleResult, err := a.UserRoleModel.Query(ctx, schema.UserRoleQueryParam{
UserID: userID,
})
if err != nil {
return nil, err
} else if len(userRoleResult.Data) == 0 {
return nil, errors.ErrNoPerm
}
roleMenuResult, err := a.RoleMenuModel.Query(ctx, schema.RoleMenuQueryParam{
RoleIDs: userRoleResult.Data.ToRoleIDs(),
})
if err != nil {
return nil, err
} else if len(roleMenuResult.Data) == 0 {
return nil, errors.ErrNoPerm
}
menuResult, err := a.MenuModel.Query(ctx, schema.MenuQueryParam{
IDs: roleMenuResult.Data.ToMenuIDs(),
Status: 1,
})
if err != nil {
return nil, err
} else if len(menuResult.Data) == 0 {
return nil, errors.ErrNoPerm
}
mData := menuResult.Data.ToMap()
var qIDs []string
for _, pid := range menuResult.Data.SplitParentIDs() {
if _, ok := mData[pid]; !ok {
qIDs = append(qIDs, pid)
}
}
if len(qIDs) > 0 {
pmenuResult, err := a.MenuModel.Query(ctx, schema.MenuQueryParam{
IDs: menuResult.Data.SplitParentIDs(),
})
if err != nil {
return nil, err
}
menuResult.Data = append(menuResult.Data, pmenuResult.Data...)
}
sort.Sort(menuResult.Data)
menuActionResult, err := a.MenuActionModel.Query(ctx, schema.MenuActionQueryParam{
IDs: roleMenuResult.Data.ToActionIDs(),
})
if err != nil {
return nil, err
}
return menuResult.Data.FillMenuAction(menuActionResult.Data.ToMenuIDMap()).ToTree(), nil
}
// UpdatePassword 更新当前用户登录密码
func (a *Login) UpdatePassword(ctx context.Context, userID string, params schema.UpdatePasswordParam) error {
if schema.CheckIsRootUser(ctx, userID) {
return errors.New400Response("root用户不允许更新密码")
}
user, err := a.checkAndGetUser(ctx, userID)
if err != nil {
return err
} else if util.SHA1HashString(params.OldPassword) != user.Password {
return errors.New400Response("旧密码不正确")
}
params.NewPassword = util.SHA1HashString(params.NewPassword)
return a.UserModel.UpdatePassword(ctx, userID, params.NewPassword)
}