|
@@ -1,12 +1,13 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
2
|
from tg.configuration.auth import TGAuthMetadata
|
3
|
|
-from who_ldap import LDAPAttributesPlugin, LDAPGroupsPlugin
|
|
3
|
+from who_ldap import LDAPAttributesPlugin as BaseLDAPAttributesPlugin
|
|
4
|
+from who_ldap import LDAPGroupsPlugin as BaseLDAPGroupsPlugin
|
4
|
5
|
from who_ldap import LDAPSearchAuthenticatorPlugin as BaseLDAPSearchAuthenticatorPlugin
|
5
|
6
|
|
6
|
7
|
from tracim.lib.auth.base import Auth
|
7
|
8
|
from tracim.lib.helpers import ini_conf_to_bool
|
8
|
9
|
from tracim.lib.user import UserApi
|
9
|
|
-from tracim.model import auth, DBSession, User
|
|
10
|
+from tracim.model import DBSession, User
|
10
|
11
|
|
11
|
12
|
|
12
|
13
|
class LDAPAuth(Auth):
|
|
@@ -21,7 +22,7 @@ class LDAPAuth(Auth):
|
21
|
22
|
super().__init__(config)
|
22
|
23
|
self.ldap_auth = self._get_ldap_auth()
|
23
|
24
|
self.ldap_user_provider = self._get_ldap_user_provider()
|
24
|
|
- if self._config.get('ldap_group_enabled', False):
|
|
25
|
+ if ini_conf_to_bool(self._config.get('ldap_group_enabled', False)):
|
25
|
26
|
self.ldap_groups_provider = self._get_ldap_groups_provider()
|
26
|
27
|
|
27
|
28
|
def wrap_config(self):
|
|
@@ -30,7 +31,7 @@ class LDAPAuth(Auth):
|
30
|
31
|
self._config['sa_auth'].authenticators = [('ldapauth', self.ldap_auth)]
|
31
|
32
|
|
32
|
33
|
mdproviders = [('ldapuser', self.ldap_user_provider)]
|
33
|
|
- if self._config.get('ldap_group_enabled', False):
|
|
34
|
+ if ini_conf_to_bool(self._config.get('ldap_group_enabled', False)):
|
34
|
35
|
mdproviders.append(('ldapgroups', self.ldap_groups_provider))
|
35
|
36
|
self._config['sa_auth'].mdproviders = mdproviders
|
36
|
37
|
|
|
@@ -105,32 +106,53 @@ class LDAPSearchAuthenticatorPlugin(BaseLDAPSearchAuthenticatorPlugin):
|
105
|
106
|
|
106
|
107
|
class LDAPApplicationAuthMetadata(TGAuthMetadata):
|
107
|
108
|
|
108
|
|
- # map from LDAP group names to TurboGears group names
|
109
|
|
- group_map = {'operators': 'managers'}
|
110
|
|
-
|
111
|
|
- # set of permissions for all mapped groups
|
112
|
|
- permissions_for_groups = {'managers': {'manage'}}
|
113
|
|
-
|
114
|
|
- def __init__(self, sa_auth):
|
115
|
|
- self.sa_auth = sa_auth
|
|
109
|
+ def __init__(self, config):
|
|
110
|
+ self.sa_auth = config.get('sa_auth')
|
|
111
|
+ self._config = config
|
116
|
112
|
|
117
|
113
|
def get_user(self, identity, userid):
|
118
|
|
- user = identity.get('user')
|
119
|
|
- if user:
|
120
|
|
- name = '{email}'.format(**user).strip()
|
121
|
|
- user.update(user_name=userid, display_name=name)
|
122
|
|
- return user
|
|
114
|
+ return identity.get('user')
|
123
|
115
|
|
124
|
116
|
def get_groups(self, identity, userid):
|
125
|
|
- get_group = self.group_map.get
|
126
|
|
- return [get_group(g, g) for g in identity.get('groups', [])]
|
|
117
|
+ if not ini_conf_to_bool(self._config.get('ldap_group_enabled')):
|
|
118
|
+
|
|
119
|
+ # TODO - B.S. - 20160212: récupérer identity['user'].groups directement produit
|
|
120
|
+ # Parent instance XXX is not bound to a Session. Voir avec Damien.
|
|
121
|
+ user = DBSession.query(User).filter(User.email == identity['user'].email).one()
|
|
122
|
+ return [g.group_name for g in user.groups]
|
127
|
123
|
|
128
|
|
- def get_permissions_for_group(self, group):
|
129
|
|
- return self.permissions_for_groups.get(group, set())
|
|
124
|
+ return [g.group_name for g in identity['user'].groups]
|
|
125
|
+ else:
|
|
126
|
+ raise NotImplementedError()
|
130
|
127
|
|
131
|
128
|
def get_permissions(self, identity, userid):
|
132
|
|
- permissions = set()
|
133
|
|
- get_permissions = self.get_permissions_for_group
|
134
|
|
- for group in self.get_groups(identity, userid):
|
135
|
|
- permissions |= get_permissions(group)
|
136
|
|
- return permissions
|
|
129
|
+ if not ini_conf_to_bool(self._config.get('ldap_group_enabled')):
|
|
130
|
+
|
|
131
|
+ # TODO - B.S. - 20160212: récupérer identity['user'].groups directement produit
|
|
132
|
+ # Parent instance XXX is not bound to a Session. Voir avec Damien.
|
|
133
|
+ user = DBSession.query(User).filter(User.email == identity['user'].email).one()
|
|
134
|
+ return [p.permission_name for p in user.permissions]
|
|
135
|
+
|
|
136
|
+ return [p.permission_name for p in identity['user'].permissions]
|
|
137
|
+ else:
|
|
138
|
+ raise NotImplementedError()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+class LDAPGroupsPlugin(BaseLDAPGroupsPlugin):
|
|
142
|
+
|
|
143
|
+ def add_metadata(self, environ, identity):
|
|
144
|
+ super().add_metadata(environ, identity)
|
|
145
|
+ groups_names = identity[self.name]
|
|
146
|
+ raise NotImplementedError() # Should sync groups etc ...
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+class LDAPAttributesPlugin(BaseLDAPAttributesPlugin):
|
|
150
|
+
|
|
151
|
+ def __init__(self, *args, **kwargs):
|
|
152
|
+ super().__init__(*args, **kwargs)
|
|
153
|
+ self._user_api = UserApi(None)
|
|
154
|
+
|
|
155
|
+ def add_metadata(self, environ, identity):
|
|
156
|
+ super().add_metadata(environ, identity)
|
|
157
|
+ # TODO - B.S. - 20160212: identity contains now som information from LDAP what we can save in local database
|
|
158
|
+ identity[self.name] = self._user_api.get_one_by_email(identity.get('repoze.who.userid'))
|