Example #1
0
    def create(cls, dn, attributes, la=None, addbase=False):
        """ Create an object.
            @param dn: Distinguished name of new user
            @param attributes: dictionary of attributes

            @return LdapObject
        """
        la = cls.get_ldap_adapator(la)
        dn = prepare_str_for_ldap(dn)
        if addbase:
            dn = "{0},{1}".format(dn, cls.get_base_dn(la))

        attrs = CaseInsensitiveDict(objectClass=cls.cfg.objectClasses)
        for key, val in attributes.iteritems():
            attrs[key] = prepare_str_for_ldap(val)


        addlist = attrs.items()
        try:
            la.add(dn, addlist)
        except ldap.ALREADY_EXISTS:
            raise DNConflict("Add failed: an entry already exists at {0}".format(dn))
        
        # If we are in dry run mode
        if la.is_dry_run():
            # We return the same data that the function got
            return cls(la, dn, attrs)
        else:
            # Non-dry-run mode.
            # The object attributes may have been changed by the LDAP server.
            # We need to fetch the object anew from the server.
            return cls.get(dn, la=la)
Example #2
0
 def set_attr(self, key, value):
     """ Set an attribute by key, value
     @param key Attribute name to set
     @param value value to which the attribute will be set
     """
     #All attributes are stored as lists, so convert as necessary
     if isinstance(value, (list, tuple)):
         self._attrs[key] = [prepare_str_for_ldap(l) for l in value]
     else:
         self._attrs[key] =  [prepare_str_for_ldap(value),]
Example #3
0
    def get(cls, dn=None, uid=None, la=None, addbase=False, attrs=None):
        """
            Retrieve a LdapObject by dn or uid
            @param dn object's dn
            @param uid object's unique identifier
            @param la LdapAdaptor to use
            @param addbase if True, the base is added to the dn
            @param attrs list of attributes to fetch


            You must provide either dn or uid.
            @return LdapObject or None
        """
        la = cls.get_ldap_adapator(la)
        dn = prepare_str_for_ldap(dn)
        uid = prepare_str_for_ldap(uid)
        if dn:
            params = {"scope":ldap.SCOPE_BASE, 
                "filterstr":cls.get_objectClass_filter(),
                }
            if addbase:
                base = "{0},{1}".format(dn,cls.get_base_dn(la))
            else:
                base = dn
        elif uid:
            uid_field = cls.cfg.uid
            if uid_field is None:
                raise TypeError("Object uid field is not defined")
            params = {"scope":ldap.SCOPE_SUBTREE, 
                "filterstr":"(&(%(field)s=%(uid)s)%(objCls)s)" % {
                    "objCls":cls.get_objectClass_filter(),
                    "field":uid_field,
                    "uid":uid,
                }
            }
            base = cls.get_base_dn(la)
        else:
            raise TypeError("You must provide either a uid or dn.")
        #print "Searching", params, "in", base

        if attrs is not None:
            params["attrs"] = attrs

        try:
            res = la.search(base, **params)
        except ldap.NO_SUCH_OBJECT, e:
            LOG.warn("Get failed for '{0}' with error: {1}".format(
                dn or uid,
                unicode(e),
                ))
            return None
Example #4
0
    def get(cls, dn=None, uid=None, la=None, addbase=False):
        """
            Retrieve a LdapObject by dn or uid
            @param dn object's dn
            @param uid object's unique identifier
            @param la LdapAdaptor to use
            @param addbase if True, the base is added to the dn


            You must provide either dn or uid.
            @return LdapObject or None
        """
        la = cls.get_ldap_adapator(la)
        dn = prepare_str_for_ldap(dn)
        uid = prepare_str_for_ldap(uid)
        if dn:
            params = {"scope":ldap.SCOPE_BASE, 
                "filterstr":cls.get_objectClass_filter(),
                }
            if addbase:
                base = "{0},{1}".format(dn,cls.get_base_dn(la))
            else:
                base = dn
        elif uid:
            uid_field = cls.cfg.uid
            if uid_field is None:
                raise TypeError("Object uid field is not defined")
            params = {"scope":ldap.SCOPE_SUBTREE, 
                "filterstr":"(&(%(field)s=%(uid)s)%(objCls)s)" % {
                    "objCls":cls.get_objectClass_filter(),
                    "field":uid_field,
                    "uid":uid,
                }
            }
            base = cls.get_base_dn(la)
        else:
            raise TypeError("You must provide either a uid or dn.")
        #print "Searching", params, "in", base
        try:
            res = la.search(base, **params)
        except ldap.NO_SUCH_OBJECT, e:
            LOG.warn("Get failed for '{0}' with error: {1}".format(
                dn or uid,
                unicode(e),
                ))
            return None
Example #5
0
    def move(self, parent_dn, addbase=False):
        """ Move this object to a new parent """
        new_parentdn = getattr(parent_dn, "dn", parent_dn)
        new_parentdn = prepare_str_for_ldap(new_parentdn)
        if addbase:
            new_parentdn = "{0},{1}".format(new_parentdn, self._ldap.base_dn)

        #TODO Check the parent object's type? (can it contain this?)
        self._rename(self._get_rdn(orig=False), new_parentdn)