| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright 2006 Waldemar Kornewald, wkornewald@haiku-os.org |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://trac.edgewall.org/wiki/TracLicense. |
|---|
| 9 | # |
|---|
| 10 | # This software consists of voluntary contributions made by many |
|---|
| 11 | # individuals. For the exact contribution history, see the revision |
|---|
| 12 | # history and logs, available at http://trac.edgewall.org/log/. |
|---|
| 13 | # |
|---|
| 14 | # Author: Waldemar Kornewald, wkornewald@haiku-os.org |
|---|
| 15 | |
|---|
| 16 | from trac.core import * |
|---|
| 17 | from trac.config import * |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class IUserStore(Interface): |
|---|
| 21 | """ |
|---|
| 22 | Extension point interface for backends that store known users. |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | def supports_user_operation(self, operation): |
|---|
| 26 | """ |
|---|
| 27 | Returns whether the operation (a method name) is supported. |
|---|
| 28 | |
|---|
| 29 | @return supported |
|---|
| 30 | """ |
|---|
| 31 | return hasattr(self, operation) |
|---|
| 32 | |
|---|
| 33 | def create_user(self, username, password): |
|---|
| 34 | """ |
|---|
| 35 | Creates a new user with the given username and password. |
|---|
| 36 | |
|---|
| 37 | @return success |
|---|
| 38 | """ |
|---|
| 39 | |
|---|
| 40 | def get_users(self): |
|---|
| 41 | """ |
|---|
| 42 | Generator that yields an ordered list of known users. |
|---|
| 43 | |
|---|
| 44 | @return username |
|---|
| 45 | """ |
|---|
| 46 | |
|---|
| 47 | def has_user(self, username): |
|---|
| 48 | """ |
|---|
| 49 | Returns whether the user exists. |
|---|
| 50 | """ |
|---|
| 51 | return username in self.get_users() |
|---|
| 52 | |
|---|
| 53 | def check_password(self, username, password): |
|---|
| 54 | """ |
|---|
| 55 | Checks if the password is correct for the given user. |
|---|
| 56 | """ |
|---|
| 57 | |
|---|
| 58 | def change_password(self, username, password): |
|---|
| 59 | """ |
|---|
| 60 | Changes a user's password. |
|---|
| 61 | |
|---|
| 62 | @return success |
|---|
| 63 | """ |
|---|
| 64 | |
|---|
| 65 | def delete_user(self, username): |
|---|
| 66 | """ |
|---|
| 67 | Deletes a user. |
|---|
| 68 | |
|---|
| 69 | @return success |
|---|
| 70 | Returns False if the user didn't exist. |
|---|
| 71 | """ |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class IUserAttributeProvider(Interface): |
|---|
| 75 | """ |
|---|
| 76 | Extension point interface for backends that store user attributes. |
|---|
| 77 | """ |
|---|
| 78 | |
|---|
| 79 | def supports_attribute_operation(self, operation): |
|---|
| 80 | """ |
|---|
| 81 | Returns whether the operation (a method name) is supported. |
|---|
| 82 | |
|---|
| 83 | @return supported |
|---|
| 84 | """ |
|---|
| 85 | return hasattr(self, operation) |
|---|
| 86 | |
|---|
| 87 | def get_user_attribute(self, username, attribute): |
|---|
| 88 | """ |
|---|
| 89 | Returns a user attribute. |
|---|
| 90 | |
|---|
| 91 | If the attribute is not set it returs an empty string. |
|---|
| 92 | If the attribute is not supported None is returned. |
|---|
| 93 | """ |
|---|
| 94 | |
|---|
| 95 | def set_user_attribute(self, username, attribute, value): |
|---|
| 96 | """ |
|---|
| 97 | Sets a user attribute. If value is None the attribute gets deleted. |
|---|
| 98 | |
|---|
| 99 | @return success |
|---|
| 100 | Returns False if setting the attribute is not supported. |
|---|
| 101 | """ |
|---|
| 102 | |
|---|
| 103 | def delete_all_user_attributes(self, username): |
|---|
| 104 | """ |
|---|
| 105 | Deletes all of the given user's attributes. |
|---|
| 106 | |
|---|
| 107 | @return success |
|---|
| 108 | """ |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | class UserManager(Component): |
|---|
| 112 | """ |
|---|
| 113 | Component responsible for managing users and user attributes. |
|---|
| 114 | """ |
|---|
| 115 | |
|---|
| 116 | store = ExtensionOption('users', |
|---|
| 117 | 'store', IUserStore, 'SessionUserStore', |
|---|
| 118 | doc="""The user store that should be used for authentication |
|---|
| 119 | (''since 0.11'').""") |
|---|
| 120 | attribute_providers = OrderedExtensionsOption('users', |
|---|
| 121 | 'attribute_providers', IUserAttributeProvider, |
|---|
| 122 | doc="""Ordered list of user attribute providers (''since 0.11'').""") |
|---|
| 123 | |
|---|
| 124 | # general methods |
|---|
| 125 | |
|---|
| 126 | def supports_operation(self, operation): |
|---|
| 127 | if store.supports_user_operation(operation): |
|---|
| 128 | return True |
|---|
| 129 | for provider in providers: |
|---|
| 130 | if provider.supports_attribute_operation(operation): |
|---|
| 131 | return True |
|---|
| 132 | return False |
|---|
| 133 | |
|---|
| 134 | # IUserStore methods |
|---|
| 135 | |
|---|
| 136 | def create_user(self, username, password): |
|---|
| 137 | if not store.supports_user_operation('create_user'): |
|---|
| 138 | return False |
|---|
| 139 | return self.store.create_user(username, password) |
|---|
| 140 | |
|---|
| 141 | def get_users(self): |
|---|
| 142 | if not store.supports_user_operation('get_users'): |
|---|
| 143 | return [] |
|---|
| 144 | return self.store.get_users() |
|---|
| 145 | |
|---|
| 146 | def has_user(self, username): |
|---|
| 147 | if not store.supports_user_operation('has_user'): |
|---|
| 148 | return False |
|---|
| 149 | return self.store.has_user(username) |
|---|
| 150 | |
|---|
| 151 | def check_password(self, username, password): |
|---|
| 152 | if not store.supports_user_operation('check_password'): |
|---|
| 153 | return False |
|---|
| 154 | return self.store.check_password(username, password) |
|---|
| 155 | |
|---|
| 156 | def change_password(self, username, password): |
|---|
| 157 | if not store.supports_user_operation('change_password'): |
|---|
| 158 | return False |
|---|
| 159 | return self.store.change_password(username, password) |
|---|
| 160 | |
|---|
| 161 | def delete_user(self, username): |
|---|
| 162 | if not store.supports_user_operation('delete_user'): |
|---|
| 163 | return False |
|---|
| 164 | return self.store.delete_user(username) |
|---|
| 165 | |
|---|
| 166 | # IUserAttributeProvider methods |
|---|
| 167 | |
|---|
| 168 | def get_user_attribute(self, username, attribute): |
|---|
| 169 | for provider in self.attribute_providers: |
|---|
| 170 | if not provider.supports_attribute_operation('get_user_attribute'): |
|---|
| 171 | continue |
|---|
| 172 | value = provider.get_user_attribute(username, attribute) |
|---|
| 173 | if value is not None: |
|---|
| 174 | return value |
|---|
| 175 | return None |
|---|
| 176 | |
|---|
| 177 | def set_user_attribute(self, username, attribute, value): |
|---|
| 178 | for provider in self.attribute_providers: |
|---|
| 179 | if not provider.supports_attribute_operation('set_user_attribute'): |
|---|
| 180 | continue |
|---|
| 181 | result = provider.set_user_attribute(username, attribute, value) |
|---|
| 182 | if result: |
|---|
| 183 | return True |
|---|
| 184 | return False |
|---|
| 185 | |
|---|
| 186 | def delete_all_user_attributes(self, username): |
|---|
| 187 | for provider in self.attribute_providers: |
|---|
| 188 | if not provider.supports_attribute_operation('delete_all_user_attributes'): |
|---|
| 189 | continue |
|---|
| 190 | result = provider.delete_all_user_attributes(username) |
|---|
| 191 | if result: |
|---|
| 192 | return True |
|---|
| 193 | return False |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | class SessionUserStore(Component): |
|---|
| 197 | """ |
|---|
| 198 | Component for managing authenticated users stored in sessions. |
|---|
| 199 | """ |
|---|
| 200 | |
|---|
| 201 | implements(IUserStore) |
|---|
| 202 | |
|---|
| 203 | def get_users(self): |
|---|
| 204 | db = self.env.get_db_cnx() |
|---|
| 205 | cursor = db.cursor() |
|---|
| 206 | cursor.execute("SELECT sid FROM session " |
|---|
| 207 | "WHERE authenticated=1 " |
|---|
| 208 | "ORDER BY sid") |
|---|
| 209 | for row in cursor: |
|---|
| 210 | yield row[0] |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | class SessionUserAttributeProvider(Component): |
|---|
| 214 | """ |
|---|
| 215 | Component for providing user attributes via Trac sessions. |
|---|
| 216 | """ |
|---|
| 217 | |
|---|
| 218 | implements(IUserAttributeProvider) |
|---|