Skip to content

Commit ecdc3c6

Browse files
committed
Add support for group in django permissions
1 parent 10ac156 commit ecdc3c6

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

conf/local.py.j2

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33
import os
44
from .settings_base import *
5+
from .django_ldap_extension import *
56

67
DEBUG = TEMPLATE_DEBUG = False
78

@@ -43,9 +44,9 @@ EMAIL_HOST = '{{ domain }}'
4344
EMAIL_HOST_USER = '{{ app }}@{{ domain }}'
4445
EMAIL_HOST_PASSWORD = '{{ mail_pwd }}'
4546

46-
# Tous acces
47+
# LDAP authentication and group management
4748
import ldap
48-
from django_auth_ldap.config import LDAPSearch, MemberDNGroupType
49+
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, MemberDNGroupType, LDAPGroupType
4950
AUTHENTICATION_BACKENDS = (
5051
'django_auth_ldap.backend.LDAPBackend',
5152
'django.contrib.auth.backends.ModelBackend',
@@ -63,13 +64,20 @@ AUTH_LDAP_USER_FLAGS_BY_GROUP = {
6364
"is_staff": "cn={{ app }}.staff,ou=permission,dc=yunohost,dc=org",
6465
"is_superuser": "cn={{ app }}.superadmin,ou=permission,dc=yunohost,dc=org"
6566
}
66-
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=permission,dc=yunohost,dc=org", ldap.SCOPE_SUBTREE)
67-
AUTH_LDAP_GROUP_TYPE = MemberDNGroupType("inheritPermission", "permissionYnh")
67+
AUTH_LDAP_GROUP_SEARCH = LDAPSearchUnion(
68+
LDAPSearch("ou=permission,dc=yunohost,dc=org", ldap.SCOPE_SUBTREE, filterstr=u'(cn=coin.*)'),
69+
LDAPSearch("ou=groups,dc=yunohost,dc=org", ldap.SCOPE_SUBTREE)
70+
)
71+
AUTH_LDAP_GROUP_TYPE = MemberDNGroupTypeUnion(
72+
MemberDNGroupType("inheritPermission"), # permissionYnh
73+
MemberDNGroupType("member")) # groupOfNamesYnh
6874
AUTH_LDAP_ALWAYS_UPDATE_USER = True
6975
AUTH_LDAP_AUTHORIZE_ALL_USERS = False
7076
AUTH_LDAP_FIND_GROUP_PERMS = True
7177
AUTH_LDAP_CACHE_GROUPS = True
7278
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 1000
79+
# Link Yunohost group with django permission group
80+
AUTH_LDAP_MIRROR_GROUPS_EXCEPT = ("{{ app }}.main", "{{ app }}.staff", "{{ app }}.superadmin")
7381
# import logging
7482
# logger = logging.getLogger('django_auth_ldap')
7583
# logger.addHandler(logging.StreamHandler())

scripts/install

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ynh_app_setting_set --app=$app --key=secret --value=$secret
1212
ynh_script_progression --message="Setting up source files..."
1313

1414
ynh_setup_source --dest_dir="$install_dir"
15+
cp ../sources/django_ldap_extension.py "$install_dir"/coin/
1516

1617
chmod 750 "$install_dir"
1718
chmod -R o-rwx "$install_dir"

scripts/upgrade

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ then
2424

2525
# Download, check integrity, uncompress and patch the source from app.src
2626
ynh_setup_source --dest_dir="$install_dir" --full_replace=1 --keep=coin/settings_local.py
27+
cp ../sources/django_ldap_extension.py "$install_dir"/coin/
2728
fi
2829

2930

sources/django_ldap_extension.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django_auth_ldap.config import LDAPGroupType
2+
3+
class MemberDNGroupTypeUnion(LDAPGroupType):
4+
5+
def __init__(self, *types, name_attr='cn'):
6+
self.types = types
7+
super(MemberDNGroupTypeUnion, self).__init__(name_attr)
8+
9+
def user_groups(self, ldap_user, group_search):
10+
res = dict()
11+
for t in self.types:
12+
res.update(t.user_groups(ldap_user, group_search))
13+
return res.items()
14+
15+
def is_member(self, ldap_user, group_dn):
16+
for t in self.types:
17+
if t.is_member(ldap_user, group_dn):
18+
return True
19+
return False

0 commit comments

Comments
 (0)