mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-02-11 11:13:40 +08:00
fix: 管理员活跃时间记录
This commit is contained in:
@@ -43,6 +43,7 @@ func NewUserHandler(
|
||||
usecase domain.UserUsecase,
|
||||
euse domain.ExtensionUsecase,
|
||||
auth *middleware.AuthMiddleware,
|
||||
active *middleware.ActiveMiddleware,
|
||||
session *session.Session,
|
||||
logger *slog.Logger,
|
||||
cfg *config.Config,
|
||||
@@ -66,7 +67,7 @@ func NewUserHandler(
|
||||
admin.POST("/login", web.BindHandler(u.AdminLogin))
|
||||
admin.GET("/setting", web.BaseHandler(u.GetSetting))
|
||||
|
||||
admin.Use(auth.Auth())
|
||||
admin.Use(auth.Auth(), active.Active("admin"))
|
||||
admin.PUT("/setting", web.BindHandler(u.UpdateSetting))
|
||||
admin.POST("/create", web.BindHandler(u.CreateAdmin))
|
||||
admin.GET("/list", web.BaseHandler(u.AdminList, web.WithPage()))
|
||||
@@ -80,7 +81,7 @@ func NewUserHandler(
|
||||
g.POST("/register", web.BindHandler(u.Register))
|
||||
g.POST("/login", web.BindHandler(u.Login))
|
||||
|
||||
g.Use(auth.Auth())
|
||||
g.Use(auth.Auth(), active.Active("admin"))
|
||||
|
||||
g.PUT("/update", web.BindHandler(u.Update))
|
||||
g.DELETE("/delete", web.BaseHandler(u.Delete))
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/GoYoko/web"
|
||||
|
||||
@@ -27,12 +28,13 @@ import (
|
||||
)
|
||||
|
||||
type UserRepo struct {
|
||||
db *db.Client
|
||||
ipdb *ipdb.IPDB
|
||||
db *db.Client
|
||||
ipdb *ipdb.IPDB
|
||||
redis *redis.Client
|
||||
}
|
||||
|
||||
func NewUserRepo(db *db.Client, ipdb *ipdb.IPDB) domain.UserRepo {
|
||||
return &UserRepo{db: db, ipdb: ipdb}
|
||||
func NewUserRepo(db *db.Client, ipdb *ipdb.IPDB, redis *redis.Client) domain.UserRepo {
|
||||
return &UserRepo{db: db, ipdb: ipdb, redis: redis}
|
||||
}
|
||||
|
||||
func (r *UserRepo) InitAdmin(ctx context.Context, username, password string) error {
|
||||
@@ -251,10 +253,21 @@ func (r *UserRepo) Delete(ctx context.Context, id string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ApiKey.Delete().Where(apikey.UserID(user.ID)).Exec(ctx); err != nil {
|
||||
|
||||
keys, err := tx.ApiKey.Query().Where(apikey.UserID(user.ID)).All(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range keys {
|
||||
if _, err := tx.ApiKey.Delete().Where(apikey.ID(v.ID)).Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.redis.Del(ctx, fmt.Sprintf("sk-%s", v.Key)).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range user.Edges.Identities {
|
||||
if _, err := tx.UserIdentity.Delete().Where(useridentity.ID(v.ID)).Exec(ctx); err != nil {
|
||||
return err
|
||||
|
||||
@@ -3,6 +3,7 @@ package usecase
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
@@ -81,7 +82,7 @@ func (u *UserUsecase) getUserActive(ctx context.Context, ids []string) (map[stri
|
||||
m := make(map[string]int64)
|
||||
for _, id := range ids {
|
||||
key := fmt.Sprintf(consts.UserActiveKeyFmt, id)
|
||||
if t, err := u.redis.Get(ctx, key).Int64(); err != nil {
|
||||
if t, err := u.redis.Get(ctx, key).Int64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
u.logger.With("key", key).With("error", err).Warn("get user active time failed")
|
||||
} else {
|
||||
m[id] = t
|
||||
@@ -98,14 +99,36 @@ func (u *UserUsecase) AdminList(ctx context.Context, page *web.Pagination) (*dom
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ids := cvt.Iter(admins, func(_ int, u *db.Admin) string { return u.ID.String() })
|
||||
m, err := u.getAdminActive(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &domain.ListAdminUserResp{
|
||||
PageInfo: p,
|
||||
Users: cvt.Iter(admins, func(_ int, e *db.Admin) *domain.AdminUser {
|
||||
return cvt.From(e, &domain.AdminUser{}).From(e)
|
||||
return cvt.From(e, &domain.AdminUser{
|
||||
LastActiveAt: m[e.ID.String()],
|
||||
})
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *UserUsecase) getAdminActive(ctx context.Context, ids []string) (map[string]int64, error) {
|
||||
m := make(map[string]int64)
|
||||
for _, id := range ids {
|
||||
key := fmt.Sprintf(consts.AdminActiveKeyFmt, id)
|
||||
if t, err := u.redis.Get(ctx, key).Int64(); err != nil && !errors.Is(err, redis.Nil) {
|
||||
u.logger.With("key", key).With("error", err).Warn("get admin active time failed")
|
||||
} else {
|
||||
m[id] = t
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// AdminLoginHistory implements domain.UserUsecase.
|
||||
func (u *UserUsecase) AdminLoginHistory(ctx context.Context, page *web.Pagination) (*domain.ListAdminLoginHistoryResp, error) {
|
||||
histories, p, err := u.repo.AdminLoginHistory(ctx, page)
|
||||
|
||||
Reference in New Issue
Block a user