Ticket #2456: userdir-for-r2637.diff
| File userdir-for-r2637.diff, 9.4 KB (added by brad <brad@…>, 6 years ago) |
|---|
-
trac/env.py
21 21 from trac.core import Component, ComponentManager, implements, Interface, \ 22 22 ExtensionPoint, TracError 23 23 from trac.db import DatabaseManager 24 from trac.userdir import * 24 25 25 26 __all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment'] 26 27 … … 60 61 * wiki and ticket attachments. 61 62 """ 62 63 setup_participants = ExtensionPoint(IEnvironmentSetupParticipant) 64 userdirs = ExtensionPoint(IUserDirectory) 63 65 64 66 def __init__(self, path, create=False, options=[]): 65 67 """Initialize the Trac environment. … … 231 233 logid = self.path # Env-path provides process-unique ID 232 234 self.log = logger_factory(logtype, logfile, loglevel, logid) 233 235 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 245 238 if not cnx: 246 239 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 258 249 def backup(self, dest=None): 259 250 """Simple SQLite-specific backup of the database. 260 251 -
trac/ticket/api.py
56 56 if self.config.getbool('ticket', 'restrict_owner'): 57 57 field['type'] = 'select' 58 58 users = [] 59 for user name, name, email in self.env.get_known_users(db):60 users.append(user name)59 for user in self.env.get_known_users(cnx=db): 60 users.append(user['username']) 61 61 field['options'] = users 62 62 field['optional'] = True 63 63 else: -
trac/versioncontrol/web_ui/log.py
153 153 if format == 'rss': 154 154 # Get the email addresses of all known users 155 155 email_map = {} 156 for user name,name,email in self.env.get_known_users():157 if email:158 email_map[user name] = email156 for user in self.env.get_known_users(attrs=('email')): 157 if user['email']: 158 email_map[user['username']] = user['email'] 159 159 for cs in changes.values(): 160 160 cs['message'] = util.escape(cs['message']) 161 161 cs['shortlog'] = util.escape(cs['shortlog'].replace('\n', ' ')) -
trac/Timeline.py
143 143 144 144 # Get the email addresses of all known users 145 145 email_map = {} 146 for user name, name, email in self.env.get_known_users():147 if email:148 email_map[user name] = email146 for user in self.env.get_known_users(attrs=('email')): 147 if user['email']: 148 email_map[user['username']] = user['email'] 149 149 150 150 idx = 0 151 151 for kind, href, title, date, author, message in events: -
trac/tests/env.py
34 34 ('joe', 'email', 'joe@example.com'), 35 35 ('jane', 'name', 'Jane')]) 36 36 users = {} 37 for user name,name,email in self.env.get_known_users(self.db):38 users[user name] = (name, email)37 for user in self.env.get_known_users(cnx=self.db): 38 users[user['username']] = (user['name'], user['email']) 39 39 40 40 assert not users.has_key('anonymous') 41 41 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öm <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 18 from trac.core import Component, implements, Interface, ExtensionPoint, TracError 19 20 class 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 62 class 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
