Edgewall Software

Ticket #2456: userdir-for-r2637.diff

File userdir-for-r2637.diff, 9.4 kB (added by brad <brad@…>, 3 years ago)

patch 1 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 * 
    2425 
    2526__all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment'] 
    2627 
     
    6061     * wiki and ticket attachments. 
    6162    """    
    6263    setup_participants = ExtensionPoint(IEnvironmentSetupParticipant) 
     64    userdirs = ExtensionPoint(IUserDirectory) 
    6365 
    6466    def __init__(self, path, create=False, options=[]): 
    6567        """Initialize the Trac environment. 
     
    231233        logid = self.path # Env-path provides process-unique ID 
    232234        self.log = logger_factory(logtype, logfile, loglevel, logid) 
    233235 
    234     def get_known_users(self, cnx=None): 
    235         """Generator that yields information about all known users, i.e. users 
    236         that have logged in to this Trac environment and possibly set their name 
    237         and email. 
    238  
    239         This function generates one tuple for every user, of the form 
    240         (username, name, email) ordered alpha-numerically by username. 
    241  
    242         @param cnx: the database connection; if ommitted, a new connection is 
    243                     retrieved 
    244         """ 
     236    def get_known_users(self, cnx=None, attrs=None, limit=None): 
     237                 
    245238        if not cnx: 
    246239            cnx = self.get_db_cnx() 
    247         cursor = cnx.cursor() 
    248         cursor.execute("SELECT DISTINCT s.sid, n.var_value, e.var_value " 
    249                        "FROM session AS s " 
    250                        " LEFT JOIN session AS n ON (n.sid=s.sid " 
    251                        "  AND n.authenticated=1 AND n.var_name = 'name') " 
    252                        " LEFT JOIN session AS e ON (e.sid=s.sid " 
    253                        "  AND e.authenticated=1 AND e.var_name = 'email') " 
    254                        "WHERE s.authenticated=1 ORDER BY s.sid") 
    255         for username,name,email in cursor: 
    256             yield username, name, email 
    257  
     240         
     241        # should accumulate users from all IUserDirectory providers - desirable? 
     242        for userdir in self.userdirs: 
     243            for user in userdir.get_known_users(cnx, attrs, limit): 
     244                yield user 
     245         
     246        # yield username, name, email 
     247         
     248     
    258249    def backup(self, dest=None): 
    259250        """Simple SQLite-specific backup of the database. 
    260251 
  • trac/ticket/api.py

     
    5656        if self.config.getbool('ticket', 'restrict_owner'): 
    5757            field['type'] = 'select' 
    5858            users = [] 
    59             for username, name, email in self.env.get_known_users(db): 
    60                 users.append(username) 
     59            for user in self.env.get_known_users(cnx=db): 
     60                users.append(user['username']) 
    6161            field['options'] = users 
    6262            field['optional'] = True 
    6363        else: 
  • trac/versioncontrol/web_ui/log.py

     
    153153        if format == 'rss': 
    154154            # Get the email addresses of all known users 
    155155            email_map = {} 
    156             for username,name,email in self.env.get_known_users(): 
    157                 if email: 
    158                     email_map[username] = email 
     156            for user in self.env.get_known_users(attrs=('email')): 
     157                if user['email']: 
     158                    email_map[user['username']] = user['email'] 
    159159            for cs in changes.values(): 
    160160                cs['message'] = util.escape(cs['message']) 
    161161                cs['shortlog'] = util.escape(cs['shortlog'].replace('\n', ' ')) 
  • trac/Timeline.py

     
    143143 
    144144        # Get the email addresses of all known users 
    145145        email_map = {} 
    146         for username, name, email in self.env.get_known_users(): 
    147             if email: 
    148                 email_map[username] = email 
     146        for user in self.env.get_known_users(attrs=('email')): 
     147            if user['email']: 
     148                email_map[user['username']] = user['email'] 
    149149 
    150150        idx = 0 
    151151        for kind, href, title, date, author, message in events: 
  • trac/tests/env.py

     
    3434                            ('joe', 'email', 'joe@example.com'), 
    3535                            ('jane', 'name', 'Jane')]) 
    3636        users = {} 
    37         for username,name,email in self.env.get_known_users(self.db): 
    38             users[username] = (name, email) 
     37        for user in self.env.get_known_users(cnx=self.db): 
     38            users[user['username']] = (user['name'], user['email']) 
    3939 
    4040        assert not users.has_key('anonymous') 
    4141        self.assertEqual(('Tom', 'tom@example.com'), users['tom']) 
  • trac/userdir.py

     
     1# -*- coding: iso8859-1 -*- 
     2# 
     3# Copyright (C) 2003-2005 Edgewall Software 
     4# Copyright (C) 2003-2005 Jonas Borgstr�jonas@edgewall.com> 
     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: Bruce Christensen <me@brucec.net> 
     16# Author: Brad Anderson <brad@dsource.org> 
     17 
     18from trac.core import Component, implements, Interface, ExtensionPoint, TracError 
     19 
     20class IUserDirectory(Interface): 
     21    def get_known_users(cnx=None, attrs=('name', 'email'), limit=None): 
     22        """Generator that yields information about known users. 
     23         
     24        Generates a dictionary for each known user, of the form 
     25        {   'username': 'john', 
     26            'attr1': attr1 value, 
     27            'attr2': attr2 value, 
     28            ... 
     29            } 
     30         
     31        Raises KeyError if any of attrs isn't supported. 
     32         
     33        @param cnx:   db connection object 
     34        @param attrs: tuple of attributes to be included in the generated dict 
     35                      (in addition to 'username', which is always included) 
     36        @param limit: maximum number of results to generate. None means no limit. 
     37        """ 
     38        pass 
     39 
     40    def get_user_attribute(user, attr): 
     41        """Provides values of one or more attributes of a user. 
     42 
     43        Raises UnknownUserError if the requested user doesn't exist. 
     44 
     45        Raises KeyError if the specified attribute isn't supported. 
     46 
     47        @param user: a username 
     48        @param attr: the name of the attribute to return. Can also be a tuple of 
     49                    attribute names. 
     50        @return:     the value of a user attribute, or, if attr is a tuple, a 
     51                    dictionary of attributes like get_known_users generates 
     52        """ 
     53        pass 
     54 
     55    def get_supported_attributes(): 
     56        """ 
     57        @return: tuple of supported attribute names. Always includes 'username'. 
     58        """ 
     59        pass 
     60 
     61 
     62class SessionUserDirectory(Component): 
     63    implements (IUserDirectory) 
     64     
     65    # This plugin basically duplicates the existing functionality in Trac, providing a 
     66    # list of users based on the 'session' table.  It makes no attempt to use the 'limit' 
     67    # parameter of the function right now. 
     68     
     69    # IUserDirectory methods 
     70    def get_known_users(self, cnx=None, attrs=None, limit=None): 
     71        """Generator that yields information about all known users, i.e. users 
     72        that have logged in to this Trac environment and possibly set their name 
     73        and email. 
     74 
     75        This function generates one dictionary for every user, of the form 
     76        {'username':username,'attr1':attr1,...} 
     77 
     78        @param cnx: the database connection 
     79        @param attrs: attributes of the user to return 
     80        @param limit: limit the number of users returned 
     81 
     82        """ 
     83 
     84        cursor = cnx.cursor() 
     85        cursor.execute("SELECT DISTINCT s.sid, n.var_value, e.var_value " 
     86                       "FROM session AS s " 
     87                       " LEFT JOIN session AS n ON (n.sid=s.sid " 
     88                       "  AND n.authenticated=1 AND n.var_name = 'name') " 
     89                       " LEFT JOIN session AS e ON (e.sid=s.sid " 
     90                       "  AND e.authenticated=1 AND e.var_name = 'email') " 
     91                       "WHERE s.authenticated=1 ORDER BY s.sid") 
     92        for username,name,email in cursor: 
     93            user = {} 
     94            user['username'] = username  # required key/value 
     95            if attrs and 'name' in attrs: 
     96                user['name'] = name 
     97            if attrs and 'email' in attrs: 
     98                user['email'] = email 
     99            yield user 
     100     
     101    def get_user_attribute(self, user, attr): 
     102        pass 
     103     
     104    def get_supported_attributes(self): 
     105        pass