def move(self, item_uuid, new_base, needed=None, user=None): """ Moves an entry to another base """ json = self.__load() if item_uuid in json: for obj in json[item_uuid]: if "dn" in json[item_uuid][obj]: # Update the source entry entry = json[item_uuid][obj] entry['dn'] = re.sub( re.escape(entry['parentDN']) + "$", new_base, entry['dn']) entry['parentDN'] = new_base # Check if we can move the entry if self.exists(entry['dn']): raise BackendError( C.make_error("TARGET_EXISTS", target=entry['dn'])) # Save the changes json[item_uuid][obj] = entry self.__save(json) return True return False
def __init__(self): # Initialize environment and logger self.env = Environment.getInstance() self.log = getLogger(__name__) # Create scope map self.scope_map = { ldap.SCOPE_SUBTREE: "sub", ldap.SCOPE_BASE: "base", ldap.SCOPE_ONELEVEL: "one" } # Read storage path from config self._file_path = self.env.config.get("backend-json.database-file", None) if not self._file_path: raise BackendError( C.make_error("DB_CONFIG_MISSING", target="backend-json.database-file")) # Create a json file on demand if not os.path.exists(self._file_path): open(self._file_path, "w").write(dumps({}))
def update(self, uuid, data, back_attrs): """ Write back changes collected for foreign objects relations. E.g. If group memberships where modified from the user plugin we will forward the changes to the group objects. """ # Extract usable information out og the backend attributes mapping = self.extractBackAttrs(back_attrs) index = PluginRegistry.getInstance("ObjectIndex") # Ensure that we have a configuration for all attributes for attr in data.keys(): if attr not in mapping: raise BackendError( C.make_error("BACKEND_ATTRIBUTE_CONFIG_MISSING", attribute=attr)) # Walk through each mapped foreign-object-attribute for targetAttr in mapping: if not targetAttr in data: continue # Get the matching attribute for the current object foreignObject, foreignAttr, foreignMatchAttr, matchAttr = mapping[ targetAttr] res = index.search({'uuid': uuid, matchAttr: "%"}, {matchAttr: 1}) if len(res) == 0: raise BackendError( C.make_error("SOURCE_OBJECT_NOT_FOUND", object=targetAttr)) matchValue = res[0][matchAttr][0] # Collect all objects that match the given value allvalues = data[targetAttr]['orig'] + data[targetAttr]['value'] object_mapping = {} for value in allvalues: res = index.search({ '_type': foreignObject, foreignAttr: value }, {'dn': 1}) if len(res) != 1: raise EntryNotFound( C.make_error("NO_UNIQUE_ENTRY", object=foreignObject, attribute=foreignAttr, value=value)) else: object_mapping[value] = ObjectProxy(res[0]['dn']) # Calculate value that have to be removed/added remove = list( set(data[targetAttr]['orig']) - set(data[targetAttr]['value'])) add = list( set(data[targetAttr]['value']) - set(data[targetAttr]['orig'])) # Remove ourselves from the foreign object for item in remove: if object_mapping[item]: current_state = getattr(object_mapping[item], foreignMatchAttr) new_state = [x for x in current_state if x != matchValue] setattr(object_mapping[item], foreignMatchAttr, new_state) # Add ourselves to the foreign object for item in add: if object_mapping[item]: current_state = getattr(object_mapping[item], foreignMatchAttr) current_state.append(matchValue) setattr(object_mapping[item], foreignMatchAttr, current_state) # Save changes for item in object_mapping: if object_mapping[item]: object_mapping[item].commit()
def get_next_id(self, attr): # pragma: nocover raise BackendError(C.make_error("ID_GENERATION_FAILED"))