2017-07-26 20:03:23 +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 setting manages both global and user level settings
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/documize/community/core/env"
|
|
|
|
"github.com/documize/community/core/event"
|
|
|
|
"github.com/documize/community/core/response"
|
|
|
|
"github.com/documize/community/domain"
|
|
|
|
"github.com/documize/community/model/audit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler contains the runtime information such as logging and database.
|
|
|
|
type Handler struct {
|
|
|
|
Runtime *env.Runtime
|
|
|
|
Store *domain.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMTP returns installation-wide SMTP settings
|
|
|
|
func (h *Handler) SMTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "setting.SMTP"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
config := h.Store.Setting.Get("SMTP", "")
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
var y map[string]interface{}
|
|
|
|
json.Unmarshal([]byte(config), &y)
|
|
|
|
|
|
|
|
j, err := json.Marshal(y)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteBytes(w, j)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSMTP persists global SMTP configuration.
|
|
|
|
func (h *Handler) SetSMTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "setting.SetSMTP"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var config string
|
|
|
|
config = string(body)
|
|
|
|
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
h.Store.Setting.Set("SMTP", config)
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSystemSMTP)
|
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// License returns product license
|
|
|
|
func (h *Handler) License(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
config := h.Store.Setting.Get("EDITION-LICENSE", "")
|
2017-07-26 20:03:23 +01:00
|
|
|
if len(config) == 0 {
|
|
|
|
config = "{}"
|
|
|
|
}
|
|
|
|
|
|
|
|
x := &licenseXML{Key: "", Signature: ""}
|
|
|
|
lj := licenseJSON{}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(config), &lj)
|
|
|
|
if err == nil {
|
|
|
|
x.Key = lj.Key
|
|
|
|
x.Signature = lj.Signature
|
|
|
|
} else {
|
|
|
|
h.Runtime.Log.Error("failed to JSON unmarshal EDITION-LICENSE", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := xml.Marshal(x)
|
|
|
|
if err != nil {
|
|
|
|
h.Runtime.Log.Error("failed to XML marshal EDITION-LICENSE", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteBytes(w, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLicense persists product license
|
|
|
|
func (h *Handler) SetLicense(w http.ResponseWriter, r *http.Request) {
|
|
|
|
method := "setting.SetLicense"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var config string
|
|
|
|
config = string(body)
|
|
|
|
lj := licenseJSON{}
|
|
|
|
x := licenseXML{Key: "", Signature: ""}
|
|
|
|
|
2017-08-10 12:38:05 +01:00
|
|
|
err1 := xml.Unmarshal([]byte(config), &x)
|
|
|
|
if err1 == nil {
|
2017-07-26 20:03:23 +01:00
|
|
|
lj.Key = x.Key
|
|
|
|
lj.Signature = x.Signature
|
|
|
|
} else {
|
|
|
|
h.Runtime.Log.Error("failed to XML unmarshal EDITION-LICENSE", err)
|
|
|
|
}
|
|
|
|
|
2017-08-10 12:38:05 +01:00
|
|
|
j, err2 := json.Marshal(lj)
|
2017-07-26 20:03:23 +01:00
|
|
|
js := "{}"
|
2017-08-10 12:38:05 +01:00
|
|
|
if err2 == nil {
|
2017-07-26 20:03:23 +01:00
|
|
|
js = string(j)
|
2017-08-10 12:38:05 +01:00
|
|
|
} else {
|
|
|
|
h.Runtime.Log.Error("failed to JSON marshal EDITION-LICENSE", err2)
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
h.Store.Setting.Set("EDITION-LICENSE", js)
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Transaction.Commit()
|
|
|
|
|
2017-08-10 12:38:05 +01:00
|
|
|
h.Runtime.Log.Info("License changed")
|
|
|
|
event.Handler().Publish(string(event.TypeSystemLicenseChange), h.Runtime, h.Store)
|
|
|
|
|
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSystemLicense)
|
|
|
|
|
2017-07-26 20:03:23 +01:00
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthConfig returns installation-wide auth configuration
|
|
|
|
func (h *Handler) AuthConfig(w http.ResponseWriter, r *http.Request) {
|
2017-08-03 10:00:24 +01:00
|
|
|
method := "global.auth"
|
2017-07-26 20:03:23 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteForbiddenError(w)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, org.AuthConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAuthConfig persists installation-wide authentication configuration
|
|
|
|
func (h *Handler) SetAuthConfig(w http.ResponseWriter, r *http.Request) {
|
2017-08-03 10:00:24 +01:00
|
|
|
method := "global.auth.save"
|
2017-07-26 20:03:23 +01:00
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
|
|
|
if !ctx.Global {
|
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var data authData
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteBadRequestError(w, method, err.Error())
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
org.AuthProvider = data.AuthProvider
|
|
|
|
org.AuthConfig = data.AuthConfig
|
|
|
|
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.Store.Organization.UpdateAuthConfig(ctx, org)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-07-26 20:03:23 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Store.Audit.Record(ctx, audit.EventTypeSystemAuth)
|
|
|
|
|
|
|
|
ctx.Transaction.Commit()
|
|
|
|
|
|
|
|
response.WriteEmpty(w)
|
|
|
|
}
|