summaryrefslogtreecommitdiffstats
path: root/LDAPRoleExtender.py
blob: d3da78d3669b8746c38a0cfcc078a8725bb0c131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#####################################################################
#
# LDAPRoleExtender	Fake user folder to change roles
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
#####################################################################
__version__='$Revision$'[11:-2]

import os, time, copy, ldap, urllib
from types import StringType
from Globals import package_home, InitializeClass, MessageDialog, DTMLFile
from Acquisition import aq_base
from AccessControl import ClassSecurityInfo
from AccessControl.ZopeSecurityPolicy import _noroles
from AccessControl.Permissions import view_management_screens, manage_users
from AccessControl.SpecialUsers import emergency_user
from OFS.SimpleItem import SimpleItem
from Products.LDAPUserFolder.LDAPUser import LDAPUser
from Products.LDAPUserFolder.utils import GROUP_MEMBER_MAP


_marker = []
_dtmldir = os.path.join( package_home( globals() ), 'dtml' )
addLDAPRoleExtenderForm = DTMLFile( 'addLDAPRoleExtender', _dtmldir )
CHANGE_LUF_PERMISSION = 'Change user folder'


class LDAPRoleExtender( SimpleItem ):
    """ 
        LDAPRoleExtender

        The LDAPRoleExtender is a fake user database.  It retrieves 
        user objects through a real LDAPUserFolder and changes the
        roles on the user object according to a group base DN 
        specified by the administrator.
    """
    security = ClassSecurityInfo()

    meta_type = 'LDAPRoleExtender'
    id = 'acl_users'
    isAUserFolder = 1


    #################################################################
    #
    # ZMI management screens
    #
    #################################################################

    manage_options=(
        (
        {'label' : 'Configure',	'action' : 'manage_main', 
         'help'  : ('LDAPRoleExtender','Configure.stx')},
        {'label' : 'Caches', 'action' : 'manage_cache',
         'help'  : ('LDAPRoleExtender', 'Caches.stx')},
        {'label' : 'Log', 'action' : 'manage_log',
         'help'  : ('LDAPRoleExtender', 'Log.stx')},
        )
        + SimpleItem.manage_options
        ) 

    security.declareProtected( view_management_screens, 'manage' )
    security.declareProtected( view_management_screens, 'manage_main' )
    manage = manage_main = DTMLFile( 'dtml/properties', globals() )
    manage_main._setName( 'manage_main' )
    
    security.declareProtected( view_management_screens, 'manage_log' )
    manage_log = DTMLFile( 'dtml/log', globals() )

    security.declareProtected( view_management_screens, 'manage_cache' )
    manage_cache = DTMLFile( 'dtml/cache', globals() )


    #################################################################
    #
    # Initialization code
    #
    #################################################################


    def __setstate__( self, v ):
        """
            __setstate__ is called whenever the instance is loaded
            from the ZODB, like when Zope is restarted.
        """
        LDAPRoleExtender.inheritedAttribute( '__setstate__' )( self, v )

        self._clearCaches()
        if not hasattr( self, 'verbose' ):
            self.verbose = 2

        if self.verbose > 2:
            self._log( 3,'Re-initialized through __setstate__' )


    def __init__( self, luf, groups_base, groups_scope, title='' ):
        """ Create a new LDAPRoleExtender instance """
        self.title = title
        self.groups_base = groups_base
        self.groups_scope = groups_scope
        self.verbose = 2    # _log needs it
        self._v_users = {}
        self._v_cache = {}
        self._v_log = []

        if not luf.startswith( '/' ):
            luf = '/%s' % luf

        self._luf = luf

       
    security.declarePrivate( '_clearCaches' )
    def _clearCaches( self ):
        """ Clear all logs and caches for user-related information """
        self._v_cache = {} # Secondary cache for non-logged in users
        self._v_users = {} # this is the cache of logged-in users
        self._v_log = []


    #################################################################
    #
    # LDAPRoleExtender administration
    #
    #################################################################


    security.declarePrivate( '_log' )
    def _log( self, level, logline ):
        """ Logs a single line and keeps log length at max. 500 lines.

        Level: 1 - Catastrophes   
               2 - Major Events   
               3 - Minor events
               4 - Login failures 
               5 - Login Successes
               7 - Login success from cache
               9 - Debugging
        """
        time_str = time.strftime( '%b %d  %H:%M:%S'
                                , time.localtime( time.time() )
                                )
        logged = '(%d)  %s:  %s' % ( level, time_str, logline )

        self._v_log.append( logged )

        if len( self._v_log ) > 500:
             self._v_log.pop( 0 )



    security.declareProtected( view_management_screens, 'getLog' )
    def getLog( self ):
        """ return the log contents """
        return self._v_log
        

    security.declareProtected( manage_users, 'manage_reinit' )
    def manage_reinit( self, REQUEST=None ):
        """ re-initialize and clear out users and log """
        self._clearCaches()
        self.verbose > 1 and self._log( 2, 'Reinitialized' )

        if REQUEST:
            msg = 'User caches cleared'
            return self.manage_cache( self, REQUEST, manage_tabs_message=msg )


    security.declareProtected( CHANGE_LUF_PERMISSION, 'manage_edit' )
    def manage_edit( self
                   , luf
                   , groups_base
                   , groups_scope
                   , verbose=2
                   , title=''
                   , REQUEST=None 
                   ):
        """ Edit the LDAPRoleExtender Object """
        self.title = title
        self.verbose = verbose
        self.groups_base = groups_base
        self.groups_scope = groups_scope

        if not luf.startswith( '/' ):
            luf = '/%s' % luf

        self._luf = luf

        if REQUEST:
            msg = 'Properties changed'
            return self.manage_main( self, REQUEST, manage_tabs_message=msg )


    security.declarePrivate( '_cacheUser' )
    def _cacheUser( self, name, extended_user, authenticated=0 ):
        """ Stick something into my internal cache """
        name = name.lower()

        if ( extended_user is None or 
             name == str( emergency_user.getUserName() ).lower() ):
            return

        if authenticated:
            self._v_users[name] = extended_user
        else:
            self._v_cache[name] = extended_user


    security.declarePrivate( 'twiddleUser' )
    def _extendRoles( self, raw_user ):
        """ extend the user roles """
        if raw_user is None:
            return raw_user

        raw_roles = list(raw_user.getRoles())
        user_dn = raw_user.getUserDN()
        luf = self.getLUF()
        group_filter = '(|(uniquemember=%s)(member=%s))' % ( user_dn, user_dn )

        add_roles = luf._searchResults( self.groups_base
                                      , self.groups_scope
                                      , group_filter
                                      , attrs = ['dn', 'cn']
                                      )

        if isinstance(add_roles, StringType) or len(add_roles) == 0:
            return raw_user

        add_role_list = []

        for i in range(len(add_roles)):
            dn = add_roles[i][0]
            try:
                cn = add_roles[i][1]['cn'][0]
            except KeyError:    # NDS oddity
                cn = ldap.explode_dn(dn, 1)[0]

            add_role_list.append(cn)


        final_roles = {}
        raw_roles.extend(add_role_list)

        for role in raw_roles:
            final_roles[role] = 1

        extended_user = LDAPUser( raw_user.getUserName()
                                , raw_user._getPassword()
                                , final_roles.keys()
                                , raw_user.getDomains()
                                , raw_user.getUserDN()
                                , {}
                                , []
                                )
        extended_user._properties = raw_user._properties

        return extended_user


    security.declareProtected( manage_users, 'getLUF' )
    def getLUF( self ):
        """ Return my LDAP User Folder """
        return self.unrestrictedTraverse(self._luf)


    #################################################################
    #
    # User Folder interface stuff
    #
    #################################################################

    security.declareProtected( manage_users, 'getUsers' )
    def getUsers( self ):
        """ Return a list of *cached* user objects """
        names = self._v_users.keys()
        names.sort()
        users = []

        for n in names:
            user = self._v_users.get( n )
            if user.notexpired():   # Show only the unexpired
                users.append( user )

        return users


    security.declareProtected( manage_users, 'getUserNames' )
    def getUserNames( self ):
        """ Return a list of usernames """
        luf = self.getLUF()

        return luf.getUserNames()

    user_names = getUserNames


    security.declareProtected( manage_users, 'getUser' )
    def getUser( self, name, pwd=None ):
        """Return the named user object or None"""
        name = name.lower()

        if ( pwd is not None and
             self._v_users.has_key( name ) and 
             self._v_users[name].notexpired() ):
            self.verbose > 8 and self._log( 9
                                          , '"%s" found in auth-cache' % name
                                          )
                                           
            return self._v_users.get( name )

        if ( pwd is None and
             self._v_cache.has_key( name ) and
             self._v_cache[name].notexpired() ):
            self.verbose > 8 and self._log( 9
                                          , '"%s" found in anon-cache' % name
                                          )

            return self._v_cache.get( name )

        luf = self.getLUF()

        raw_user = luf.getUser( name, pwd )
        extended_user = self._extendRoles( raw_user )
        self._cacheUser( name, extended_user, pwd )

        return extended_user


    security.declareProtected( manage_users, 'getUserById' )
    def getUserById( self, id, default=_marker ):
        """ Return a user object by ID (in this case by username) """
        try:
            return self.getUser( id )

        except:
            if default is _marker: 
                raise

            return default


    security.declareProtected( manage_users, 'getUserByDN' )
    def getUserByDN( self, user_dn ):
        """ Make a user object from a DN """
        luf = self.getLUF()
        raw_user = luf.getUserByDN( user_dn )
        extended_user = self._extendRoles( raw_user )
        self._cacheUser( extended_user.getUserName(), extended_user )
        
        return extended_user


    # This must stay accessible to everyone
    def validate( self, request, auth='', roles=_noroles ):
        """ The main engine """
        luf = self.getLUF()
        raw_user = luf.validate( request, auth, roles )

        if raw_user is not None:
            extended_user = self.getUser( raw_user.getUserName() 
                                        , raw_user._getPassword()
                                        )
            extended_user = extended_user.__of__( luf )
        else:
            extended_user = raw_user

        return extended_user


    def manage_beforeDelete( self, item, container ):
        """ Clean up after myself """
        if ( hasattr( container, '__allow_groups__' ) and
             container.__allow_groups__ == item ):
            del container.__allow_groups__

    security.declareProtected( manage_users, 'getGroups' )
    def getGroups(self, dn='*', attr=None):
        """ return group records i know about """
        group_list = []
        no_show = ( 'Anonymous', 'Authenticated', 'Shared' )
        group_filter = '(|(uniquemember=%s)(member=%s))' % ( dn, dn )
        luf = self.getLUF()

        res = luf._searchResults( self.groups_base
                                , self.groups_scope
                                , group_filter
                                , attrs=['dn', 'cn']
                                )

        if isinstance(res, StringType):
            return group_list

        if len(res) > 0:
            for i in range( len( res ) ):
                dn = res[i][0]
                try:
                    cn = res[i][1]['cn'][0]
                except KeyError:    # NDS oddity
                    cn = ldap.explode_dn( dn, 1 )[0]

                if attr is None:
                    group_list.append( ( cn, dn ) )
                elif attr == 'cn':
                    group_list.append( cn )
                elif attr == 'dn':
                    group_list.append( dn )

        return group_list

    security.declareProtected( manage_users, 'getGroupDetails')
    def getGroupDetails( self, encoded_cn ):
        """ Return all group details """
        result = []
        cn = urllib.unquote( encoded_cn )
        luf = self.getLUF()

        res = luf._searchResults( self.groups_base
                                , self.groups_scope
                                , 'cn=%s' % cn
                                , ['uniqueMember', 'member']
                                )

        if isinstance(res, StringType):
            result = ( ( 'Exception', res ), )
        elif len( res ) > 0:
            result = res[0][1].items()
            result.sort()

        return tuple(result)


    security.declareProtected( manage_users, 'getGroupedUsers')
    def getGroupedUsers(self, groups=None):
        """ Retrieve all users that in the groups i know about """
        all_dns = {}
        users = []

        if groups is None:
            groups = self.getGroups()

        for group_id, group_dn in groups:
            group_details = self.getGroupDetails( group_id )

            for attribute_name, dn_list in group_details:
                for dn in dn_list:
                    all_dns[dn] = 1

        for dn in all_dns.keys():
            user = self.getUserByDN( dn )

            if user is not None:
                users.append( user.__of__( self ) )

        return tuple( users )


    security.declareProtected( manage_users, 'manage_editUserRoles' )
    def manage_editUserRoles( self, user_dn, role_dns=[], REQUEST=None ):
        """ Edit the roles (groups) of a user """
        all_groups = self.getGroups( attr='dn' )
        cur_groups = self.getGroups( dn=user_dn, attr='dn' )
        operations = []
        luf = self.getLUF()

        for group in all_groups:
            if group in cur_groups and group not in role_dns:
                operations.append({ 'op'     : ldap.MOD_DELETE
                                  , 'target' : group
                                  , 'type'   : luf.getGroupType( group )
                                  } )
            elif group in role_dns and group not in cur_groups:
                operations.append({ 'op'     : ldap.MOD_ADD
                                  , 'target' : group
                                  , 'type'   : luf.getGroupType( group )
                                  } )

        if operations:
            connection = luf._connect()

            for to_do in operations:
                mod_list = ( ( to_do['op']
                             , GROUP_MEMBER_MAP.get( to_do['type'] )
                             , user_dn
                             ), )
                try:
                    connection.modify_s( to_do['target'], mod_list )
                except Exception, e:
                    msg = luf._formatException( self, e )

            msg = 'Roles changed for %s' % ( user_dn )
        else:
            msg = 'No roles changed for %s' % ( user_dn )

        user_obj = self.getUserByDN( user_dn )
        if user_obj is not None:
            self._expireUser( user_obj )

        if REQUEST:
            return self.manage_userrecords( self
                                          , REQUEST
                                          , manage_tabs_message=msg
                                          , user_dn=user_dn
                                          )


    security.declareProtected( manage_users, '_expireUser' )
    def _expireUser( self, user_obj ):
        """ Purge user object from caches """
        user_name = user_obj.getUserName().lower()
        for cache_dict in ( self._v_users, self._v_cache ):
            if cache_dict.has_key( user_name ):
                del cache_dict[user_name]


def manage_addLDAPRoleExtender( self
                              , luf
                              , groups_base
                              , groups_scope
                              , title=''
                              , REQUEST=None 
                              ):
    """ Called by Zope to create and install an LDAPRoleExtender """

    if hasattr( aq_base( self ), 'acl_users' ) and REQUEST is not None:
        msg = 'This object already contains a User Folder'
        
        return MessageDialog(
               title  = 'Item Exists',
               message = msg,
               action = '%s/manage_main' % REQUEST['URL1'])

    n = LDAPRoleExtender( luf, groups_base, groups_scope, title )

    self = self.this()
    self._setObject( 'acl_users', n )
    self.__allow_groups__ = self.acl_users
 
    # return to the parent object's manage_main
    if REQUEST:
        url = REQUEST['URL1']
        REQUEST.RESPONSE.redirect( '%s/manage_main' % url )


InitializeClass( LDAPRoleExtender )