github.com/resonatecoop/id@v1.1.0-43/oauth/service.go (about)

     1  package oauth
     2  
     3  import (
     4  	"github.com/resonatecoop/id/config"
     5  	"github.com/uptrace/bun"
     6  
     7  	"github.com/resonatecoop/user-api/model"
     8  )
     9  
    10  // Service struct keeps objects to avoid passing them around
    11  type Service struct {
    12  	cnf          *config.Config
    13  	db           *bun.DB
    14  	allowedRoles []int32
    15  }
    16  
    17  // NewService returns a new Service instance
    18  func NewService(cnf *config.Config, db *bun.DB) *Service {
    19  	return &Service{
    20  		cnf:          cnf,
    21  		db:           db,
    22  		allowedRoles: []int32{int32(model.SuperAdminRole), int32(model.AdminRole), int32(model.TenantAdminRole), int32(model.LabelRole), int32(model.ArtistRole), int32(model.UserRole)},
    23  	}
    24  }
    25  
    26  // GetConfig returns config.Config instance
    27  func (s *Service) GetConfig() *config.Config {
    28  	return s.cnf
    29  }
    30  
    31  // RestrictToRoles restricts this service to only specified roles
    32  func (s *Service) RestrictToRoles(allowedRoles ...int32) {
    33  	s.allowedRoles = allowedRoles
    34  	// for i := range s.allowedRoles {
    35  	// 	s.allowedRoles[i] = model.AccessRole(allowedRoles[i])
    36  	// }
    37  }
    38  
    39  // IsRoleAllowed returns true if the role is allowed to use this service
    40  func (s *Service) IsRoleAllowed(role int32) bool {
    41  	for _, allowedRole := range s.allowedRoles {
    42  		if role == allowedRole {
    43  			return true
    44  		}
    45  	}
    46  	return false
    47  }
    48  
    49  // Close stops any running services
    50  func (s *Service) Close() {}