python實現LDAP中組(group)添加使用者(user)功能__python

來源:互聯網
上載者:User

因為沒發現群組清空使用者的功能,最開始寫的函數是三個參數:群組標識、添加使用者、刪除使用者。覺得介面不好用,後來發現組合下即可,代碼如下:

    #清除,在將使用者全部插入    def update_users(self, groupname, users=[]):        """        :param groupname: 組名,字串類型如"groupname";        :param users:使用者列表,如addusers為['user1','user2']        :return:        注函數中定義的參數modlist可以接受多個參數列表,其中:        MOD_ADD: 如果屬性存在,這個屬性可以有多個值,那麼新值加進去,舊值保留        MOD_DELETE :如果屬性的值存在,值將被刪除        MOD_REPLACE :這個屬性所有的舊值將會被刪除,這個值被加進去        舉例:         [( ldap.MOD_ADD, 'memberUid', 'user1' ),            ( ldap.MOD_DELETE, 'memberUid', 'user3')]        """        modlist = []        if len(users) == 0:            modlist.append((ldap.MOD_REPLACE, 'memberUid', ""))        for index in range(len(users)):            if index == 0:                modlist.append((ldap.MOD_REPLACE, 'memberUid', str(users[index])))            else:                modlist.append((ldap.MOD_ADD, 'memberUid', str(users[index])))        try:            obj = self.ldapconn            obj.protocal_version = ldap.VERSION3            modifyDN = "cn=%s,ou=Group,%s" % (groupname, self.base_dn)            obj.modify_s(modifyDN, modlist)            obj.unbind_s()            return True        except ldap.LDAPError, e:            print("%s update users failed,reason: %s" % (groupname, str(e)))        return False


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.