本文整理汇总了Python中lib.auth.backends.MozLdapBackend.make_search_filter方法的典型用法代码示例。如果您正苦于以下问题:Python MozLdapBackend.make_search_filter方法的具体用法?Python MozLdapBackend.make_search_filter怎么用?Python MozLdapBackend.make_search_filter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.auth.backends.MozLdapBackend
的用法示例。
在下文中一共展示了MozLdapBackend.make_search_filter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from lib.auth.backends import MozLdapBackend [as 别名]
# 或者: from lib.auth.backends.MozLdapBackend import make_search_filter [as 别名]
def handle(self, *args, **options):
mail = args[0]
def show(key, value):
if (isinstance(value, list) and value
and isinstance(value[0], basestring)):
value = ', '.join(value)
self.stdout.write(key.ljust(20) + " " + str(value))
self.stdout.write("\nLOCAL USER ".ljust(79, '-'))
try:
user = User.objects.get(email=mail)
show("Username", user.username)
show("Email", user.email)
show("First name", user.first_name)
show("Last name", user.last_name)
show("Active", user.is_active)
show("Superuser", user.is_superuser)
show("Staff", user.is_staff)
self.stdout.write("Groups:".ljust(20), ending='')
if user.groups.all():
self.stdout.write(
', '.join([x.name for x in user.groups.all()])
)
else:
self.stdout.write("none")
except User.DoesNotExist:
self.stdout.write("Does NOT exist locally")
backend = MozLdapBackend()
backend.connect()
try:
search_filter = backend.make_search_filter(dict(mail=mail))
results = backend.ldo.search_s(
"dc=mozilla",
ldap.SCOPE_SUBTREE,
search_filter,
)
self.stdout.write("\nIN LDAP ".ljust(79, '-'))
uid = None
for uid, data in results:
for key, value in data.iteritems():
if key in LDAP_IGNORE_ATTRIBUTES:
continue
show(key, value)
if uid:
group_names = flatten_group_names(GROUP_MAPPINGS.values())
search_filter1 = backend.make_search_filter(
dict(cn=group_names),
any_parameter=True
)
search_filter2 = backend.make_search_filter({
'memberUID': [uid, mail],
'member': ['mail=%s,o=com,dc=mozilla' % mail,
'mail=%s,o=org,dc=mozilla' % mail,
'mail=%s,o=net,dc=mozilla' % mail],
}, any_parameter=True)
# combine the group part with the mail part
search_filter = '(&%s%s)' % (search_filter1, search_filter2)
group_results = backend.ldo.search_s(
"ou=groups,dc=mozilla",
ldap.SCOPE_SUBTREE,
search_filter,
['cn']
)
self.stdout.write("\nLDAP GROUPS ".ljust(79, '-'))
_group_mappings_reverse = {}
for django_name, ldap_names in GROUP_MAPPINGS.items():
ldap_names = flatten_group_names(ldap_names)
for name in ldap_names:
_group_mappings_reverse[name] = django_name
groups = [x[1]['cn'][0] for x in group_results]
for group in groups:
self.stdout.write(group.ljust(16) + ' -> ' +
_group_mappings_reverse.get(
group,
'*not a Django group*'
))
finally:
backend.disconnect()
示例2: handle
# 需要导入模块: from lib.auth.backends import MozLdapBackend [as 别名]
# 或者: from lib.auth.backends.MozLdapBackend import make_search_filter [as 别名]
def handle(self, *args, **options):
mail = args[0]
def show(key, value):
if isinstance(value, list) and value and isinstance(value[0], basestring):
value = ", ".join(value)
print key.ljust(20), value
print "\nLOCAL USER ".ljust(79, "-")
try:
user = User.objects.get(email=mail)
show("Username", user.username)
show("Email", user.email)
show("First name", user.first_name)
show("Last name", user.last_name)
show("Active", user.is_active)
show("Superuser", user.is_superuser)
show("Staff", user.is_staff)
print "Groups:".ljust(20),
if user.groups.all():
print ", ".join([x.name for x in user.groups.all()])
else:
print "none"
except User.DoesNotExist:
print "Does NOT exist locally"
backend = MozLdapBackend()
backend.connect()
try:
search_filter = backend.make_search_filter(dict(mail=mail))
results = backend.ldo.search_s("dc=mozilla", ldap.SCOPE_SUBTREE, search_filter)
print "\nIN LDAP ".ljust(79, "-")
uid = None
for uid, data in results:
for key, value in data.iteritems():
if key in LDAP_IGNORE_ATTRIBUTES:
continue
show(key, value)
if uid:
group_names = flatten_group_names(GROUP_MAPPINGS.values())
search_filter1 = backend.make_search_filter(dict(cn=group_names), any_parameter=True)
search_filter2 = backend.make_search_filter(
{
"memberUID": [uid, mail],
"member": [
"mail=%s,o=com,dc=mozilla" % mail,
"mail=%s,o=org,dc=mozilla" % mail,
"mail=%s,o=net,dc=mozillacom" % mail,
],
},
any_parameter=True,
)
# combine the group part with the mail part
search_filter = "(&%s%s)" % (search_filter1, search_filter2)
group_results = backend.ldo.search_s("ou=groups,dc=mozilla", ldap.SCOPE_SUBTREE, search_filter, ["cn"])
print "\nLDAP GROUPS ".ljust(79, "-")
_group_mappings_reverse = {}
for django_name, ldap_names in GROUP_MAPPINGS.items():
ldap_names = flatten_group_names(ldap_names)
for name in ldap_names:
_group_mappings_reverse[name] = django_name
groups = [x[1]["cn"][0] for x in group_results]
for group in groups:
print group.ljust(16), "-> ",
print _group_mappings_reverse.get(group, "*not a Django group*")
finally:
backend.disconnect()