utils.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Tuckshop 作者: MatthewJohn 项目源码 文件源码
def login(username, password):
    from tuckshop.core.config import Config
    from tuckshop.app.models import User
    if 'TUCKSHOP_DEVEL' in environ and environ['TUCKSHOP_DEVEL']:
        # If tuckshop in development mode, match all passwords
        # again 'password'
        if password != 'password':
            return False
    else:
        # Otherwise authenticate against LDAP server
        ldap_obj = ldap.initialize('ldap://%s:389' % Config.LDAP_SERVER())
        dn = 'uid=%s,%s' % (username, Config.LDAP_USER_BASE())
        try:
            # Attempt to bind to LDAP
            ldap_obj.simple_bind_s(dn, password)
        except:
            # If the connection throws an exception, return False
            return False

    # Create user object for currently logged in user
    user_object = User.objects.filter(uid=username)

    # If a user object does not exist, create a new one
    if (not len(user_object)):
        user_object = User(uid=username)
        user_object.save()
    else:
        user_object = user_object[0]

        # Determine if the user account is a shared account.
        # If it is, do not allow the user to login
        if user_object.shared:
            return False

    # Return user object
    return user_object
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号