Derive Entity class from Entry, and move it to ldapupdate

The two classes were nearly identical, and the updater is
the only code that uses Entity.

Part of the work for: https://fedorahosted.org/freeipa/ticket/2660
This commit is contained in:
Petr Viktorin
2013-01-10 09:50:22 -05:00
committed by Martin Kosek
parent 83f99070d6
commit 7e1495b404
3 changed files with 37 additions and 137 deletions

View File

@@ -1,131 +0,0 @@
# Copyright (C) 2007 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import copy
from ipapython import ipautil
from ipapython.dn import DN
def copy_CIDict(x):
"""Do a deep copy of a CIDict"""
y = {}
for key, value in x.iteritems():
y[copy.deepcopy(key)] = copy.deepcopy(value)
return y
class Entity:
"""This class represents an IPA user. An LDAP entry consists of a DN
and a list of attributes. Each attribute consists of a name and a list of
values. For the time being I will maintain this.
In python-ldap, entries are returned as a list of 2-tuples.
Instance variables:
dn - string - the string DN of the entry
data - CIDict - case insensitive dict of the attributes and values
orig_data - CIDict - case insentiive dict of the original attributes and values"""
def __init__(self,entrydata=None):
"""data is the raw data returned from the python-ldap result method,
which is a search result entry or a reference or None.
If creating a new empty entry, data is the string DN."""
if entrydata:
if isinstance(entrydata,tuple):
self.dn = entrydata[0]
self.data = ipautil.CIDict(entrydata[1])
elif isinstance(entrydata, DN):
self.dn = entrydata
self.data = ipautil.CIDict()
elif isinstance(entrydata, basestring):
self.dn = DN(entrydata)
self.data = ipautil.CIDict()
elif isinstance(entrydata,dict):
if hasattr(entrydata, 'dn'):
entrydata['dn'] = entrydata.dn
self.dn = entrydata['dn']
del entrydata['dn']
self.data = ipautil.CIDict(entrydata)
else:
self.dn = DN()
self.data = ipautil.CIDict()
assert isinstance(self.dn, DN)
self.orig_data = ipautil.CIDict(copy_CIDict(self.data))
dn = ipautil.dn_attribute_property('_dn')
def __nonzero__(self):
"""This allows us to do tests like if entry: returns false if there is no data,
true otherwise"""
return self.data != None and len(self.data) > 0
def __str__(self):
return "dn: %s data: %s" % (self.dn, self.data)
def getValues(self,name):
"""Get the list (array) of values for the attribute named name"""
return self.data.get(name)
def getValue(self,name,default=None):
"""Get the first value for the attribute named name"""
value = self.data.get(name,default)
if isinstance(value,list) or isinstance(value,tuple):
return value[0]
else:
return value
def setValue(self,name,*value):
"""Value passed in may be a single value, several values, or a single sequence.
For example:
ent.setValue('name', 'value')
ent.setValue('name', 'value1', 'value2', ..., 'valueN')
ent.setValue('name', ['value1', 'value2', ..., 'valueN'])
ent.setValue('name', ('value1', 'value2', ..., 'valueN'))
Since *value is a tuple, we may have to extract a list or tuple from that
tuple as in the last two examples above"""
if (len(value) < 1):
return
if (len(value) == 1):
self.data[name] = ipautil.utf8_encode_values(value[0])
else:
self.data[name] = ipautil.utf8_encode_values(value)
setValues = setValue
def toTupleList(self):
"""Convert the attrs and values to a list of 2-tuples. The first element
of the tuple is the attribute name. The second element is either a
single value or a list of values."""
return self.data.items()
def toDict(self):
"""Convert the attrs and values to a dict. The dict is keyed on the
attribute name. The value is either single value or a list of values."""
assert isinstance(self.dn, DN)
result = ipautil.CIDict(self.data)
result['dn'] = self.dn
return result
def attrList(self):
"""Return a list of all attributes in the entry"""
return self.data.keys()
def origDataDict(self):
"""Returns a dict of the original values of the user. Used for updates."""
assert isinstance(self.dn, DN)
result = ipautil.CIDict(self.orig_data)
result['dn'] = self.dn
return result

View File

@@ -33,6 +33,7 @@ import pwd
import fnmatch
import csv
import inspect
import copy
import krbV
import ldap
@@ -40,13 +41,37 @@ from ldap.schema.models import ObjectClass, AttributeType
from ipaserver.install import installutils
from ipaserver import ipaldap
from ipapython import entity, ipautil
from ipapython import ipautil
from ipalib import errors
from ipalib import api
from ipapython.dn import DN
from ipapython.ipa_log_manager import *
from ipaserver.install.plugins import PRE_UPDATE, POST_UPDATE
class Entity(ipaldap.Entry):
# TODO: Use ldap2 instead
def __init__(self, entrydata=None):
ipaldap.Entry.__init__(self, entrydata)
y = {}
for key, value in self.data.iteritems():
y[copy.deepcopy(key)] = copy.deepcopy(value)
self.orig_data = ipautil.CIDict(y)
def attrList(self):
"""Return a list of all attributes in the entry"""
return self.data.keys()
def origDataDict(self):
"""Returns a dict of the original values of the user.
Used for updates.
"""
result = ipautil.CIDict(self.orig_data)
result['dn'] = self.dn
return result
class BadSyntax(installutils.ScriptError):
def __init__(self, value):
self.value = value
@@ -255,7 +280,7 @@ class LDAPUpdate:
entry[key] = ''
elif len(value) == 1:
entry[key] = value[0]
return entity.Entity(entry)
return Entity(entry)
def _combine_updates(self, all_updates, update):
'Combine a new update with the list of total updates'
@@ -483,7 +508,7 @@ class LDAPUpdate:
def _create_default_entry(self, dn, default):
"""Create the default entry from the values provided.
The return type is entity.Entity
The return type is Entity
"""
assert isinstance(dn, DN)
entry = ipaldap.Entry(dn)

View File

@@ -38,7 +38,6 @@ from ipapython import ipautil
from ipalib import errors
from ipapython.ipautil import format_netloc, wait_for_open_socket, wait_for_open_ports
from ipapython.dn import DN
from ipapython.entity import Entity
from ipaserver.plugins.ldap2 import IPASimpleLDAPObject, LDAPEntry
# Global variable to define SASL auth
@@ -113,6 +112,12 @@ class Entry:
elif isinstance(entrydata, basestring):
self.dn = DN(entrydata)
self.data = ipautil.CIDict()
elif isinstance(entrydata, dict):
if hasattr(entrydata, 'dn'):
entrydata['dn'] = entrydata.dn
self.dn = entrydata['dn']
del entrydata['dn']
self.data = ipautil.CIDict(entrydata)
else:
raise TypeError("entrydata must be 2-tuple, DN, or basestring, got %s" % type(entrydata))
else:
@@ -205,6 +210,7 @@ class Entry:
ldif.LDIFWriter(sio,Entry.base64_attrs,1000).unparse(str(self.dn),newdata)
return sio.getvalue()
class IPAdmin(IPAEntryLDAPObject):
def __localinit(self):
@@ -437,8 +443,8 @@ class IPAdmin(IPAEntryLDAPObject):
"""This wraps the add function. It assumes that the entry is already
populated with all of the desired objectclasses and attributes"""
if not isinstance(entry, (Entry, Entity)):
raise TypeError('addEntry expected an Entry or Entity object, got %s instead' % entry.__class__)
if not isinstance(entry, Entry):
raise TypeError('addEntry expected an Entry object, got %s instead' % entry.__class__)
sctrl = self.__get_server_controls()