aboutsummaryrefslogblamecommitdiff
path: root/memberof.go
blob: c4074e498aee1c158256948ab2cd70b5707c4001 (plain) (tree)



























































                                                                                                                                                
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)
	}
}