1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-02 20:15:26 +02:00

set defaults for request context

This commit is contained in:
Harvey Kandola 2017-08-27 16:39:09 +01:00
parent 63b24aed3f
commit 8081b60146
23 changed files with 692 additions and 666 deletions

View file

@ -68,9 +68,11 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
}
dom := strings.TrimSpace(strings.ToLower(credentials[0]))
dom = h.Store.Organization.CheckDomain(ctx, dom) // TODO optimize by removing this once js allows empty domains
email := strings.TrimSpace(strings.ToLower(credentials[1]))
password := credentials[2]
dom = h.Store.Organization.CheckDomain(ctx, dom) // TODO optimize by removing this once js allows empty domains
h.Runtime.Log.Info("logon attempt " + email + " @ " + dom)
u, err := h.Store.User.GetByDomain(ctx, dom, email)
@ -108,6 +110,8 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
return
}
h.Runtime.Log.Info("login " + email + " @ " + dom)
authModel := auth.AuthenticationModel{}
authModel.Token = GenerateJWT(h.Runtime, u.RefID, org.RefID, dom)
authModel.User = u

View file

@ -64,5 +64,9 @@ func GetRequestContext(r *http.Request) (ctx RequestContext) {
return
}
return RequestContext{}
ctx = RequestContext{}
ctx.AppURL = r.Host
ctx.SSL = r.TLS != nil
return
}

View file

@ -96,8 +96,8 @@ func (h *Handler) convert(w http.ResponseWriter, r *http.Request, job, folderID
method := "conversion.upload"
ctx := domain.GetRequestContext(r)
licenseKey := h.Store.Setting.Get("EDITION-LICENSE", "key")
licenseSignature := h.Store.Setting.Get("EDITION-LICENSE", "signature")
licenseKey, _ := h.Store.Setting.Get("EDITION-LICENSE", "key")
licenseSignature, _ := h.Store.Setting.Get("EDITION-LICENSE", "signature")
k, _ := hex.DecodeString(licenseKey)
s, _ := hex.DecodeString(licenseSignature)

View file

@ -291,9 +291,18 @@ func (m *Mailer) GetHost() string {
// LoadCredentials loads up SMTP details from database
func (m *Mailer) LoadCredentials() {
m.Credentials.SMTPuserid = strings.TrimSpace(m.Store.Setting.Get("SMTP", "userid"))
m.Credentials.SMTPpassword = strings.TrimSpace(m.Store.Setting.Get("SMTP", "password"))
m.Credentials.SMTPhost = strings.TrimSpace(m.Store.Setting.Get("SMTP", "host"))
m.Credentials.SMTPport = strings.TrimSpace(m.Store.Setting.Get("SMTP", "port"))
m.Credentials.SMTPsender = strings.TrimSpace(m.Store.Setting.Get("SMTP", "sender"))
userID, _ := m.Store.Setting.Get("SMTP", "userid")
m.Credentials.SMTPuserid = strings.TrimSpace(userID)
pwd, _ := m.Store.Setting.Get("SMTP", "password")
m.Credentials.SMTPpassword = strings.TrimSpace(pwd)
host, _ := m.Store.Setting.Get("SMTP", "host")
m.Credentials.SMTPhost = strings.TrimSpace(host)
port, _ := m.Store.Setting.Get("SMTP", "port")
m.Credentials.SMTPport = strings.TrimSpace(port)
sender, _ := m.Store.Setting.Get("SMTP", "sender")
m.Credentials.SMTPsender = strings.TrimSpace(sender)
}

View file

@ -80,7 +80,7 @@ func (s Scope) GetOrganization(ctx domain.RequestContext, id string) (org org.Or
// No context is required because user might no be authenticated yet.
func (s Scope) GetOrganizationByDomain(subdomain string) (org org.Organization, err error) {
err = nil
subdomain = strings.ToLower(subdomain)
subdomain = strings.TrimSpace(strings.ToLower(subdomain))
if s.Runtime.Flags.SiteMode == env.SiteModeNormal { // only return an organization when running normally
var stmt *sqlx.Stmt

View file

@ -25,16 +25,19 @@ import (
)
func clientID(ctx domain.RequestContext, s *domain.Store) string {
return s.Setting.Get(meta.ConfigHandle(), "clientID")
c, _ := s.Setting.Get(meta.ConfigHandle(), "clientID")
return c
}
func clientSecret(ctx domain.RequestContext, s *domain.Store) string {
return s.Setting.Get(meta.ConfigHandle(), "clientSecret")
c, _ := s.Setting.Get(meta.ConfigHandle(), "clientSecret")
return c
}
func authorizationCallbackURL(ctx domain.RequestContext, s *domain.Store) string {
// NOTE: URL value must have the path and query "/api/public/validate?section=github"
return s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
c, _ := s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
return c
}
func validateToken(ctx provider.Context, s *domain.Store, ptoken string) error {

View file

@ -222,7 +222,9 @@ func (c *Context) MarshalSecrets(sec interface{}, s *domain.Store) error {
// Errors return the empty string.
func (c *Context) GetSecrets(JSONpath string, s *domain.Store) string {
m := c.prov.Meta()
return s.Setting.GetUser(c.OrgID, c.UserID, m.ContentType, JSONpath)
v, _ := s.Setting.GetUser(c.OrgID, c.UserID, m.ContentType, JSONpath)
return v
}
// ErrNoSecrets is returned if no secret is found in the database.

View file

@ -69,7 +69,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
}
config.Clean()
config.AppKey = p.Store.Setting.Get(meta.ConfigHandle(), "appKey")
config.AppKey, _ = p.Store.Setting.Get(meta.ConfigHandle(), "appKey")
if len(config.AppKey) == 0 {
p.Runtime.Log.Info("missing trello App Key")

View file

@ -41,7 +41,7 @@ func (h *Handler) SMTP(w http.ResponseWriter, r *http.Request) {
return
}
config := h.Store.Setting.Get("SMTP", "")
config, _ := h.Store.Setting.Get("SMTP", "")
var y map[string]interface{}
json.Unmarshal([]byte(config), &y)
@ -101,7 +101,7 @@ func (h *Handler) License(w http.ResponseWriter, r *http.Request) {
return
}
config := h.Store.Setting.Get("EDITION-LICENSE", "")
config, _ := h.Store.Setting.Get("EDITION-LICENSE", "")
if len(config) == 0 {
config = "{}"
}

View file

@ -14,7 +14,6 @@ package mysql
import (
"bytes"
"database/sql"
"fmt"
"github.com/documize/community/core/env"
"github.com/documize/community/core/streamutil"
@ -27,7 +26,7 @@ type Scope struct {
}
// Get fetches a configuration JSON element from the config table.
func (s Scope) Get(area, path string) (value string) {
func (s Scope) Get(area, path string) (value string, err error) {
if path != "" {
path = "." + path
}
@ -37,16 +36,14 @@ func (s Scope) Get(area, path string) (value string) {
defer streamutil.Close(stmt)
if err != nil {
s.Runtime.Log.Error(fmt.Sprintf("setting.Get %s %s", area, path), err)
return ""
return "", err
}
var item = make([]uint8, 0)
err = stmt.Get(&item)
if err != nil {
s.Runtime.Log.Error(fmt.Sprintf("setting.Get %s %s", area, path), err)
return ""
return "", err
}
if len(item) > 1 {
@ -54,7 +51,7 @@ func (s Scope) Get(area, path string) (value string) {
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
}
return value
return value, nil
}
// Set writes a configuration JSON element to the config table.
@ -81,7 +78,7 @@ func (s Scope) Set(area, json string) error {
// GetUser fetches a configuration JSON element from the userconfig table for a given orgid/userid combination.
// Errors return the empty string. A blank path returns the whole JSON object, as JSON.
func (s Scope) GetUser(orgID, userID, area, path string) (value string) {
func (s Scope) GetUser(orgID, userID, area, path string) (value string, err error) {
if path != "" {
path = "." + path
}
@ -93,15 +90,14 @@ func (s Scope) GetUser(orgID, userID, area, path string) (value string) {
defer streamutil.Close(stmt)
if err != nil {
return ""
return "", err
}
var item = make([]uint8, 0)
err = stmt.Get(&item)
if err != nil && err != sql.ErrNoRows {
s.Runtime.Log.Error(fmt.Sprintf("setting.GetUser for user %s %s %s", userID, area, path), err)
return ""
return "", err
}
if len(item) > 1 {
@ -109,7 +105,7 @@ func (s Scope) GetUser(orgID, userID, area, path string) (value string) {
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
}
return value
return value, nil
}
// SetUser writes a configuration JSON element to the userconfig table for the current user.

View file

@ -145,9 +145,9 @@ type DocumentStorer interface {
// SettingStorer defines required methods for persisting global and user level settings
type SettingStorer interface {
Get(area, path string) string
Get(area, path string) (val string, err error)
Set(area, value string) error
GetUser(orgID, userID, area, path string) string
GetUser(orgID, userID, area, path string) (val string, err error)
SetUser(orgID, userID, area, json string) error
}

View file

@ -32,6 +32,7 @@ import (
"github.com/documize/community/core/uniqueid"
"github.com/documize/community/domain"
"github.com/documize/community/domain/mail"
"github.com/documize/community/domain/organization"
"github.com/documize/community/model/account"
"github.com/documize/community/model/audit"
"github.com/documize/community/model/space"
@ -566,6 +567,7 @@ func (h *Handler) UserSpacePermissions(w http.ResponseWriter, r *http.Request) {
func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
method := "user.ForgotPassword"
ctx := domain.GetRequestContext(r)
ctx.Subdomain = organization.GetSubdomainFromHost(r)
defer streamutil.Close(r.Body)
body, err := ioutil.ReadAll(r.Body)
@ -619,6 +621,7 @@ func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request) {
method := "user.ForgotUserPassword"
ctx := domain.GetRequestContext(r)
ctx.Subdomain = organization.GetSubdomainFromHost(r)
token := request.Param(r, "token")
if len(token) == 0 {