2017-09-19 48 views
0

按組訪問我有一個具有兩個應用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 

回答

1

首先,我將映射用戶對象的屬性,用於指定用戶所在的組:

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作品。

+0

謝謝你的回覆。我試過你的代碼,現在我根本無法登錄。我想我需要一種方法來檢查組名 – Eyla

+1

我發現這個: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

+0

我試過你的解決方案,但仍然沒有結果,沒有錯誤,我無法登錄,我嘗試了不同的配置,總是得到相同的結果 – Eyla

1

我個人推薦一個稍微不同的方式於此,在使用Django's built-in permissions

你可以做的是create custom permissions,例如,can_access_app1can_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()) 

這種方法將被詳細記錄,不會引入任何特殊弄虛作假具有巧妙的用戶對象,同時也將有一個好處,你可以將這些權限分配給個人,以及如果你想給予特殊訪問。此外,任何超級用戶帳戶都將自動擁有兩個權限,無需額外的努力!