def add_entry_to_group(self, dn, group_dn, member_attr='member', allow_same=False): """ Add entry designaed by dn to group group_dn in the member attribute member_attr. Adding a group as a member of itself is not allowed unless allow_same is True. """ assert isinstance(dn, DN) assert isinstance(group_dn, DN) self.log.debug( "add_entry_to_group: dn=%s group_dn=%s member_attr=%s", dn, group_dn, member_attr) # check if the entry exists entry = self.get_entry(dn, ['']) dn = entry.dn # check if we're not trying to add group into itself if dn == group_dn and not allow_same: raise errors.SameGroupError() # add dn to group entry's `member_attr` attribute modlist = [(_ldap.MOD_ADD, member_attr, [dn])] # update group entry try: with self.error_handler(): modlist = [(a, b, self.encode(c)) for a, b, c in modlist] self.conn.modify_s(str(group_dn), modlist) except errors.DatabaseError: raise errors.AlreadyGroupMember()
def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options): """ Add memberPrincipal values. This is done afterward because it isn't a DN and the LDAPAddMember method explicitly only handles DNs. A separate fake attribute name is used for failed members. This is a reverse of the way this is typically handled in the *Member routines, where a successful addition will be represented as member/memberof_<attribute>. In this case, because memberPrincipal isn't a DN, I'm doing the reverse, and creating a fake failed attribute instead. """ ldap = self.obj.backend members = [] failed[self.principal_failedattr] = {} failed[self.principal_failedattr][self.principal_attr] = [] names = options.get(self.member_names[self.principal_attr], []) ldap_obj = self.api.Object['service'] if names: for name in names: if not name: continue name = normalize_principal(name) obj_dn = ldap_obj.get_dn(name) try: ldap.get_entry(obj_dn, ['krbprincipalname']) except errors.NotFound as e: failed[self.principal_failedattr][ self.principal_attr].append((name, unicode(e))) continue try: if name not in entry_attrs.get(self.principal_attr, []): members.append(name) else: raise errors.AlreadyGroupMember() except errors.PublicError as e: failed[self.principal_failedattr][ self.principal_attr].append((name, unicode(e))) else: completed += 1 if members: value = entry_attrs.setdefault(self.principal_attr, []) value.extend(members) try: ldap.update_entry(entry_attrs) except errors.EmptyModlist: pass return completed, dn