Exemple #1
0
 def handle(self, *args, **options):
     try:
         backend = BackendHelper()
         target = TargetHelper()
     except Exception as e:
         print >> sys.stderr, e
         sys.exit(1)
     for lbeObjectTemplate in LBEObjectTemplate.objects.all():
         now = datetime.datetime.now(utc)
         # the searchNewObjectcs methods search for backend object where createTimestamp > objectTemplate.imported_at
         for lbeObject in target.searchNewObjects(lbeObjectTemplate):
             #
             # TODO: take care about virtual/reference attributes
             # Example: an object where uid is computed as 'bbonfils', but backend value is 'asyd'
             lbeObject.synced_at = now
             backend.createObject(lbeObjectTemplate, lbeObject)
         lbeObjectTemplate.imported_at = now
         lbeObjectTemplate.save()
Exemple #2
0
 def handle(self, *args, **options):
     try:
         backend = BackendHelper()
         target = TargetHelper()
     except Exception as e:
         print >>sys.stderr, e
         sys.exit(1)
     for lbeObjectTemplate in LBEObjectTemplate.objects.all():
         now = datetime.datetime.now(utc)
         # the searchNewObjectcs methods search for backend object where createTimestamp > objectTemplate.imported_at
         for lbeObject in target.searchNewObjects(lbeObjectTemplate):
             #
             # TODO: take care about virtual/reference attributes
             # Example: an object where uid is computed as 'bbonfils', but backend value is 'asyd'
             lbeObject.synced_at = now
             backend.createObject(lbeObjectTemplate, lbeObject)
         lbeObjectTemplate.imported_at = now
         lbeObjectTemplate.save()
Exemple #3
0
class ImportTarget():
    def __init__(self):
        self.backend = BackendHelper()
        self.target = TargetHelper()
        self.start_date = django.utils.timezone.now()

    def _getID(self, listRDN):
        listID = []
        for rdn in listRDN:
            listID.append(rdn.split('=')[1].split(',')[0])
        return listID

    def save(self):
        print 'Checking for Objects which do not exist into LBE Backend but in LDAP Server:'
        for objectTemplate in LBEObjectTemplate.objects.all():
            objectHelper = LBEObjectInstanceHelper(objectTemplate)
            try:
                scope = objectHelper.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            filter = '(&'
            for oc in objectHelper.callScriptClassMethod('object_classes'):
                filter += '(objectClass=' + oc + ')'
            filter += ')'
            print '\033[91m' + objectTemplate.name + '\033[0m: (\033[95m' + objectHelper.callScriptClassMethod("base_dn") + '\033[0m) using \033[95m' + filter + '\033[0m'
            objTarget = self.target.searchObjects(objectTemplate, scope)
            objBackend = self.backend.searchObjects(objectTemplate)
            number = 0
            for ot in objTarget:
                exist = False
                for ob in objBackend:
                    if ot.name == ob.name:
                        exist = True
                        break
                if not exist:
                    number += 1
                    print '=> Adding \033[95m' + ot.name + '\033[0m object into LBE Backend... '
                    print " values: " + str(ot.attributes)
                    try:
                        self.backend.createObject(objectTemplate, ot, True)
                        print "\033[92mDone.\033[0m\n"
                    except BaseException as e:
                        print "\033[91mFail.\033[0m"
                        print "''''''''"
                        print e
                        print "''''''''"
            if number == 0:
                print '<None>'
            # Synced object:
            objectTemplate.synced_at = django.utils.timezone.now()
            objectTemplate.save()
        print '.........................'
        print 'Checking for Groups which do not exist into LBE Backend but in Target:'
        for groupTemplate in LBEGroup.objects.all():
            groupInstance = GroupInstanceHelper(groupTemplate)
            try:
                scope = groupInstance.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            grpTarget = self.target.searchObjects(groupTemplate, scope)
            grpBackend = self.backend.searchObjects(groupTemplate)
            for gt in grpTarget:
                exist = False
                for gb in grpBackend:
                    if gt.name == gb.name:
                        exist = True
                        break
                if not exist:
                    # import only existing group into LBE config
                    try:
                        LBEGroup.objects.get(displayName__iexact=gt.displayName)
                    except BaseException:
                        continue
                    print '=> Adding \033[95m' + gt.name + '\033[0m group into LBE Backend... '
                    print " values: " + str(gt.attributes)
                    try:
                        if groupInstance.attributeName in gt.attributes:
                            gt.attributes[groupInstance.attributeName] = self._getID(gt.attributes[groupInstance.attributeName])
                        groupHelper = GroupInstanceHelper(groupTemplate, gt)
                        groupHelper.createTemplate(True)
                            #print " >\033[91mThis group does not exists in LBE Configuration Group.\033[0m"
                            #print " >\033[91mIn order to see, manage it, please create it using some extra attribute:"
                            #print "  >\033[91m'Display Name': \033[95m" + gt.name + "\033[0m"
                            #print " >\033[91mInto the Script file:"
                            #print "  >'DN Attribute': \033[95m" + groupHelper.callScriptClassMethod("base_dn") + "\033[91m"
                            #print "  >'Attribute Name' & 'Object Classes': as you wish.\033[0m"
                        print "\033[92mDone.\033[0m\n"
                    except BaseException as e:
                        print "\033[91mFail.\033[0m\n"
                        print "''''''''"
                        print e
                        print "''''''''"
            # Synced group:
            groupTemplate.synced_at = django.utils.timezone.now()
            groupTemplate.save()
        print "End."
Exemple #4
0
class UpgradeBT():
    def __init__(self):
        self.backend = BackendHelper()
        self.target = TargetHelper()
        self.start_date = django.utils.timezone.now()

    def _deleteORCreate(self, objectTemplate, ot):
        if objectTemplate.reconciliation_object_missing_policy == OBJECT_ADD_BACKEND:
            print "    |-> Adding \033[95m'" + ot.displayName + "'\033[0m object into Backend... "
            try:
                self.backend.createObject(objectTemplate, ot, True)
                changes = {}
                changes['status'] = OBJECT_STATE_SYNCED
                changes['changes'] = {}
                changes['changes']['set'] = {}
                changes['changes']['type'] = -1
                changes['synced_at'] = django.utils.timezone.now()
                self.backend.updateObject(objectTemplate, ot, changes)
            except BaseException as e:
                print "''''''''"
                print e
                print "''''''''"
        elif objectTemplate.reconciliation_object_missing_policy == OBJECT_DELETE_TARGET:
            print "    |-> Removing \033[95m'" + ot.displayName + "'\033[0m object from Target... "
            try:
                self.target.delete(objectTemplate, ot)
            except BaseException as e:
                print "''''''''"
                print e
                print "''''''''"

    def _upgradeObject(self, objectTemplate, objHelper, ot, ob):
        # check and replace ignore attributes:
        ignoreAttributes = objHelper.callScriptClassMethod("ignore_attributes")
        for key, values in ot.attributes.items():
            if key in ignoreAttributes:
                ob.attributes[key] = ot.attributes[key]

        if not ot.attributes == ob.attributes:
            if objectTemplate.reconciliation_object_different_policy == TARGET:
                # check if values are empty []:
                # Then, skip it.
                numberEmpty = 0
                for values in set(ot.attributes) ^ set(ob.attributes):
                    try:
                        # either empty or empty string:
                        if ob.attributes[values] == [] or ob.attributes[values] == ['']:
                            numberEmpty += 1
                    except BaseException:
                        pass
                    if not numberEmpty == 0 and numberEmpty == len(set(ot.attributes) ^ set(ob.attributes)):
                        return
                print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Target..."
                print " |-> -------------------------------------------- "
                print " ||-> Old Values: " + str(ot.attributes)
                print " ||-> New Values: " + str(ob.attributes)
                print " |-> -------------------------------------------- "
                # Remove & Add in order to add new attributes:
                try:
                    # Remove:
                    self.target.delete(objectTemplate, ob)
                    # Add
                    self.target.create(objectTemplate, ob)
                    # Synced:
                    changes = {}
                    changes['status'] = OBJECT_STATE_SYNCED
                    changes['changes'] = {}
                    changes['changes']['set'] = {}
                    changes['changes']['type'] = -1
                    changes['synced_at'] = django.utils.timezone.now()
                    self.backend.updateObject(objectTemplate, ob, changes)
                except BaseException as e:
                    print e
            elif objectTemplate.reconciliation_object_different_policy == BACKEND:
                print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Backend..."
                print " |-> -------------------------------------------- "
                print " ||-> Old Values: " + str(ob.attributes)
                print " ||-> New Values: " + str(ot.attributes)
                print " |-> -------------------------------------------- "
                try:
                    self.backend.updateObject(objectTemplate, ob, ot)
                except BaseException as e:
                    print e

    def start(self):
        print " Upgrade Server..."
        for objectTemplate in LBEObjectTemplate.objects.all():
            print " |-> \033[91m" + objectTemplate.name + '\033[0m:'
            objHelper = LBEObjectInstanceHelper(objectTemplate)
            try:
                scope = objHelper.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            objTarget = self.target.searchObjects(objectTemplate, scope)
            objBackend = self.backend.searchObjects(objectTemplate)
            # Target to Backend:
            for ot in objTarget:
                exist = False
                for ob in objBackend:
                    if ot.name == ob.name:
                        self._upgradeObject(objectTemplate, objHelper, ot, ob)
                        exist = True
                        break
                if not exist:
                    self._deleteORCreate(objectTemplate, ot)
            # Synced object:
            objectTemplate.synced_at = django.utils.timezone.now()
            objectTemplate.save()
        print " End."
Exemple #5
0
class LBEObjectInstanceHelper(object):
    def __init__(self, lbeObjectTemplate, lbeObjectInstance=None):
        self.template = lbeObjectTemplate
        self.instance = lbeObjectInstance
        self.scriptInstance = None
        self.backend = None

    def _backend(self):
        if self.backend is not None:
            return
        self.backend = BackendHelper()

    """
		LOAD Script
	"""

    def _load_script(self):
        if self.scriptInstance is not None:
            return

        # if lbeObjectTemplate.script is defined, create an instance
        scriptName = self.template.script.name
        if scriptName is not None:
            # the scriptName is like 'custom.employee.EmployeePostConfig', so we need to extract the module, aka custom.employee
            moduleName = '.'.join(scriptName.split('.')[:-1])
            # and the classname, EmployeePostConfig
            className = scriptName.split('.')[-1]
            __import__(moduleName)
            module = sys.modules[moduleName]
            self.scriptClass = getattr(module, className)
            # Create an instance
            #self.scriptInstance = self.scriptClass(self.template,self.instance)
        else:
            logging.error('This object does not have an associate script')

    def _create_script_instance(self, data=None):
        self._load_script()
        if self.scriptInstance is not None:
            return
        self.scriptInstance = self.scriptClass(self.template, self.instance, data)
        """
            END LOAD Script
        """

    """
		DE/COMPRESS Datas:
	"""

    def _compress_data(self, data):
        query = {}
        for key in data:
            if len(data.getlist(key)) == 1:
                query[key] = data[key]
            else:  # compress MultiValue:
                query[key] = '\0'.join(str(val) for val in data.getlist(key))
        return query

    def decompress_data(self, data):
        query = {}
        for key in data:
            if len(data.getlist(key)) == 1:
                query[key] = data[key]
            else: # decompress MultiValue:
                query[key] = data[key].split('\0')
        return query

    """
		END DE/COMPRESS Datas
	"""

    """
		MANAGE Object/Values:
	"""

    def save(self, ):
        self._checkUnique()
        # Search for an existing object
        searchResult = self.backend.getUserForObject(self.template, self.instance.name)
        if searchResult is None:
            return self.backend.createObject(self.template, self.instance)
        else:
            raise BackendObjectAlreadyExist('Already exists')

    def update(self):
        self._checkUnique()
        self.backend.createObject(self.template, self.instance.changes['set'])

    def getStatus(self, objectName):
        self._backend()
        return self.backend.getStatus(self.template, objectName)

    def modify(self):
        self._checkUnique()
        if not self.backend.getStatus(self.template, self.instance.name) == OBJECT_STATE_AWAITING_RECONCILIATION:
            self.backend.modifyObject(self.template, self.instance.name, self.instance.changes['set'],
                                  self.instance.displayName)
        else:
            raise TypeError("In order to change the Object, Your administrator needs to launch reconciliation first.")

    def remove(self, uid):
        self._backend()
        return self.backend.removeObject(self.template, uid)

    def approval(self, uid):
        self._backend()
        return self.backend.approvalObject(self.template, uid)

    def form(self, uid, data=None):
        if data is None:
            data = self.getValues(uid)
        else:
            data = self._compress_data(data)
        self._create_script_instance(data)
        return self.scriptInstance

    """
		END MANAGE Object/Values:
	"""

    """
		CALL Script
    """

    def callScriptMethod(self, methodName):
        self._create_script_instance()
        method = getattr(self.scriptInstance, methodName)
        return method()

    def callScriptClassMethod(self, methodName):
        self._load_script()
        method = getattr(self.scriptClass, methodName)
        return method()

    def callAttributeScriptMethod(self, attributeType, methodPrefix):
        for attributeInstance in self.template.lbeattributeinstance_set.filter(attributeType=attributeType):
            attributeName = attributeInstance.lbeAttribute.name
            try:
                if not self.instance.changes['set'] == {}:
                    self.instance.changes['set'][attributeName] = self.callScriptMethod(methodPrefix + attributeName)
                else:
                    self.instance.attributes[attributeName] = self.callScriptMethod(methodPrefix + attributeName)
            except AttributeError as e:
                try:
                    self.instance[attributeName] = self.callScriptMethod(methodPrefix + attributeName)
                except AttributeError as e:
                    logger.info(
                        'LBEObjectInstanceHelper: Method ' + methodPrefix + attributeName + ' not found or AttributeError exception. ' + e.__str__())
                    print (
                    'LBEObjectInstanceHelper: Method ' + methodPrefix + attributeName + ' not found or AttributeError exception. ' + e.__str__())

    def applyCustomScript(self):
        # Clean attributes before manage virtuals attributes
        #self.callAttributeScriptMethod(ATTRIBUTE_TYPE_FINAL, 'clean_')
        # Now, compute virtual attributes
        self.callAttributeScriptMethod(ATTRIBUTE_TYPE_VIRTUAL, 'compute_')

    def applyCustomScriptAttribute(self, attribute):
        try:
            attributeInstance = self.template.lbeattributeinstance_set.get(attributeType=ATTRIBUTE_TYPE_FINAL,
                                                                           lbeAttribute=LBEAttribute.objects.get(
                                                                               name__iexact=attribute))
            self.instance[attributeInstance.lbeAttribute.name] = self.callScriptMethod(
                "clean_" + attributeInstance.lbeAttribute.name)
        except BaseException as e:
            print e

    """
		END CALL Script
    """

    def _checkUnique(self):
        self._backend()
        attributesInstance = LBEAttributeInstance.objects.filter(lbeObjectTemplate=self.template, unique=True)
        objectInstances = self.backend.searchObjects(self.template)
        for attribute in attributesInstance:
            for obj in objectInstances:
                if not obj.changes['set'] == {}:
                    if obj.changes['set'].has_key(attribute.lbeAttribute.name) and self.instance.changes['set'].has_key(
                            attribute.lbeAttribute.name) \
                        and not self.instance.name == obj.name:
                        # MultiValue:
                        for objAttribute in obj.changes['set'][attribute.lbeAttribute.name]:
                            for instanceAttribute in self.instance.changes['set'][attribute.lbeAttribute.name]:
                                if objAttribute == instanceAttribute:
                                    raise ValueError("The value '" + str(
                                        self.instance.changes['set'][attribute.lbeAttribute.name][
                                            0]) + "' from the '" + attribute.lbeAttribute.name + "' attribute must be unique.\nPlease change its value or their computed values.")
                else:
                    if obj.attributes.has_key(attribute.lbeAttribute.name) and self.instance.changes['set'].has_key(
                            attribute.lbeAttribute.name) \
                        and not self.instance.name == obj.name:
                        # MultiValue:
                        for objAttribute in obj.attributes[attribute.lbeAttribute.name]:
                            for instanceAttribute in self.instance.changes['set'][attribute.lbeAttribute.name]:
                                if objAttribute == instanceAttribute:
                                    raise ValueError("The value '" + str(
                                        self.instance.changes['set'][attribute.lbeAttribute.name][
                                            0]) + "' from the '" + attribute.lbeAttribute.name + "' attribute must be unique.\nPlease change its value or their computed values.")

    def getValues(self, UID):
        """
		Function enables to get values from attributes fields and
		changes.set fields, return the new values (changes.set > attributes)
        """
        try:
            self._backend()
            valuesUser = self.backend.getUserForObject(self.template, UID)
            # Get all attributes from objects:
            attributes = LBEAttributeInstance.objects.filter(lbeObjectTemplate=self.template)
            d = dict()
            for attribute in attributes:
                # Only FINAL Attributes:
                if attribute.attributeType == 0:
                    try:
                        if valuesUser['changes']['set'].has_key(attribute.lbeAttribute.name):
                            q = QueryDict(attribute.lbeAttribute.name + '=' +
                                          valuesUser['changes']['set'][attribute.lbeAttribute.name][0])
                            q = q.copy()
                            for value in valuesUser['changes']['set'][attribute.lbeAttribute.name][1:]:
                                q.update({attribute.lbeAttribute.name: value})
                            d[attribute.lbeAttribute.name] = self._compress_data(q)[attribute.lbeAttribute.name]
                        else:
                            q = QueryDict(attribute.lbeAttribute.name + '=' +
                                          valuesUser['attributes'][attribute.lbeAttribute.name][0])
                            q = q.copy()
                            for value in valuesUser['attributes'][attribute.lbeAttribute.name][1:]:
                                q.update({attribute.lbeAttribute.name: value})
                            d[attribute.lbeAttribute.name] = self._compress_data(q)[attribute.lbeAttribute.name]
                    except BaseException:
                        d[attribute.lbeAttribute.name] = attribute.defaultValue
            return d
        except BaseException:
            # Create part:
            return None

    def getObject(self, UID):
        self._backend()
        return self.backend.searchObjectsByPattern(self.template, UID)[0]

    def searchPattern(self, pattern):
        self._backend()
        objects = self.backend.searchObjects(self.template)
        tabResult = []

        prog = re.compile(pattern, re.I)

        for object in objects:
            # check the status object & get its values
            if object.status == OBJECT_STATE_AWAITING_SYNC:
                objectValues = object.changes['set']
            else:
                objectValues = object.attributes
            # check values and pattern corresponding
            correspond = ''
            for key, values in objectValues.items():
                for cel in values:
                    if prog.search(cel):
                        attributeDisplayName = LBEAttribute.objects.get(name__iexact=key).displayName
                        correspond += cel.lower().replace(pattern, '<b>' + pattern + '</b>') + ' (<i>' + attributeDisplayName + '</i>) '
            if correspond:
                tabResult.append({'object': self.template.id, "name": object.name, 'displayName': object.displayName,
                                  "values": correspond})
        return tabResult

    def getValuesDecompressed(self, UID):
        """
		Function enables to get values from attributes fields and
		changes.set fields, return the new values (changes.set > attributes)
        """
        self._backend()
        valuesUser = self.backend.getUserForObject(self.template, UID)
        # Get all attributes from objects:
        attributes = LBEAttributeInstance.objects.filter(lbeObjectTemplate=self.template).order_by('position')
        d = dict()
        for attribute in attributes:
            try:
                if valuesUser['changes']['set'].has_key(attribute.lbeAttribute.name):
                    d[attribute.lbeAttribute.name] = valuesUser['changes']['set'][attribute.lbeAttribute.name]
                else:
                    d[attribute.lbeAttribute.name] = valuesUser['attributes'][attribute.lbeAttribute.name]
            except KeyError:
                # if no values, just set the default value:
                d[attribute.lbeAttribute.name] = attribute.defaultValue
        return d

    def createFromDict(self, request):
        # Reinit script configuration file:
        self.scriptInstance = None
        # attributes:
        attributes = {}
        for attributeInstance in self.template.lbeattributeinstance_set.all():
            # Only fetch real attributes from the request (mono and/or multi values)
            if attributeInstance.attributeType == ATTRIBUTE_TYPE_FINAL:
                attributeName = attributeInstance.lbeAttribute.name
                if len(request.POST[attributeName].split('�')) > 1:
                    attributes[attributeName] = request.POST[attributeName].split('�')
                else:
                    attributes[attributeName] = [request.POST[attributeName]]
            # IMPORTANT: We need to create an instance without the uniqueBecause because it may be a computed attribute, for example uid (compute from firstname/name)
        self.instance = LBEObjectInstance(self.template, attributes=attributes)
        self.instance.status = OBJECT_STATE_AWAITING_SYNC
        self.applyCustomScript()
        # Set uniqueName and displayName
        try:
            self.instance.name = self.instance.attributes[self.template.instanceNameAttribute.name][0]
            self.instance.displayName = self.instance.attributes[self.template.instanceDisplayNameAttribute.name][0]
            # It's a new object, the changesSet apply to all attributes
            self.instance.changes['type'] = OBJECT_CHANGE_CREATE_OBJECT
            self.instance.changes['set'] = self.instance.attributes
        except BaseException as e:
            print e.__str__()
            # TODO: Remove technical message, use another handler to send message to administrator
            messages.add_message(request, messages.ERROR,
                                 'nameAttribute or displayNameAttribute does not exist in object attributes')


    def updateFromDict(self, ID, values):
        self.scriptInstance = None
        self.instance = LBEObjectInstance(self.template, attributes=None)
        self.instance.changes['set'] = values
        self.instance.attributes = values
        # compute attributes:
        self._create_script_instance()
        self.applyCustomScript()
        # set them (if not been yet) to array:
        for key in self.instance.attributes:
            if isinstance(self.instance.attributes[key], str) or isinstance(self.instance.attributes[key], unicode):
                self.instance.attributes[key] = [self.instance.attributes[key]]
            # change the displayName value:
        self.instance.displayName = self.instance.changes['set'][self.template.instanceDisplayNameAttribute.name][0]
        # ID object:
        self.instance.name = ID

    def compute(self, lbeObjectInstance):
        self.instance = lbeObjectInstance
        self._create_script_instance()
        # Do not make change if changes.set is empty:
        if not lbeObjectInstance.changes['set'] == {}:
            self.applyCustomScript()
Exemple #6
0
class UpgradeBT():
    def __init__(self):
        self.backend = BackendHelper()
        self.target = TargetHelper()
        self.start_date = django.utils.timezone.now()

    def _deleteORCreate(self, objectTemplate, ot):
        if objectTemplate.reconciliation_object_missing_policy == OBJECT_ADD_BACKEND:
            print "    |-> Adding \033[95m'" + ot.displayName + "'\033[0m object into Backend... "
            try:
                self.backend.createObject(objectTemplate, ot, True)
                changes = {}
                changes['status'] = OBJECT_STATE_SYNCED
                changes['changes'] = {}
                changes['changes']['set'] = {}
                changes['changes']['type'] = -1
                changes['synced_at'] = django.utils.timezone.now()
                self.backend.updateObject(objectTemplate, ot, changes)
            except BaseException as e:
                print "''''''''"
                print e
                print "''''''''"
        elif objectTemplate.reconciliation_object_missing_policy == OBJECT_DELETE_TARGET:
            print "    |-> Removing \033[95m'" + ot.displayName + "'\033[0m object from Target... "
            try:
                self.target.delete(objectTemplate, ot)
            except BaseException as e:
                print "''''''''"
                print e
                print "''''''''"

    def _upgradeObject(self, objectTemplate, objHelper, ot, ob):
        # check and replace ignore attributes:
        ignoreAttributes = objHelper.callScriptClassMethod("ignore_attributes")
        for key, values in ot.attributes.items():
            if key in ignoreAttributes:
                ob.attributes[key] = ot.attributes[key]

        if not ot.attributes == ob.attributes:
            if objectTemplate.reconciliation_object_different_policy == TARGET:
                # check if values are empty []:
                # Then, skip it.
                numberEmpty = 0
                for values in set(ot.attributes) ^ set(ob.attributes):
                    try:
                        # either empty or empty string:
                        if ob.attributes[values] == [] or ob.attributes[
                                values] == ['']:
                            numberEmpty += 1
                    except BaseException:
                        pass
                    if not numberEmpty == 0 and numberEmpty == len(
                            set(ot.attributes) ^ set(ob.attributes)):
                        return
                print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Target..."
                print " |-> -------------------------------------------- "
                print " ||-> Old Values: " + str(ot.attributes)
                print " ||-> New Values: " + str(ob.attributes)
                print " |-> -------------------------------------------- "
                # Remove & Add in order to add new attributes:
                try:
                    # Remove:
                    self.target.delete(objectTemplate, ob)
                    # Add
                    self.target.create(objectTemplate, ob)
                    # Synced:
                    changes = {}
                    changes['status'] = OBJECT_STATE_SYNCED
                    changes['changes'] = {}
                    changes['changes']['set'] = {}
                    changes['changes']['type'] = -1
                    changes['synced_at'] = django.utils.timezone.now()
                    self.backend.updateObject(objectTemplate, ob, changes)
                except BaseException as e:
                    print e
            elif objectTemplate.reconciliation_object_different_policy == BACKEND:
                print " |-> Upgrade Object '\033[35m" + ob.displayName + "\033[0m' into Backend..."
                print " |-> -------------------------------------------- "
                print " ||-> Old Values: " + str(ob.attributes)
                print " ||-> New Values: " + str(ot.attributes)
                print " |-> -------------------------------------------- "
                try:
                    self.backend.updateObject(objectTemplate, ob, ot)
                except BaseException as e:
                    print e

    def start(self):
        print " Upgrade Server..."
        for objectTemplate in LBEObjectTemplate.objects.all():
            print " |-> \033[91m" + objectTemplate.name + '\033[0m:'
            objHelper = LBEObjectInstanceHelper(objectTemplate)
            try:
                scope = objHelper.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            objTarget = self.target.searchObjects(objectTemplate, scope)
            objBackend = self.backend.searchObjects(objectTemplate)
            # Target to Backend:
            for ot in objTarget:
                exist = False
                for ob in objBackend:
                    if ot.name == ob.name:
                        self._upgradeObject(objectTemplate, objHelper, ot, ob)
                        exist = True
                        break
                if not exist:
                    self._deleteORCreate(objectTemplate, ot)
            # Synced object:
            objectTemplate.synced_at = django.utils.timezone.now()
            objectTemplate.save()
        print " End."
Exemple #7
0
class ImportTarget():
    def __init__(self):
        self.backend = BackendHelper()
        self.target = TargetHelper()
        self.start_date = django.utils.timezone.now()

    def _getID(self, listRDN):
        listID = []
        for rdn in listRDN:
            listID.append(rdn.split('=')[1].split(',')[0])
        return listID

    def save(self):
        print 'Checking for Objects which do not exist into LBE Backend but in LDAP Server:'
        for objectTemplate in LBEObjectTemplate.objects.all():
            objectHelper = LBEObjectInstanceHelper(objectTemplate)
            try:
                scope = objectHelper.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            filter = '(&'
            for oc in objectHelper.callScriptClassMethod('object_classes'):
                filter += '(objectClass=' + oc + ')'
            filter += ')'
            print '\033[91m' + objectTemplate.name + '\033[0m: (\033[95m' + objectHelper.callScriptClassMethod(
                "base_dn") + '\033[0m) using \033[95m' + filter + '\033[0m'
            objTarget = self.target.searchObjects(objectTemplate, scope)
            objBackend = self.backend.searchObjects(objectTemplate)
            number = 0
            for ot in objTarget:
                exist = False
                for ob in objBackend:
                    if ot.name == ob.name:
                        exist = True
                        break
                if not exist:
                    number += 1
                    print '=> Adding \033[95m' + ot.name + '\033[0m object into LBE Backend... '
                    print " values: " + str(ot.attributes)
                    try:
                        self.backend.createObject(objectTemplate, ot, True)
                        print "\033[92mDone.\033[0m\n"
                    except BaseException as e:
                        print "\033[91mFail.\033[0m"
                        print "''''''''"
                        print e
                        print "''''''''"
            if number == 0:
                print '<None>'
            # Synced object:
            objectTemplate.synced_at = django.utils.timezone.now()
            objectTemplate.save()
        print '.........................'
        print 'Checking for Groups which do not exist into LBE Backend but in Target:'
        for groupTemplate in LBEGroup.objects.all():
            groupInstance = GroupInstanceHelper(groupTemplate)
            try:
                scope = groupInstance.callScriptClassMethod("search_scope")
            except BaseException:
                scope = 0
            grpTarget = self.target.searchObjects(groupTemplate, scope)
            grpBackend = self.backend.searchObjects(groupTemplate)
            for gt in grpTarget:
                exist = False
                for gb in grpBackend:
                    if gt.name == gb.name:
                        exist = True
                        break
                if not exist:
                    # import only existing group into LBE config
                    try:
                        LBEGroup.objects.get(
                            displayName__iexact=gt.displayName)
                    except BaseException:
                        continue
                    print '=> Adding \033[95m' + gt.name + '\033[0m group into LBE Backend... '
                    print " values: " + str(gt.attributes)
                    try:
                        if groupInstance.attributeName in gt.attributes:
                            gt.attributes[
                                groupInstance.attributeName] = self._getID(
                                    gt.attributes[groupInstance.attributeName])
                        groupHelper = GroupInstanceHelper(groupTemplate, gt)
                        groupHelper.createTemplate(True)
                        #print " >\033[91mThis group does not exists in LBE Configuration Group.\033[0m"
                        #print " >\033[91mIn order to see, manage it, please create it using some extra attribute:"
                        #print "  >\033[91m'Display Name': \033[95m" + gt.name + "\033[0m"
                        #print " >\033[91mInto the Script file:"
                        #print "  >'DN Attribute': \033[95m" + groupHelper.callScriptClassMethod("base_dn") + "\033[91m"
                        #print "  >'Attribute Name' & 'Object Classes': as you wish.\033[0m"
                        print "\033[92mDone.\033[0m\n"
                    except BaseException as e:
                        print "\033[91mFail.\033[0m\n"
                        print "''''''''"
                        print e
                        print "''''''''"
            # Synced group:
            groupTemplate.synced_at = django.utils.timezone.now()
            groupTemplate.save()
        print "End."