aboutsummaryrefslogtreecommitdiff
path: root/memberof.go
diff options
context:
space:
mode:
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)
+ }
+}