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
|
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
package setting
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
import (
|
2019-10-07 15:44:07 +01:00
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2017-07-26 20:03:23 +01:00
|
|
|
|
2019-10-07 15:44:07 +01:00
|
|
|
"github.com/pkg/errors"
|
2019-07-18 10:14:41 -04:00
|
|
|
|
2019-10-07 15:44:07 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2017-07-26 20:03:23 +01:00
|
|
|
)
|
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
// Store provides data access to user permission information.
|
|
|
|
type Store struct {
|
|
|
|
store.Context
|
2018-09-27 15:14:48 +01:00
|
|
|
store.SettingStorer
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get fetches a configuration JSON element from the config table.
|
2018-09-26 17:59:56 +01:00
|
|
|
func (s Store) Get(area, path string) (value string, err error) {
|
|
|
|
qry := fmt.Sprintf("SELECT %s FROM dmz_config WHERE c_key = '%s';", s.GetJSONValue("c_config", path), area)
|
2017-07-26 20:03:23 +01:00
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
item := []byte{}
|
2018-09-26 17:59:56 +01:00
|
|
|
err = s.Runtime.Db.Get(&item, qry)
|
2017-07-26 20:03:23 +01:00
|
|
|
if err != nil {
|
2017-08-27 16:39:09 +01:00
|
|
|
return "", err
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(item) > 1 {
|
|
|
|
q := []byte(`"`)
|
|
|
|
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
|
|
|
|
}
|
|
|
|
|
2017-08-27 16:39:09 +01:00
|
|
|
return value, nil
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set writes a configuration JSON element to the config table.
|
2018-09-26 17:59:56 +01:00
|
|
|
func (s Store) Set(area, json string) (err error) {
|
2017-07-26 20:03:23 +01:00
|
|
|
if area == "" {
|
|
|
|
return errors.New("no area")
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
tx, err := s.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 20:03:23 +01:00
|
|
|
|
2019-10-07 15:44:07 +01:00
|
|
|
_, err = tx.Exec(s.Bind("DELETE FROM dmz_config WHERE c_key = ?"), area)
|
2018-09-26 17:59:56 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
tx.Rollback()
|
|
|
|
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:44:07 +01:00
|
|
|
_, err = tx.Exec(s.Bind("INSERT INTO dmz_config (c_key,c_config) VALUES (?, ?)"), area, json)
|
2018-09-26 17:59:56 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
tx.Rollback()
|
|
|
|
s.Runtime.Log.Error(fmt.Sprintf("setting.Set %s", area), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-08-06 19:39:31 +01:00
|
|
|
// You can store org level settings by providing an empty user ID.
|
2018-09-26 17:59:56 +01:00
|
|
|
func (s Store) GetUser(orgID, userID, key, path string) (value string, err error) {
|
2018-08-06 19:39:31 +01:00
|
|
|
var item = make([]uint8, 0)
|
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
qry := fmt.Sprintf("SELECT %s FROM dmz_user_config WHERE c_key = '%s' AND c_orgid='%s' AND c_userid='%s';",
|
2018-09-26 17:59:56 +01:00
|
|
|
s.GetJSONValue("c_config", path), key, orgID, userID)
|
2017-07-26 20:03:23 +01:00
|
|
|
|
2017-09-25 14:37:11 +01:00
|
|
|
err = s.Runtime.Db.Get(&item, qry)
|
2017-08-08 11:24:21 +01:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2017-08-27 16:39:09 +01:00
|
|
|
return "", err
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(item) > 1 {
|
|
|
|
q := []byte(`"`)
|
|
|
|
value = string(bytes.TrimPrefix(bytes.TrimSuffix(item, q), q))
|
|
|
|
}
|
|
|
|
|
2017-08-27 16:39:09 +01:00
|
|
|
return value, nil
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:39:31 +01:00
|
|
|
// SetUser writes a configuration JSON element to the userconfig table for the specified user.
|
|
|
|
// You can store org level settings by providing an empty user ID.
|
2018-09-26 17:59:56 +01:00
|
|
|
func (s Store) SetUser(orgID, userID, key, json string) (err error) {
|
2018-08-06 19:39:31 +01:00
|
|
|
if key == "" {
|
|
|
|
return errors.New("no key")
|
2017-07-26 20:03:23 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:39:31 +01:00
|
|
|
tx, err := s.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-26 20:03:23 +01:00
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
_, err = tx.Exec(s.Bind("DELETE FROM dmz_user_config WHERE c_orgid=? AND c_userid=? AND c_key=?"),
|
|
|
|
orgID, userID, key)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:59:56 +01:00
|
|
|
_, err = tx.Exec(s.Bind("INSERT INTO dmz_user_config (c_orgid, c_userid, c_key, c_config) VALUES (?, ?, ?, ?)"),
|
|
|
|
orgID, userID, key, json)
|
2018-08-06 19:39:31 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
tx.Commit()
|
|
|
|
} else {
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|