3
0
Files
corteza/system/rest/subscription.go
2020-11-04 14:18:33 +01:00

49 lines
1.1 KiB
Go

package rest
import (
"context"
"github.com/cortezaproject/corteza-server/pkg/api"
"github.com/cortezaproject/corteza-server/system/rest/request"
"github.com/cortezaproject/corteza-server/system/service"
"net/http"
"strings"
)
type Subscription struct{}
func (Subscription) New() *Subscription {
return &Subscription{}
}
func (ctrl *Subscription) Current(ctx context.Context, r *request.SubscriptionCurrent) (interface{}, error) {
if service.CurrentSubscription == nil {
// Nothing to do here
return api.OK(), nil
}
// Returning function that gets called with writter & request
//
// This is the only way to get to the request URL we need to do a domain check
// for the permit
return func(w http.ResponseWriter, r *http.Request) {
var (
domain = r.Host
pos = strings.IndexByte(domain, ':')
// Anyone that has access permissions is considered admin
isAdmin = service.DefaultAccessControl.CanAccess(ctx)
)
if pos > -1 {
// Strip port
domain = domain[:pos]
}
if err := service.CurrentSubscription.Validate(domain, isAdmin); err != nil {
api.Send(w, r, err)
} else {
api.Send(w, r, api.OK())
}
}, nil
}