def test_setProxies(self):
        """
        Read and Write proxies can be set en masse
        """
        directory = self.directory
        record = yield recordForPrincipalID(directory, "users:user01")

        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(readProxies, [])  # initially empty
        self.assertEquals(writeProxies, [])  # initially empty

        readProxies = [
            (yield recordForPrincipalID(directory, "users:user03")),
            (yield recordForPrincipalID(directory, "users:user04")),
        ]
        writeProxies = [(yield recordForPrincipalID(directory, "users:user05"))]
        yield setProxies(record, readProxies, writeProxies)

        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(set([r.uid for r in readProxies]), set(["user03", "user04"]))
        self.assertEquals(set([r.uid for r in writeProxies]), set(["user05"]))

        # Using None for a proxy list indicates a no-op
        yield setProxies(record, [], None)
        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(readProxies, [])  # now empty
        self.assertEquals(set([r.uid for r in writeProxies]), set(["user05"]))  # unchanged
    def test_setProxies(self):
        """
        Read and Write proxies can be set en masse
        """
        directory = self.directory
        record = yield recordForPrincipalID(directory, "users:user01")

        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(readProxies, [])  # initially empty
        self.assertEquals(writeProxies, [])  # initially empty

        readProxies = [
            (yield recordForPrincipalID(directory, "users:user03")),
            (yield recordForPrincipalID(directory, "users:user04")),
        ]
        writeProxies = [
            (yield recordForPrincipalID(directory, "users:user05")),
        ]
        yield setProxies(record, readProxies, writeProxies)

        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(set([r.uid for r in readProxies]),
                          set(["user03", "user04"]))
        self.assertEquals(set([r.uid for r in writeProxies]), set(["user05"]))

        # Using None for a proxy list indicates a no-op
        yield setProxies(record, [], None)
        readProxies, writeProxies = yield getProxies(record)
        self.assertEquals(readProxies, [])  # now empty
        self.assertEquals(set([r.uid for r in writeProxies]),
                          set(["user05"]))  # unchanged
    def test_setProxies(self):
        """
        Read and Write proxies can be set en masse
        """
        directory = directoryFromConfig(config)

        principal = principalForPrincipalID("users:user01", directory=directory)
        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
        self.assertEquals(readProxies, []) # initially empty
        self.assertEquals(writeProxies, []) # initially empty

        (yield setProxies(None, principal, ["users:user03", "users:user04"], ["users:user05"], directory=directory))
        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
        self.assertEquals(set(readProxies), set(["user03", "user04"]))
        self.assertEquals(set(writeProxies), set(["user05"]))

        # Using None for a proxy list indicates a no-op
        (yield setProxies(None, principal, [], None, directory=directory))
        readProxies, writeProxies = (yield getProxies(principal, directory=directory))
        self.assertEquals(readProxies, []) # now empty
        self.assertEquals(set(writeProxies), set(["user05"])) # unchanged
    def command_createLocation(self, command):
        kwargs = {}
        for key, info in attrMap.iteritems():
            if key in command:
                kwargs[info['attr']] = command[key]

        try:
            record = (yield updateRecord(True, self.dir, "locations", **kwargs))
        except DirectoryError, e:
            self.respondWithError(str(e))
            return

        readProxies = command.get("ReadProxies", None)
        writeProxies = command.get("WriteProxies", None)
        principal = principalForPrincipalID(record.guid, directory=self.dir)
        (yield setProxies(self.store, principal, readProxies, writeProxies, directory=self.dir))

        self.respondWithRecordsOfTypes(self.dir, command, ["locations"])


    @inlineCallbacks
    def command_getLocationAttributes(self, command):
        guid = command['GeneratedUID']
        record = self.dir.recordWithGUID(guid)
        if record is None:
            self.respondWithError("Principal not found: %s" % (guid,))
            return
        recordDict = recordToDict(record)
        principal = principalForPrincipalID(guid, directory=self.dir)
        if principal is None:
            self.respondWithError("Principal not found: %s" % (guid,))
Exemple #5
0
    def _saveRecord(self, typeName, recordType, command, oldFields=None):
        """
        Save a record using the values in the command plist, starting with
        any fields in the optional oldFields.

        @param typeName: one of "locations", "resources", "addresses"; used
            to return the appropriate list of records afterwards.
        @param recordType: the type of record to save
        @param command: the command containing values
        @type command: C{dict}
        @param oldFields: the optional fields to start with, which will be
            overridden by values from command
        @type oldFiles: C{dict}
        """

        if oldFields is None:
            fields = {
                self.dir.fieldName.recordType: recordType,
                self.dir.fieldName.hasCalendars: True,
                self.dir.fieldName.hasContacts: True,
            }
            create = True
        else:
            fields = oldFields.copy()
            create = False

        for key, info in attrMap.iteritems():
            if key in command:
                attrName = info['attr']
                field = self.dir.fieldName.lookupByName(attrName)
                valueType = self.dir.fieldName.valueType(field)
                value = command[key]

                # For backwards compatibility, convert to a list if needed
                if (
                    self.dir.fieldName.isMultiValue(field) and
                    not isinstance(value, list)
                ):
                    value = [value]

                if valueType == int:
                    value = int(value)
                elif issubclass(valueType, Names):
                    if value is not None:
                        value = valueType.lookupByName(value)
                else:
                    if isinstance(value, list):
                        newList = []
                        for item in value:
                            if isinstance(item, str):
                                newList.append(item.decode("utf-8"))
                            else:
                                newList.append(item)
                        value = newList
                    elif isinstance(value, str):
                        value = value.decode("utf-8")

                fields[field] = value

        if FieldName.uid not in fields:
            # No uid provided, so generate one
            fields[FieldName.uid] = unicode(uuid.uuid4()).upper()

        if FieldName.shortNames not in fields:
            # No short names were provided, so copy from uid
            fields[FieldName.shortNames] = [fields[FieldName.uid]]

        record = DirectoryRecord(self.dir, fields)
        yield self.dir.updateRecords([record], create=create)

        readProxies = command.get("ReadProxies", None)
        if readProxies:
            proxyRecords = []
            for proxyUID in readProxies:
                proxyRecord = yield self.dir.recordWithUID(proxyUID)
                if proxyRecord is not None:
                    proxyRecords.append(proxyRecord)
            readProxies = proxyRecords

        writeProxies = command.get("WriteProxies", None)
        if writeProxies:
            proxyRecords = []
            for proxyUID in writeProxies:
                proxyRecord = yield self.dir.recordWithUID(proxyUID)
                if proxyRecord is not None:
                    proxyRecords.append(proxyRecord)
            writeProxies = proxyRecords

        yield setProxies(record, readProxies, writeProxies)

        yield self.respondWithRecordsOfTypes(self.dir, command, [typeName])
Exemple #6
0
    def _saveRecord(self, typeName, recordType, command, oldFields=None):
        """
        Save a record using the values in the command plist, starting with
        any fields in the optional oldFields.

        @param typeName: one of "locations", "resources", "addresses"; used
            to return the appropriate list of records afterwards.
        @param recordType: the type of record to save
        @param command: the command containing values
        @type command: C{dict}
        @param oldFields: the optional fields to start with, which will be
            overridden by values from command
        @type oldFiles: C{dict}
        """

        if oldFields is None:
            fields = {
                self.dir.fieldName.recordType: recordType,
                self.dir.fieldName.hasCalendars: True,
                self.dir.fieldName.hasContacts: True,
            }
            create = True
        else:
            fields = oldFields.copy()
            create = False

        for key, info in attrMap.iteritems():
            if key in command:
                attrName = info['attr']
                field = self.dir.fieldName.lookupByName(attrName)
                valueType = self.dir.fieldName.valueType(field)
                value = command[key]

                # For backwards compatibility, convert to a list if needed
                if (self.dir.fieldName.isMultiValue(field)
                        and not isinstance(value, list)):
                    value = [value]

                if valueType == int:
                    value = int(value)
                elif issubclass(valueType, Names):
                    if value is not None:
                        value = valueType.lookupByName(value)
                else:
                    if isinstance(value, list):
                        newList = []
                        for item in value:
                            if isinstance(item, str):
                                newList.append(item.decode("utf-8"))
                            else:
                                newList.append(item)
                        value = newList
                    elif isinstance(value, str):
                        value = value.decode("utf-8")

                fields[field] = value

        if FieldName.uid not in fields:
            # No uid provided, so generate one
            fields[FieldName.uid] = unicode(uuid.uuid4()).upper()

        if FieldName.shortNames not in fields:
            # No short names were provided, so copy from uid
            fields[FieldName.shortNames] = [fields[FieldName.uid]]

        record = DirectoryRecord(self.dir, fields)
        yield self.dir.updateRecords([record], create=create)

        readProxies = command.get("ReadProxies", None)
        if readProxies:
            proxyRecords = []
            for proxyUID in readProxies:
                proxyRecord = yield self.dir.recordWithUID(proxyUID)
                if proxyRecord is not None:
                    proxyRecords.append(proxyRecord)
            readProxies = proxyRecords

        writeProxies = command.get("WriteProxies", None)
        if writeProxies:
            proxyRecords = []
            for proxyUID in writeProxies:
                proxyRecord = yield self.dir.recordWithUID(proxyUID)
                if proxyRecord is not None:
                    proxyRecords.append(proxyRecord)
            writeProxies = proxyRecords

        yield setProxies(record, readProxies, writeProxies)

        yield self.respondWithRecordsOfTypes(self.dir, command, [typeName])