def testKnownValues(self):
        for s, l in self.knownValues:
            fromString = dn.DistinguishedName(s)
            listOfRDNs = []
            for av in l:
                listOfAttributeTypesAndValues = []
                for a,v in av:
                    listOfAttributeTypesAndValues.append(dn.LDAPAttributeTypeAndValue(attributeType=a, value=v))
                r=dn.RelativeDistinguishedName(listOfAttributeTypesAndValues)
                listOfRDNs.append(r)
            fromList = dn.DistinguishedName(listOfRDNs)

            self.assertEqual(fromString, fromList)

            fromStringToText = fromString.getText()
            fromListToText = fromList.getText()

            assert fromStringToText == fromListToText

            canon = fromStringToText
            # DNs equal their byte string representation. Note this does
            # not mean they equal all the possible string
            # representations -- just the canonical one.
            self.assertEqual(fromString, canon)
            self.assertEqual(fromList, canon)
            self.assertEqual(canon, fromString)
            self.assertEqual(canon, fromList)

            # DNs can be used interchangeably with their canonical
            # string representation as hash keys.
            self.assertEqual(hash(fromString), hash(canon))
            self.assertEqual(hash(fromList), hash(canon))
            self.assertEqual(hash(canon), hash(fromString))
            self.assertEqual(hash(canon), hash(fromList))
Beispiel #2
0
 def testOpenLDAPEqualsEscape(self):
     """Slapd wants = to be escaped in RDN attributeValues."""
     got = dn.DistinguishedName(listOfRDNs=[
         dn.RelativeDistinguishedName(attributeTypesAndValues=[
             dn.LDAPAttributeTypeAndValue(attributeType='cn',
                                          value=r'test'),
             dn.LDAPAttributeTypeAndValue(attributeType='owner',
                                          value=r'uid=foo,ou=depart' +
                                          r'ment,dc=example,dc=com'),
         ]),
         dn.RelativeDistinguishedName('dc=example'),
         dn.RelativeDistinguishedName('dc=com'),
     ])
     got = str(got)
     self.assertEqual(
         got, r'cn=test+owner=uid\=foo\,ou\=depar' +
         r'tment\,dc\=example\,dc\=com,dc=ex' + r'ample,dc=com')
Beispiel #3
0
    def add(self, context, **kw):
        cfg = context.locate(interfaces.ILDAPConfig)
        dnAttr = self._getDNAttr()
        assert kw.has_key(
            'add_' + dnAttr), 'Must have attribute dn %s points to.' % dnAttr
        assert kw['add_' +
                  dnAttr], 'Attribute %s must have value.' % 'add_' + dnAttr
        # TODO ugly
        rdn = distinguishedname.RelativeDistinguishedName(
            attributeTypesAndValues=[
                distinguishedname.LDAPAttributeTypeAndValue(
                    attributeType=dnAttr, value=kw['add_' + dnAttr]),
            ])

        #TODO verify
        changes = []
        for k, v in kw.items():
            if hasattr(self, "nonUserEditableAttributeType_" + k):
                raise "Can't set attribute %s when adding." % k
            elif k[:len("add_")] == "add_":
                if not v:
                    continue
                attrtype = self._get_attrtype(k[len("add_"):])
                assert attrtype

                if attrtype.single_value or attrtype.uiHint_multiline:
                    v = [v]
                else:
                    v = self._textarea_to_list(v)

                if v and [1 for x in v if x]:
                    attr = k[len("add_"):]
                    changes.append(defer.succeed((attr, v)))
                    #TODO

        for attributeType in self.nonUserEditableAttributes:
            thing = getattr(self,
                            'nonUserEditableAttributeType_' + attributeType)
            if callable(thing):
                changes.append(thing(attributeType, context))
            else:
                changes.append(defer.succeed((attributeType, thing)))

        dl = defer.DeferredList(changes, fireOnOneErrback=1)

        #dl.addErrback(lambda x: x[0]) # throw away index
        def _pruneSuccessFlags(l):
            r = []
            for succeeded, result in l:
                assert succeeded
                r.append(result)
            return r

        dl.addCallback(_pruneSuccessFlags)
        dl.addCallback(self._process2, context, rdn, kw)
        return dl
Beispiel #4
0
    def _cbSetPassword(self, ctx, newPassword, serviceName):
        e = getEntry(ctx, self.dn)
        rdn = distinguishedname.RelativeDistinguishedName(
            attributeTypesAndValues=[
                distinguishedname.LDAPAttributeTypeAndValue(attributeType='cn',
                                                            value=serviceName),
                distinguishedname.LDAPAttributeTypeAndValue(
                    attributeType='owner', value=str(self.dn))
            ])
        d = e.addChild(
            rdn, {
                'objectClass': ['serviceSecurityObject'],
                'cn': [serviceName],
                'owner': [str(self.dn)],
                'userPassword': ['{crypt}!'],
            })

        def _setPass(e, newPassword):
            d = e.setPassword(newPassword)
            return d

        d.addCallback(_setPass, newPassword)
        return d