Django的限制/允许从LDAP
问题描述:
按组访问我有一个具有两个应用App1和应用2) 每个应用程序具有仅1视图Django项目。 。我的项目使用django-auth-ldap连接到openldap。 我有两个组(Group1,Group2)。Django的限制/允许从LDAP
我之前我在APP1和App2(@login_required)和结果的看法添加装饰如预期那样从1与组2的所有用户将能够登录到这两个应用程序。
我希望能够只允许GROUP1只能访问APP1,只有第2组的访问APP 2。
我尝试了许多代码,但我没有一个工作。
这里是我的代码:
app1.views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.contrib.auth import views as auth_views
@login_required(login_url='/accounts/login/')
def index(request):
#getting our template
template = loader.get_template('main/index.html')
#rendering the template in HttpResponse
return HttpResponse(template.render())
这里是settings.py我的LDAP设置:
#Generated by 'django-admin startproject' using Django 1.11.
import os
import django
AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend',)
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
AUTH_LDAP_SERVER_URI = "ldap://mydomain.com"
AUTH_LDAP_BIND_DN = "cn=admin,dc=mydomain,dc=com"
AUTH_LDAP_BIND_PASSWORD = "mypass"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=ou_org_unit,dc=mydomain,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=ou_org_unit,cn=group1,cn=group2,dc=mydomain,dc=com",
ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 3600
答
首先,我将映射用户对象的属性,用于指定用户所在的组:
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
"ldap_group": "cn" # not 100% sure if this is what's required, just guessing
}
然后做一个装饰用user_passes_test
:
from django.contrib.auth.decorators import user_passes_test
def ldap_group_required(group_name):
"""
Checks if a user is in the specified LDAP group.
"""
return user_passes_test(
lambda u: hasattr(u, 'ldap_group') and u.ldap_group == group_name,
login_url='/accounts/login/'
)
使用它在一个视图像这样:
@ldap_group_required('group1')
def index(request):
#getting our template
template = loader.get_template('main/index.html')
#rendering the template in HttpResponse
return HttpResponse(template.render())
如果检查出source code,这实际上是如何login_required
作品。
答
我个人推荐一个稍微不同的方式于此,在使用Django's built-in permissions。
你可以做的是create custom permissions,例如,can_access_app1
和can_access_app2
。然后,由于django-auth-ldap会自动将您的所有组复制到您的Django数据库中,因此可以将这些权限分配给适当的组。
现在,您的组和它们各自的权限设置,那么您需要适当的装饰您的观点。例如:
# app1/views.py
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
@permission_required('app1.can_access_app1')
def index(request):
#getting our template
template = loader.get_template('main/index.html')
#rendering the template in HttpResponse
return HttpResponse(template.render())
这种方法将被详细记录,不会引入任何特殊弄虚作假具有巧妙的用户对象,同时也将有一个好处,你可以将这些权限分配给个人,以及如果你想给予特殊访问。此外,任何超级用户帐户都将自动拥有两个权限,无需额外的努力!
谢谢你的回复。我试过你的代码,现在我根本无法登录。我想我需要一种方法来检查组名 – Eyla
我发现这个:https://django-auth-ldap.readthedocs.io/en/stable/users.html#direct-attribute-access(请参阅页面底部)。您可以从属性映射中删除''ldap_group“:”cn“',并将组检查修改为像'hasattr(u,'ldap_user')和u.ldap_user.group_names'中的group_name。你必须稍微玩一下,但看起来好像获得用户所在的组的列表是可能的。 – mindcruzer
我试过你的解决方案,但仍然没有结果,没有错误,我无法登录,我尝试了不同的配置,总是得到相同的结果 – Eyla