![](/img/trans.png)
[英]django-ldap auth and django 1.6 deprecated warning on user_profile
[英]Understanding Django-LDAP authentication
我是 Django 的新手,并被分配了一个任务,即以 LDAP 作为后端实现用户身份验证系统。 我猜文档假设最终开发人员在 Django 中有足够的经验能够理解和实现这样的系统。 这是我无法理解如何使用基于 LDAP 的身份验证实现简单的 django 应用程序的地方。 这是我到目前为止所了解的:
仅将更改发布到文件:
settings.py
....
import ldap
from django_auth_ldap.config import LDAPSearch
AUTH_LDAP_SERVER_URI = "ldap://<my url>"
AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend')
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
....
)
auth.html
<html>
<head>
<title>Login</title>
</head>
<body>
{{state}}
<form action="" method="post"> {% csrf_token %}
Email address: <input type="text" name="email" value="{{ email }}" />
Password: <input type="password" name="password" value="" />
<input type="submit" value="Log in" />
</form>
</body>
</html>
模型.py:
??
视图.py:
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext
def login_user(request):
username = password = ""
state = ""
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
print username, password
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
state = "Valid account"
else:
state = "Inactive account"
return render_to_response('auth_user/auth.html', RequestContext(request, {'state': state, 'username': username}))
我无法理解什么?
1> 我很确定我必须在views.py
实现一个函数来获取email
和password
的POST
值并验证它, 例如: [SO] 。 该文档指定实施搜索/绑定或直接绑定。 为什么? 如果views.py
将包含实际的一段身份验证代码,那么文档中指定的代码是做什么的?
2> 如果views.py
会执行实际的身份验证,那么为什么我们需要文档中指定的变量?
3> 作者在库方面做得很好,但是文档没有提供一个简单的准系统示例,说明如何使用 LDAP 实现整个身份验证系统。 任何人都可以指出这样的资源,如果它存在吗? 要实现这样的系统需要添加/修改哪些文件并不容易理解。
此页面可能包含您要查找的内容:关于 LDAP 后端的https://pypi.python.org/pypi/django-auth-ldap 。 您很幸运,存在一个,因此您不必自己编写身份验证后端 :-)
基本上 django.contrib.auth.models 已经有一个 User 对象,它包含你需要的关于用户的一切。 所以你不需要创建一个新的models.py。
您只需要在您的 views.py 中的登录功能中验证您自己,使用
from django.contrib.auth import authenticate, login
user = authenticate(username=request.REQUEST.get('email'), password=request.REQUEST.get('password'))
# handle error cases, inactive users, ...
login(request, user)
如果用户是无,则身份验证失败。 如果没有,您可以探索此对象以查看后端为您提供了什么。
然后,如果您希望将此应用程序的首选项链接到此用户,但不属于 LDAP,您可以选择创建另一个模型,并将用户作为外键。
在这种情况下,您将需要:
模型.py
根据您的应用程序定义对您很重要的数据。 您将从 LDAP 中提取用户数据,并用它和链接到用户的其他首选项填充此模型:
from django.contrib.auth.models import User
class Profile(models.Model):
"""User profile. Contains some basic configurable settings"""
user = models.ForeignKey(User, unique=True)
phone_number = models.CharField(max_length=256, blank=True, default='')
...
视图.py
在登录函数中,如果 request.method == 'POST',则 get_or_create 使用您刚刚从身份验证中获得的用户配置文件。
profile, profile_is_new = Profile.objects.get_or_create(user=user)
django-auth-ldap 文档确实是为熟悉 Django 的开发人员编写的。 还有 LDAP。 如果您是从头开始,我建议您一步一步来:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.