def fetchChildList(self): """ Gets the list of baseDNs for the server and return them. """ connection = LumaConnectionWrapper(self.serverMeta) # If baseDNs are aleady spesified if self.serverMeta.autoBase == False: self.logger.debug("autoBase=False") tmpList = self.serverMeta.baseDN #Need to bind in order to fetch the data for the baseDNs bindSuccess, exceptionObject = connection.bindSync() if not bindSuccess: self.logger.debug("Bind failed.") tmp = LDAPErrorItem(str("["+exceptionObject[0]["desc"]+"]"), self, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) # Else get them from the server else: self.logger.debug("Using getBaseDNList()") #self.isWorking.emit() success, tmpList, exceptionObject = connection.getBaseDNListSync() if not success: self.logger.debug("getBaseDNList failed:"+str(exceptionObject)) tmp = LDAPErrorItem(str("["+exceptionObject[0]["desc"]+"]"), self, self) return (True, [tmp], exceptionObject) #See above #getBaseDNList calles unbind(), so let's rebind connection.bindSync() self.logger.debug("Entering for-loop") # Get the info for the baseDNs newChildList = [] for base in tmpList: success, resultList, exceptionObject = connection.searchSync(base, \ scope=ldap.SCOPE_BASE,filter='(objectclass=*)', sizelimit=1) if not success: self.logger.debug("Couldn't search item:"+str(exceptionObject)) tmp = LDAPErrorItem(str(base+" ["+exceptionObject[0]["desc"]+"]"), self, self) newChildList.append(tmp) continue if resultList: self.logger.debug("Found item") tmp = LDAPTreeItem(resultList[0], self, self) newChildList.append(tmp) self.logger.debug("End populatItem") return (True, newChildList, exceptionObject)
def reloadModel(self): """ Refreshes the LDAP data from server, """ lumaConnection = LumaConnectionWrapper( self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, resultList, exceptionObject = lumaConnection.searchSync( self.smartObject.getDN(), ldap.SCOPE_BASE) lumaConnection.unbind() if success and (len(resultList) > 0): self.smartObject = resultList[0] self.smartObject.checkIntegrity() self.VALID = self.smartObject.isValid self.EDITED = False self.modelChangedSignal.emit(True) return (True, None, None) else: message = self.str_REFRESH return (False, message, exceptionObject)
def saveModel(self): """ Save changes to the current object. """ lumaConnection = LumaConnectionWrapper(self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) if self.CREATE: success, exceptionObject = lumaConnection.addDataObject(self.smartObject) lumaConnection.unbind() if success: #self.CREATE = False self.EDITED = False self.modelChangedSignal.emit(False) return (True, None, None) else: message = self.str_ADD return (False, message, exceptionObject) else: success, exceptionObject = lumaConnection.updateDataObject(self.smartObject) lumaConnection.unbind() if success: self.EDITED = False self.modelChangedSignal.emit(False) return (True, None, None) else: message = self.str_SAVE return (False, message, exceptionObject)
def fetchChildList(self): """ (Re)aquire the list of childs for this item (if any). """ lumaConnection = LumaConnectionWrapper(self.serverParent.serverMeta) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: tmp = LDAPErrorItem(str("["+exceptionObject[0]["desc"]+"]"), self.serverParent, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) # Search for items at the level under this one success, resultList, exceptionObject = lumaConnection.searchSync(self.itemData.getDN(), \ scope=ldap.SCOPE_ONELEVEL, filter=self.filter.encode('utf8'), sizelimit=self.limit) lumaConnection.unbind() if not success: tmp = LDAPErrorItem(str("["+exceptionObject[0]["desc"]+"]"), self.serverParent, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) self.error = False # Default behavior: return all return (True, [LDAPTreeItem(x, self.serverParent, self) for x in resultList], exceptionObject)
def fetchChildList(self): """ (Re)aquire the list of childs for this item (if any). """ lumaConnection = LumaConnectionWrapper(self.serverParent.serverMeta) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: tmp = LDAPErrorItem(str("[" + exceptionObject[0]["desc"] + "]"), self.serverParent, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) # Search for items at the level under this one success, resultList, exceptionObject = lumaConnection.searchSync(self.itemData.getDN(), \ scope=ldap.SCOPE_ONELEVEL, filter=self.filter.encode('utf8'), sizelimit=self.limit) lumaConnection.unbind() if not success: tmp = LDAPErrorItem(str("[" + exceptionObject[0]["desc"] + "]"), self.serverParent, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) self.error = False # Default behavior: return all return (True, [LDAPTreeItem(x, self.serverParent, self) for x in resultList], exceptionObject)
def deleteObject(self): """ Deletes the remote object that this model represents """ lumaConnection = LumaConnectionWrapper( self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, exceptionObject = lumaConnection.delete( self.smartObject.getDN()) lumaConnection.unbind() if success: #serverName = self.smartObject.getServerAlias() #dn = self.smartObject.getPrettyParentDN() #self.model().reloadItem(self.smartObject.parent()) #self.modelChangedSignal.emit() return (True, None, None) else: message = self.str_DELETE return (False, message, exceptionObject)
def delete(self): """ Tries to delete the item on the server """ lumaConnection = LumaConnectionWrapper(self.serverParent.serverMeta) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = QtCore.QCoreApplication.translate( "LDAPTreeItem", "Could not bind to server.") return (False, message, exceptionObject) success, exceptionObject = lumaConnection.delete( self.smartObject().getDN()) lumaConnection.unbind() if success: self.childItems = [] self.populated = True return (True, None, None) else: message = QtCore.QCoreApplication.translate( "LDAPTreeItem", "Could not delete entry: " + exceptionObject[0]["desc"]) return (False, message, exceptionObject)
def initModel(self, create=False): if create: self.EDITED = True self.ISLEAF = False self.CREATE = True self.VALID = True else: self.EDITED = False isLeave = False self.smartObject.checkIntegrity() self.VALID = self.smartObject.isValid serverMeta = self.smartObject.getServerMeta() lumaConnection = LumaConnectionWrapper(serverMeta, self) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, resultList, exceptionObject = lumaConnection.searchSync( self.smartObject.dn, ldap.SCOPE_ONELEVEL, filter="(objectClass=*)", attrList=None, attrsonly=1, sizelimit=1) lumaConnection.unbind() # Our search succeeded. No errors if success: # There are no leaves below if len(resultList) == 0: self.ISLEAF = True # Leaves are below else: self.ISLEAF = False # Error during search request else: self.ISLEAF = False message = self.str_CHECK_LEAF self.modelChangedSignal.emit(True) return (False, message, exceptionObject) self.CREATE = False self.modelChangedSignal.emit(True) return (True, None, None)
def initModel(self, create=False): if create: self.EDITED = True self.ISLEAF = False self.CREATE = True self.VALID = True else: self.EDITED = False isLeave = False self.smartObject.checkIntegrity() self.VALID = self.smartObject.isValid serverMeta = self.smartObject.getServerMeta() lumaConnection = LumaConnectionWrapper(serverMeta, self) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, resultList, exceptionObject = lumaConnection.searchSync(self.smartObject.dn, ldap.SCOPE_ONELEVEL, filter="(objectClass=*)", attrList=None, attrsonly=1, sizelimit=1) lumaConnection.unbind() # Our search succeeded. No errors if success: # There are no leaves below if len(resultList) == 0: self.ISLEAF = True # Leaves are below else: self.ISLEAF = False # Error during search request else: self.ISLEAF = False message = self.str_CHECK_LEAF self.modelChangedSignal.emit(True) return (False, message, exceptionObject) self.CREATE = False self.modelChangedSignal.emit(True) return (True, None, None)
class Search(QObject): """Object representing a search.""" #: Signal used to inform about returned search results resultsRetrieved = pyqtSignal(list, object) __logger = logging.getLogger(__name__) def __init__(self, parent, server, **kwargs): """ :param parent: the connection object to perform the search :type parent: QObject :param server: the sever to search on. :type server: ServerObject :param **kwargs: a dict with keyword arguments to pass to the LDAP search operation. :type **kwargs: dict """ super(Search, self).__init__(parent) self.filter = kwargs['filter'] self.connection = LumaConnectionWrapper(server, self) self.connection.searchFinished.connect(self.onSearchFinished) bindSuccess, e = self.connection.bindSync() if not bindSuccess: msg = 'Unable to bind to {0}. Reason\n{1}' self.__logger.error(msg.format(server, str(e))) else: self.connection.searchAsync(**kwargs) def onSearchFinished(self, success, result, e): """Slot for the searchFinished signal in LumaConnectionWrapper. As of now this reemits a signal that SearchPlugin has connected to. This is done in order to be able to provide the filter and attributes needed for the result view. """ # If self.connection wasn't an object attribute # it should probably be unparented here so that it # is GCed. #self.sender().setParent(None) if success: result.append(self.filter) self.resultsRetrieved.emit(result, None) else: self.resultsRetrieved.emit([], e)
class Search(QObject): """Object representing a search.""" #: Signal used to inform about returned search results resultsRetrieved = pyqtSignal(list, object) __logger = logging.getLogger(__name__) def __init__(self, parent, server, **kwargs): """ :param parent: the connection object to perform the search :type parent: QObject :param server: the sever to search on. :type server: ServerObject :param **kwargs: a dict with keyword arguments to pass to the LDAP search operation. :type **kwargs: dict """ super(Search, self).__init__(parent) self.filter = kwargs['filter'] self.connection = LumaConnectionWrapper(server, self) self.connection.searchFinished.connect(self.onSearchFinished) bindSuccess, e = self.connection.bindSync() if not bindSuccess: msg = 'Unable to bind to {0}. Reason\n{1}' self.__logger.error(msg.format(server, str(e))) else: self.connection.searchAsync(**kwargs) def onSearchFinished(self, success, result, e): """Slot for the searchFinished signal in LumaConnectionWrapper. As of now this reemits a signal that SearchPlugin has connected to. This is done in order to be able to provide the filter and attributes needed for the result view. """ # If self.connection wasn't an object attribute # it should probably be unparented here so that it # is GCed. #self.sender().setParent(None) if success: result.append(self.filter) self.resultsRetrieved.emit(result, None) else: self.resultsRetrieved.emit([], e)
def delete(self): """ Tries to delete the item on the server """ lumaConnection = LumaConnectionWrapper(self.serverParent.serverMeta) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = QtCore.QCoreApplication.translate("LDAPTreeItem","Could not bind to server.") return (False, message, exceptionObject) success, exceptionObject = lumaConnection.delete(self.smartObject().getDN()) lumaConnection.unbind() if success: self.childItems = [] self.populated = True return (True, None, None) else: message = QtCore.QCoreApplication.translate("LDAPTreeItem","Could not delete entry: "+exceptionObject[0]["desc"]) return (False, message, exceptionObject)
def saveModel(self): """ Save changes to the current object. """ lumaConnection = LumaConnectionWrapper( self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) if self.CREATE: success, exceptionObject = lumaConnection.addDataObject( self.smartObject) lumaConnection.unbind() if success: #self.CREATE = False self.EDITED = False self.modelChangedSignal.emit(False) return (True, None, None) else: message = self.str_ADD return (False, message, exceptionObject) else: success, exceptionObject = lumaConnection.updateDataObject( self.smartObject) lumaConnection.unbind() if success: self.EDITED = False self.modelChangedSignal.emit(False) return (True, None, None) else: message = self.str_SAVE return (False, message, exceptionObject)
def deleteObject(self): """ Deletes the remote object that this model represents """ lumaConnection = LumaConnectionWrapper(self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, exceptionObject = lumaConnection.delete(self.smartObject.getDN()) lumaConnection.unbind() if success: #serverName = self.smartObject.getServerAlias() #dn = self.smartObject.getPrettyParentDN() #self.model().reloadItem(self.smartObject.parent()) #self.modelChangedSignal.emit() return (True, None, None) else: message = self.str_DELETE return (False, message, exceptionObject)
def reloadModel(self): """ Refreshes the LDAP data from server, """ lumaConnection = LumaConnectionWrapper(self.smartObject.getServerMeta()) bindSuccess, exceptionObject = lumaConnection.bindSync() if not bindSuccess: message = self.str_BIND return (False, message, exceptionObject) success, resultList, exceptionObject = lumaConnection.searchSync(self.smartObject.getDN(), ldap.SCOPE_BASE) lumaConnection.unbind() if success and (len(resultList) > 0): self.smartObject = resultList[0] self.smartObject.checkIntegrity() self.VALID = self.smartObject.isValid self.EDITED = False self.modelChangedSignal.emit(True) return (True, None, None) else: message = self.str_REFRESH return (False, message, exceptionObject)
def __exportSelection(self, scope=0): """Slot for the context menu. Opens the ExportDialog with the selected entries, giving the user the option to validate the selection before exporting. :param scope: The scope selection. 0 = SCOPE_BASE -> Item(s), 1 = SCOPE_ONELEVEL -> Subtree(s); 2 = SCOPE_SUBTREE -> Subtree(s) with parent :type scope: int """ exportObjects = [] msg = '' self.setBusy(True) for index in self.selection: smartObject = index.internalPointer().smartObject() serverName = smartObject.getServerAlias() dn = smartObject.getDN() serverObject = self.serverList.getServerObject(serverName) con = LumaConnectionWrapper(serverObject, self) # For both subtree and subtree with parent, we fetch the # whole subtree including the parent, with a basic sync # search operation. Then, if only the subtree is to be # exported, we remove the smartObject(s) selected. if scope > 0: pass # Do a search on the whole subtree # 2 = ldap.SCOPE_SUBTREE #elif scope == 2: success, e = con.bindSync() if not success: self.__logger.error(str(e)) continue success, result, e = con.searchSync(base=dn, scope=2) if success: exportObjects.extend(result) else: self.__logger.error(str(e)) # If only the subtree is to be selected, we remove # the parent, which happens to be the smartObject(s) # initialy selected. if scope == 1: exportObjects.remove(smartObject) # For scope == 0 we need not do any LDAP search operation # because we already got what we need else: exportObjects.append(smartObject) # Initialize the export dialog # and give it the items for export dialog = ExportDialog(msg) dialog.setExportItems(exportObjects) self.setBusy(False) dialog.exec_()
def fetchChildList(self): """ Gets the list of baseDNs for the server and return them. """ connection = LumaConnectionWrapper(self.serverMeta) # If baseDNs are aleady spesified if self.serverMeta.autoBase == False: self.logger.debug("autoBase=False") tmpList = self.serverMeta.baseDN #Need to bind in order to fetch the data for the baseDNs bindSuccess, exceptionObject = connection.bindSync() if not bindSuccess: self.logger.debug("Bind failed.") tmp = LDAPErrorItem( str("[" + exceptionObject[0]["desc"] + "]"), self, self) # We're adding the error as LDAPErrorItem-child, so return True return (True, [tmp], exceptionObject) # Else get them from the server else: self.logger.debug("Using getBaseDNList()") #self.isWorking.emit() success, tmpList, exceptionObject = connection.getBaseDNListSync() if not success: self.logger.debug("getBaseDNList failed:" + str(exceptionObject)) tmp = LDAPErrorItem( str("[" + exceptionObject[0]["desc"] + "]"), self, self) return (True, [tmp], exceptionObject) #See above #getBaseDNList calles unbind(), so let's rebind connection.bindSync() self.logger.debug("Entering for-loop") # Get the info for the baseDNs newChildList = [] for base in tmpList: success, resultList, exceptionObject = connection.searchSync(base, \ scope=ldap.SCOPE_BASE,filter='(objectclass=*)', sizelimit=1) if not success: self.logger.debug("Couldn't search item:" + str(exceptionObject)) tmp = LDAPErrorItem( str(base + " [" + exceptionObject[0]["desc"] + "]"), self, self) newChildList.append(tmp) continue if resultList: self.logger.debug("Found item") tmp = LDAPTreeItem(resultList[0], self, self) newChildList.append(tmp) self.logger.debug("End populatItem") return (True, newChildList, exceptionObject)
def __exportSelection(self, scope=0): """Slot for the context menu. Opens the ExportDialog with the selected entries, giving the user the option to validate the selection before exporting. :param scope: The scope selection. 0 = SCOPE_BASE -> Item(s), 1 = SCOPE_ONELEVEL -> Subtree(s); 2 = SCOPE_SUBTREE -> Subtree(s) with parent :type scope: int """ exportObjects = [] msg = '' self.setBusy(True) for index in self.selection: smartObject = index.internalPointer().smartObject() serverName = smartObject.getServerAlias() dn = smartObject.getDN() serverObject = self.serverList.getServerObject(serverName) con = LumaConnectionWrapper(serverObject, self) # For both subtree and subtree with parent, we fetch the # whole subtree including the parent, with a basic sync # search operation. Then, if only the subtree is to be # exported, we remove the smartObject(s) selected. if scope > 0: pass # Do a search on the whole subtree # 2 = ldap.SCOPE_SUBTREE #elif scope == 2: success, e = con.bindSync() if not success: self.__logger.error(str(e)) continue success, result, e = con.searchSync(base=dn, scope=2) if success: exportObjects.extend(result) else: self.__logger.error(str(e)) # If only the subtree is to be selected, we remove # the parent, which happens to be the smartObject(s) # initialy selected. if scope == 1: exportObjects.remove(smartObject) # For scope == 0 we need not do any LDAP search operation # because we already got what we need else: exportObjects.append(smartObject) # Initialize the export dialog # and give it the items for export dialog = ExportDialog(msg) dialog.setExportItems(exportObjects) self.setBusy(False) dialog.exec_()