two_factors.go 823 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "github.com/jinzhu/gorm"
  7. log "unknwon.dev/clog/v2"
  8. )
  9. // TwoFactorsStore is the persistent interface for 2FA.
  10. //
  11. // NOTE: All methods are sorted in alphabetical order.
  12. type TwoFactorsStore interface {
  13. // IsUserEnabled returns true if the user has enabled 2FA.
  14. IsUserEnabled(userID int64) bool
  15. }
  16. var TwoFactors TwoFactorsStore
  17. type twoFactors struct {
  18. *gorm.DB
  19. }
  20. func (db *twoFactors) IsUserEnabled(userID int64) bool {
  21. var count int64
  22. err := db.Model(new(TwoFactor)).Where("user_id = ?", userID).Count(&count).Error
  23. if err != nil {
  24. log.Error("Failed to count two factors [user_id: %d]: %v", userID, err)
  25. }
  26. return count > 0
  27. }