Project

General

Profile

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

root / extra / modules / ezldap.py @ 3f079b20

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': self,
138
            'user_display_name': self.display_name,
139
            'user_email': self.email,
140
        }
141
        formdef = self.get_formdef()
142
        if formdef:
143
            from formdata import get_dict_with_varnames
144
            data = get_dict_with_varnames(formdef.fields, self.form_data)
145
            for k, v in data.items():
146
                d['user_'+k] = v
147
        return d
148

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

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

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

    
172

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

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

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

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

    
(12-12/26)