aboutsummaryrefslogtreecommitdiff
path: root/write.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-03-09 18:30:59 +0100
committerAlex Auvolat <alex@adnab.me>2021-03-09 18:30:59 +0100
commit1a20a64eff34336789c0e3b41db1de727e3fe998 (patch)
tree27e32e575eeace4260f21f76969f6e1ab5c8b5c2 /write.go
parentdc3fd4df659bb35d7858714a429fc797bf5f1222 (diff)
downloadbottin-1a20a64eff34336789c0e3b41db1de727e3fe998.tar.gz
bottin-1a20a64eff34336789c0e3b41db1de727e3fe998.zip
Refactoring
Diffstat (limited to 'write.go')
-rw-r--r--write.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/write.go b/write.go
index 41d00c6..e4c7de1 100644
--- a/write.go
+++ b/write.go
@@ -1,14 +1,58 @@
package main
import (
+ "encoding/json"
"fmt"
"strings"
ldap "bottin/ldapserver"
+ consul "github.com/hashicorp/consul/api"
message "github.com/lor00x/goldap/message"
)
+// Generic item modification function --------
+
+func (server *Server) putAttributes(dn string, attrs Entry) error {
+ prefix, err := dnToConsul(dn)
+ if err != nil {
+ return err
+ }
+
+ for k, valuesNC := range attrs {
+ path := prefix + "/attribute=" + k
+
+ // Trim spaces and remove empty values
+ values := []string{}
+ for _, v := range valuesNC {
+ vv := strings.TrimSpace(v)
+ if len(vv) > 0 {
+ values = append(values, vv)
+ }
+ }
+
+ // If we have zero values, delete associated k/v pair
+ // Otherwise, write new values
+ if len(values) == 0 {
+ _, err := server.kv.Delete(path, nil)
+ if err != nil {
+ return err
+ }
+ } else {
+ json, err := json.MarshalIndent(values, "", " ")
+ if err != nil {
+ return err
+ }
+ pair := &consul.KVPair{Key: path, Value: json}
+ _, err = server.kv.Put(pair, nil)
+ if err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
+
// Add request ------------------------
func (server *Server) handleAdd(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {