Edgewall Software

Ticket #2456: userdir-for-r4673-0.10-stable2.diff

File userdir-for-r4673-0.10-stable2.diff, 10.6 kB (added by Peter Dimov <peter.dimov@…>, 18 months ago)

The previous patch does not create trac/userdir.py and does not patch trac/notification.py this one does...

  • trac/env.py

     
    2121from trac.core import Component, ComponentManager, implements, Interface, \ 
    2222                      ExtensionPoint, TracError 
    2323from trac.db import DatabaseManager 
     24from trac.userdir import * 
    2425from trac.versioncontrol import RepositoryManager 
    2526 
    2627__all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment'] 
     
    6162     * wiki and ticket attachments. 
    6263    """    
    6364    setup_participants = ExtensionPoint(IEnvironmentSetupParticipant) 
     65    userdirs = ExtensionPoint(IUserDirectory) 
    6466 
    6567    base_url = Option('trac', 'base_url', '', 
    6668        """Base URL of the Trac deployment. 
     
    296298        self.log = logger_factory(logtype, logfile, self.log_level, self.path, 
    297299                                  format=format) 
    298300 
    299     def get_known_users(self, cnx=None): 
    300         """Generator that yields information about all known users, i.e. users 
    301         that have logged in to this Trac environment and possibly set their name 
    302         and email. 
     301    def get_known_user_info(self, limit=None): 
     302        cnx = self.get_db_cnx() 
     303         
     304        # should accumulate user_info from all IUserDirectory providers - desirable? 
     305        for userdir in self.userdirs: 
     306            for username, name, email in userdir.get_known_user_info(limit): 
     307                yield username, name, email 
    303308 
    304         This function generates one tuple for every user, of the form 
    305         (username, name, email) ordered alpha-numerically by username. 
     309    def get_known_users(self, limit=None): 
     310        cnx = self.get_db_cnx() 
     311         
     312        # should accumulate users from all IUserDirectory providers - desirable? 
     313        for userdir in self.userdirs: 
     314            for username in userdir.get_known_users(limit): 
     315                yield username 
    306316 
    307         @param cnx: the database connection; if ommitted, a new connection is 
    308                     retrieved 
    309         """ 
    310         if not cnx: 
    311             cnx = self.get_db_cnx() 
    312         cursor = cnx.cursor() 
    313         cursor.execute("SELECT DISTINCT s.sid, n.value, e.value " 
    314                        "FROM session AS s " 
    315                        " LEFT JOIN session_attribute AS n ON (n.sid=s.sid " 
    316                        "  and n.authenticated=1 AND n.name = 'name') " 
    317                        " LEFT JOIN session_attribute AS e ON (e.sid=s.sid " 
    318                        "  AND e.authenticated=1 AND e.name = 'email') " 
    319                        "WHERE s.authenticated=1 ORDER BY s.sid") 
    320         for username,name,email in cursor: 
    321             yield username, name, email 
    322  
    323317    def backup(self, dest=None): 
    324318        """Simple SQLite-specific backup of the database. 
    325319 
  • trac/ticket/api.py

     
    100100        if self.restrict_owner: 
    101101            field['type'] = 'select' 
    102102            users = [] 
     103 
    103104            perm = PermissionSystem(self.env) 
    104             for username, name, email in self.env.get_known_users(db): 
     105            for username in self.env.get_known_users(): 
    105106                if perm.get_user_permissions(username).get('TICKET_MODIFY'): 
    106107                    users.append(username) 
    107108            field['options'] = users 
  • trac/versioncontrol/web_ui/log.py

     
    164164        if format == 'rss': 
    165165            # Get the email addresses of all known users 
    166166            email_map = {} 
    167             for username,name,email in self.env.get_known_users(): 
     167            for username,name,email in self.env.get_known_user_info(): 
    168168                if email: 
    169169                    email_map[username] = email 
    170170            for cs in changes.values(): 
  • trac/Timeline.py

     
    155155 
    156156        # Get the email addresses of all known users 
    157157        email_map = {} 
    158         for username, name, email in self.env.get_known_users(): 
     158        for username, name, email in self.env.get_known_user_info(): 
    159159            if email: 
    160160                email_map[username] = email 
    161161 
  • trac/tests/env.py

     
    2323        """Testing env.get_version""" 
    2424        assert self.env.get_version() == db_default.db_version 
    2525 
    26     def test_get_known_users(self): 
    27         """Testing env.get_known_users""" 
     26    def test_get_known_user_info(self): 
     27        """Testing env.get_known_user_info""" 
    2828        cursor = self.db.cursor() 
    2929        cursor.executemany("INSERT INTO session VALUES (%s,%s,0)", 
    3030                           [('123', 0),('tom', 1), ('joe', 1), ('jane', 1)]) 
     
    3535                            ('joe', 1, 'email', 'joe@example.com'), 
    3636                            ('jane', 1, 'name', 'Jane')]) 
    3737        users = {} 
    38         for username,name,email in self.env.get_known_users(self.db): 
     38        for username,name,email in self.env.get_known_user_info(): 
    3939            users[username] = (name, email) 
    4040 
    4141        assert not users.has_key('anonymous') 
  • trac/notification.py

     
    163163        self._init_pref_encoding() 
    164164        # Get the email addresses of all known users 
    165165        self.email_map = {} 
    166         for username, name, email in self.env.get_known_users(self.db): 
     166        for username, name, email in self.env.get_known_user_info(): 
    167167            if email: 
    168168                self.email_map[username] = email 
    169169 
  • trac/userdir.py

     
     1# -*- coding: iso8859-1 -*- 
     2# 
     3# Copyright (C) 2003-2005 Edgewall Software 
     4# Copyright (C) 2005 Brad Anderson <brad@dsource.org> 
     5# All rights reserved. 
     6# 
     7# This software is licensed as described in the file COPYING, which 
     8# you should have received as part of this distribution. The terms 
     9# are also available at http://trac.edgewall.com/license.html. 
     10# 
     11# This software consists of voluntary contributions made by many 
     12# individuals. For the exact contribution history, see the revision 
     13# history and logs, available at http://projects.edgewall.com/trac/. 
     14# 
     15# Author: Brad Anderson <brad@dsource.org> 
     16 
     17from trac.core import Component, implements, Interface, ExtensionPoint, TracError 
     18 
     19class IUserDirectory(Interface): 
     20    def get_known_user_info(self, limit=None): 
     21        """Generator that yields information about known users. 
     22         
     23        This function generates one tuple for every user, of the form 
     24        (username, name, email) ordered alpha-numerically by username. 
     25         
     26        @param cnx:   db connection object 
     27        @param limit: maximum number of results to generate. None means no limit. 
     28         
     29        @return (username, name, email) 
     30            if plugin does not support name or email, return None in their place 
     31        """ 
     32        pass 
     33 
     34    def get_known_users(self, limit=None): 
     35        """ 
     36        Generator that yields a list of known users. 
     37 
     38        @param cnx:   db connection object 
     39        @param limit: maximum number of results to generate. None means no limit. 
     40         
     41        @return username 
     42        """ 
     43        pass 
     44     
     45    def get_user_attribute(self, user, attr): 
     46        """Provides values of one or more attributes of a user. 
     47 
     48        Raises UnknownUserError if the requested user doesn't exist. 
     49 
     50        Raises KeyError if the specified attribute isn't supported. 
     51 
     52        @param user: a username 
     53        @param attr: the name of the attribute to return. Can also be a tuple of 
     54                    attribute names. 
     55        @return:     the value of a user attribute, or, if attr is a tuple, a 
     56                    dictionary of attributes like get_known_users generates 
     57        """ 
     58        pass 
     59 
     60    def get_supported_attributes(self): 
     61        """ 
     62        @return: tuple of supported attribute names. Always includes 'username'. 
     63        """ 
     64        pass 
     65 
     66 
     67class DefaultUserDirectory(Component): 
     68    implements (IUserDirectory) 
     69     
     70    # This plugin basically duplicates the existing functionality in Trac, providing a 
     71    # list of users based on the 'session' table.   
     72     
     73    # IUserDirectory methods 
     74     
     75    def get_known_user_info(self, limit=None): 
     76        """Generator that yields information about all known users, i.e. users 
     77        that have logged in to this Trac environment and possibly set their name 
     78        and email. 
     79 
     80        This function generates one tuple for every user, of the form 
     81        (username, name, email) ordered alpha-numerically by username. 
     82         
     83        @param cnx: the database connection 
     84        @param attrs: attributes of the user to return 
     85        @param limit: limit the number of users returned 
     86 
     87        @return (username, name, email) 
     88        """ 
     89        cnx = self.env.get_db_cnx() 
     90        cursor = cnx.cursor() 
     91        cursor.execute("SELECT DISTINCT s.sid, n.value, e.value " 
     92                       "FROM session AS s " 
     93                       " LEFT JOIN session_attribute AS n ON (n.sid=s.sid " 
     94                       "  and n.authenticated=1 AND n.name = 'name') " 
     95                       " LEFT JOIN session_attribute AS e ON (e.sid=s.sid " 
     96                       "  AND e.authenticated=1 AND e.name = 'email') " 
     97                       "WHERE s.authenticated=1 ORDER BY s.sid") 
     98        iter = 0 
     99        for username,name,email in cursor: 
     100            if limit: 
     101                iter += 1 
     102                if iter > limit: 
     103                    break 
     104            yield username,name,email 
     105     
     106    def get_known_users(self, limit=None): 
     107        cnx = self.env.get_db_cnx() 
     108        cursor = cnx.cursor() 
     109        cursor.execute("SELECT DISTINCT sid " 
     110                       "FROM session " 
     111                       "WHERE authenticated=1 " 
     112                       "ORDER BY sid") 
     113        iter = 0 
     114        for username in cursor: 
     115            if limit: 
     116                iter += 1 
     117                if iter > limit: 
     118                    break 
     119            yield username[0] 
     120             
     121    def get_user_attribute(self, user, attr): 
     122        pass 
     123     
     124    def get_supported_attributes(self): 
     125        pass