优化结构
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type APIToken struct {
|
||||
ID uint
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
UserID uint
|
||||
AuthorityID uint
|
||||
Token string
|
||||
Status bool
|
||||
ExpiresAt time.Time
|
||||
Remark string
|
||||
User *User
|
||||
}
|
||||
|
||||
type APITokenRepo interface {
|
||||
UserHasAuthority(context.Context, uint, uint) (*User, bool, error)
|
||||
CreateAPIToken(context.Context, *APIToken) error
|
||||
ListAPITokens(context.Context, int, int, uint, *bool) ([]*APIToken, int64, error)
|
||||
DisableAPIToken(context.Context, uint) (string, error)
|
||||
DisableAndBlacklistAPIToken(context.Context, uint) error
|
||||
BlacklistToken(context.Context, string) error
|
||||
IsTokenDisabled(context.Context, string) (bool, error)
|
||||
}
|
||||
|
||||
type TokenUsecase struct{ APITokenRepo }
|
||||
|
||||
func NewTokenUsecase(repo APITokenRepo) *TokenUsecase {
|
||||
return &TokenUsecase{APITokenRepo: repo}
|
||||
}
|
||||
|
||||
func (uc *TokenUsecase) PrepareAPIToken(ctx context.Context, userID, authorityID uint, days int) (*User, time.Duration, error) {
|
||||
user, allowed, err := uc.UserHasAuthority(ctx, userID, authorityID)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if !allowed {
|
||||
return nil, 0, errors.New("用户不具备该角色权限")
|
||||
}
|
||||
duration := time.Duration(days) * 24 * time.Hour
|
||||
if days == -1 {
|
||||
duration = 100 * 365 * 24 * time.Hour
|
||||
}
|
||||
return user, duration, nil
|
||||
}
|
||||
|
||||
func (uc *TokenUsecase) DisableToken(ctx context.Context, id uint) error {
|
||||
return uc.APITokenRepo.DisableAndBlacklistAPIToken(ctx, id)
|
||||
}
|
||||
Reference in New Issue
Block a user