Logo Search packages:      
Sourcecode: zope-ldapuserfolder version File versions

def LDAPUserFolder::LDAPDelegate::LDAPDelegate::modify (   self,
  dn,
  mod_type = None,
  attrs = None 
)

Modify a record 

Definition at line 379 of file LDAPDelegate.py.

00379                                                    :
        """ Modify a record """
        if self.read_only:
            return 'Running in read-only mode, modification is disabled'

        res = self.search(base=dn, scope=BASE)
        attrs = attrs and attrs or {}

        if res['exception']:
            return res['exception']

        if res['size'] == 0:
            return 'LDAPDelegate.modify: Cannot find dn "%s"' % dn

        cur_rec = res['results'][0]
        mod_list = []
        msg = ''

        for key, values in attrs.items():
            values = map(to_utf8, values)

            if mod_type is None:
                if cur_rec.get(key, ['']) != values and values != ['']:
                    mod_list.append((REPLACE, key, values))
                elif cur_rec.has_key(key) and values == ['']:
                    mod_list.append((DELETE, key, None))
            else:
                mod_list.append((mod_type, key, values))

        try:
            connection = self.connect()
            connection.modify_s(dn, mod_list)
        except ldap.INVALID_CREDENTIALS, e:
            e_name = e.__class__.__name__
            msg = '%s No permission to modify "%s"' % (e_name, dn)
        except ldap.REFERRAL, e:
            try:
                connection = self.handle_referral(e)
                connection.modify_s(dn, mod_list)
            except ldap.INVALID_CREDENTIALS, e:
                e_name = e.__class__.__name__
                msg = '%s No permission to modify "%s"' % (e_name, dn)
            except Exception, e:
                e_name = e.__class__.__name__
                msg = '%s LDAPDelegate.modify: %s' % (e_name, str(e))
        except Exception, e:
            e_name = e.__class__.__name__
            msg = '%s LDAPDelegate.modify: %s' % (e_name, str(e))

        if msg:
            return msg

        rdn = explode_dn(dn)[0]
        rdn_attr = rdn[:rdn.find('=')]
        new_rdn = attrs.get(rdn_attr, [''])[0]

        if new_rdn and new_rdn != cur_rec.get(rdn_attr):
            new_dn = '%s=%s' % (rdn_attr, new_rdn)
            try:
                connection.modrdn_s(dn, new_dn, 0)
            except ldap.INVALID_CREDENTIALS, e:
                e_name = e.__class__.__name__
                msg = '%s No permission to modify "%s"' % (e_name, dn)
            except ldap.REFERRAL, e:
                try:
                    connection = self.handle_referral(e)
                    connection.modrdn_s(dn, new_dn, 0)
                except ldap.INVALID_CREDENTIALS, e:
                    e_name = e.__class__.__name__
                    msg = '%s No permission to modify "%s"' % (e_name, dn)
                except Exception, e:
                    e_name = e.__class__.__name__
                    msg = '%s LDAPDelegate.modify: %s' % (e_name, str(e))
            except Exception, e:
                e_name = e.__class__.__name__
                msg = '%s LDAPDelegate.modify: %s' % (e_name, str(e))


        return msg
        return msg


Generated by  Doxygen 1.6.0   Back to index