Mercurial > public > src > rhodecode
comparison pylons_app/lib/auth.py @ 52:25e516447a33
implemented autentication
author | marcink |
---|---|
date | Thu, 08 Apr 2010 12:00:06 +0200 |
parents | 8e250e86a670 |
children | 08707974eae4 |
comparison
equal
deleted
inserted
replaced
51:a699c0088344 | 52:25e516447a33 |
---|---|
2 import os | 2 import os |
3 import logging | 3 import logging |
4 from os.path import dirname as dn | 4 from os.path import dirname as dn |
5 from datetime import datetime | 5 from datetime import datetime |
6 import crypt | 6 import crypt |
7 | 7 from pylons import session, url |
8 from pylons.controllers.util import abort, redirect | |
9 from decorator import decorator | |
8 log = logging.getLogger(__name__) | 10 log = logging.getLogger(__name__) |
9 ROOT = dn(dn(dn(os.path.realpath(__file__)))) | 11 ROOT = dn(dn(dn(os.path.realpath(__file__)))) |
10 | 12 |
11 def get_sqlite_conn_cur(): | 13 def get_sqlite_conn_cur(): |
12 conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite')) | 14 conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite')) |
58 for qry in environ['QUERY_STRING'].split('&'): | 60 for qry in environ['QUERY_STRING'].split('&'): |
59 if qry.startswith('cmd'): | 61 if qry.startswith('cmd'): |
60 cmd += "|" + qry | 62 cmd += "|" + qry |
61 | 63 |
62 try: | 64 try: |
63 cur.execute('''INSERT INTO | 65 cur.execute("""INSERT INTO |
64 user_logs | 66 user_logs |
65 VALUES(?,?,?,?)''', | 67 VALUES(?,?,?,?)""", |
66 (None, data[0], cmd, datetime.now())) | 68 (None, data[0], cmd, datetime.now())) |
67 conn.commit() | 69 conn.commit() |
68 except Exception as e: | 70 except Exception as e: |
69 conn.rollback() | 71 conn.rollback() |
70 log.error(e) | 72 log.error(e) |
73 else: | 75 else: |
74 log.error('user %s is disabled', username) | 76 log.error('user %s is disabled', username) |
75 | 77 |
76 return False | 78 return False |
77 | 79 |
80 | |
81 @decorator | |
82 def authenticate(fn, *args, **kwargs): | |
83 if not session.get('admin_user', False): | |
84 redirect(url('admin_home'), 301) | |
85 return fn(*args, **kwargs) | |
86 | |
78 def create_user_table(): | 87 def create_user_table(): |
79 ''' | 88 """ |
80 Create a auth database | 89 Create a auth database |
81 ''' | 90 """ |
82 conn, cur = get_sqlite_conn_cur() | 91 conn, cur = get_sqlite_conn_cur() |
83 try: | 92 try: |
84 log.info('creating table %s', 'users') | 93 log.info('creating table %s', 'users') |
85 cur.execute('''DROP TABLE IF EXISTS users ''') | 94 cur.execute("""DROP TABLE IF EXISTS users """) |
86 cur.execute('''CREATE TABLE users | 95 cur.execute("""CREATE TABLE users |
87 (user_id INTEGER PRIMARY KEY AUTOINCREMENT, | 96 (user_id INTEGER PRIMARY KEY AUTOINCREMENT, |
88 username TEXT, | 97 username TEXT, |
89 password TEXT, | 98 password TEXT, |
90 active INTEGER, | 99 active INTEGER, |
91 admin INTEGER)''') | 100 admin INTEGER)""") |
92 log.info('creating table %s', 'user_logs') | 101 log.info('creating table %s', 'user_logs') |
93 cur.execute('''DROP TABLE IF EXISTS user_logs ''') | 102 cur.execute("""DROP TABLE IF EXISTS user_logs """) |
94 cur.execute('''CREATE TABLE user_logs | 103 cur.execute("""CREATE TABLE user_logs |
95 (id INTEGER PRIMARY KEY AUTOINCREMENT, | 104 (id INTEGER PRIMARY KEY AUTOINCREMENT, |
96 user_id INTEGER, | 105 user_id INTEGER, |
97 last_action TEXT, | 106 last_action TEXT, |
98 last_action_date DATETIME)''') | 107 last_action_date DATETIME)""") |
99 conn.commit() | 108 conn.commit() |
100 except: | 109 except: |
101 conn.rollback() | 110 conn.rollback() |
102 raise | 111 raise |
103 | 112 |
106 def create_user(username, password, admin=False): | 115 def create_user(username, password, admin=False): |
107 conn, cur = get_sqlite_conn_cur() | 116 conn, cur = get_sqlite_conn_cur() |
108 password_crypt = crypt.crypt(password, '6a') | 117 password_crypt = crypt.crypt(password, '6a') |
109 log.info('creating user %s', username) | 118 log.info('creating user %s', username) |
110 try: | 119 try: |
111 cur.execute('''INSERT INTO users values (?,?,?,?,?) ''', | 120 cur.execute("""INSERT INTO users values (?,?,?,?,?) """, |
112 (None, username, password_crypt, 1, admin)) | 121 (None, username, password_crypt, 1, admin)) |
113 conn.commit() | 122 conn.commit() |
114 except: | 123 except: |
115 conn.rollback() | 124 conn.rollback() |
116 raise | 125 raise |