1
Fork 0
photoview/api/graphql/directive.go

28 lines
612 B
Go
Raw Normal View History

2020-01-31 23:30:34 +01:00
package api
import (
"context"
"errors"
"github.com/99designs/gqlgen/graphql"
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/graphql/auth"
2020-01-31 23:30:34 +01:00
)
func IsAdmin(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
user := auth.UserFromContext(ctx)
if user == nil || user.Admin == false {
return nil, errors.New("user must be admin")
}
2020-01-31 23:30:34 +01:00
return next(ctx)
}
2020-01-31 23:30:34 +01:00
func IsAuthorized(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
user := auth.UserFromContext(ctx)
if user == nil {
return nil, auth.ErrUnauthorized
2020-01-31 23:30:34 +01:00
}
return next(ctx)
2020-01-31 23:30:34 +01:00
}