from pyramid.security import Everyone
from pyramid.security import Authenticated
from pyramid.security import Allow
from brewman.models.auth import Role


class RootFactory(object):
    @property
    def __acl__(self):
        acl = [
            (Allow, Everyone, 'view'),
            (Allow, Authenticated, 'Authenticated')]
        for permission in Role.list():
            acl.append((Allow, permission.name, permission.name))
        return acl

    def __init__(self, request):
        pass


def add_route(config, name, url, has_list=True, variable='id'):
    config.add_route(name + '_' + variable, url + '/{' + variable + '}')
    config.add_route(name, url)
    if has_list:
        config.add_route(name + '_list', url + 's')

    config.add_route('api_' + name + '_' + variable, '/api' + url + '/{' + variable + '}')
    config.add_route('api_' + name, '/api' + url)
