Project

General

Profile

Download (7.04 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / ezldap.py @ 2750dbae

1
'''
2
Module to provide the kind of interaction needed for a eZ publish website
3
acting as reverse proxy and passing in an HTTP header a pointer to an LDAP
4
entry.
5
'''
6

    
7
import ldap
8
import ldap.modlist
9
LDAP_EMAIL = 'courriel'
10

    
11
import time
12
import random
13

    
14
from quixote import get_publisher, get_request, get_session
15
from qommon import get_cfg, get_logger
16
from qommon.form import *
17
from qommon.admin.texts import TextsDirectory
18
from qommon import misc
19

    
20
from wcs.users import User
21

    
22

    
23
def get_ldap_conn():
24
    if hasattr(get_request(), 'ldap_conn'):
25
        return get_request().ldap_conn
26

    
27
    misc_cfg = get_cfg('misc', {})
28
    ldap_url = misc_cfg.get('aq-ezldap-url')
29
    if not ldap_url:
30
        return
31
    ldap_binddn = misc_cfg.get('aq-ezldap-binddn')
32
    ldap_bindpassword = misc_cfg.get('aq-ezldap-bindpassword')
33

    
34
    ldap_conn = ldap.initialize(ldap_url)
35
    ldap_conn.protocol_version = ldap.VERSION3
36
    ldap_conn.simple_bind(ldap_binddn, ldap_bindpassword)
37
    get_request().ldap_conn = ldap_conn
38
    return ldap_conn
39

    
40

    
41
class EzEmailWidget(EmailWidget):
42
    extra_css_class = 'EmailWidget'
43
    def _parse(self, request):
44
        EmailWidget._parse(self, request)
45
        if self.value and not self.error:
46
            # check if an account already exists
47
            misc_cfg = get_cfg('misc', {})
48
            ldap_basedn = misc_cfg.get('aq-ezldap-basedn')
49
            ldap_conn = get_ldap_conn()
50
            ldap_result_id = ldap_conn.search(ldap_basedn, ldap.SCOPE_SUBTREE,
51
                    filterstr='%s=%s' % (LDAP_EMAIL, self.value))
52
            result_type, result_data = ldap_conn.result(ldap_result_id, all=0)
53
            if result_type == ldap.RES_SEARCH_ENTRY:
54
                self.error = _('This email already has an account.')
55

    
56

    
57
class EzLdapUser(object):
58
    anonymous = False
59
    is_admin = False
60
    roles = []
61

    
62
    def __init__(self, dn):
63
        self.id = dn
64
        self.dn = dn
65

    
66
    _ldap_dict = None
67
    def get_ldap_dict(self):
68
        if self._ldap_dict:
69
            return self._ldap_dict
70
        else:
71
            t = get_ldap_conn().search_s(self.dn, ldap.SCOPE_BASE)[0][1]
72
            self._ldap_dict = {}
73
            for k, v in t.items():
74
                if v:
75
                    if k.startswith('date'):
76
                        date = datetime.datetime.strptime(v[0][:14], '%Y%m%d%H%M%S')
77
                        v[0] = date.strftime(misc.date_format())
78
                    self._ldap_dict[k] = v[0]
79
            return self._ldap_dict
80

    
81
    _form_data = None
82
    def get_form_data(self):
83
        if self._form_data:
84
            return self._form_data
85
        else:
86
            self._form_data = {}
87
            ldap_dict = self.get_ldap_dict()
88
            for k, v in ldap_dict.items():
89
                # get the corresponding id from wcs user
90
                for admin_field in User.get_formdef().fields:
91
                    if admin_field.varname == k:
92
                        self._form_data[admin_field.id] = v
93
                        break
94
            return self._form_data
95

    
96
    def set_form_data(self, data):
97
        pass
98
    form_data = property(get_form_data, set_form_data)
99

    
100
    _name = None
101
    def get_display_name(self):
102
        if self._name is None:
103
            parts = []
104
            user = self.get_ldap_dict()
105
            users_cfg = get_cfg('users', {})
106
            # get the corresponding ldap attribute
107
            parts = [user.get(admin_field.varname, '')
108
                        for admin_field in User.get_formdef().fields
109
                            if admin_field.id in users_cfg.get('field_name')]
110
            self._name = ' '.join(parts)
111
        return self._name
112
    display_name = property(get_display_name)
113
    name = property(get_display_name)
114

    
115
    def get_email(self):
116
        return self.get_ldap_dict().get(LDAP_EMAIL)
117
    email = property(get_email)
118

    
119
    def can_go_in_admin(self):
120
        return False
121

    
122
    def can_go_in_backoffice(self):
123
        return False
124

    
125
    def store(self):
126
        pass
127

    
128
    def get_formdef(cls):
129
        formdef = User.get_formdef()
130
        # use only fields with a varname (= ldap attribute)
131
        formdef.fields = [f for f in User.get_formdef().fields if f.varname ]
132
        return formdef
133
    get_formdef = classmethod(get_formdef)
134

    
135
    def get_substitution_variables(self):
136
        d = {
137
            'user_display_name': self.display_name,
138
            'user_email': self.email,
139
        }
140
        formdef = self.get_formdef()
141
        if formdef:
142
            from formdata import get_dict_with_varnames
143
            data = get_dict_with_varnames(formdef.fields, self.form_data)
144
            for k, v in data.items():
145
                d['user_'+k] = v
146
        return d
147

    
148
    def get_substitution_variables_list(self):
149
        return User.get_substitution_variables_list()
150

    
151
    def set_attributes_from_formdata(self, formdata):
152
        new_entry = self.get_ldap_dict().copy()
153
        for k, v in formdata.items():
154
            attr = k
155
            # if k is an wcs user ID, the ldap attribute is the varname
156
            for admin_field in User.get_formdef().fields:
157
                if k == admin_field.id:
158
                    attr = admin_field.varname
159
                    break
160
            if v:
161
                if isinstance(v, time.struct_time):
162
                    v = time.strftime('%Y%m%d%H%M%SZ',v)
163
                new_entry[attr] = v
164
            else:
165
                new_entry[attr] = ''
166

    
167
        mod_list = ldap.modlist.modifyModlist(self.get_ldap_dict(), new_entry)
168
        if mod_list:
169
            get_ldap_conn().modify_s(get_session().user, mod_list)
170

    
171

    
172
class EzMagicUser(User):
173
    def get(cls, id, ignore_errors=False, ignore_migration=False):
174
        if '=' in str(id):
175
            try:
176
                # FIXME : do a ldap search on each get is not very efficient,
177
                # perhaps add a little cache ? (ttl 5 min ?)
178
                get_ldap_conn().search_s(str(id), ldap.SCOPE_BASE)
179
            except ldap.NO_SUCH_OBJECT:
180
                raise KeyError
181
            return EzLdapUser(id)
182
        else:
183
            return User.get(id, ignore_errors, ignore_migration)
184
    get = classmethod(get)
185

    
186
    def search(cls, q):
187
        '''search in names and email (from wcs user configuration)'''
188
        result = []
189

    
190
        # build the ldap filter string : (|(attr1=*q*)(attr2=*q*)...)
191
        users_cfg = get_cfg('users', {})
192
        search_fields = [ LDAP_EMAIL ] + users_cfg.get('field_name')
193
        attrs = [f.varname for f in User.get_formdef().fields if f.id in search_fields]
194
        filterstr = '(|' + ''.join(['(%%s=*%s*)' % q % a for a in attrs]) + ')'
195

    
196
        ldap_basedn = get_cfg('misc', {}).get('aq-ezldap-basedn')
197
        ldap_conn = get_ldap_conn()
198
        try:
199
            ldap_result_id = ldap_conn.search(ldap_basedn, ldap.SCOPE_SUBTREE,
200
                    filterstr=filterstr)
201
        except ldap.FILTER_ERROR:
202
            return []
203
        while True:
204
            result_type, result_data = ldap_conn.result(ldap_result_id, all=0)
205
            if not result_data:
206
                break
207
            if result_type == ldap.RES_SEARCH_ENTRY:
208
                result.append(result_data[0][0])
209
        return [EzLdapUser(dn) for dn in result]
210
    search = classmethod(search)
211

    
(12-12/26)