Exemple #1
0
def make_app(cfg, baselayer_handlers, baselayer_settings):
    """Create and return a `tornado.web.Application` object with specified
    handlers and settings.

    Parameters
    ----------
    cfg : Config
        Loaded configuration.  Can be specified with '--config'
        (multiple uses allowed).
    baselayer_handlers : list
        Tornado handlers needed for baselayer to function.
    baselayer_settings : cfg
        Settings needed for baselayer to function.

    """
    if cfg['cookie_secret'] == 'abc01234':
        print('!' * 80)
        print('  Your server is insecure. Please update the secret string ')
        print('  in the configuration file!')
        print('!' * 80)

    handlers = baselayer_handlers + [
        # API endpoints
        (r'/api/sources/filter', FilterSourcesHandler),
        (r'/api/sources(/.*)?', SourceHandler),
        (r'/api/groups/(.*)/users/(.*)?', GroupUserHandler),
        (r'/api/groups(/.*)?', GroupHandler),
        (r'/api/comment(/[0-9]+)?', CommentHandler),
        (r'/api/comment(/[0-9]+)/(download_attachment)', CommentHandler),
        (r'/api/photometry(/.*)?', PhotometryHandler),
        (r'/api/user(/.*)?', UserInfoHandler),
        (r'/api/sysinfo', SysInfoHandler),

        (r'/api/internal/tokens(/.*)?', TokenHandler),
        (r'/api/internal/profile', ProfileHandler),
        (r'/api/internal/plot/photometry/(.*)', PlotPhotometryHandler),
        (r'/api/internal/plot/spectroscopy/(.*)', PlotSpectroscopyHandler),

        (r'/become_user(/.*)?', BecomeUserHandler),
        (r'/logout', LogoutHandler),

        # User-facing pages
        (r'/.*', MainPageHandler)  # Route all frontend pages, such as
                                   # `/source/g647ba`, through the main page.
                                   #
                                   # Refer to Main.jsx for routing info.
    ]

    settings = baselayer_settings
    settings.update({})  # Specify any additional settings here

    app = tornado.web.Application(handlers, **settings)
    models.init_db(**cfg['database'])
    model_util.create_tables()
    model_util.setup_permissions()
    app.cfg = cfg

    app.openapi_spec = openapi.spec_from_handlers(handlers)

    return app
Exemple #2
0
def set_user_role(username=None, role=None):
    if not role:
        print(
            f'{BOLD}{RED}\nNo role provided;{END} setting to {BOLD}{GREEN}Super admin{END}{BOLD}{END}.'
        )
        role = 'Super admin'
    if role not in role_acls:
        print(
            f'{BOLD}{RED}\nRole not found!{END} Try a role from the list below:\n'
        )
        for i, role in enumerate(role_acls):
            print(f'{BOLD}{i+1}. {GREEN}{role}{END}')
        print('\n')

    elif username is not None:
        users = get_users()
        if username in [user.username for user in users]:
            if role not in [
                    role.id for role in [
                        user.roles[0] for user in users
                        if user.username == username and len(user.roles) > 0
                    ]
            ]:
                setup_permissions()
                add_user(username, roles=[role], auth=True)
                print(
                    f'\nSuccessfully assigned role {BOLD}{GREEN}{role}{END} to {BOLD}{YELLOW}User {username}{END}\n'
                )
            else:
                print(
                    f'\nUser {BOLD}{YELLOW}{username}{END} already has role {BOLD}{GREEN}{role}{END}\n'
                )

        else:
            print(
                f'\n{BOLD}{RED}User{END} {BOLD}{YELLOW}{username}{END} {BOLD}{RED}does not exist{END}\n'
            )
Exemple #3
0
    if src.get("drop_tables", False):
        with status("Dropping all tables"):
            drop_tables()

    if src.get("create_tables", False):
        with status("Creating tables"):
            create_tables()

    if src.get("print_tables", False):
        for model in Base.metadata.tables:
            print("    -", model)

    if src.get("create_permissions", False):
        with status(f"Creating permissions"):
            setup_permissions()

    if src.get("users") is not None:
        with status(f"Creating users & sitewide public group"):
            DBSession().add(Group(name=cfg["misc"]["public_group_name"]))
            DBSession().commit()

            users = []
            for user in src.get('users', []):
                users.append(
                    User(username=user["username"], role_ids=user["role_ids"])
                )
            DBSession().add_all(users)
            for user in users:
                DBSession().add(
                    TornadoStorage.user.create_social_auth(user, user.username, "google-oauth2")