fix: align admin behavior and Kratos integrations
This commit is contained in:
@@ -2,6 +2,7 @@ package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,6 +24,29 @@ type APITokenRepo interface {
|
||||
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)
|
||||
}
|
||||
|
||||
func (uc *SettingsUsecase) 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
|
||||
}
|
||||
if duration <= 0 {
|
||||
return nil, 0, errors.New("有效天数必须大于0或为-1")
|
||||
}
|
||||
return user, duration, nil
|
||||
}
|
||||
|
||||
func (uc *SettingsUsecase) DisableToken(ctx context.Context, id uint) error {
|
||||
return uc.SettingsRepo.DisableAndBlacklistAPIToken(ctx, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user