-
Notifications
You must be signed in to change notification settings - Fork 12
/
role_checker.go
26 lines (24 loc) · 1.06 KB
/
role_checker.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
package handler
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/errors"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-api/graph/model"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/common"
)
func GetRoleChecker() func(ctx context.Context, obj interface{}, next graphql.Resolver, roles []model.Role) (res interface{}, err error) {
return func(ctx context.Context, obj interface{}, next graphql.Resolver, roles []model.Role) (res interface{}, err error) {
currentRoles := common.GetRolesFromContext(ctx)
// Check if the current role is in the list of allowed roles
for _, allowedRole := range roles {
for _, currentRole := range currentRoles {
if currentRole == allowedRole.String() {
// If the role is in the list of allowed roles, call the next resolver
return next(ctx)
}
}
}
// If the role is not in the list of allowed roles, return an error
return nil, errors.ErrAccessDenied
}
}