github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/endpoint/global_endpoint.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>.
     9  //
    10  // https://documize.com
    11  
    12  package endpoint
    13  
    14  import (
    15  	"encoding/json"
    16  	"io/ioutil"
    17  	"net/http"
    18  
    19  	"github.com/documize/community/core/api/request"
    20  	"github.com/documize/community/core/api/util"
    21  )
    22  
    23  // GetGlobalConfig returns installation-wide settings
    24  func GetGlobalConfig(w http.ResponseWriter, r *http.Request) {
    25  	method := "GetGlobalConfig"
    26  	p := request.GetPersister(r)
    27  
    28  	if !p.Context.Global {
    29  		writeForbiddenError(w)
    30  		return
    31  	}
    32  
    33  	// SMTP settings
    34  	config := request.ConfigString("SMTP", "")
    35  
    36  	// marshall as JSON
    37  	var y map[string]interface{}
    38  	json.Unmarshal([]byte(config), &y)
    39  
    40  	json, err := json.Marshal(y)
    41  	if err != nil {
    42  		writeJSONMarshalError(w, method, "GetGlobalConfig", err)
    43  		return
    44  	}
    45  
    46  	util.WriteSuccessBytes(w, json)
    47  }
    48  
    49  // SaveGlobalConfig persists global configuration.
    50  func SaveGlobalConfig(w http.ResponseWriter, r *http.Request) {
    51  	method := "SaveGlobalConfig"
    52  	p := request.GetPersister(r)
    53  
    54  	if !p.Context.Global {
    55  		writeForbiddenError(w)
    56  		return
    57  	}
    58  
    59  	defer r.Body.Close()
    60  
    61  	body, err := ioutil.ReadAll(r.Body)
    62  	if err != nil {
    63  		writePayloadError(w, method, err)
    64  		return
    65  	}
    66  
    67  	var config string
    68  	config = string(body)
    69  
    70  	tx, err := request.Db.Beginx()
    71  	if err != nil {
    72  		writeTransactionError(w, method, err)
    73  		return
    74  	}
    75  
    76  	p.Context.Transaction = tx
    77  
    78  	request.ConfigSet("SMTP", config)
    79  
    80  	util.WriteSuccessEmptyJSON(w)
    81  }