1
|
from quixote import get_publisher, get_response, get_request, redirect, get_session
|
2
|
from qommon import get_cfg, get_logger
|
3
|
from qommon.storage import StorableObject
|
4
|
|
5
|
from wcs.users import User
|
6
|
|
7
|
class DrupalUser(StorableObject):
|
8
|
# Simple mapping between a drupal user and a wcs user
|
9
|
_names = 'drupalusers'
|
10
|
wcs_user_id = None
|
11
|
|
12
|
def get_wcs_user(self):
|
13
|
if not self.wcs_user_id:
|
14
|
raise KeyError()
|
15
|
return User.get(self.wcs_user_id)
|
16
|
|
17
|
def create_wcs_user(self):
|
18
|
# XXX: look for an existing user with such an email?
|
19
|
user = User()
|
20
|
self.update_attributes(user)
|
21
|
self.wcs_user_id = user.id
|
22
|
self.store()
|
23
|
return user
|
24
|
|
25
|
def update_attributes(self, user):
|
26
|
users_cfg = get_cfg('users', {})
|
27
|
drupal_user_name = get_request().get_header('X-WCS-Drupal-User-Name')
|
28
|
drupal_user_mail = get_request().get_header('X-WCS-Drupal-User-Mail')
|
29
|
|
30
|
formdata = {}
|
31
|
field_email = users_cfg.get('field_email')
|
32
|
if field_email:
|
33
|
formdata[field_email] = drupal_user_mail
|
34
|
else:
|
35
|
user.email = drupal_user_mail
|
36
|
|
37
|
field_name_values = users_cfg.get('field_name')
|
38
|
if type(field_name_values) is str: # it was a string in previous versions
|
39
|
field_name_values = [field_name_values]
|
40
|
if field_name_values:
|
41
|
formdata[field_name_values[0]] = drupal_user_name
|
42
|
else:
|
43
|
user.name = drupal_user_name
|
44
|
|
45
|
if formdata:
|
46
|
user.form_data = formdata
|
47
|
user.set_attributes_from_formdata(formdata)
|
48
|
|
49
|
user.store()
|
50
|
|
51
|
|
52
|
def try_auth():
|
53
|
misc_cfg = get_cfg('misc', {})
|
54
|
drupal_ip = misc_cfg.get('aq-drupal-ip')
|
55
|
if not (drupal_ip and get_request().environ.get('REMOTE_ADDR') == drupal_ip):
|
56
|
return
|
57
|
|
58
|
drupal_user_id = get_request().get_header('X-WCS-Drupal-User-Uid')
|
59
|
if not drupal_user_id or str(drupal_user_id) == '0':
|
60
|
return
|
61
|
|
62
|
try:
|
63
|
drupal_user = DrupalUser.get(drupal_user_id)
|
64
|
except KeyError:
|
65
|
drupal_user = DrupalUser(id=drupal_user_id)
|
66
|
drupal_user.store()
|
67
|
|
68
|
try:
|
69
|
wcs_user = drupal_user.get_wcs_user()
|
70
|
except KeyError:
|
71
|
wcs_user = drupal_user.create_wcs_user()
|
72
|
else:
|
73
|
pass # TODO: update wcs user with drupal headers
|
74
|
|
75
|
session = get_session()
|
76
|
session.set_user(wcs_user.id)
|
77
|
|