Skip to content

Commit 8081b60

Browse files
committed
set defaults for request context
1 parent 63b24ae commit 8081b60

File tree

23 files changed

+692
-666
lines changed

23 files changed

+692
-666
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The mission is to bring software dev inspired features (refactoring, testing, li
88

99
## Latest version
1010

11-
v1.53.0
11+
v1.53.1
1212

1313
## OS Support
1414

core/api/plugins/glick.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func Setup(s *domain.Store) error {
9999

100100
var json = make([]byte, 0)
101101
if PluginFile == "DB" {
102-
json = []byte(s.Setting.Get("FILEPLUGINS", ""))
102+
c, _ := s.Setting.Get("FILEPLUGINS", "")
103+
json = []byte(c)
103104
if len(bytes.TrimSpace(json)) == 0 {
104105
return nil // don't fail if the DB does not exist yet
105106
}

core/database/create.go

-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
6060
dbname := r.Form.Get("dbname")
6161
dbhash := r.Form.Get("dbhash")
6262

63-
h.Runtime.Log.Info(dbname)
64-
h.Runtime.Log.Info(dbhash)
65-
h.Runtime.Log.Info(web.SiteInfo.DBname)
66-
h.Runtime.Log.Info(web.SiteInfo.DBhash)
67-
6863
if dbname != web.SiteInfo.DBname || dbhash != web.SiteInfo.DBhash {
6964
h.Runtime.Log.Error("database.Create()'s security credentials error ", errors.New("bad db name or validation code"))
7065
return

domain/auth/endpoint.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
6868
}
6969

7070
dom := strings.TrimSpace(strings.ToLower(credentials[0]))
71-
dom = h.Store.Organization.CheckDomain(ctx, dom) // TODO optimize by removing this once js allows empty domains
7271
email := strings.TrimSpace(strings.ToLower(credentials[1]))
7372
password := credentials[2]
73+
74+
dom = h.Store.Organization.CheckDomain(ctx, dom) // TODO optimize by removing this once js allows empty domains
75+
7476
h.Runtime.Log.Info("logon attempt " + email + " @ " + dom)
7577

7678
u, err := h.Store.User.GetByDomain(ctx, dom, email)
@@ -108,6 +110,8 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
108110
return
109111
}
110112

113+
h.Runtime.Log.Info("login " + email + " @ " + dom)
114+
111115
authModel := auth.AuthenticationModel{}
112116
authModel.Token = GenerateJWT(h.Runtime, u.RefID, org.RefID, dom)
113117
authModel.User = u

domain/context.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,9 @@ func GetRequestContext(r *http.Request) (ctx RequestContext) {
6464
return
6565
}
6666

67-
return RequestContext{}
67+
ctx = RequestContext{}
68+
ctx.AppURL = r.Host
69+
ctx.SSL = r.TLS != nil
70+
71+
return
6872
}

domain/conversion/conversion.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (h *Handler) convert(w http.ResponseWriter, r *http.Request, job, folderID
9696
method := "conversion.upload"
9797
ctx := domain.GetRequestContext(r)
9898

99-
licenseKey := h.Store.Setting.Get("EDITION-LICENSE", "key")
100-
licenseSignature := h.Store.Setting.Get("EDITION-LICENSE", "signature")
99+
licenseKey, _ := h.Store.Setting.Get("EDITION-LICENSE", "key")
100+
licenseSignature, _ := h.Store.Setting.Get("EDITION-LICENSE", "signature")
101101
k, _ := hex.DecodeString(licenseKey)
102102
s, _ := hex.DecodeString(licenseSignature)
103103

domain/mail/mailer.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,18 @@ func (m *Mailer) GetHost() string {
291291

292292
// LoadCredentials loads up SMTP details from database
293293
func (m *Mailer) LoadCredentials() {
294-
m.Credentials.SMTPuserid = strings.TrimSpace(m.Store.Setting.Get("SMTP", "userid"))
295-
m.Credentials.SMTPpassword = strings.TrimSpace(m.Store.Setting.Get("SMTP", "password"))
296-
m.Credentials.SMTPhost = strings.TrimSpace(m.Store.Setting.Get("SMTP", "host"))
297-
m.Credentials.SMTPport = strings.TrimSpace(m.Store.Setting.Get("SMTP", "port"))
298-
m.Credentials.SMTPsender = strings.TrimSpace(m.Store.Setting.Get("SMTP", "sender"))
294+
userID, _ := m.Store.Setting.Get("SMTP", "userid")
295+
m.Credentials.SMTPuserid = strings.TrimSpace(userID)
296+
297+
pwd, _ := m.Store.Setting.Get("SMTP", "password")
298+
m.Credentials.SMTPpassword = strings.TrimSpace(pwd)
299+
300+
host, _ := m.Store.Setting.Get("SMTP", "host")
301+
m.Credentials.SMTPhost = strings.TrimSpace(host)
302+
303+
port, _ := m.Store.Setting.Get("SMTP", "port")
304+
m.Credentials.SMTPport = strings.TrimSpace(port)
305+
306+
sender, _ := m.Store.Setting.Get("SMTP", "sender")
307+
m.Credentials.SMTPsender = strings.TrimSpace(sender)
299308
}

domain/organization/mysql/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (s Scope) GetOrganization(ctx domain.RequestContext, id string) (org org.Or
8080
// No context is required because user might no be authenticated yet.
8181
func (s Scope) GetOrganizationByDomain(subdomain string) (org org.Organization, err error) {
8282
err = nil
83-
subdomain = strings.ToLower(subdomain)
83+
subdomain = strings.TrimSpace(strings.ToLower(subdomain))
8484

8585
if s.Runtime.Flags.SiteMode == env.SiteModeNormal { // only return an organization when running normally
8686
var stmt *sqlx.Stmt

domain/section/github/auth.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ import (
2525
)
2626

2727
func clientID(ctx domain.RequestContext, s *domain.Store) string {
28-
return s.Setting.Get(meta.ConfigHandle(), "clientID")
28+
c, _ := s.Setting.Get(meta.ConfigHandle(), "clientID")
29+
return c
2930
}
3031

3132
func clientSecret(ctx domain.RequestContext, s *domain.Store) string {
32-
return s.Setting.Get(meta.ConfigHandle(), "clientSecret")
33+
c, _ := s.Setting.Get(meta.ConfigHandle(), "clientSecret")
34+
return c
3335
}
3436

3537
func authorizationCallbackURL(ctx domain.RequestContext, s *domain.Store) string {
3638
// NOTE: URL value must have the path and query "/api/public/validate?section=github"
37-
return s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
39+
c, _ := s.Setting.Get(meta.ConfigHandle(), "authorizationCallbackURL")
40+
return c
3841
}
3942

4043
func validateToken(ctx provider.Context, s *domain.Store, ptoken string) error {

domain/section/provider/provider.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ func (c *Context) MarshalSecrets(sec interface{}, s *domain.Store) error {
222222
// Errors return the empty string.
223223
func (c *Context) GetSecrets(JSONpath string, s *domain.Store) string {
224224
m := c.prov.Meta()
225-
return s.Setting.GetUser(c.OrgID, c.UserID, m.ContentType, JSONpath)
225+
v, _ := s.Setting.GetUser(c.OrgID, c.UserID, m.ContentType, JSONpath)
226+
227+
return v
226228
}
227229

228230
// ErrNoSecrets is returned if no secret is found in the database.

domain/section/trello/trello.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
6969
}
7070

7171
config.Clean()
72-
config.AppKey = p.Store.Setting.Get(meta.ConfigHandle(), "appKey")
72+
config.AppKey, _ = p.Store.Setting.Get(meta.ConfigHandle(), "appKey")
7373

7474
if len(config.AppKey) == 0 {
7575
p.Runtime.Log.Info("missing trello App Key")

domain/setting/endpoint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (h *Handler) SMTP(w http.ResponseWriter, r *http.Request) {
4141
return
4242
}
4343

44-
config := h.Store.Setting.Get("SMTP", "")
44+
config, _ := h.Store.Setting.Get("SMTP", "")
4545

4646
var y map[string]interface{}
4747
json.Unmarshal([]byte(config), &y)
@@ -101,7 +101,7 @@ func (h *Handler) License(w http.ResponseWriter, r *http.Request) {
101101
return
102102
}
103103

104-
config := h.Store.Setting.Get("EDITION-LICENSE", "")
104+
config, _ := h.Store.Setting.Get("EDITION-LICENSE", "")
105105
if len(config) == 0 {
106106
config = "{}"
107107
}

domain/setting/mysql/store.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package mysql
1414
import (
1515
"bytes"
1616
"database/sql"
17-
"fmt"
1817

1918
"github.com/documize/community/core/env"
2019
"github.com/documize/community/core/streamutil"
@@ -27,7 +26,7 @@ type Scope struct {
2726
}
2827

2928
// Get fetches a configuration JSON element from the config table.
30-
func (s Scope) Get(area, path string) (value string) {
29+
func (s Scope) Get(area, path string) (value string, err error) {
3130
if path != "" {
3231
path = "." + path
3332
}
@@ -37,24 +36,22 @@ func (s Scope) Get(area, path string) (value string) {
3736
defer streamutil.Close(stmt)
3837

3938
if err != nil {
40-
s.Runtime.Log.Error(fmt.Sprintf("setting.Get %s %s", area, path), err)
41-
return ""
39+
return "", err
4240
}
4341

4442
var item = make([]uint8, 0)
4543

4644
err = stmt.Get(&item)
4745
if err != nil {
48-
s.Runtime.Log.Error(fmt.Sprintf("setting.Get %s %s", area, path), err)
49-
return ""
46+
return "", err
5047
}
5148

5249
if len(item) > 1 {
5350
q := []byte(`"`)
5451
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
5552
}
5653

57-
return value
54+
return value, nil
5855
}
5956

6057
// Set writes a configuration JSON element to the config table.
@@ -81,7 +78,7 @@ func (s Scope) Set(area, json string) error {
8178

8279
// GetUser fetches a configuration JSON element from the userconfig table for a given orgid/userid combination.
8380
// Errors return the empty string. A blank path returns the whole JSON object, as JSON.
84-
func (s Scope) GetUser(orgID, userID, area, path string) (value string) {
81+
func (s Scope) GetUser(orgID, userID, area, path string) (value string, err error) {
8582
if path != "" {
8683
path = "." + path
8784
}
@@ -93,23 +90,22 @@ func (s Scope) GetUser(orgID, userID, area, path string) (value string) {
9390
defer streamutil.Close(stmt)
9491

9592
if err != nil {
96-
return ""
93+
return "", err
9794
}
9895

9996
var item = make([]uint8, 0)
10097

10198
err = stmt.Get(&item)
10299
if err != nil && err != sql.ErrNoRows {
103-
s.Runtime.Log.Error(fmt.Sprintf("setting.GetUser for user %s %s %s", userID, area, path), err)
104-
return ""
100+
return "", err
105101
}
106102

107103
if len(item) > 1 {
108104
q := []byte(`"`)
109105
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
110106
}
111107

112-
return value
108+
return value, nil
113109
}
114110

115111
// SetUser writes a configuration JSON element to the userconfig table for the current user.

domain/storer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ type DocumentStorer interface {
145145

146146
// SettingStorer defines required methods for persisting global and user level settings
147147
type SettingStorer interface {
148-
Get(area, path string) string
148+
Get(area, path string) (val string, err error)
149149
Set(area, value string) error
150-
GetUser(orgID, userID, area, path string) string
150+
GetUser(orgID, userID, area, path string) (val string, err error)
151151
SetUser(orgID, userID, area, json string) error
152152
}
153153

domain/user/endpoint.go

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/documize/community/core/uniqueid"
3333
"github.com/documize/community/domain"
3434
"github.com/documize/community/domain/mail"
35+
"github.com/documize/community/domain/organization"
3536
"github.com/documize/community/model/account"
3637
"github.com/documize/community/model/audit"
3738
"github.com/documize/community/model/space"
@@ -566,6 +567,7 @@ func (h *Handler) UserSpacePermissions(w http.ResponseWriter, r *http.Request) {
566567
func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
567568
method := "user.ForgotPassword"
568569
ctx := domain.GetRequestContext(r)
570+
ctx.Subdomain = organization.GetSubdomainFromHost(r)
569571

570572
defer streamutil.Close(r.Body)
571573
body, err := ioutil.ReadAll(r.Body)
@@ -619,6 +621,7 @@ func (h *Handler) ForgotPassword(w http.ResponseWriter, r *http.Request) {
619621
func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request) {
620622
method := "user.ForgotUserPassword"
621623
ctx := domain.GetRequestContext(r)
624+
ctx.Subdomain = organization.GetSubdomainFromHost(r)
622625

623626
token := request.Param(r, "token")
624627
if len(token) == 0 {

edition/community.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
rt.Product = env.ProdInfo{}
3939
rt.Product.Major = "1"
4040
rt.Product.Minor = "53"
41-
rt.Product.Patch = "0"
41+
rt.Product.Patch = "1"
4242
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
4343
rt.Product.Edition = "Community"
4444
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)

0 commit comments

Comments
 (0)