aboutsummaryrefslogtreecommitdiff
path: root/memberof.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-13 14:14:27 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-13 14:41:49 +0100
commit49be2069f63d8f2909840ad504063c773d0ba7ed (patch)
treebee9df56230ff484822d4b83510d72bd1aa72f7f /memberof.go
parent6f893138a3b593e6918c3dc1bd04d12f01d6c4a5 (diff)
downloadbottin-49be2069f63d8f2909840ad504063c773d0ba7ed.tar.gz
bottin-49be2069f63d8f2909840ad504063c773d0ba7ed.zip
Refactor memberOf management logic
Diffstat (limited to 'memberof.go')
-rw-r--r--memberof.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/memberof.go b/memberof.go
new file mode 100644
index 0000000..c4074e4
--- /dev/null
+++ b/memberof.go
@@ -0,0 +1,60 @@
+package main
+
+func (server *Server) memberOfAdd(member string, group string) {
+ // Retreive previous memberOf value
+ memberGroups, err := server.getAttribute(member, ATTR_MEMBEROF)
+ if err != nil {
+ server.logger.Warnf("Could not add %s to memberOf of %s: %s", group, member, err)
+ return
+ }
+
+ // Return early if group is already in memberOf
+ for _, mb := range memberGroups {
+ if mb == group {
+ server.logger.Warnf("Inconsistency detected, %s was memberOf %s at a time when it didn't exist! (not an issue anymore)",
+ member, group)
+ return
+ }
+ }
+
+ // Add group to memberOf
+ memberGroups = append(memberGroups, group)
+ err = server.addElements(member, Entry{
+ ATTR_MEMBEROF: memberGroups,
+ })
+ if err != nil {
+ server.logger.Warnf("Could not add %s to memberOf of %s: %s", group, member, err)
+ }
+}
+
+func (server *Server) memberOfRemove(member string, group string) {
+ // Retreive previous memberOf value
+ memberOf, err := server.getAttribute(member, ATTR_MEMBEROF)
+ if err != nil || memberOf == nil {
+ server.logger.Warnf("Could not remove %s from memberOf of %s: %s", group, member, err)
+ return
+ }
+
+ // Filter out group
+ newMemberOf := []string{}
+ for _, mb := range memberOf {
+ if mb != group {
+ newMemberOf = append(newMemberOf, group)
+ }
+ }
+
+ // Return early if group already not in memberOf
+ if len(newMemberOf) == len(memberOf) {
+ server.logger.Warnf("Inconsistency detected, %s was not memberOf %s at a time when it should have been! (not an issue anymore)",
+ member, group)
+ return
+ }
+
+ // Update value of memberOf
+ err = server.addElements(member, Entry{
+ ATTR_MEMBEROF: newMemberOf,
+ })
+ if err != nil {
+ server.logger.Warnf("Could not remove %s from memberOf of %s: %s", group, member, err)
+ }
+}