From 1a8317ff7471214811d39ab846d402dc22a03779 Mon Sep 17 00:00:00 2001 From: Rob Crittenden Date: Wed, 15 Oct 2008 17:46:01 -0400 Subject: [PATCH] Port group-add to use LDAP backend Have create and update return the record that was just added/modified --- ipa_server/plugins/b_ldap.py | 17 +++++++++-- ipalib/plugins/f_group.py | 57 +++++++++++++++++------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/ipa_server/plugins/b_ldap.py b/ipa_server/plugins/b_ldap.py index b40a957f8..dca9b3fea 100644 --- a/ipa_server/plugins/b_ldap.py +++ b/ipa_server/plugins/b_ldap.py @@ -48,6 +48,16 @@ class ldap(CrudBackend): self.api.env.basedn, ) + def make_group_dn(self, cn): + """ + Construct user dn from cn. + """ + return 'cn=%s,%s,%s' % ( + self.dn.escape_dn_chars(cn), + self.api.env.container_group, + self.api.env.basedn, + ) + def find_entry_dn(self, key_attribute, primary_key, object_type=None): """ Find an existing entry's dn from an attribute @@ -113,7 +123,8 @@ class ldap(CrudBackend): for k in kw: entry.setValues(k, kw[k]) - return servercore.add_entry(entry) + servercore.add_entry(entry) + return self.retrieve(entry.dn) def retrieve(self, dn, attributes=None): return servercore.get_entry_by_dn(dn, attributes) @@ -126,7 +137,9 @@ class ldap(CrudBackend): for k in kw: entry.setValues(k, kw[k]) - return servercore.update_entry(entry.toDict()) + servercore.update_entry(entry.toDict()) + + return self.retrieve(dn) def delete(self, dn): return servercore.delete_entry(dn) diff --git a/ipalib/plugins/f_group.py b/ipalib/plugins/f_group.py index eeb18c5cf..a07d314be 100644 --- a/ipalib/plugins/f_group.py +++ b/ipalib/plugins/f_group.py @@ -38,6 +38,7 @@ class group(frontend.Object): takes_params = ( 'description', Param('cn', + cli_name='name', primary_key=True, normalize=lambda value: value.lower(), ) @@ -47,47 +48,43 @@ api.register(group) class group_add(crud.Add): 'Add a new group.' - def execute(self, *args, **kw): - """args[0] = uid of the group to add - kw{container} is the location in the DIT to add the group, not - required - kw otherwise contains all the attributes + + def execute(self, cn, **kw): """ - # FIXME: ug, really? - if not kw.get('container'): - group_container = servercore.DefaultGroupContainer - else: - group_container = kw['container'] - del kw['container'] + Execute the group-add operation. - group = kw + The dn should not be passed as a keyword argument as it is constructed + by this method. - group['cn'] = args[0] + Returns the entry as it will be created in LDAP. + + No need to explicitly set gidNumber. The dna_plugin will do this + for us if the value isn't provided by the caller. + + :param cn: The name of the group being added. + :param kw: Keyword arguments for the other LDAP attributes. + """ + assert 'cn' not in kw + assert 'dn' not in kw + ldap = self.api.Backend.ldap + kw['cn'] = cn + kw['dn'] = ldap.make_group_dn(cn) # Get our configuration config = servercore.get_ipa_config() - dn="cn=%s,%s,%s" % (ldap.dn.escape_dn_chars(group['cn']), - group_container,servercore.basedn) - - entry = ipaldap.Entry(dn) - # some required objectclasses - entry.setValues('objectClass', (config.get('ipagroupobjectclasses'))) + kw['objectClass'] = config.get('ipagroupobjectclasses') - # No need to explicitly set gidNumber. The dna_plugin will do this - # for us if the value isn't provided by the user. + return ldap.create(**kw) - # fill in our new entry with everything sent by the user - for g in group: - entry.setValues(g, group[g]) + def output_for_cli(self, ret): + """ + Output result of this command to command line interface. + """ + if ret: + print "Group added" - result = servercore.add_entry(entry) - return result - def forward(self, *args, **kw): - result = super(crud.Add, self).forward(*args, **kw) - if result: - print "Group %s added" % args[0] api.register(group_add)