##// END OF EJS Templates
added is mercurial method in utils,
marcink -
r96:f24b9a29 default
parent child Browse files
Show More
@@ -1,84 +1,83
1 import logging
1 import logging
2 from datetime import datetime
2 from datetime import datetime
3 import crypt
3 import crypt
4 from pylons import session, url
4 from pylons import session, url
5 from pylons.controllers.util import abort, redirect
5 from pylons.controllers.util import abort, redirect
6 from decorator import decorator
6 from decorator import decorator
7 from sqlalchemy.exc import OperationalError
7 from sqlalchemy.exc import OperationalError
8 log = logging.getLogger(__name__)
8 log = logging.getLogger(__name__)
9 from pylons_app.model import meta
9 from pylons_app.model import meta
10 from pylons_app.model.db import Users, UserLogs
10 from pylons_app.model.db import Users, UserLogs
11 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
11 from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
12
12
13 def get_crypt_password(password):
13 def get_crypt_password(password):
14 return crypt.crypt(password, '6a')
14 return crypt.crypt(password, '6a')
15
15
16 def admin_auth(username, password):
16 def admin_auth(username, password):
17 sa = meta.Session
17 sa = meta.Session
18 password_crypt = get_crypt_password(password)
18 password_crypt = get_crypt_password(password)
19
19
20 try:
20 try:
21 user = sa.query(Users).filter(Users.username == username).one()
21 user = sa.query(Users).filter(Users.username == username).one()
22 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
22 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
23 log.error(e)
23 log.error(e)
24 user = None
24 user = None
25
25
26 if user:
26 if user:
27 if user.active:
27 if user.active:
28 if user.username == username and user.password == password_crypt and user.admin:
28 if user.username == username and user.password == password_crypt and user.admin:
29 log.info('user %s authenticated correctly', username)
29 log.info('user %s authenticated correctly', username)
30 return True
30 return True
31 else:
31 else:
32 log.error('user %s is disabled', username)
32 log.error('user %s is disabled', username)
33
33
34 return False
34 return False
35
35
36 def authfunc(environ, username, password):
36 def authfunc(environ, username, password):
37 sa = meta.Session
37 sa = meta.Session
38 password_crypt = get_crypt_password(password)
38 password_crypt = get_crypt_password(password)
39
40 try:
39 try:
41 user = sa.query(Users).filter(Users.username == username).one()
40 user = sa.query(Users).filter(Users.username == username).one()
42 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
41 except (NoResultFound, MultipleResultsFound, OperationalError) as e:
43 log.error(e)
42 log.error(e)
44 user = None
43 user = None
45
44
46 if user:
45 if user:
47 if user.active:
46 if user.active:
48 if user.username == username and user.password == password_crypt:
47 if user.username == username and user.password == password_crypt:
49 log.info('user %s authenticated correctly', username)
48 log.info('user %s authenticated correctly', username)
50 if environ:
49 if environ:
51 http_accept = environ.get('HTTP_ACCEPT')
50 http_accept = environ.get('HTTP_ACCEPT')
52
51
53 if http_accept.startswith('application/mercurial') or \
52 if http_accept.startswith('application/mercurial') or \
54 environ['PATH_INFO'].find('raw-file') != -1:
53 environ['PATH_INFO'].find('raw-file') != -1:
55 repo = environ['PATH_INFO']
54 repo = environ['PATH_INFO']
56 for qry in environ['QUERY_STRING'].split('&'):
55 for qry in environ['QUERY_STRING'].split('&'):
57 if qry.startswith('cmd'):
56 if qry.startswith('cmd'):
58
57
59 try:
58 try:
60 user_log = UserLogs()
59 user_log = UserLogs()
61 user_log.user_id = user.user_id
60 user_log.user_id = user.user_id
62 user_log.action = qry
61 user_log.action = qry
63 user_log.repository = repo
62 user_log.repository = repo
64 user_log.action_date = datetime.now()
63 user_log.action_date = datetime.now()
65 sa.add(user_log)
64 sa.add(user_log)
66 sa.commit()
65 sa.commit()
67 log.info('Adding user %s, action %s', username, qry)
66 log.info('Adding user %s, action %s', username, qry)
68 except Exception as e:
67 except Exception as e:
69 sa.rollback()
68 sa.rollback()
70 log.error(e)
69 log.error(e)
71
70
72 return True
71 return True
73 else:
72 else:
74 log.error('user %s is disabled', username)
73 log.error('user %s is disabled', username)
75
74
76 return False
75 return False
77
76
78
77
79 @decorator
78 @decorator
80 def authenticate(fn, *args, **kwargs):
79 def authenticate(fn, *args, **kwargs):
81 if not session.get('admin_user', False):
80 if not session.get('admin_user', False):
82 redirect(url('admin_home'), 301)
81 redirect(url('admin_home'), 301)
83 return fn(*args, **kwargs)
82 return fn(*args, **kwargs)
84
83
@@ -1,8 +1,16
1
1
2 def get_repo_slug(request):
2 def get_repo_slug(request):
3 path_info = request.environ.get('PATH_INFO')
3 path_info = request.environ.get('PATH_INFO')
4 uri_lst = path_info.split('/')
4 uri_lst = path_info.split('/')
5 print uri_lst
6 print 'len', len(uri_lst)
7 repo_name = uri_lst[1]
5 repo_name = uri_lst[1]
8 return repo_name
6 return repo_name
7
8 def is_mercurial(environ):
9 """
10 Returns True if request's target is mercurial server - header
11 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``.
12 """
13 http_accept = environ.get('HTTP_ACCEPT')
14 if http_accept and http_accept.startswith('application/mercurial'):
15 return True
16 return False
General Comments 0
You need to be logged in to leave comments. Login now