fix: align admin behavior and Kratos integrations
This commit is contained in:
@@ -36,12 +36,6 @@ func opFromPO(v operationPO) *biz.OperationRecord {
|
||||
return &biz.OperationRecord{ID: v.ID, CreatedAt: v.CreatedAt, IP: v.IP, Method: v.Method, Path: v.Path, Status: v.Status, LatencyMS: v.LatencyMS, Agent: v.Agent, ErrorMessage: v.ErrorMessage, Body: v.Body, Response: v.Response, UserID: v.UserID, RequestID: v.RequestID, TraceID: v.TraceID, DeviceID: v.DeviceID}
|
||||
}
|
||||
func (r *auditRepo) ListOperations(ctx context.Context, page, size int, q *biz.OperationRecord) ([]*biz.OperationRecord, int64, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 {
|
||||
size = 10
|
||||
}
|
||||
db := r.data.gormDB.WithContext(ctx).Model(&operationPO{})
|
||||
if q != nil {
|
||||
if q.Path != "" {
|
||||
@@ -53,27 +47,49 @@ func (r *auditRepo) ListOperations(ctx context.Context, page, size int, q *biz.O
|
||||
if q.Status != 0 {
|
||||
db = db.Where("status = ?", q.Status)
|
||||
}
|
||||
if q.IP != "" {
|
||||
db = db.Where("ip LIKE ?", "%"+q.IP+"%")
|
||||
}
|
||||
if q.UserID != 0 {
|
||||
db = db.Where("user_id = ?", q.UserID)
|
||||
}
|
||||
}
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var pos []operationPO
|
||||
if err := db.Order("id desc").Offset((page - 1) * size).Limit(size).Find(&pos).Error; err != nil {
|
||||
if err := applyPagination(db.Order("id desc"), page, size, 100).Find(&pos).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
users := auditUsers(ctx, r.data.gormDB.WithContext(ctx), operationUserIDs(pos))
|
||||
out := make([]*biz.OperationRecord, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
out = append(out, opFromPO(po))
|
||||
value := opFromPO(po)
|
||||
value.User = users[po.UserID]
|
||||
out = append(out, value)
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
|
||||
func operationUserIDs(values []operationPO) []uint {
|
||||
ids := make([]uint, 0, len(values))
|
||||
for _, value := range values {
|
||||
if value.UserID != 0 {
|
||||
ids = append(ids, value.UserID)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func auditUsers(ctx context.Context, db *gorm.DB, ids []uint) map[uint]*biz.User {
|
||||
result := make(map[uint]*biz.User)
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
var users []userPO
|
||||
if db.WithContext(ctx).Where("id IN ?", ids).Find(&users).Error != nil {
|
||||
return result
|
||||
}
|
||||
for _, value := range users {
|
||||
result[value.ID] = &biz.User{ID: value.ID, CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, UUID: value.UUID, Username: value.Username, NickName: value.NickName, HeaderImg: value.HeaderImg, AuthorityID: value.AuthorityID, DeptID: value.DeptID, Phone: value.Phone, Email: value.Email, Enable: value.Enable}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func (r *auditRepo) FindOperation(ctx context.Context, id uint) (*biz.OperationRecord, error) {
|
||||
var po operationPO
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user