-
Notifications
You must be signed in to change notification settings - Fork 315
/
service.go
46 lines (39 loc) · 1.09 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package oauth
import (
"github.com/RichardKnop/go-oauth2-server/config"
"github.com/RichardKnop/go-oauth2-server/oauth/roles"
"github.com/jinzhu/gorm"
)
// Service struct keeps objects to avoid passing them around
type Service struct {
cnf *config.Config
db *gorm.DB
allowedRoles []string
}
// NewService returns a new Service instance
func NewService(cnf *config.Config, db *gorm.DB) *Service {
return &Service{
cnf: cnf,
db: db,
allowedRoles: []string{roles.Superuser, roles.User},
}
}
// GetConfig returns config.Config instance
func (s *Service) GetConfig() *config.Config {
return s.cnf
}
// RestrictToRoles restricts this service to only specified roles
func (s *Service) RestrictToRoles(allowedRoles ...string) {
s.allowedRoles = allowedRoles
}
// IsRoleAllowed returns true if the role is allowed to use this service
func (s *Service) IsRoleAllowed(role string) bool {
for _, allowedRole := range s.allowedRoles {
if role == allowedRole {
return true
}
}
return false
}
// Close stops any running services
func (s *Service) Close() {}