Changeset 1102 for trunk/rp/trac
- Timestamp:
- 11/21/07 09:40:55 (14 months ago)
- Location:
- trunk/rp/trac/infocard_acct/0.11
- Files:
-
- 1 added
- 5 modified
-
infocard_acct/console.py (modified) (4 diffs)
-
infocard_acct/db.py (modified) (2 diffs)
-
infocard_acct/db_default.py (added)
-
infocard_acct/ldapstore.py (modified) (5 diffs)
-
infocard_acct/templates/authenticate.html (modified) (2 diffs)
-
setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/rp/trac/infocard_acct/0.11/infocard_acct/console.py
r1061 r1102 99 99 from acct_mgr.api import AccountManager 100 100 101 import db_default 102 from db import InfoCardAccountEnv 103 101 104 INFOCARD_ACCT_VERSION = pkg_resources.get_distribution('InfoCardAccountManager').version 102 105 TRAC_VERSION = pkg_resources.get_distribution('Trac').version … … 199 202 if params: 200 203 cursor.execute(sql, params) 204 print 'finished executing %s :: %s' % (sql, params) 201 205 else: 202 206 cursor.execute(sql) 207 print 'finished executing %s' % (sql) 203 208 if cnx: 204 209 cnx.commit() 210 211 def db_attempt(self, sql, cursor=None, params=None): 212 try: 213 if not cursor: 214 cnx = self.db_open() 215 cursor = cnx.cursor() 216 else: 217 cnx = None 218 if params: 219 cursor.execute(sql, params) 220 print 'finished executing %s :: %s' % (sql, params) 221 else: 222 cursor.execute(sql) 223 print 'finished executing %s' % (sql) 224 if cnx: 225 cnx.commit() 226 except Exception, e: 227 print 'Ignoring error executing: %s' % (sql) 205 228 206 229 ## … … 285 308 env = self.env_open() 286 309 ctx = self.db_open() 287 cursor = ctx.cursor()288 310 289 311 tables = None 290 user = Table('user', key=('username'))[291 Column('username'),292 Column('passwordhash'),293 Index(['username'])294 ]295 cards = Table('cardkey', key=('cardkeyhash'))[296 Column('username'),297 Column('cardkeyhash'),298 Index(['username']),299 Index(['cardkeyhash'])300 ]301 usertables = [ user ]302 cardtables = [ cards ]303 alltables = [user, cards]304 305 306 312 if arg[0] == 'user': 307 tables = usertables313 tables = db_default.usertables 308 314 elif arg[0] == 'card': 309 tables = cardtables310 else: 311 tables = alltables312 315 tables = db_default.cardtables 316 else: 317 tables = db_default.alltables 318 313 319 try: 314 315 db_connector, _ = DatabaseManager(env)._get_connector() 316 for table in tables: 317 for stmt in db_connector.to_sql(table): 318 cursor.execute(stmt) 320 ienv = InfoCardAccountEnv(env) 321 ienv.upgrade_environment(ctx, tables) 319 322 except TracError, e: 320 323 msg = unicode(e) … … 337 340 338 341 if arg[0] == 'user': 339 cursor.execute("DROP TABLE user")342 self.db_attempt("DROP TABLE user", cursor) 340 343 elif arg[0] == 'card': 341 cursor.execute("DROP TABLE cardkey") 342 else: 343 cursor.execute("DROP TABLE user") 344 cursor.execute("DROP TABLE cardkey") 344 self.db_attempt("DROP TABLE cardkey", cursor) 345 else: 346 self.db_attempt("DROP TABLE user", cursor) 347 self.db_attempt("DROP TABLE cardkey", cursor) 348 self.db_update("DELETE from system WHERE name=%s", cursor, (db_default.db_name,)) 345 349 ctx.commit() 346 350 print 'cleanenv done.' -
trunk/rp/trac/infocard_acct/0.11/infocard_acct/db.py
r1061 r1102 36 36 from trac.config import ExtensionOption 37 37 from trac.db import Table, Column, Index, DatabaseManager 38 from trac.env import IEnvironmentSetupParticipant 39 40 import db_default 38 41 39 42 from acct_mgr.api import IPasswordStore … … 161 164 # is there another way to get count instead of using has_user? 162 165 return True 163 166 167 168 class InfoCardAccountEnv(Component): 169 170 implements(IEnvironmentSetupParticipant) 171 # IEnvironmentSetupParticipant methods 172 """Extension point interface for components that need to participate in the 173 creation and upgrading of Trac environments, used to setup our database 174 tables, we store our component version in the .db for later use.""" 175 176 def environment_created(self): 177 """Called when a new Trac environment is created.""" 178 self.found_db_version = 0 179 self.upgrade_environment(self.env.get_db_cnx()) 180 181 def environment_needs_upgrade(self, db): 182 """Called when Trac checks whether the environment needs to be upgraded. 183 Should return `True` if this participant needs an upgrade to be 184 performed, `False` otherwise. If the version in the db is newer 185 should we downgrade or abort? 186 """ 187 188 self.found_db_version = 0 189 cursor = db.cursor() 190 cursor.execute("SELECT value FROM system WHERE name=%s", (db_default.db_name,)) 191 value = cursor.fetchone() 192 if value: 193 self.found_db_version = int(value[0]) 194 195 self.log.debug('InfoCardAccountEnv: Current db version %s, required is %s' \ 196 % (self.found_db_version, db_default.db_version)) 197 198 if self.found_db_version == db_default.db_version: 199 return False 200 201 return True 202 203 def upgrade_environment(self, db, tables=db_default.alltables): 204 """Actually perform an environment upgrade, the magic all happens here, 205 Note that the commit is called by the caller if we don't raise an error 206 """ 207 208 if self.environment_needs_upgrade(db): 209 db_connector, _ = DatabaseManager(self.env)._get_connector() 210 211 # Insert the default table 212 old_data = {} # {table_name: (col_names, [row, ...]), ...} 213 cursor = db.cursor() 214 if not self.found_db_version: 215 cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)",(db_default.db_name, db_default.db_version)) 216 else: 217 cursor.execute("UPDATE system SET value=%s WHERE name=%s",(db_default.db_version, db_default.db_name)) 218 for tbl in tables: 219 try: 220 cursor.execute('SELECT * FROM %s'%tbl.name) 221 old_data[tbl.name] = ([d[0] for d in cursor.description], cursor.fetchall()) 222 cursor.execute('DROP TABLE %s'%tbl.name) 223 except Exception, e: 224 if 'OperationalError' not in e.__class__.__name__: 225 raise e # If it is an OperationalError, just move on to the next table 226 227 for tbl in tables: 228 for sql in db_connector.to_sql(tbl): 229 cursor.execute(sql) 230 231 # Try to reinsert any old data 232 if tbl.name in old_data: 233 data = old_data[tbl.name] 234 sql = 'INSERT INTO %s (%s) VALUES (%s)' % \ 235 (tbl.name, ','.join(data[0]), ','.join(['%s'] * len(data[0]))) 236 for row in data[1]: 237 try: 238 cursor.execute(sql, row) 239 except Exception, e: 240 if 'OperationalError' not in e.__class__.__name__: 241 raise e 242 243 -
trunk/rp/trac/infocard_acct/0.11/infocard_acct/ldapstore.py
r1098 r1102 79 79 return None; 80 80 81 def _get_attr (self, url):81 def _get_attrs(self, url): 82 82 if len(url.attrs) > 0: 83 return url.attrs[0].encode('utf-8') 83 attrList = [] 84 for attr in url.attrs: 85 attrList.append(attr.encode('utf-8')) 86 return attrList 84 87 else: 85 return 'uid'88 return ('uid',) 86 89 87 90 def _user_is_invalid(self, user): … … 93 96 return None 94 97 98 def _get_userfilterstr(self, search_url, user): 99 attrs = self._get_attrs(search_url) 100 if len(attrs) == 1: 101 return '(& ' + search_url.filterstr + '('+ self._get_attrs(search_url)+'='+ user +'))' 102 else: 103 return '(& ' + search_url.filterstr + '(|' + ''.join("(%s=%s)" \ 104 % (attr, user) for attr in attrs)+'))' 105 95 106 def get_users(self): 96 """Return s an iterable of theknown usernames107 """Return known usernames 97 108 """ 98 109 search_url = self._setup_search_url() 99 users =[]100 110 if search_url: 111 self.log.debug('LDAPUserStore:get_users: binding as \"%s\" ' 112 + 'using \"%s\"', search_url.who, search_url.cred) 101 113 l = ldap.open(search_url.hostport) 102 114 l.simple_bind_s(search_url.who, search_url.cred) 103 115 104 116 #get first attr, place in list 105 attrs = (self._get_attr(search_url)) 117 attrs = self._get_attrs(search_url) 118 self.log.debug('LDAPUserStore:get_users: searching \"%s\" ' 119 + 'for \"%s\"', search_url.dn, search_url.filterstr) 106 120 results = l.search_s(search_url.dn, search_url.scope, 107 121 search_url.filterstr, attrs) 108 122 109 #works because there is only one attr by convention 123 #works because there is only one attr per entry based on the number 124 # of attributes in the attrs variable above 110 125 for entry in results: 111 for attr in entry[1]: 112 users.append(entry[1][attr]) 113 return users 114 126 # if entry[1].items() == 1: 127 for attr in entry[1].values(): 128 self.log.debug('LDAPUserStore:get_users returning: %s' % (attr[0], )) 129 yield attr[0] 130 # else: 131 # self.log.debug('LDAPUserStore:get_users found incorrect number of items') 132 115 133 def has_user(self, user): 116 134 # if self._user_is_invalid(user): 117 135 # return False 118 search_url = self._setup_search_url() 119 if search_url: 120 l = ldap.open(search_url.hostport) 121 l.simple_bind_s(search_url.who, search_url.cred) 122 #todo fix so it looks for the user. 123 results = l.search_s(search_url.dn, search_url.scope, 124 search_url.filterstr) 125 if len(results) == 1: 126 return True 136 try: 137 search_url = self._setup_search_url() 138 if search_url: 139 l = ldap.open(search_url.hostport) 140 l.simple_bind_s(search_url.who, search_url.cred) 141 #todo fix so it looks for the user. 142 results = l.search_s(search_url.dn, search_url.scope, 143 self._userfilterstr(search_url, user)) 144 if len(results) == 1: 145 return True 146 except: 147 pass 127 148 return False 128 149 … … 135 156 l = ldap.open(search_url.hostport) 136 157 l.simple_bind_s(search_url.who, search_url.cred) 137 searchfilter = '(& ' + search_url.filterstr + '('+ self._get_attr(search_url)+'='+ user +'))'138 #self.log.debug('LDAPUserStore:check_password: searching \"%s\" '139 #+ 'for \"%s\"', search_url.dn, searchfilter)158 searchfilter = self._get_userfilterstr(search_url, user) 159 self.log.debug('LDAPUserStore:check_password: searching \"%s\" ' 160 + 'for \"%s\"', search_url.dn, searchfilter) 140 161 results = l.search_s(search_url.dn, search_url.scope, searchfilter) 141 162 if len(results) == 1: … … 148 169 # self.log.debug('LDAPUserStore:check_password succeeded') 149 170 return True 150 #elif len(results) == 0:151 #self.log.debug('LDAPUserStore:check_password search failed')171 elif len(results) == 0: 172 self.log.debug('LDAPUserStore:check_password search failed') 152 173 # else: 153 174 # for entry in results: … … 156 177 except: 157 178 #on error just return the password failed 158 self.log.debug('LDAPUserStore:check_password f ailed')179 self.log.debug('LDAPUserStore:check_password for \"%s\" failed' % (user)) 159 180 return False 160 181 161 def delete_user(self, user): 162 """Deletes the user account. 163 Returns True if the account existed and was deleted, False otherwise. 164 """ 165 if not self.has_user(user): 166 return False 167 db = self.env.get_db_cnx() 168 cursor = db.cursor() 169 cursor.execute("DELETE FROM user " 170 "WHERE username=%s", (user,)) 171 # TODO cursor.rowcount doesn't seem to get # deleted 172 # is there another way to get count instead of using has_user? 173 return True 182 # def delete_user(self, user): 183 # """Deletes the user account. 184 # Returns True if the account existed and was deleted, False otherwise. 185 # """ 186 # return False 174 187 -
trunk/rp/trac/infocard_acct/0.11/infocard_acct/templates/authenticate.html
r1061 r1102 12 12 $('#user')[0].focus(); 13 13 }); 14 15 submited = false; 16 function image_disable(ref) 17 { 18 if (!submited) 19 { 20 ref.submit(); 21 submited = true; 22 } 23 }; 14 24 </script> 15 25 </head> … … 59 69 src="${href.chrome('/infocard_acct/images/infocard_92x64.png')}" 60 70 alt="InfoCard Login" 61 onClick='i nfocardLogin.submit()'/>71 onClick='image_disable(infocardLogin)'/> 62 72 <input type="hidden" name="referer" value="${referer}" /> 63 73 <input py:if="blockID" type="hidden" name="blockID" value="${blockID}" /> -
trunk/rp/trac/infocard_acct/0.11/setup.py
r1098 r1102 130 130 131 131 install_requires = [ 132 # 'Trac>=0.11', 133 # 'M2Crypto>=0.18' 132 'TracAccountManager', 133 'Trac >= 0.11dev_r6153', 134 'M2Crypto >= 0.18' 134 135 ], 135 136