comparison pylons_app/lib/middleware/simplehg.py @ 351:a9a6c74ad2a6

added ip loggin into mercurial middleware
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 01 Jul 2010 20:10:22 +0200
parents 4c9a295d80a4
children 6c23e72437e3
comparison
equal deleted inserted replaced
350:4c9a295d80a4 351:a9a6c74ad2a6
96 'repository.admin')\ 96 'repository.admin')\
97 (user, repo_name): 97 (user, repo_name):
98 return HTTPForbidden()(environ, start_response) 98 return HTTPForbidden()(environ, start_response)
99 99
100 #log action 100 #log action
101 self.__log_user_action(user, action, repo_name) 101 proxy_key = 'HTTP_X_REAL_IP'
102 def_key = 'REMOTE_ADDR'
103 ipaddr = environ.get(proxy_key, environ.get(def_key, '0.0.0.0'))
104 self.__log_user_action(user, action, repo_name, ipaddr)
102 105
103 #=================================================================== 106 #===================================================================
104 # MERCURIAL REQUEST HANDLING 107 # MERCURIAL REQUEST HANDLING
105 #=================================================================== 108 #===================================================================
106 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path 109 environ['PATH_INFO'] = '/'#since we wrap into hgweb, reset the path
160 if qry.startswith('cmd'): 163 if qry.startswith('cmd'):
161 cmd = qry.split('=')[-1] 164 cmd = qry.split('=')[-1]
162 if mapping.has_key(cmd): 165 if mapping.has_key(cmd):
163 return mapping[cmd] 166 return mapping[cmd]
164 167
165 def __log_user_action(self, user, action, repo): 168 def __log_user_action(self, user, action, repo, ipaddr):
166 sa = meta.Session 169 sa = meta.Session
167 try: 170 try:
168 user_log = UserLog() 171 user_log = UserLog()
169 user_log.user_id = user.user_id 172 user_log.user_id = user.user_id
170 user_log.action = action 173 user_log.action = action
171 user_log.repository = repo.replace('/', '') 174 user_log.repository = repo.replace('/', '')
172 user_log.action_date = datetime.now() 175 user_log.action_date = datetime.now()
176 user_log.user_ip = ipaddr
173 sa.add(user_log) 177 sa.add(user_log)
174 sa.commit() 178 sa.commit()
175 log.info('Adding user %s, action %s on %s', 179 log.info('Adding user %s, action %s on %s',
176 user.username, action, repo) 180 user.username, action, repo)
177 except Exception as e: 181 except Exception as e: