aboutsummaryrefslogtreecommitdiff
path: root/memberof.go
blob: c4074e498aee1c158256948ab2cd70b5707c4001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)
	}
}