Django 限制/允许来自 ldap 的组访问

标签 django python-3.5 openldap django-auth-ldap

我有一个有两个应用程序 App1 和 App2 的 Django 项目)
每个应用程序只有 1 个 View 。
.我的项目使用 django-auth-ldap 连接到 openldap。
我有两个组(Group1,Group2)。

我在 app1 和 app2 (@login_required) 中的 View 之前添加了装饰器,结果正如预期的那样,group1 和 group2 中的所有用户都可以登录到这两个应用程序。

我希望能够只允许 group1 仅访问 app1,而 group2 仅允许访问 app2。

我尝试了很多代码,但没有人与我合作。

这是我的代码:

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/'
    )

在像这样的 View 上使用它:
@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 限制/允许来自 ldap 的组访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46311149/

相关文章:

python - Django 模型查询使用 join

django - 如何测试 Django 装饰器?

ldap - 在Docker镜像中, “Slapd stop”成功,但slapd仍在运行

django - Gunicorn 启动时抛出 OSError Errno 1

python - 虚拟环境查找错误 : no codec search functions registered: can't find encoding

在Python中将列表列表的所有元素转换为 float

Python - 在 "detached"进程中生成函数的最佳方式

python - 如何创建返回列表包含字符串的类型提示?

go - 如何在 Golang 上通过 IPC 为 OpenLDAP 客户端实现 SASL/EXTERNAL?

java - 从 Java 修改 LDAP 属性失败,但使用 ldapmodify 命令成功