Пример #1
0
    def __call__(self, REQUEST=None, src__=0, tst__=0, **kw):
        """ call the object """
        if REQUEST is None:
            if kw: REQUEST = kw
            else:
                if getattr(self, 'REQUEST', None) is not None: REQUEST=self.REQUEST
                else: REQUEST={}
        c = self._getConn()
        if not c:
            raise LDAPError('LDAP Connection not open')

        if getattr(self, 'aq_parent', None) is not None:
            p = self.aq_parent
        else: p = None

        argdata = self._argdata(REQUEST)  #use our BaseQuery's magic.  :)

        # Also need the authenticated user.
        auth_user = REQUEST.get('AUTHENTICATED_USER', None)
        if auth_user is None:
            auth_user = getattr(self, 'REQUEST', None)
            if auth_user is not None:
                try: auth_user = auth_user.get('AUTHENTICATED_USER', None)
                except: auth_user = None

        if auth_user is not None:
            if getSecurityManager is None:
                # working in a pre-Zope 2.2.x instance
                from AccessControl.User import verify_watermark
                verify_watermark(auth_user)
                argdata['AUTHENTICATED_USER'] = auth_user

        f = Filter(self.filters)        # make a FilterTemplate
        f.cook()
        if getSecurityManager is None:
            # working in a pre-Zope 2.2 instance
            f = apply(f, (p,argdata))       #apply the template
        else:
            # Working with the new security manager (Zope 2.2.x ++)
            security = getSecurityManager()
            security.addContext(self)
            try:     f = apply(f, (p,), argdata)  # apply the template
            finally: security.removeContext(self)

        f = str(f)                      #ensure it's a string
        if src__: return f              #return the rendered source
        f = self.cleanse(f)
        ### run the search
        res = c.search_s(self.basedn, self._scope, f)
        if tst__: return res            #return test-friendly data

        ### instantiate Entry objects based on results
        l = []                          #list of entries to return
        conn=self._connection()         #ZLDAPConnection
        Entry = conn._EntryFactory()
        for dn, attrdict in res:
            e = Entry(dn, attrdict, conn).__of__(self)
            l.append(e)

        return l
Пример #2
0
  def __call__(self, REQUEST=None, src__=0, tst__=0, **kw):
    """ call the object """
    if REQUEST is None:
      if kw: REQUEST = kw
      else:
        if hasattr(self, 'REQUEST'): REQUEST = self.REQUEST
        else: REQUEST={}
    c = self._connection().GetConnection()
    if not c:
      raise LDAPError('LDAP Connection not open')

    if hasattr(self, 'aq_parent'):
      p = self.aq_parent
    else: p = None

    argdata = self._argdata(REQUEST)  #use our BaseQuery's magic.  :)
    argdata['basedn'] = self.basedn
    # Also need the authenticated user.
    auth_user = REQUEST.get('AUTHENTICATED_USER', None)
    if auth_user is None:
      auth_user = getattr(self, 'REQUEST', None)
      if auth_user is not None:
        try: auth_user = auth_user.get('AUTHENTICATED_USER', None)
        except: auth_user = None

    if auth_user is not None:
      if getSecurityManager is None:
        # working in a pre-Zope 2.2.x instance
        from AccessControl.User import verify_watermark
        verify_watermark(auth_user)
        argdata['AUTHENTICATED_USER'] = auth_user

    ldif = Ldif(self.ldif)        # make a FilterTemplate
    ldif.cook()
    if getSecurityManager is None:
      # working in a pre-Zope 2.2 instance
      ldif = apply(ldif, (p, argdata))       #apply the template
    else:
      # Working with the new security manager (Zope 2.2.x ++)
      security = getSecurityManager()
      security.addContext(self)
      try:     ldif = apply(ldif, (p,), argdata)  # apply the template
      finally: security.removeContext(self)

    ldif = str(ldif)                      #ensure it's a string
    #LOG('ldif', 0, ldif)
    if src__: return ldif              #return the rendered source
    ### Apply Query
    from cStringIO import StringIO
    file = StringIO(ldif)
    l = ERP5LDIFRecordList(file)
    l.parse()
    res = l.all_records

    def delete(c, dn):
      try:
        c.delete_s(dn)
      except ldap.NO_SUCH_OBJECT:
        pass
      except:
        LOG('ldif', INFO, ldif)
        raise

    def add(c, dn, mod_list):
      try:
        c.add_s(dn, mod_list)
      except ldap.ALREADY_EXISTS:
        pass
      except:
        LOG('ldif', INFO, ldif)
        raise

    for record in res:
      dn = record[0]
      entry = record[1]
      if type(entry) == type({}):
        authorized_modify_key = [key for key in entry.keys() if key in CHANGE_TYPES]
        if len(authorized_modify_key):
          for key in authorized_modify_key:
            tuple_list = entry[key]
            if key == 'delete':
              try:
                delete(c, dn)
              except ldap.SERVER_DOWN:
                c = self._connection().getForcedConnection()
                delete(c, dn)
            else:
              for mod_tuple in tuple_list:
                c.modify_s(dn, mod_tuple)
        else:
          mod_list = modlist.addModlist(entry)
          try:
            add(c, dn, mod_list)
          except ldap.SERVER_DOWN:
            c = self._connection().getForcedConnection()
            add(c, dn, mod_list)
      else:
        LOG('LDIFMethod Type unknow', INFO, '')
    return res
Пример #3
0
    def __call__(self, REQUEST=None, src__=0, tst__=0, **kw):
        """ call the object """
        if REQUEST is None:
            if kw: REQUEST = kw
            else:
                if hasattr(self, 'REQUEST'): REQUEST = self.REQUEST
                else: REQUEST = {}
        c = self._connection().GetConnection()
        if not c:
            raise LDAPError('LDAP Connection not open')

        if hasattr(self, 'aq_parent'):
            p = self.aq_parent
        else:
            p = None

        argdata = self._argdata(REQUEST)  #use our BaseQuery's magic.  :)
        argdata['basedn'] = self.basedn
        # Also need the authenticated user.
        auth_user = REQUEST.get('AUTHENTICATED_USER', None)
        if auth_user is None:
            auth_user = getattr(self, 'REQUEST', None)
            if auth_user is not None:
                try:
                    auth_user = auth_user.get('AUTHENTICATED_USER', None)
                except:
                    auth_user = None

        if auth_user is not None:
            if getSecurityManager is None:
                # working in a pre-Zope 2.2.x instance
                from AccessControl.User import verify_watermark
                verify_watermark(auth_user)
                argdata['AUTHENTICATED_USER'] = auth_user

        ldif = Ldif(self.ldif)  # make a FilterTemplate
        ldif.cook()
        if getSecurityManager is None:
            # working in a pre-Zope 2.2 instance
            ldif = apply(ldif, (p, argdata))  #apply the template
        else:
            # Working with the new security manager (Zope 2.2.x ++)
            security = getSecurityManager()
            security.addContext(self)
            try:
                ldif = apply(ldif, (p, ), argdata)  # apply the template
            finally:
                security.removeContext(self)

        ldif = str(ldif)  #ensure it's a string
        #LOG('ldif', 0, ldif)
        if src__: return ldif  #return the rendered source
        ### Apply Query
        from cStringIO import StringIO
        file = StringIO(ldif)
        l = ERP5LDIFRecordList(file)
        l.parse()
        res = l.all_records

        def delete(c, dn):
            try:
                c.delete_s(dn)
            except ldap.NO_SUCH_OBJECT:
                pass
            except:
                LOG('ldif', INFO, ldif)
                raise

        def add(c, dn, mod_list):
            try:
                c.add_s(dn, mod_list)
            except ldap.ALREADY_EXISTS:
                pass
            except:
                LOG('ldif', INFO, ldif)
                raise

        for record in res:
            dn = record[0]
            entry = record[1]
            if type(entry) == type({}):
                authorized_modify_key = [
                    key for key in entry.keys() if key in CHANGE_TYPES
                ]
                if len(authorized_modify_key):
                    for key in authorized_modify_key:
                        tuple_list = entry[key]
                        if key == 'delete':
                            try:
                                delete(c, dn)
                            except ldap.SERVER_DOWN:
                                c = self._connection().getForcedConnection()
                                delete(c, dn)
                        else:
                            for mod_tuple in tuple_list:
                                c.modify_s(dn, mod_tuple)
                else:
                    mod_list = modlist.addModlist(entry)
                    try:
                        add(c, dn, mod_list)
                    except ldap.SERVER_DOWN:
                        c = self._connection().getForcedConnection()
                        add(c, dn, mod_list)
            else:
                LOG('LDIFMethod Type unknow', INFO, '')
        return res
Пример #4
0
    def __call__(self, REQUEST=None, src__=0, tst__=0, **kw):
        """ call the object """
        if REQUEST is None:
            if kw: REQUEST = kw
            else:
                if getattr(self, 'REQUEST', None) is not None:
                    REQUEST = self.REQUEST
                else:
                    REQUEST = {}
        c = self._getConn()
        if not c:
            raise LDAPError('LDAP Connection not open')

        if getattr(self, 'aq_parent', None) is not None:
            p = self.aq_parent
        else:
            p = None

        argdata = self._argdata(REQUEST)  #use our BaseQuery's magic.  :)

        # Also need the authenticated user.
        auth_user = REQUEST.get('AUTHENTICATED_USER', None)
        if auth_user is None:
            auth_user = getattr(self, 'REQUEST', None)
            if auth_user is not None:
                try:
                    auth_user = auth_user.get('AUTHENTICATED_USER', None)
                except:
                    auth_user = None

        if auth_user is not None:
            if getSecurityManager is None:
                # working in a pre-Zope 2.2.x instance
                from AccessControl.User import verify_watermark
                verify_watermark(auth_user)
                argdata['AUTHENTICATED_USER'] = auth_user

        f = Filter(self.filters)  # make a FilterTemplate
        f.cook()
        if getSecurityManager is None:
            # working in a pre-Zope 2.2 instance
            f = apply(f, (p, argdata))  #apply the template
        else:
            # Working with the new security manager (Zope 2.2.x ++)
            security = getSecurityManager()
            security.addContext(self)
            try:
                f = apply(f, (p, ), argdata)  # apply the template
            finally:
                security.removeContext(self)

        f = str(f)  #ensure it's a string
        if src__: return f  #return the rendered source
        f = self.cleanse(f)
        ### run the search
        res = c.search_s(self.basedn, self._scope, f)
        if tst__: return res  #return test-friendly data

        ### instantiate Entry objects based on results
        l = []  #list of entries to return
        conn = self._connection()  #ZLDAPConnection
        Entry = conn._EntryFactory()
        for dn, attrdict in res:
            e = Entry(dn, attrdict, conn).__of__(self)
            l.append(e)

        return l