code.gitea.io/gitea@v1.21.7/services/auth/source.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package auth
     5  
     6  import (
     7  	"code.gitea.io/gitea/models/auth"
     8  	"code.gitea.io/gitea/models/db"
     9  	user_model "code.gitea.io/gitea/models/user"
    10  )
    11  
    12  // DeleteSource deletes a AuthSource record in DB.
    13  func DeleteSource(source *auth.Source) error {
    14  	count, err := db.GetEngine(db.DefaultContext).Count(&user_model.User{LoginSource: source.ID})
    15  	if err != nil {
    16  		return err
    17  	} else if count > 0 {
    18  		return auth.ErrSourceInUse{
    19  			ID: source.ID,
    20  		}
    21  	}
    22  
    23  	count, err = db.GetEngine(db.DefaultContext).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID})
    24  	if err != nil {
    25  		return err
    26  	} else if count > 0 {
    27  		return auth.ErrSourceInUse{
    28  			ID: source.ID,
    29  		}
    30  	}
    31  
    32  	if registerableSource, ok := source.Cfg.(auth.RegisterableSource); ok {
    33  		if err := registerableSource.UnregisterSource(); err != nil {
    34  			return err
    35  		}
    36  	}
    37  
    38  	_, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(auth.Source))
    39  	return err
    40  }