# Path: bluechips/lib/permissions.py
# class BlueChipResident(RequestPermission):
#     def check(self, app, environ, start_response):
#         if 'user' not in environ:
#             raise NotAuthenticatedError('Not Authenticated')
# 
#         if not getattr(environ['user'], 'resident', False):
#             raise NotAuthorizedError('You are not allowed access.')
# 
#         return app(environ, start_response)


import logging
import sqlalchemy
from bluechips.lib.base import *
from bluechips.lib.permissions import BlueChipResident
from sqlalchemy import orm
from authkit.authorize.pylons_adaptors import authorize
from pylons import request
from pylons.decorators import validate
from pylons.decorators.secure import authenticate_form
from formencode import validators, Schema, FancyValidator, Invalid
        validators.FieldsMatch('password', 'confirm_password'),
        ]


class UserController(BaseController):
    def index(self):
        c.title = 'User Settings'
        return render('/user/index.mako')

    def email(self):
        c.title = 'User Settings'
        return render('/user/email.mako')

    @validate(schema=EmailSchema(), form='index')
    @authenticate_form
    def update(self):
        new_email = self.form_result['new_email']
        request.environ['user'].email = new_email
        meta.Session.commit()
        if new_email is None:
            h.flash("Removed email address.")
        else:
            h.flash("Updated email address to '%s'." % new_email)
        return h.redirect_to('/')

    @authorize(BlueChipResident())
    def new(self):
        c.title = 'Register a New User'
        return render('/user/new.mako')


# Path: bluechips/lib/permissions.py
# class BlueChipResident(RequestPermission):
#     def check(self, app, environ, start_response):
#         if 'user' not in environ:
#             raise NotAuthenticatedError('Not Authenticated')
# 
#         if not getattr(environ['user'], 'resident', False):
#             raise NotAuthorizedError('You are not allowed access.')
# 
#         return app(environ, start_response)


# Path: bluechips/controllers/user.py
import logging
import sqlalchemy
from bluechips.lib.base import *
from bluechips.lib.permissions import BlueChipResident
from sqlalchemy import orm
from authkit.authorize.pylons_adaptors import authorize
from pylons import request
from pylons.decorators import validate
from pylons.decorators.secure import authenticate_form
from formencode import validators, Schema, FancyValidator, Invalid

        validators.FieldsMatch('password', 'confirm_password'),
        ]


class UserController(BaseController):
    def index(self):
        c.title = 'User Settings'
        return render('/user/index.mako')

    def email(self):
        c.title = 'User Settings'
        return render('/user/email.mako')

    @validate(schema=EmailSchema(), form='index')
    @authenticate_form
    def update(self):
        new_email = self.form_result['new_email']
        request.environ['user'].email = new_email
        meta.Session.commit()
        if new_email is None:
            h.flash("Removed email address.")
        else:
            h.flash("Updated email address to '%s'." % new_email)
        return h.redirect_to('/')

    @authorize(BlueChipResident())
    def new(self):
        c.title = 'Register a New User'
        return render('/user/new.mako')


    @authorize(BlueChipResident())