Esempio n. 1
0
 def init_db(self):
     """
     Establishes a connection to the database when the command
     starts up and the .ini file was loaded successfully.
     """
     if 'sqlalchemy.url' in self.settings:
         engine = engine_from_config(self.settings, 'sqlalchemy.')
         DBSession.configure(bind=engine)
Esempio n. 2
0
 def list_by_group(self):
     """
     Returns a list of tuples (Permission, Group), only returns rows where
     permissions are used by a group.
     """
     return DBSession.query(Permission, Group).join(Group.permissions)
Esempio n. 3
0
 def get_permissions(self):
     """
     Returns a list of Permissions for this user based on their Groups.
     """
     group_ids = [group.id for group in self.groups]
     return DBSession.query(Permission).join(Group.permissions).filter(Group.id.in_(group_ids))
Esempio n. 4
0
def main(global_config, **settings):
    """
    This function returns a WSGI application.
    """
    # load custom settings from the ini file, provide defaults if missing
    debug = asbool(settings.get('lwp.debug', 'false'))
    static_dir = settings.get('lwp.static_dir', '/var/www/lwp/static')

    # write modified settings back to settings dict for use in the app
    settings['lwp.debug'] = debug
    settings['lwp.static_dir'] = static_dir
    settings['buckets.enabled'] = asbool(settings.get('buckets.enabled', 'false'))
    settings['storage_repos'] = [(key[8:], value) for key, value in settings.items() if key.startswith('storage.')]

    # initialise SQLAlchemy
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine

    # setup security policies
    authn_policy = AuthTktAuthenticationPolicy(
        settings['session.secret'],
        http_only=True,
        callback=groupfinder,
        hashalg='sha512'
    )
    authz_policy = ACLAuthorizationPolicy()

    # setup the Configurator
    config = Configurator(
        settings=settings,
        authentication_policy=authn_policy,
        authorization_policy=authz_policy,
        root_factory=RootFactory,
    )

    # static routes
    config.add_static_view('static', static_dir, cache_max_age=3600)

    # This works in Pyramid 1.5, but not in 1.4 (though the docs say it should)
    # config.add_request_method(get_current_user, 'user', reify=True)

    # This method works fine with Pyramid 1.4
    config.set_request_factory(BaseRequest)

    # routing table
    config.add_route('home', '/')
    config.add_route('about', '/about')
    config.add_route('edit', '/container/{container}/edit')
    config.add_route('start-container', '/container/{container}/start')
    config.add_route('stop-container', '/container/{container}/stop')
    config.add_route('freeze-container', '/container/{container}/freeze')
    config.add_route('unfreeze-container', '/container/{container}/unfreeze')
    config.add_route('destroy-container', '/container/{container}/destroy')
    config.add_route('push-container', '/container/{container}/push')
    config.add_route('reboot-container', '/container/{container}/reboot')
    config.add_route('create-container', '/container/create')
    config.add_route('clone-container', '/container/clone')
    config.add_route('backup-container', '/container/backup')
    config.add_route('lxc-net', '/settings/lxc-net')
    config.add_route('api-tokens', '/lwp/tokens')
    config.add_route('users', '/lwp/users')
    config.add_route('checkconfig', '/checkconfig')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    config.add_route('refresh-host-info', '/_refresh_host_info')
    config.add_route('refresh-memory', '/_refresh_memory_{name}')
    config.add_route('check-version', '/_check_version')
    config.add_route('api-container', '/api/v1/container')
    config.add_route('api-container-detail', '/api/v1/container/{container}')
    config.add_route('api-token', '/api/v1/token')
    config.add_route('api-token-detail', '/api/v1/token/{token}')
    config.scan()

    # start app
    return config.make_wsgi_app()