|
@@ -1,4 +1,5 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
|
2
|
+import transaction
|
2
|
3
|
from tg.configuration.auth import TGAuthMetadata
|
3
|
4
|
from who_ldap import LDAPAttributesPlugin as BaseLDAPAttributesPlugin
|
4
|
5
|
from who_ldap import LDAPGroupsPlugin as BaseLDAPGroupsPlugin
|
|
@@ -92,19 +93,27 @@ class LDAPSearchAuthenticatorPlugin(BaseLDAPSearchAuthenticatorPlugin):
|
92
|
93
|
# Note: super().authenticate return None if already authenticated or not found
|
93
|
94
|
email = super().authenticate(environ, identity)
|
94
|
95
|
if email:
|
95
|
|
- self._sync_ldap_user(email)
|
|
96
|
+ self._sync_ldap_user(email, environ, identity)
|
96
|
97
|
return email
|
97
|
98
|
|
98
|
|
- def _sync_ldap_user(self, email):
|
|
99
|
+ def _sync_ldap_user(self, email, environ, identity):
|
|
100
|
+ # Create or get user for connected email
|
99
|
101
|
if not self._user_api.user_with_email_exists(email):
|
100
|
102
|
user = User(email=email, imported_from=LDAPAuth.name)
|
101
|
103
|
DBSession.add(user)
|
102
|
|
- import transaction
|
103
|
|
- transaction.commit()
|
|
104
|
+ else:
|
|
105
|
+ user = self._user_api.get_one_by_email(email)
|
|
106
|
+
|
|
107
|
+ # Retrieve ldap user attributes
|
|
108
|
+ self._auth.ldap_user_provider.add_metadata_for_auth(environ, identity)
|
104
|
109
|
|
105
|
|
- # TODO - B.S. - 20160208: Voir avec Damien, si je ne fait pas de transaction.commit()
|
106
|
|
- # manuellement la donnée n'est pas en base.
|
107
|
|
- # self._user_api.create_user(email=email, save_now=True)
|
|
110
|
+ # Update user with ldap attributes
|
|
111
|
+ user_ldap_values = identity.get('user').copy()
|
|
112
|
+ for field_name in user_ldap_values:
|
|
113
|
+ setattr(user, field_name, user_ldap_values[field_name])
|
|
114
|
+
|
|
115
|
+ DBSession.flush()
|
|
116
|
+ transaction.commit()
|
108
|
117
|
|
109
|
118
|
|
110
|
119
|
class LDAPApplicationAuthMetadata(TGAuthMetadata):
|
|
@@ -156,9 +165,11 @@ class LDAPAttributesPlugin(BaseLDAPAttributesPlugin):
|
156
|
165
|
self._user_api = UserApi(None)
|
157
|
166
|
|
158
|
167
|
def add_metadata(self, environ, identity):
|
|
168
|
+ # We disable metadata recuperation, we do it at connection in LDAPSearchAuthenticatorPlugin._sync_ldap_user
|
|
169
|
+ return
|
|
170
|
+
|
|
171
|
+ def add_metadata_for_auth(self, environ, identity):
|
159
|
172
|
super().add_metadata(environ, identity)
|
160
|
|
- # TODO - B.S. - 20160212: identity contains now som information from LDAP what we can save in local database
|
161
|
|
- identity[self.name] = self._user_api.get_one_by_email(identity.get('repoze.who.userid'))
|
162
|
173
|
|
163
|
174
|
@property
|
164
|
175
|
def local_fields(self):
|