2017-07-26 10:50:26 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
// Package domain defines data structures for moving data between services.
|
2017-07-24 16:24:21 +01:00
|
|
|
package domain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-04-11 14:45:36 +01:00
|
|
|
"github.com/documize/community/core/request"
|
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
)
|
|
|
|
|
2018-11-05 19:48:50 +00:00
|
|
|
// RequestContext provides per request scoped values required for HTTP handlers.
|
2017-07-24 16:24:21 +01:00
|
|
|
type RequestContext struct {
|
|
|
|
AllowAnonymousAccess bool
|
|
|
|
Authenticated bool
|
|
|
|
Guest bool
|
|
|
|
UserID string
|
|
|
|
OrgID string
|
|
|
|
OrgName string
|
|
|
|
SSL bool
|
|
|
|
AppURL string // e.g. https://{url}.documize.com
|
|
|
|
Subdomain string
|
|
|
|
ClientIP string
|
|
|
|
Expires time.Time
|
|
|
|
Fullname string
|
|
|
|
Transaction *sqlx.Tx
|
2018-11-05 19:48:50 +00:00
|
|
|
Administrator bool
|
|
|
|
Analytics bool
|
|
|
|
Active bool
|
|
|
|
Editor bool
|
|
|
|
GlobalAdmin bool
|
|
|
|
ViewUsers bool
|
|
|
|
Subscription Subscription
|
2022-03-16 16:58:42 -04:00
|
|
|
Locale string
|
2022-03-19 18:07:38 -04:00
|
|
|
OrgLocale string
|
2017-07-24 16:24:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetAppURL returns full HTTP url for the app
|
|
|
|
func (c *RequestContext) GetAppURL(endpoint string) string {
|
|
|
|
scheme := "http://"
|
|
|
|
if c.SSL {
|
|
|
|
scheme = "https://"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s/%s", scheme, c.AppURL, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
type key string
|
|
|
|
|
|
|
|
// DocumizeContextKey prevents key name collisions.
|
|
|
|
const DocumizeContextKey key = "documize context key"
|
|
|
|
|
|
|
|
// GetRequestContext returns RequestContext from context.Context
|
2017-08-07 14:42:02 +01:00
|
|
|
func GetRequestContext(r *http.Request) (ctx RequestContext) {
|
|
|
|
c := r.Context()
|
|
|
|
if c != nil && c.Value(DocumizeContextKey) != nil {
|
|
|
|
ctx = c.Value(DocumizeContextKey).(RequestContext)
|
|
|
|
return
|
|
|
|
}
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-08-27 16:39:09 +01:00
|
|
|
ctx = RequestContext{}
|
|
|
|
ctx.AppURL = r.Host
|
2019-04-11 14:45:36 +01:00
|
|
|
ctx.SSL = request.IsSSL(r)
|
2017-08-27 16:39:09 +01:00
|
|
|
|
|
|
|
return
|
2017-08-07 14:42:02 +01:00
|
|
|
}
|