1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-22 06:39:43 +02:00
documize/domain/setting/store.go

134 lines
3.4 KiB
Go
Raw Normal View History

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