Skip to content

Create import object

Cédric Joron edited this page Aug 29, 2013 · 2 revisions

In this tutorial, you will be able to create a Object Template from LBE and import its data from the Target Server.

Before beginning this tutorial, you must have already done the installation & configuration of LBE.

Object From Target

I want to import this object:

Object From Target

As you can see, the RDN of the object is uid=jdoe,ou=Employees,ou=People,dc=opencsi,dc=com. This object has some attributes which are:

  • cn
  • sn
  • givenName
  • mail
  • telephoneNumber
  • title
  • uid

And some Objects Class too:

  • Top
  • person
  • organizationalPerson
  • inetOrgPerson

Those attributes and objects class are very important for creating the Object Template.

Object Template

Now, let's create the Object Template from LBE. (You need to have the right to go to the administration part in order to create new Object Template).

For created a new object Template, go to: Object -> Add Object menu from the navbar. (for more information about Object Template view page: https://github.com/OpenCSI/lbe/wiki/ObjectEmployee)

Create Object Template

As you can see, the Instance Name Attribute value corresponds to the RDN Attribute of the Target (here: uid, from uid=jdoe).

If your attribute is not listed in the Object Template, you need to create it from:
Attribute -> Add Attribute menu (navbar)
By default, use the Default Script file for Your Object: use EmployeePostConfig.
You should be able to change it after. (recommended)

Instance Attribute

In order to use the Target Attribute (cn, sn, uid, ...), you need to link those attribute to your Object Template. Links here are called Instance Attribute. You need to create each instance attribute for your object template.

To create them, go to the Object -> List Object menu (navbar) and select your new Object Temaplte created. (here: employee).

This page is divided in two parts:

  • Object Template Information that you can manage. (as create part)
  • Attribute Instance.

To add new Attribute Instance, just click on the Add button.

Attribute Instance

As you can see in the picture, I want to add the telephoneNumber attribute.

  • The name value must be the telephoneNumber Attribute.
  • I want to make this field multi value, so I check its value.
  • I choose Final value because I want to define the value manually (not computed: https://github.com/OpenCSI/lbe/wiki/API for more information)
  • I don't care for other arguments for the attribute. (But not for the title (Gender) attribute: go to https://github.com/OpenCSI/lbe/wiki/Attributes and Widget part for this object.)

Finally, this is a picture about all Attribute Instance:

All Attribute Instance

As you can see, some attributes are Virtual (that is to say computed thanks to the Script File for the Object Template): cn, mail, uid. and the others are Final.

Script File

After created the Object Template and its Attribute Instances. I need to configure the Script File for the Object Template. (information about Script File https://github.com/OpenCSI/lbe/wiki/API)

Do you remember that some attributes are Virtual?:

this is the attributes Virtual and their reasons:

  • cn -> givenName (John) + sn (Doe) values = (John Doe)
  • mail -> uid (jdoe) value + '@opencsi.com' = (jdoe@opencsi.com)
  • uid -> First latter of givenName (j) + sn (doe) value = (jdoe)

This is the Custom Script File for the Object Template employee:

# -*- coding: utf-8 -*-
from django import forms

from directory.forms import LBEObjectInstanceForm

class EmployeePostConfig(LBEObjectInstanceForm):
    # BEGINNING OF REQUIRED SECTION ----------------------------------------------
    def __init__(self, lbeObjectTemplate, lbeObjectInstance=None, *args, **kwargs):
        self.template = lbeObjectTemplate
        self.instance = lbeObjectInstance
        super(EmployeePostConfig, self).__init__(self.template, *args, **kwargs)

    # END OF REQUIRED SECTION ----------------------------------------------------

    # REQUIRED SECTION FOR LDAP BACKEND ------------------------------------------    

    # These methods are used only for LDAP target. Must be class methods
    # instanceNameAttribute will be used as RDN attribute
    @classmethod
    def base_dn(cls):
        return 'ou=Employees,ou=People,dc=opencsi,dc=com'

    # Note this method must return a list
    @classmethod
    def object_classes(cls):
        return ['top', 'person', 'organizationalPerson', 'inetOrgPerson']

    @classmethod
    def search_scope(cls):
        return 2

    # END OF REQUIRED SECTION ----------------------------------------------------

    # This method enables to ignore some attributes into reconciliation part
    @classmethod
    def ignore_attributes(cls):
        return []

    # TODO: Think about implements is_valid method here to be called by LBEObjectInstanceForm if possible    
    # def is_valid():

    # Validators methods are used to alter, verify, compute the values of an attribute
    # IMPORTANT: Remembers all attributes are store in a list, even mono valued. Therefore, you must return a list

    # Prototype:
    # def clean_<attributeName>(self): (NOT the displayName) for FINAL attributes [django form template]
    # def compute_<attributeName>(self): (NOT the displayName) for VIRTUAL attributes

    def clean_givenName(self):
        try:
            # Mono value:
            return [self.cleaned_data['givenName'].capitalize()]
        except:
            raise forms.ValidationError("The field must be a valid attribute.")

    def clean_telephoneNumber(self):
        try:
            # Multi value:
            tab = []
            i = 0
            for value in self.cleaned_data['telephoneNumber'].split('\0'):
                if not value == "":
                    tab.append(value)
                i = i + 1
            return tab
        except:
            raise forms.ValidationError("The field #" + str(i) + " must be a valid attribute.")

    def clean_sn(self):
        try:
            # modify attribut object:
            # for multi-value: just create an list to set and return it.
            return [self.cleaned_data['sn'].capitalize()]
        except BaseException:
            raise forms.ValidationError("This field must be a valid attribute.")

    def compute_cn(self):
        return [self.instance.attributes['givenName'][0] + ' ' + self.instance.attributes['sn'][0]]

    def compute_uid(self):
        return [(self.instance.attributes['givenName'][0][0] + self.instance.attributes['sn'][0].replace(' ', '')).lower()]

    def compute_mail(self):
        return [self.compute_uid()[0] + '@opencsi.com']

After created the Script File, you need to import it to LBE.

Got to Script -> Add Script menu (navbar).

Select your script file. For the full name value, you need to specify the file name '.' the Class Name (here employee.EmployeePostConfig).

After added the Script file into LBE, you need to specify it for the Object Template: Object -> List Object: employee and Script file choice to yours.

Clone this wiki locally