Port group-add to use LDAP backend

Have create and update return the record that was just added/modified
This commit is contained in:
Rob Crittenden 2008-10-15 17:46:01 -04:00
parent 14a33d4619
commit 1a8317ff74
2 changed files with 42 additions and 32 deletions

View File

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

View File

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