def action_setAutoAcceptGroup(store, record, autoAcceptGroup): if record.recordType == RecordType.group: print("Setting auto-accept-group for {record} is not allowed.".format( record=prettyRecord(record))) elif (record.recordType == RecordType.user and not config.Scheduling.Options.AutoSchedule.AllowUsers): print("Setting auto-accept-group for {record} is not allowed.".format( record=prettyRecord(record))) else: groupRecord = yield recordForPrincipalID(record.service, autoAcceptGroup) if groupRecord is None or groupRecord.recordType != RecordType.group: print("Invalid principal ID: {id}".format(id=autoAcceptGroup)) else: print("Setting auto-accept-group to {group} for {record}".format( group=prettyRecord(groupRecord), record=prettyRecord(record), )) # Get original fields newFields = record.fields.copy() # Set new values newFields[ record.service.fieldName.autoAcceptGroup] = groupRecord.uid updatedRecord = DirectoryRecord(record.service, newFields) yield record.service.updateRecords([updatedRecord], create=False)
def changeRecord(self, record, fieldname, value, directory=None): if directory is None: directory = self.directory fields = record.fields.copy() fields[fieldname] = value updatedRecord = DirectoryRecord(directory, fields) return directory.updateRecords((updatedRecord, ))
def action_setAutoScheduleMode(store, record, autoScheduleMode): if record.recordType == RecordType.group: print( "Setting auto-schedule-mode for {record} is not allowed.".format( record=prettyRecord(record) ) ) elif ( record.recordType == RecordType.user and not config.Scheduling.Options.AutoSchedule.AllowUsers ): print( "Setting auto-schedule-mode for {record} is not allowed.".format( record=prettyRecord(record) ) ) else: print( "Setting auto-schedule-mode to {mode} for {record}".format( mode=autoScheduleMode.description, record=prettyRecord(record), ) ) # Get original fields newFields = record.fields.copy() # Set new values newFields[record.service.fieldName.autoScheduleMode] = autoScheduleMode updatedRecord = DirectoryRecord(record.service, newFields) yield record.service.updateRecords([updatedRecord], create=False)
def runAddPrincipal(service, store, addType, uid, shortNames, fullNames): directory = store.directoryService() recordType = directory.oldNameToRecordType(addType) # See if that UID is in use record = yield directory.recordWithUID(uid) if record is not None: print("UID already in use: {uid}".format(uid=uid)) returnValue(None) # See if the shortnames are in use for shortName in shortNames: record = yield directory.recordWithShortName(recordType, shortName) if record is not None: print("Record name already in use: {name}".format(name=shortName)) returnValue(None) fields = { directory.fieldName.recordType: recordType, directory.fieldName.uid: uid, directory.fieldName.shortNames: shortNames, directory.fieldName.fullNames: fullNames, directory.fieldName.hasCalendars: True, directory.fieldName.hasContacts: True, } record = DirectoryRecord(directory, fields) yield record.service.updateRecords([record], create=True) print("Added '{name}'".format(name=fullNames[0]))
def updateRecords(self, records, create=False): """ Pull out the augmented fields from each record, apply those to the augments database, then update the base records. """ baseRecords = [] augmentRecords = [] for record in records: # Split out the base fields from the augment fields baseFields, augmentFields = self._splitFields(record) if augmentFields: # Create an AugmentRecord autoScheduleMode = { AutoScheduleMode.none: "none", AutoScheduleMode.accept: "accept-always", AutoScheduleMode.decline: "decline-always", AutoScheduleMode.acceptIfFree: "accept-if-free", AutoScheduleMode.declineIfBusy: "decline-if-busy", AutoScheduleMode.acceptIfFreeDeclineIfBusy: "automatic", }.get(augmentFields.get(FieldName.autoScheduleMode, None), None) kwargs = { "uid": record.uid, "autoScheduleMode": autoScheduleMode, } if FieldName.hasCalendars in augmentFields: kwargs["enabledForCalendaring"] = augmentFields[ FieldName.hasCalendars] if FieldName.hasContacts in augmentFields: kwargs["enabledForAddressBooks"] = augmentFields[ FieldName.hasContacts] if FieldName.loginAllowed in augmentFields: kwargs["enabledForLogin"] = augmentFields[ FieldName.loginAllowed] if FieldName.autoAcceptGroup in augmentFields: kwargs["autoAcceptGroup"] = augmentFields[ FieldName.autoAcceptGroup] if FieldName.serviceNodeUID in augmentFields: kwargs["serverID"] = augmentFields[ FieldName.serviceNodeUID] augmentRecord = AugmentRecord(**kwargs) augmentRecords.append(augmentRecord) # Create new base records: baseRecords.append(DirectoryRecord(self._directory, baseFields)) # Apply the augment records if augmentRecords: yield self._augmentDB.addAugmentRecords(augmentRecords) # Apply the base records if baseRecords: yield self._directory.updateRecords(baseRecords, create=create)
def migrateWiki(store, principalID): """ Iterate calendar homes looking for wiki principals; create resources for each. """ directory = store.directoryService() recordType = CalRecordType.resource prefix = WikiDirectoryService.uidPrefix ch = schema.CALENDAR_HOME if principalID is not None: delegateRecord = yield recordForPrincipalID(directory, principalID) else: delegateRecord = None # Look up in the DB all the uids starting with the wiki prefix txn = store.newTransaction() rows = (yield Select( [ ch.OWNER_UID, ], From=ch, Where=(ch.OWNER_UID.StartsWith(prefix)), ).on(txn)) yield txn.commit() # For each wiki uid, if the resource record does not already exist, # create a record for uid in [row[0] for row in rows]: uid = uid.decode("utf-8") record = yield directory.recordWithUID(uid) if record is None: name = uid[len(prefix):] fields = { directory.fieldName.recordType: recordType, directory.fieldName.uid: uid, directory.fieldName.shortNames: [name], directory.fieldName.fullNames: [name], directory.fieldName.hasCalendars: True, directory.fieldName.hasContacts: False, } record = DirectoryRecord(directory, fields) yield record.service.updateRecords([record], create=True) print("Added '{}'".format(name)) # Refetch the record yield directory.flush() record = yield directory.recordWithUID(uid) if delegateRecord is not None: txn = store.newTransaction() yield Delegates.addDelegate(txn, record, delegateRecord, True) yield txn.commit()
def action_setValue(store, record, name, value): print("Setting {name} to {value} for {record}".format( name=name, value=value, record=prettyRecord(record), )) # Get original fields newFields = record.fields.copy() # Set new value newFields[record.service.fieldName.lookupByName(name)] = value updatedRecord = DirectoryRecord(record.service, newFields) yield record.service.updateRecords([updatedRecord], create=False)
def __init__(self, service, baseRecord, augmentedFields): DirectoryRecord.__init__(self, service, augmentedFields) self._baseRecord = baseRecord
def __init__(self, service, fields): BaseDirectoryRecord.__init__(self, service, fields) CalendarDirectoryRecordMixin.__init__(self)
def recordWithShortName(self, recordType, shortName, timeoutSeconds=None): return DirectoryRecord(self, shortName)
def test_updateRecords(self): service = InMemoryDirectoryService(u"xyzzy") # Record does not exist record = yield service.recordWithUID(u"foo") self.assertEquals(None, record) records = (DirectoryRecord( service, { service.fieldName.uid: u"foo", service.fieldName.shortNames: (u"foo1", u"foo2"), service.fieldName.recordType: RecordType.user, }), ) try: # Trying to update a record when it does not exist should fail yield service.updateRecords(records, create=False) except NoSuchRecordError: pass except: self.fail( "Did not raise NoSuchRecordError when create=False and record does not exist" ) record = yield service.recordWithUID(u"foo") self.assertEquals(None, record) # Create the record yield service.updateRecords(records, create=True) record = yield service.recordWithUID(u"foo") self.assertEquals(record.uid, u"foo") records = yield service.recordsWithRecordType(RecordType.user) self.assertEquals(len(records), 1) self.assertEquals(list(records)[0].uid, u"foo") record = yield service.recordWithShortName(RecordType.user, u"foo1") self.assertEquals(record.uid, u"foo") record = yield service.recordWithShortName(RecordType.user, u"foo2") self.assertEquals(record.uid, u"foo") records = ( DirectoryRecord( service, { service.fieldName.uid: u"foo", service.fieldName.shortNames: (u"foo3", u"foo4"), service.fieldName.recordType: RecordType.group, }), DirectoryRecord( service, { service.fieldName.uid: u"bar", service.fieldName.shortNames: (u"bar1", u"bar2"), service.fieldName.recordType: RecordType.user, }), ) # Update the existing record and create a new one yield service.updateRecords(records, create=True) record = yield service.recordWithUID(u"foo") self.assertEquals(record.uid, u"foo") self.assertEquals(set(record.shortNames), set((u'foo3', u'foo4'))) records = yield service.recordsWithRecordType(RecordType.group) self.assertEquals(len(records), 1) self.assertEquals(list(records)[0].uid, u"foo") records = yield service.recordsWithRecordType(RecordType.user) self.assertEquals(len(records), 1) self.assertEquals(list(records)[0].uid, u"bar") record = yield service.recordWithShortName(RecordType.group, u"foo3") self.assertEquals(record.uid, u"foo") record = yield service.recordWithShortName(RecordType.group, u"foo4") self.assertEquals(record.uid, u"foo") # Remove a record yield service.removeRecords((u"foo", )) record = yield service.recordWithUID(u"foo") self.assertEquals(None, record)
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])
def addRecordFromFields(self, fields): updatedRecord = DirectoryRecord(self.directory, fields) yield self.directory.updateRecords((updatedRecord, ), create=True)
def __init__(self, service, baseRecord, augmentedFields): DirectoryRecord.__init__(self, service, augmentedFields) CalendarDirectoryRecordMixin.__init__(self) self._baseRecord = baseRecord
def changeRecord(self, record, fieldname, value): fields = record.fields.copy() fields[fieldname] = value updatedRecord = DirectoryRecord(self.directory, fields) yield self.directory.updateRecords((updatedRecord, ))
def recordWithUID(self, uid, timeoutSeconds=None): return DirectoryRecord(self, uid)