from repoze.what.plugins.quickstart import setup_sql_auth
from repoze.what.plugins.quickstart import FriendlyRedirectingFormPlugin
from {{package}}.model import User, Group, Permission, Session
from pylons import request

# Bind values to the variables that will be provided to 
# :func:`setup_sql_auth`. This is a good example of the 
# difference between the ``.ini`` config file approach
# and this one, i.e. of effecting the configuration within
# the friendly confines of an executable Python source file.
# Otherwise, the model entity classes would have to be 
# expressed as strings, to be later turned into real
# references to the object's class.

user_name = 'username'
user_class = User
group_class = Group
permission_class = Permission
dbsession = Session
translations={'user_name': 'username',
            'users': 'users',
            'group_name': 'name',
            'groups': 'groups',
            'permission_name': 'name',
            'permissions': 'permissions',
            'validate_password': 'validate_password' }

# Bind values to the variables that will be provided to 
# :class:`FriendlyRedirectingFormPlugin` and create an
# instance of same.
# ATM, an actual (configured) instance is required, ya 
# can't just supply the class name.

loginform = FriendlyRedirectingFormPlugin(
    '/login/index',                 # arg - login_form_url
    '/login_handler',               # arg - login_handler_path
    '/logout_handler',              # arg - logout_handler_path
    'cookie',                       # arg - rememberer_name
    reason_param='reason',          # kwarg = reason_param
    login_counter_name = '__tries', # kwarg = login_counter_name, 
                                    # the name of the login counter 
                                    # variable in the query string.
                                    # Defaults to ``__logins``.
    post_login_url = '/login/welcome_back',         # The URL/path to the post-login 
                                    # page, if any.
    post_logout_url = '/login/see_you_later',         # The URL/path to the post-logout 
                                    # page, if any.
    )

def add_auth(app, skip_authentication):
    """Add authentication and authorization middleware to the ``app``."""
    return setup_sql_auth(app,
            user_class, 
            group_class, 
            permission_class, 
            dbsession, 
            form_plugin=loginform, 
            form_identifies=True, 
            cookie_secret='secretsquirrel', 
            cookie_name='authtkt', 
            login_url='/login/index', 
            #login_handler='/login/signin', 
            post_login_url='/login/welcome_back', 
            #logout_handler='/login/signout', 
            post_logout_url='/login/see_you_later', 
            login_counter_name='__tries', 
            translations=translations,
            skip_authentication=skip_authentication)

def get_user():
    """Return the current user's database object."""
    if 'repoze.who.identity' in request.environ:
        print("Have user")
        return request.environ.get('repoze.who.identity')['user']
    else:
        print("Don't have user")
        return None
    
