Python ldap.MOD_REPLACE Examples

The following are 6 code examples of ldap.MOD_REPLACE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ldap , or try the search function .
Example #1
Source File: ldap_attr.py    From isam-ansible-roles with Apache License 2.0 6 votes vote down vote up
def exact(self):
        try:
            results = self.connection.search_s(
                self.dn, ldap.SCOPE_BASE, attrlist=[self.name])
        except ldap.LDAPError:
            e = get_exception()
            self.module.fail_json(
                msg="Cannot search for attribute %s" % self.name,
                details=str(e))

        current = results[0][1].get(self.name, [])
        modlist = []

        if frozenset(self.values) != frozenset(current):
            if len(current) == 0:
                modlist = [(ldap.MOD_ADD, self.name, self.values)]
            elif len(self.values) == 0:
                modlist = [(ldap.MOD_DELETE, self.name, None)]
            else:
                modlist = [(ldap.MOD_REPLACE, self.name, self.values)]

        return modlist 
Example #2
Source File: openldap2opendj.py    From community-edition-setup with MIT License 6 votes vote down vote up
def post_ldap_update(ldap_bind_dn, ldap_bind_pw):
    conn = ldap.initialize('ldaps://localhost:1636')
    conn.protocol_version = 3 
    conn.simple_bind_s(ldap_bind_dn, ldap_bind_pw)

    result = conn.search_s('ou=appliances,o=gluu',ldap.SCOPE_SUBTREE,'(oxIDPAuthentication=*)',['oxIDPAuthentication'])

    dn = result[0][0]
    oxIDPAuthentication = json.loads(result[0][1]['oxIDPAuthentication'][0])

    config = json.loads(oxIDPAuthentication['config'])

    if config['servers'][0]=='localhost:1636' and config['bindDN'].lower()=='cn=directory manager,o=gluu':
        config['bindDN'] = 'cn=Directory Manager'
        oxIDPAuthentication['config'] = json.dumps(config)
        oxIDPAuthentication = json.dumps(oxIDPAuthentication, indent=2)
        conn.modify_s(dn, [( ldap.MOD_REPLACE, 'oxIDPAuthentication',  oxIDPAuthentication)])

    result = conn.search_s('ou=appliances,o=gluu',ldap.SCOPE_SUBTREE,'(oxTrustConfCacheRefresh=*)',['oxTrustConfCacheRefresh'])

    dn = result[0][0]
    oxTrustConfCacheRefresh = json.loads(result[0][1]['oxTrustConfCacheRefresh'][0])
    oxTrustConfCacheRefresh['inumConfig']['bindDN'] = 'cn=Directory Manager'
    oxTrustConfCacheRefresh = json.dumps(oxTrustConfCacheRefresh, indent=2)
    conn.modify_s(dn, [( ldap.MOD_REPLACE, 'oxTrustConfCacheRefresh',  oxTrustConfCacheRefresh)]) 
Example #3
Source File: ldapadmin.py    From diting with GNU General Public License v2.0 6 votes vote down vote up
def ldap_update_password(self, uid, new_password=None, old_password=None):
        """
        更新密码
        :param uid: 用户uid,新password
        :return: True|None
        """
        result = None
        try:
            obj = self.ldapconn
            obj.protocal_version = ldap.VERSION3
            modifyDN = "uid=%s,%s" % (uid, BASE_DN)
            new_password_encrypt = pass_encrypt(new_password)
            #有old_password情况下
            if old_password:
                obj.passwd_s(modifyDN, [str(old_password).encode('utf-8')], [new_password_encrypt.encode('utf-8')])
                result = True
            else:
                obj.modify_s(modifyDN, [(ldap.MOD_REPLACE, 'userPassword', [new_password_encrypt.encode('utf-8')])])
                result = True
            obj.unbind_s()
        except ldap.LDAPError as e:
            logger.error("%s 密码更新失败,原因为: %s" % (uid, str(e)))
            return False
        return result 
Example #4
Source File: plugin.py    From allura with Apache License 2.0 6 votes vote down vote up
def set_password(self, user, old_password, new_password):
        dn = ldap_user_dn(user.username)
        if old_password:
            ldap_ident = dn
            ldap_pass = old_password.encode('utf-8')
        else:
            ldap_ident = ldap_pass = None
        try:
            con = ldap_conn(ldap_ident, ldap_pass)
            new_password = self._encode_password(new_password)
            con.modify_s(
                dn, [(ldap.MOD_REPLACE, b'userPassword', new_password)])
            con.unbind_s()
            user.last_password_updated = datetime.utcnow()
            session(user).flush(user)
        except ldap.INVALID_CREDENTIALS:
            raise exc.HTTPUnauthorized() 
Example #5
Source File: plugin.py    From allura with Apache License 2.0 5 votes vote down vote up
def set_pref(self, user, pref_name, pref_value):
        if pref_name in self.fields:
            con = ldap_conn()
            ldap_attr = self.fields[pref_name]
            con.modify_s(ldap_user_dn(user.username),
                         [(ldap.MOD_REPLACE, ldap_attr.encode('utf-8'), pref_value.encode('utf-8'))])
            con.unbind_s()
        else:
            return LocalUserPreferencesProvider().set_pref(user, pref_name, pref_value) 
Example #6
Source File: ldap.py    From Adminset_Zabbix with Apache License 2.0 5 votes vote down vote up
def ldap_update_pass(self,uid=None,oldpass=None,newpass=None):
        modify_entry = [(ldap.MOD_REPLACE,'userpassword',newpass)]
        obj = self.ldapconn
        target_cn = self.ldap_search_dn(uid)
        try:
            obj.simple_bind_s(target_cn,oldpass)
            obj.passwd_s(target_cn,oldpass,newpass)
            return True
        except ldap.LDAPError,e:
            return False