Ticket #2456: userdir-for-r3271.diff
| File userdir-for-r3271.diff, 10.0 KB (added by brad <brad@…>, 3 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 from trac.versioncontrol import RepositoryManager 25 26 26 27 __all__ = ['Environment', 'IEnvironmentSetupParticipant', 'open_environment'] … … 61 62 * wiki and ticket attachments. 62 63 """ 63 64 setup_participants = ExtensionPoint(IEnvironmentSetupParticipant) 65 userdirs = ExtensionPoint(IUserDirectory) 64 66 65 67 base_url = Option('trac', 'base_url', '', 66 68 """Base URL of the Trac deployment.""") … … 259 261 logfile = os.path.join(self.get_log_dir(), logfile) 260 262 self.log = logger_factory(logtype, logfile, self.log_level, self.path) 261 263 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 266 271 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 269 279 270 @param cnx: the database connection; if ommitted, a new connection is271 retrieved272 """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, email285 286 280 def backup(self, dest=None): 287 281 """Simple SQLite-specific backup of the database. 288 282 -
trac/ticket/api.py
100 100 if self.restrict_owner: 101 101 field['type'] = 'select' 102 102 users = [] 103 for username , name, email in self.env.get_known_users(db):103 for username in self.env.get_known_users(): 104 104 users.append(username) 105 105 field['options'] = users 106 106 field['optional'] = True -
trac/versioncontrol/web_ui/log.py
163 163 if format == 'rss': 164 164 # Get the email addresses of all known users 165 165 email_map = {} 166 for username,name,email in self.env.get_known_user s():166 for username,name,email in self.env.get_known_user_info(): 167 167 if email: 168 168 email_map[username] = email 169 169 for cs in changes.values(): -
trac/Timeline.py
154 154 155 155 # Get the email addresses of all known users 156 156 email_map = {} 157 for username, name, email in self.env.get_known_user s():157 for username, name, email in self.env.get_known_user_info(): 158 158 if email: 159 159 email_map[username] = email 160 160 -
trac/tests/env.py
23 23 """Testing env.get_version""" 24 24 assert self.env.get_version() == db_default.db_version 25 25 26 def test_get_known_user s(self):27 """Testing env.get_known_user s"""26 def test_get_known_user_info(self): 27 """Testing env.get_known_user_info""" 28 28 cursor = self.db.cursor() 29 29 cursor.executemany("INSERT INTO session VALUES (%s,%s,0)", 30 30 [('123', 0),('tom', 1), ('joe', 1), ('jane', 1)]) … … 35 35 ('joe', 1, 'email', 'joe@example.com'), 36 36 ('jane', 1, 'name', 'Jane')]) 37 37 users = {} 38 for username,name,email in self.env.get_known_user s(self.db):38 for username,name,email in self.env.get_known_user_info(): 39 39 users[username] = (name, email) 40 40 41 41 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 17 from trac.core import Component, implements, Interface, ExtensionPoint, TracError 18 19 class 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 67 class 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
