generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
50 lines (42 loc) · 1.28 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
47
48
49
50
package oauth
import (
"github.com/resonatecoop/id/config"
"github.com/uptrace/bun"
"github.com/resonatecoop/user-api/model"
)
// Service struct keeps objects to avoid passing them around
type Service struct {
cnf *config.Config
db *bun.DB
allowedRoles []int32
}
// NewService returns a new Service instance
func NewService(cnf *config.Config, db *bun.DB) *Service {
return &Service{
cnf: cnf,
db: db,
allowedRoles: []int32{int32(model.SuperAdminRole), int32(model.AdminRole), int32(model.TenantAdminRole), int32(model.LabelRole), int32(model.ArtistRole), int32(model.UserRole)},
}
}
// 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 ...int32) {
s.allowedRoles = allowedRoles
// for i := range s.allowedRoles {
// s.allowedRoles[i] = model.AccessRole(allowedRoles[i])
// }
}
// IsRoleAllowed returns true if the role is allowed to use this service
func (s *Service) IsRoleAllowed(role int32) bool {
for _, allowedRole := range s.allowedRoles {
if role == allowedRole {
return true
}
}
return false
}
// Close stops any running services
func (s *Service) Close() {}