本文整理汇总了Python中pyvac.models.User.create_from_ldap方法的典型用法代码示例。如果您正苦于以下问题:Python User.create_from_ldap方法的具体用法?Python User.create_from_ldap怎么用?Python User.create_from_ldap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyvac.models.User
的用法示例。
在下文中一共展示了User.create_from_ldap方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: populate
# 需要导入模块: from pyvac.models import User [as 别名]
# 或者: from pyvac.models.User import create_from_ldap [as 别名]
def populate(engine, ldap):
""" Retrieve users from ldap directory and import them in local database
"""
session = DBSession()
# retrieve managers from dedicated group
managers = ldap.list_manager()
# retrieve users
searchFilter = '(&(objectClass=inetOrgPerson)(employeetype=Employee))'
required = ['objectClass', 'employeeType', 'cn', 'givenName', 'sn',
'manager', 'mail', 'ou', 'uid', 'userPassword']
users = ldap._search(searchFilter, required)
for user_dn, user_entry in users:
user_data = ldap.parse_ldap_entry(user_dn, user_entry)
login = user_data['login'].decode('utf-8')
# check what type of user it is
group = u'user'
# if it's a manager he should be in manager group
if user_data['dn'] in managers:
group = u'manager'
# if it's an admin he should be in admin group
what = '(member=%s)' % user_data['dn']
if len(ldap._search_admin(what, None)) > 0:
group = u'admin'
user = User.by_login(session, login)
if not user:
user = User.create_from_ldap(session, user_data, group)
else:
# update user with ldap informations in case it changed
user.email = user_data['email'].decode('utf-8')
user.firstname = user_data['firstname'].decode('utf-8')
user.lastname = user_data['lastname'].decode('utf-8')
user.manager_dn = user_data['manager_dn'].decode('utf-8')
user.dn = user_data['dn'].decode('utf-8')
user.role = group
session.add(user)
session.commit()