<commit_before>from django.conf import settings
from django.contrib import auth
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.shortcuts import render, redirect
from django.utils import timezone
from apps.core.backends import get_username, \
    init_fb, init_tw, auth_fb, auth_tw, auth_kaist
from apps.core.models import Notice, Service
from urlparse import urlparse, parse_qs
import logging


logger = logging.getLogger('sso.core.auth')
account_logger = logging.getLogger('sso.core.account')
profile_logger = logging.getLogger('sso.core.profile')


# /login/
def login(request):
    if request.user.is_authenticated():
        return redirect('/')

    current_time = timezone.now()
    notice = Notice.objects.filter(valid_from__lte=current_time,
                                   valid_to__gt=current_time).first()

    if 'next' in request.GET:
        request.session['next'] = request.GET['next']

    query_dict = parse_qs(urlparse(request.session.get('next', '/')).query)
    service = Service.objects.filter(name=query_dict.get('client_id', [''])[0]).first()
    srv_name = service.alias if service else ''

    if request.method == 'POST':
        email = request.POST.get('email', 'none')
        password = request.POST.get('password', '<PASSWORD>')

        username = get_username(email)
        user = auth.authenticate(username=username, password=password)
        if not user:
            logger.info('login.fail', {'r': request, 'uid': username})
            return render(request, 'account/login.html',
                          {'fail': 1, 'notice': notice, 'service': srv_name})
        elif not user.is_active:
            logger.info('login.reject', {'r': request, 'uid': username})
            raise PermissionDenied()
        else:
            request.session.pop('info_signup', None)
            auth.login(request, user)
            logger.info('login.success', {'r': request})

            if not settings.DEBUG and user.is_staff:
                title = '[SPARCS SSO] Staff Login'
                message = 'time:%s; id:%s; ip:%s;'
                emails = map(lambda x: x[1], settings.ADMINS)
                time = timezone.now()
                ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
                send_mail(title, '', '<EMAIL>', emails,
                          html_message=message % (time, username, ip))

            if user.profile.activate():
                account_logger.warning('activate', {'r': request})

            nexturl = request.session.pop('next', '/')
            return redirect(nexturl)

    context = {
        'notice': notice,
        'service': srv_name,
        'fail': request.session.pop('result_login', ''),
        'kaist_enabled': settings.KAIST_APP_ENABLED,
    }
    return render(request, 'account/login.html', context)


# /logout/
def logout(request):
    if request.method != 'POST' or not request.user.is_authenticated():
        return redirect('/')

    logger.info('logout', {'r': request})
    auth.logout(request)
    return render(request, 'account/logout.html')


# /login/{fb,tw,kaist}/, /connect/{fb,tw,kaist}/, /renew/kaist/
def init(request, mode, type):
    is_authed = request.user.is_authenticated()
    if (mode == 'LOGIN' and is_authed) or \
       (mode == 'CONN' and not is_authed) or \<commit_msg>bug fix - kaist login is disabled when a user fails to login by email<commit_after>from django.conf import settings
from django.contrib import auth
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.shortcuts import render, redirect
from django.utils import timezone
from apps.core.backends import get_username, \
    init_fb, init_tw, auth_fb, auth_tw, auth_kaist
from apps.core.models import Notice, Service
from urlparse import urlparse, parse_qs
import logging


logger = logging.getLogger('sso.core.auth')
account_logger = logging.getLogger('sso.core.account')
profile_logger = logging.getLogger('sso.core.profile')


# /login/
def login(request):
    if request.user.is_authenticated():
        return redirect('/')

    current_time = timezone.now()
    notice = Notice.objects.filter(valid_from__lte=current_time,
                                   valid_to__gt=current_time).first()

    if 'next' in request.GET:
        request.session['next'] = request.GET['next']

    query_dict = parse_qs(urlparse(request.session.get('next', '/')).query)
    service = Service.objects.filter(name=query_dict.get('client_id', [''])[0]).first()
    srv_name = service.alias if service else ''

    if request.method == 'POST':
        email = request.POST.get('email', 'none')
        password = request.POST.get('password', '<PASSWORD>')

        username = get_username(email)
        user = auth.authenticate(username=username, password=password)
        if user and not user.is_active:
            logger.info('login.reject', {'r': request, 'uid': username})
            raise PermissionDenied()
        elif user:
            request.session.pop('info_signup', None)
            auth.login(request, user)
            logger.info('login.success', {'r': request})

            if not settings.DEBUG and user.is_staff:
                title = '[SPARCS SSO] Staff Login'
                message = 'time:%s; id:%s; ip:%s;'
                emails = map(lambda x: x[1], settings.ADMINS)
                time = timezone.now()
                ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
                send_mail(title, '', '<EMAIL>', emails,
                          html_message=message % (time, username, ip))

            if user.profile.activate():
                account_logger.warning('activate', {'r': request})

            nexturl = request.session.pop('next', '/')
            return redirect(nexturl)
        else:
            logger.info('login.fail', {'r': request, 'uid': username})
            request.session['result_login'] = 1

    context = {
        'notice': notice,
        'service': srv_name,
        'fail': request.session.pop('result_login', ''),
        'kaist_enabled': settings.KAIST_APP_ENABLED,
    }
    return render(request, 'account/login.html', context)


# /logout/
def logout(request):
    if request.method != 'POST' or not request.user.is_authenticated():
        return redirect('/')

    logger.info('logout', {'r': request})
    auth.logout(request)
    return render(request, 'account/logout.html')


# /login/{fb,tw,kaist}/, /connect/{fb,tw,kaist}/, /renew/kaist/
def init(request, mode, type):
    is_authed = request.user.is_authenticated()
    if (mode == 'LOGIN' and is_authed) or \
       (mode == 'CONN' and not is_authed) or \
{'fraction_non_alphanumeric': 0.13845693779904306, 'fraction_numerical': 0.0020933014354066986, 'mean_word_length': 2.7368715083798882, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3271198', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1962, 'n_words': 451}