Example #1
0
def load_role():
    request.authz = Authz(role=None)
    if session.get('user'):
        role = Role.by_id(session.get('user'))
        request.authz = Authz(role=role)
    else:
        api_key = request.args.get('api_key')
        if api_key is None:
            auth_header = request.headers.get('Authorization') or ''
            if auth_header.lower().startswith('apikey'):
                api_key = auth_header.split(' ', 1).pop()

        role = Role.by_api_key(api_key)
        if role is not None:
            request.authz = Authz(role=role)
Example #2
0
def check_alerts():
    """Go through all users and execute their alerts."""
    for role_id, in Role.notifiable():
        with current_app.test_request_context('/'):
            role = Role.by_id(role_id)
            authz = Authz(role=role)
            check_role_alerts(authz)
Example #3
0
File: leads.py Project: wcyn/aleph
def generate_leads(entity_id):
    """Compute likely duplicates of a given entity and index these leads."""
    # Get rid of everything, also for deleted entities etc.
    delete_entity_leads(entity_id)

    entity = load_entity(entity_id)
    if entity is None:
        # log.warning("[%r] not indexed, skip lead generation.", entity_id)
        return
    if not entity.get('collection_id'):
        # log.warning("[%r] is not in a collecton, skip lead generation.", entity_id)  # noqa
        return

    log.debug("Generating leads for [%(id)s]: %(name)s", entity)
    authz = Authz(override=True)
    judgements = EntityIdentity.judgements_by_entity(entity_id)
    state = QueryState({}, authz, limit=100)
    result = similar_entities(entity, state)
    for other in result.get('results', []):
        score = entity_distance(entity, other)
        log.debug(" -[%.2f]-> %s", score, other.get('name'))
        # TODO: implement some cut-off
        index_lead({
            'entity_id': entity.get('id'),
            'entity_collection_id': entity.get('collection_id'),
            'score': score,
            'judgement': judgements.get(other.get('id'), 0),
            'match_id': other.get('id'),
            'schema': other.get('schema'),
            'schemata': other.get('schemata'),
            'collection_id': other.get('collection_id'),
            'dataset': other.get('dataset'),
            'roles': other.get('roles')
        })
Example #4
0
def load_role():
    role = None
    credential = request.headers.get('Authorization', '')
    if len(credential):
        if ' ' in credential:
            mechanism, credential = credential.split(' ', 1)
        role = _get_credential_role(credential)
    elif 'api_key' in request.args:
        role = _get_credential_role(request.args.get('api_key'))
    request.authz = Authz(role=role)
Example #5
0
def load_role():
    role = None
    if 'Authorization' in request.headers:
        credential = request.headers.get('Authorization')
        if ' ' in credential:
            mechanism, credential = credential.split(' ', 1)
        data = check_token(credential)
        if data is not None:
            role = Role.by_id(data.get('id'))
        else:
            role = Role.by_api_key(credential)
    elif 'api_key' in request.args:
        role = Role.by_api_key(request.args.get('api_key'))
    request.authz = Authz(role=role)
Example #6
0
def check_alert(alert):
    authz = Authz(role=alert.role)
    query = alert_query(alert, authz)
    found = 0
    for result in scan(es, query=query, index=entities_index()):
        entity = unpack_result(result)
        found += 1
        params = {'alert': alert, 'role': authz.role, 'entity': entity}
        publish(Events.MATCH_ALERT,
                actor_id=entity.get('uploader_id'),
                params=params)

    alert.update()
    log.info('Found %d new results for: %s', found, alert.label)
    db.session.commit()
Example #7
0
def metadata():
    locale = get_locale()
    enable_cache(vary_user=False)

    auth = {}
    if settings.PASSWORD_LOGIN:
        auth['password_login_uri'] = url_for('sessions_api.password_login')
        auth['registration_uri'] = url_for('roles_api.create_code')
    if settings.OAUTH:
        auth['oauth_uri'] = url_for('sessions_api.oauth_init')

    country_names = {}
    for code, label in countries.names.items():
        country_names[code] = locale.territories.get(code.upper(), label)

    language_names = {}
    for code, label in languages.names.items():
        language_names[code] = locale.languages.get(code, label)

    return jsonify({
        'status': 'ok',
        'maintenance': request.authz.in_maintenance,
        'app': {
            'title': settings.APP_TITLE,
            'version': __version__,
            'ui_uri': six.text_type(app_ui_url),
            'samples': settings.SAMPLE_SEARCHES,
            'logo': settings.APP_LOGO,
            'favicon': settings.APP_FAVICON,
            'locale': six.text_type(locale),
            'locales': settings.UI_LANGUAGES
        },
        'categories': Collection.CATEGORIES,
        'statistics': get_instance_stats(Authz()),
        'countries': country_names,
        'languages': language_names,
        'schemata': model,
        'auth': auth
    })
Example #8
0
def check_alerts():
    """Go through all users and execute their alerts."""
    for role in Role.all():
        authz = Authz(role=role)
        for alert in Alert.by_role(role).all():
            check_alert(authz, alert)