mirror of
https://github.com/kakwa/ldapcherry
synced 2025-07-04 20:37:48 +02:00
various changes to support python3
* changes in urllib imports since quote_plus in urllib with python 2 and in urllib.parse in python 3 * changes in imports for Sets since set is a native type in python 3 and doesn't requires an import * fix in __import__, '-1' level for module path discovery is not supported anymore, switching to 0 (absolute import only).
This commit is contained in:
parent
69526610f3
commit
74dc6c5894
5 changed files with 70 additions and 50 deletions
|
@ -7,7 +7,11 @@
|
|||
|
||||
# This is a demo backend
|
||||
|
||||
from sets import Set
|
||||
|
||||
import sys
|
||||
if sys.version < '3':
|
||||
from sets import Set as set
|
||||
|
||||
import ldapcherry.backend
|
||||
from ldapcherry.exceptions import UserDoesntExist, \
|
||||
GroupDoesntExist, MissingParameter, \
|
||||
|
@ -37,12 +41,12 @@ class Backend(ldapcherry.backend.Backend):
|
|||
self.backend_name = name
|
||||
admin_user = self.get_param('admin.user', 'admin')
|
||||
admin_password = self.get_param('admin.password', 'admin')
|
||||
admin_groups = Set(re.split('\W+', self.get_param('admin.groups')))
|
||||
admin_groups = set(self._basic_splitter(self.get_param('admin.groups')))
|
||||
basic_user = self.get_param('basic.user', 'user')
|
||||
basic_password = self.get_param('basic.password', 'user')
|
||||
basic_groups = Set(re.split('\W+', self.get_param('basic.groups')))
|
||||
basic_groups = set(self._basic_splitter(self.get_param('basic.groups')))
|
||||
pwd_attr = self.get_param('pwd_attr')
|
||||
self.search_attrs = Set(
|
||||
self.search_attrs = set(
|
||||
re.split('\W+', self.get_param('search_attributes')),
|
||||
)
|
||||
self.pwd_attr = pwd_attr
|
||||
|
@ -60,6 +64,10 @@ class Backend(ldapcherry.backend.Backend):
|
|||
'groups': basic_groups,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _basic_splitter(in_str):
|
||||
return [re.sub(r'(?<!\\)\\', '', x) for x in re.split(r'(?<!\\),\W*', in_str)]
|
||||
|
||||
def _check_fix_users(self, username):
|
||||
if self.admin_user == username or self.basic_user == username:
|
||||
raise Exception('User cannot be modified')
|
||||
|
@ -91,7 +99,7 @@ class Backend(ldapcherry.backend.Backend):
|
|||
if username in self.users:
|
||||
raise UserAlreadyExists(username, self.backend_name)
|
||||
self.users[username] = attrs
|
||||
self.users[username]['groups'] = Set([])
|
||||
self.users[username]['groups'] = set([])
|
||||
|
||||
def del_user(self, username):
|
||||
""" Delete a user from the backend
|
||||
|
@ -107,7 +115,7 @@ class Backend(ldapcherry.backend.Backend):
|
|||
raise UserDoesntExist(username, self.backend_name)
|
||||
|
||||
def set_attrs(self, username, attrs):
|
||||
""" Set a list of attributes for a given user
|
||||
""" set a list of attributes for a given user
|
||||
|
||||
:param username: 'key' attribute of the user
|
||||
:type username: string
|
||||
|
@ -128,7 +136,7 @@ class Backend(ldapcherry.backend.Backend):
|
|||
"""
|
||||
self._check_fix_users(username)
|
||||
current_groups = self.users[username]['groups']
|
||||
new_groups = current_groups | Set(groups)
|
||||
new_groups = current_groups | set(groups)
|
||||
self.users[username]['groups'] = new_groups
|
||||
|
||||
def del_from_groups(self, username, groups):
|
||||
|
@ -143,7 +151,7 @@ class Backend(ldapcherry.backend.Backend):
|
|||
"""
|
||||
self._check_fix_users(username)
|
||||
current_groups = self.users[username]['groups']
|
||||
new_groups = current_groups - Set(groups)
|
||||
new_groups = current_groups - set(groups)
|
||||
self.users[username]['groups'] = new_groups
|
||||
|
||||
def search(self, searchstring):
|
||||
|
|
|
@ -11,7 +11,10 @@ import ldap.modlist as modlist
|
|||
import ldap.filter
|
||||
import logging
|
||||
import ldapcherry.backend
|
||||
from sets import Set
|
||||
import sys
|
||||
if sys.version < '3':
|
||||
from sets import Set as set
|
||||
|
||||
from ldapcherry.exceptions import UserDoesntExist, \
|
||||
GroupDoesntExist, \
|
||||
UserAlreadyExists
|
||||
|
@ -74,12 +77,12 @@ class Backend(ldapcherry.backend.Backend):
|
|||
for o in re.split('\W+', self.get_param('objectclasses')):
|
||||
self.objectclasses.append(self._str(o))
|
||||
self.group_attrs = {}
|
||||
self.group_attrs_keys = Set([])
|
||||
self.group_attrs_keys = set([])
|
||||
for param in config:
|
||||
name, sep, group = param.partition('.')
|
||||
if name == 'group_attr':
|
||||
self.group_attrs[group] = self.get_param(param)
|
||||
self.group_attrs_keys |= Set(
|
||||
self.group_attrs_keys |= set(
|
||||
self._extract_format_keys(self.get_param(param))
|
||||
)
|
||||
|
||||
|
@ -393,7 +396,7 @@ class Backend(ldapcherry.backend.Backend):
|
|||
ldap_client.unbind_s()
|
||||
|
||||
def set_attrs(self, username, attrs):
|
||||
""" Set user attributes"""
|
||||
""" set user attributes"""
|
||||
ldap_client = self._bind()
|
||||
tmp = self._get_user(self._str(username), ALL_ATTRS)
|
||||
if tmp is None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue