Changeset - 25e516447a33
[Not reviewed]
default
0 4 0
marcink - 16 years ago 2010-04-08 12:00:06

implemented autentication
4 files changed with 30 insertions and 14 deletions:
0 comments (0 inline, 0 general)
pylons_app/controllers/admin.py
Show inline comments
 
@@ -12,14 +12,14 @@ from pylons_app.lib import auth
 
from pylons_app.model.forms import LoginForm
 
import formencode
 
import formencode.htmlfill as htmlfill
 
from pylons_app.lib.auth import authenticate
 
log = logging.getLogger(__name__)
 

	
 
class AdminController(BaseController):
 

	
 

	
 
    def __before__(self):
 
        c.staticurl = g.statics
 
        c.admin_user = session.get('admin_user')
 
        c.admin_user = session.get('admin_user', False)
 
        c.admin_username = session.get('admin_username')
 
        
 
    def index(self):
pylons_app/controllers/repos.py
Show inline comments
 
@@ -6,6 +6,8 @@ from pylons_app.lib import auth
 
from pylons_app.lib.base import BaseController, render
 
from pylons_app.model import meta
 
from pylons_app.model.db import Users, UserLogs
 
from pylons_app.lib.auth import authenticate
 

	
 
log = logging.getLogger(__name__)
 

	
 
class ReposController(BaseController):
 
@@ -13,6 +15,8 @@ class ReposController(BaseController):
 
    # To properly map this controller, ensure your config/routing.py
 
    # file has a resource setup:
 
    #     map.resource('repo', 'repos')
 
    
 
    @authenticate
 
    def __before__(self):
 
        c.staticurl = g.statics
 
        c.admin_user = session.get('admin_user')
pylons_app/controllers/users.py
Show inline comments
 
@@ -7,7 +7,9 @@ from pylons_app.lib.base import BaseCont
 
from formencode import htmlfill
 
from pylons_app.model import meta
 
from pylons_app.model.db import Users, UserLogs
 
from pylons_app.lib.auth import authenticate
 
import crypt
 

	
 
log = logging.getLogger(__name__)
 

	
 
class UsersController(BaseController):
 
@@ -16,6 +18,7 @@ class UsersController(BaseController):
 
    # file has a resource setup:
 
    #     map.resource('user', 'users')
 
    
 
    @authenticate
 
    def __before__(self):
 
        c.staticurl = g.statics
 
        c.admin_user = session.get('admin_user')
pylons_app/lib/auth.py
Show inline comments
 
@@ -4,7 +4,9 @@ import logging
 
from os.path import dirname as dn
 
from datetime import datetime
 
import crypt
 

	
 
from pylons import session, url
 
from pylons.controllers.util import abort, redirect
 
from decorator import decorator
 
log = logging.getLogger(__name__)
 
ROOT = dn(dn(dn(os.path.realpath(__file__))))
 

	
 
@@ -60,9 +62,9 @@ def authfunc(environ, username, password
 
                                cmd += "|" + qry
 
                                
 
                                try:
 
                                    cur.execute('''INSERT INTO 
 
                                    cur.execute("""INSERT INTO 
 
                                                        user_logs 
 
                                                   VALUES(?,?,?,?)''',
 
                                                   VALUES(?,?,?,?)""",
 
                                                    (None, data[0], cmd, datetime.now()))
 
                                    conn.commit()
 
                                except Exception as e:
 
@@ -75,27 +77,34 @@ def authfunc(environ, username, password
 
            
 
    return False
 

	
 

	
 
@decorator
 
def authenticate(fn, *args, **kwargs):
 
    if not session.get('admin_user', False):
 
        redirect(url('admin_home'), 301)
 
    return fn(*args, **kwargs)
 

	
 
def create_user_table():
 
    '''
 
    """
 
    Create a auth database
 
    '''
 
    """
 
    conn, cur = get_sqlite_conn_cur()
 
    try:
 
        log.info('creating table %s', 'users')
 
        cur.execute('''DROP TABLE IF EXISTS users ''')
 
        cur.execute('''CREATE TABLE users
 
        cur.execute("""DROP TABLE IF EXISTS users """)
 
        cur.execute("""CREATE TABLE users
 
                        (user_id INTEGER PRIMARY KEY AUTOINCREMENT, 
 
                         username TEXT, 
 
                         password TEXT,
 
                         active INTEGER,
 
                         admin INTEGER)''')
 
                         admin INTEGER)""")
 
        log.info('creating table %s', 'user_logs')
 
        cur.execute('''DROP TABLE IF EXISTS user_logs ''')
 
        cur.execute('''CREATE TABLE user_logs
 
        cur.execute("""DROP TABLE IF EXISTS user_logs """)
 
        cur.execute("""CREATE TABLE user_logs
 
                        (id INTEGER PRIMARY KEY AUTOINCREMENT,
 
                            user_id INTEGER,
 
                            last_action TEXT, 
 
                            last_action_date DATETIME)''')
 
                            last_action_date DATETIME)""")
 
        conn.commit()
 
    except:
 
        conn.rollback()
 
@@ -108,7 +117,7 @@ def create_user(username, password, admi
 
    password_crypt = crypt.crypt(password, '6a')
 
    log.info('creating user %s', username)
 
    try:
 
        cur.execute('''INSERT INTO users values (?,?,?,?,?) ''',
 
        cur.execute("""INSERT INTO users values (?,?,?,?,?) """,
 
                    (None, username, password_crypt, 1, admin))     
 
        conn.commit()
 
    except:
0 comments (0 inline, 0 general)