| | 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, cnx=None, 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, cnx=None, 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, cnx=None, 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 | |
| | 90 | if not cnx: |
| | 91 | pass # FIXME - need to implement other plugin here to get cnx? For now, the |
| | 92 | # DefaultUserDirectory is fine, b/c the method in env gets a cnx. |
| | 93 | # But what about other plugins? |
| | 94 | |
| | 95 | cursor = cnx.cursor() |
| | 96 | cursor.execute("SELECT DISTINCT s.sid, n.var_value, e.var_value " |
| | 97 | "FROM session AS s " |
| | 98 | " LEFT JOIN session AS n ON (n.sid=s.sid " |
| | 99 | " AND n.authenticated=1 AND n.var_name = 'name') " |
| | 100 | " LEFT JOIN session AS e ON (e.sid=s.sid " |
| | 101 | " AND e.authenticated=1 AND e.var_name = 'email') " |
| | 102 | "WHERE s.authenticated=1 ORDER BY s.sid") |
| | 103 | iter = 0 |
| | 104 | for username,name,email in cursor: |
| | 105 | if limit: |
| | 106 | iter += 1 |
| | 107 | if iter > limit: |
| | 108 | break |
| | 109 | yield username,name,email |
| | 110 | |
| | 111 | def get_known_users(self, cnx=None, limit=None): |
| | 112 | cursor = cnx.cursor() |
| | 113 | cursor.execute("SELECT DISTINCT sid " |
| | 114 | "FROM session " |
| | 115 | "WHERE authenticated=1 " |
| | 116 | "ORDER BY sid") |
| | 117 | iter = 0 |
| | 118 | for username in cursor: |
| | 119 | if limit: |
| | 120 | iter += 1 |
| | 121 | if iter > limit: |
| | 122 | break |
| | 123 | yield username[0] |
| | 124 | |
| | 125 | def get_user_attribute(self, user, attr): |
| | 126 | pass |
| | 127 | |
| | 128 | def get_supported_attributes(self): |
| | 129 | pass |