Edgewall Software

Ticket #2456: userdir-for-r3271.diff

File userdir-for-r3271.diff, 10.0 KB (added by brad <brad@…>, 3 years ago)

patch 3 for UserDirectory? plugin

  • 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.""") 
     
    259261            logfile = os.path.join(self.get_log_dir(), logfile) 
    260262        self.log = logger_factory(logtype, logfile, self.log_level, self.path) 
    261263 
    262     def get_known_users(self, cnx=None): 
    263         """Generator that yields information about all known users, i.e. users 
    264         that have logged in to this Trac environment and possibly set their name 
    265         and email. 
     264    def get_known_user_info(self, limit=None): 
     265        cnx = self.get_db_cnx() 
     266         
     267        # should accumulate user_info from all IUserDirectory providers - desirable? 
     268        for userdir in self.userdirs: 
     269            for username, name, email in userdir.get_known_user_info(limit): 
     270                yield username, name, email 
    266271 
    267         This function generates one tuple for every user, of the form 
    268         (username, name, email) ordered alpha-numerically by username. 
     272    def get_known_users(self, limit=None): 
     273        cnx = self.get_db_cnx() 
     274         
     275        # should accumulate users from all IUserDirectory providers - desirable? 
     276        for userdir in self.userdirs: 
     277            for username in userdir.get_known_users(limit): 
     278                yield username 
    269279 
    270         @param cnx: the database connection; if ommitted, a new connection is 
    271                     retrieved 
    272         """ 
    273         if not cnx: 
    274             cnx = self.get_db_cnx() 
    275         cursor = cnx.cursor() 
    276         cursor.execute("SELECT DISTINCT s.sid, n.value, e.value " 
    277                        "FROM session AS s " 
    278                        " LEFT JOIN session_attribute AS n ON (n.sid=s.sid " 
    279                        "  and n.authenticated=1 AND n.name = 'name') " 
    280                        " LEFT JOIN session_attribute AS e ON (e.sid=s.sid " 
    281                        "  AND e.authenticated=1 AND e.name = 'email') " 
    282                        "WHERE s.authenticated=1 ORDER BY s.sid") 
    283         for username,name,email in cursor: 
    284             yield username, name, email 
    285  
    286280    def backup(self, dest=None): 
    287281        """Simple SQLite-specific backup of the database. 
    288282 
  • trac/ticket/api.py

     
    100100        if self.restrict_owner: 
    101101            field['type'] = 'select' 
    102102            users = [] 
    103             for username, name, email in self.env.get_known_users(db): 
     103            for username in self.env.get_known_users(): 
    104104                users.append(username) 
    105105            field['options'] = users 
    106106            field['optional'] = True 
  • trac/versioncontrol/web_ui/log.py

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

     
    154154 
    155155        # Get the email addresses of all known users 
    156156        email_map = {} 
    157         for username, name, email in self.env.get_known_users(): 
     157        for username, name, email in self.env.get_known_user_info(): 
    158158            if email: 
    159159                email_map[username] = email 
    160160 
  • 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/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