<commit_before>from flask import abort, Blueprint, session
from flask.ext.login import current_user, UserMixin
from functools import wraps
import ldap

from wuvt import app
from wuvt import login_manager
from wuvt.models import User


bp = Blueprint('auth', __name__)


@login_manager.user_loader
def load_user(userid):
    return User.query.get(userid)


def build_dn(username):
    return app.config['LDAP_AUTH_DN'].format(ldap.dn.escape_dn_chars(username))


def build_group_query(groups):
    query = '(|'
    for group in groups:
        query += '(cn={})'.format(ldap.dn.escape_dn_chars(group))
    query += ')'
    return query


def ldap_group_test(client, groups, username):
    result = client.search_s(app.config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
                             build_group_query(groups))
    for r in result:
        if 'memberUid' in r[1]:
            for u in r[1]['memberUid']:
                if u == username:
                    return True

    return False


def check_access(*sections):
    sections = set(sections)
    def access_decorator(f):
        @wraps(f)
        def access_wrapper(*args, **kwargs):
            if not current_user.is_authenticated():
                return app.login_manager.unauthorized()

            access_sections = set(session.get('access', []))
            if sections.isdisjoint(access_sections):
                abort(403)

            return f(*args, **kwargs)
        return access_wrapper
    return access_decorator


from wuvt.auth import views<commit_msg>auth: fix is_authenticated check<commit_after>from flask import abort, Blueprint, session
from flask.ext.login import current_user, UserMixin
from functools import wraps
import ldap

from wuvt import app
from wuvt import login_manager
from wuvt.models import User


bp = Blueprint('auth', __name__)


@login_manager.user_loader
def load_user(userid):
    return User.query.get(userid)


def build_dn(username):
    return app.config['LDAP_AUTH_DN'].format(ldap.dn.escape_dn_chars(username))


def build_group_query(groups):
    query = '(|'
    for group in groups:
        query += '(cn={})'.format(ldap.dn.escape_dn_chars(group))
    query += ')'
    return query


def ldap_group_test(client, groups, username):
    result = client.search_s(app.config['LDAP_BASE_DN'], ldap.SCOPE_SUBTREE,
                             build_group_query(groups))
    for r in result:
        if 'memberUid' in r[1]:
            for u in r[1]['memberUid']:
                if u == username:
                    return True

    return False


def check_access(*sections):
    sections = set(sections)
    def access_decorator(f):
        @wraps(f)
        def access_wrapper(*args, **kwargs):
            if not current_user.is_authenticated:
                return app.login_manager.unauthorized()

            access_sections = set(session.get('access', []))
            if sections.isdisjoint(access_sections):
                abort(403)

            return f(*args, **kwargs)
        return access_wrapper
    return access_decorator


from wuvt.auth import views
{'fraction_non_alphanumeric': 0.10081037277147488, 'fraction_numerical': 0.0032414910858995136, 'mean_word_length': 2.781862745098039, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3061625', 'n_tokens_mistral': 965, 'n_tokens_neox': 925, 'n_words': 224}