Skip to content

Commit ed741ee

Browse files
authored
Merge pull request #150 from yokowu/fix-user-lock
fix: 用户锁定判断
2 parents 0a0e801 + e12b5b2 commit ed741ee

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

backend/domain/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type UserUsecase interface {
3636

3737
type UserRepo interface {
3838
List(ctx context.Context, page *web.Pagination) ([]*db.User, *db.PageInfo, error)
39-
Update(ctx context.Context, id string, fn func(*db.User, *db.UserUpdateOne) error) (*db.User, error)
39+
Update(ctx context.Context, id string, fn func(*db.Tx, *db.User, *db.UserUpdateOne) error) (*db.User, error)
4040
Delete(ctx context.Context, id string) error
4141
InitAdmin(ctx context.Context, username, password string) error
4242
CreateUser(ctx context.Context, user *db.User) (*db.User, error)

backend/errcode/errcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var LocalFS embed.FS
1212
var (
1313
ErrPermission = web.NewBadRequestErr("err-permission")
1414
ErrUserNotFound = web.NewBadRequestErr("err-user-not-found")
15+
ErrUserLock = web.NewBadRequestErr("err-user-lock")
1516
ErrPassword = web.NewBadRequestErr("err-password")
1617
ErrInviteCodeInvalid = web.NewBadRequestErr("err-invite-code-invalid")
1718
ErrEmailInvalid = web.NewBadRequestErr("err-email-invalid")

backend/errcode/locale.zh.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ other = "无权操作"
44
[err-user-not-found]
55
other = "用户不存在"
66

7+
[err-user-lock]
8+
other = "用户已锁定"
9+
710
[err-password]
811
other = "密码错误"
912

backend/internal/user/repo/user.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ func (r *UserRepo) AdminByName(ctx context.Context, username string) (*db.Admin,
7474
}
7575

7676
func (r *UserRepo) GetByName(ctx context.Context, username string) (*db.User, error) {
77-
return r.db.User.Query().Where(
78-
user.Or(
79-
user.Username(username),
80-
user.Email(username),
81-
),
82-
).Only(ctx)
77+
return r.db.User.Query().
78+
Where(
79+
user.Or(
80+
user.Username(username),
81+
user.Email(username),
82+
),
83+
).
84+
Only(ctx)
8385
}
8486

8587
func (r *UserRepo) ValidateInviteCode(ctx context.Context, code string) (*db.InviteCode, error) {
@@ -167,12 +169,12 @@ func (r *UserRepo) CreateInviteCode(ctx context.Context, userID string, code str
167169
}
168170

169171
func (r *UserRepo) AdminList(ctx context.Context, page *web.Pagination) ([]*db.Admin, *db.PageInfo, error) {
170-
q := r.db.Admin.Query()
172+
q := r.db.Admin.Query().Order(admin.ByCreatedAt(sql.OrderDesc()))
171173
return q.Page(ctx, page.Page, page.Size)
172174
}
173175

174176
func (r *UserRepo) List(ctx context.Context, page *web.Pagination) ([]*db.User, *db.PageInfo, error) {
175-
q := r.db.User.Query()
177+
q := r.db.User.Query().Order(user.ByCreatedAt(sql.OrderDesc()))
176178
return q.Page(ctx, page.Page, page.Size)
177179
}
178180

@@ -241,7 +243,7 @@ func (r *UserRepo) UpdateSetting(ctx context.Context, fn func(*db.Setting, *db.S
241243
return res, err
242244
}
243245

244-
func (r *UserRepo) Update(ctx context.Context, id string, fn func(*db.User, *db.UserUpdateOne) error) (*db.User, error) {
246+
func (r *UserRepo) Update(ctx context.Context, id string, fn func(*db.Tx, *db.User, *db.UserUpdateOne) error) (*db.User, error) {
245247
uid, err := uuid.Parse(id)
246248
if err != nil {
247249
return nil, err
@@ -254,7 +256,7 @@ func (r *UserRepo) Update(ctx context.Context, id string, fn func(*db.User, *db.
254256
return err
255257
}
256258
up := tx.User.UpdateOneID(u.ID)
257-
if err = fn(u, up); err != nil {
259+
if err = fn(tx, u, up); err != nil {
258260
return err
259261
}
260262
return up.Exec(ctx)
@@ -372,6 +374,9 @@ func (r *UserRepo) OAuthLogin(ctx context.Context, platform consts.UserPlatform,
372374
if err != nil {
373375
return nil, errcode.ErrNotInvited.Wrap(err)
374376
}
377+
if ui.Edges.User.Status != consts.UserStatusActive {
378+
return nil, errcode.ErrUserLock
379+
}
375380
if ui.AvatarURL != req.AvatarURL {
376381
if err = entx.WithTx(ctx, r.db, func(tx *db.Tx) error {
377382
return r.updateAvatar(ctx, tx, ui, req.AvatarURL)
@@ -409,6 +414,9 @@ func (r *UserRepo) SignUpOrIn(ctx context.Context, platform consts.UserPlatform,
409414
First(ctx)
410415
if err == nil {
411416
u = ui.Edges.User
417+
if u.Status != consts.UserStatusActive {
418+
return errcode.ErrUserLock
419+
}
412420
if ui.AvatarURL != req.AvatarURL {
413421
if err = r.updateAvatar(ctx, tx, ui, req.AvatarURL); err != nil {
414422
return err

backend/internal/user/usecase/user.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/chaitin/MonkeyCode/backend/config"
2020
"github.com/chaitin/MonkeyCode/backend/consts"
2121
"github.com/chaitin/MonkeyCode/backend/db"
22+
"github.com/chaitin/MonkeyCode/backend/db/apikey"
2223
"github.com/chaitin/MonkeyCode/backend/domain"
2324
"github.com/chaitin/MonkeyCode/backend/ent/types"
2425
"github.com/chaitin/MonkeyCode/backend/errcode"
@@ -206,6 +207,9 @@ func (u *UserUsecase) Login(ctx context.Context, req *domain.LoginReq) (*domain.
206207
if err != nil {
207208
return nil, errcode.ErrUserNotFound.Wrap(err)
208209
}
210+
if user.Status != consts.UserStatusActive {
211+
return nil, errcode.ErrUserLock
212+
}
209213
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
210214
return nil, errcode.ErrPassword.Wrap(err)
211215
}
@@ -394,17 +398,34 @@ func (u *UserUsecase) UpdateSetting(ctx context.Context, req *domain.UpdateSetti
394398
return cvt.From(s, &domain.Setting{}), nil
395399
}
396400

401+
func (u *UserUsecase) cleanApiKey(ctx context.Context, tx *db.Tx, user *db.User) error {
402+
if apikey, err := tx.ApiKey.Query().Where(apikey.UserID(user.ID)).First(ctx); err == nil {
403+
if err := tx.ApiKey.DeleteOneID(apikey.ID).Exec(ctx); err != nil {
404+
return err
405+
}
406+
rkey := "sk-" + apikey.Key
407+
return u.redis.Del(ctx, rkey).Err()
408+
409+
}
410+
return nil
411+
}
412+
397413
func (u *UserUsecase) Update(ctx context.Context, req *domain.UpdateUserReq) (*domain.User, error) {
398-
user, err := u.repo.Update(ctx, req.ID, func(_ *db.User, u *db.UserUpdateOne) error {
414+
user, err := u.repo.Update(ctx, req.ID, func(tx *db.Tx, old *db.User, up *db.UserUpdateOne) error {
399415
if req.Status != nil {
400-
u.SetStatus(*req.Status)
416+
if *req.Status == consts.UserStatusLocked {
417+
if err := u.cleanApiKey(ctx, tx, old); err != nil {
418+
return err
419+
}
420+
}
421+
up.SetStatus(*req.Status)
401422
}
402423
if req.Password != nil {
403424
hash, err := bcrypt.GenerateFromPassword([]byte(*req.Password), bcrypt.DefaultCost)
404425
if err != nil {
405426
return err
406427
}
407-
u.SetPassword(string(hash))
428+
up.SetPassword(string(hash))
408429
}
409430
return nil
410431
})
@@ -613,7 +634,7 @@ func (u *UserUsecase) WithOAuthCallback(ctx context.Context, req *domain.OAuthCa
613634
}
614635

615636
func (u *UserUsecase) ProfileUpdate(ctx context.Context, req *domain.ProfileUpdateReq) (*domain.User, error) {
616-
user, err := u.repo.Update(ctx, req.UID, func(old *db.User, uuo *db.UserUpdateOne) error {
637+
user, err := u.repo.Update(ctx, req.UID, func(_ *db.Tx, old *db.User, uuo *db.UserUpdateOne) error {
617638
if req.Avatar != nil {
618639
uuo.SetAvatarURL(*req.Avatar)
619640
}

0 commit comments

Comments
 (0)