gitee.com/quant1x/engine@v1.8.4/permissions/permission.go (about)

     1  package permissions
     2  
     3  import (
     4  	"errors"
     5  	"gitee.com/quant1x/engine/models"
     6  	"sync"
     7  )
     8  
     9  var (
    10  	ErrAlreadyExists = errors.New("the validator already exists") // 权限验证已经存在
    11  )
    12  
    13  type Validator func(id uint64) error
    14  
    15  var (
    16  	mutexPermission    sync.Mutex
    17  	validatePermission Validator = nil
    18  )
    19  
    20  // RegisterValidatePermission 注册权限验证模块
    21  func RegisterValidatePermission(f Validator) error {
    22  	mutexPermission.Lock()
    23  	defer mutexPermission.Unlock()
    24  	if validatePermission != nil {
    25  		return ErrAlreadyExists
    26  	}
    27  	validatePermission = f
    28  	return nil
    29  }
    30  
    31  // CheckPermission 权限验证
    32  func CheckPermission(model models.Strategy) error {
    33  	if validatePermission == nil {
    34  		// 没有权限验证, 直接返回成功
    35  		return nil
    36  	}
    37  	return validatePermission(model.Code())
    38  }