优化
This commit is contained in:
@@ -35,7 +35,7 @@ type jwtBlacklistPO struct {
|
||||
|
||||
func (jwtBlacklistPO) TableName() string { return "jwt_blacklists" }
|
||||
|
||||
func (r *settingsRepo) UserHasAuthority(ctx context.Context, userID, authorityID uint) (*biz.User, bool, error) {
|
||||
func (r *apiTokenRepo) UserHasAuthority(ctx context.Context, userID, authorityID uint) (*biz.User, bool, error) {
|
||||
var po userPO
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, userID).Error; err != nil {
|
||||
return nil, false, err
|
||||
@@ -45,10 +45,10 @@ func (r *settingsRepo) UserHasAuthority(ctx context.Context, userID, authorityID
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
user, err := (&systemRepo{data: r.data}).toBizUser(ctx, &po)
|
||||
user, err := (&userRepo{data: r.data}).toBizUser(ctx, &po)
|
||||
return user, count > 0 || po.AuthorityID == authorityID, err
|
||||
}
|
||||
func (r *settingsRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) error {
|
||||
func (r *apiTokenRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) error {
|
||||
po := apiTokenPO{UserID: v.UserID, AuthorityID: v.AuthorityID, Token: v.Token, Status: v.Status, ExpiresAt: v.ExpiresAt, Remark: v.Remark}
|
||||
if err := r.data.gormDB.WithContext(ctx).Create(&po).Error; err != nil {
|
||||
return err
|
||||
@@ -57,7 +57,7 @@ func (r *settingsRepo) CreateAPIToken(ctx context.Context, v *biz.APIToken) erro
|
||||
v.CreatedAt = po.CreatedAt
|
||||
return nil
|
||||
}
|
||||
func (r *settingsRepo) ListAPITokens(ctx context.Context, page, size int, userID uint, status *bool) ([]*biz.APIToken, int64, error) {
|
||||
func (r *apiTokenRepo) ListAPITokens(ctx context.Context, page, size int, userID uint, status *bool) ([]*biz.APIToken, int64, error) {
|
||||
db := r.data.gormDB.WithContext(ctx).Model(&apiTokenPO{})
|
||||
if userID != 0 {
|
||||
db = db.Where("user_id = ?", userID)
|
||||
@@ -94,14 +94,14 @@ func (r *settingsRepo) ListAPITokens(ctx context.Context, page, size int, userID
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
func (r *settingsRepo) DisableAPIToken(ctx context.Context, id uint) (string, error) {
|
||||
func (r *apiTokenRepo) DisableAPIToken(ctx context.Context, id uint) (string, error) {
|
||||
var po apiTokenPO
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return po.Token, r.data.gormDB.WithContext(ctx).Model(&po).Update("status", false).Error
|
||||
}
|
||||
func (r *settingsRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint) error {
|
||||
func (r *apiTokenRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint) error {
|
||||
return r.data.gormDB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var po apiTokenPO
|
||||
if err := tx.First(&po, id).Error; err != nil {
|
||||
@@ -114,13 +114,13 @@ func (r *settingsRepo) DisableAndBlacklistAPIToken(ctx context.Context, id uint)
|
||||
return tx.Model(&po).Update("status", false).Error
|
||||
})
|
||||
}
|
||||
func (r *settingsRepo) BlacklistToken(ctx context.Context, token string) error {
|
||||
func (r *apiTokenRepo) BlacklistToken(ctx context.Context, token string) error {
|
||||
if token == "" {
|
||||
return errors.New("token is empty")
|
||||
}
|
||||
return r.data.gormDB.WithContext(ctx).Create(&jwtBlacklistPO{JWT: token}).Error
|
||||
}
|
||||
func (r *settingsRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
|
||||
func (r *apiTokenRepo) IsTokenDisabled(ctx context.Context, token string) (bool, error) {
|
||||
var blacklistCount int64
|
||||
if err := r.data.gormDB.WithContext(ctx).Model(&jwtBlacklistPO{}).Where("jwt = ?", token).Count(&blacklistCount).Error; err != nil {
|
||||
return false, err
|
||||
|
||||
Reference in New Issue
Block a user