<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import PermissionDenied

from .models import Site
from .utils import current_site_id


class SitePermissionBackend(ModelBackend):
    """
    Checks for permission to the current site for the logging in user.
    """
    def authenticate(self, request, username=None, password=<PASSWORD>, **kwargs):
        user = super(SitePermissionBackend, self).authenticate(
            request, username, password, **kwargs)

        if user and user.is_active:
            if user.is_superuser:
                return user

        '''TODO: Decide what to do if sitepermission is not available.'''
        if hasattr(user, 'sitepermission'):
            site = Site.objects.get(id=current_site_id())
            if site in user.sitepermission.sites.all():
                return user
            else:
                '''TODO: Handle in UI with a more verbose message.'''
                raise PermissionDenied

    def get_user(self, user_id):
        return super(SitePermissionBackend, self).get_user(user_id)
<commit_msg>Disregard superuser status for authentication. All users are affected by SitePermission settings.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import PermissionDenied

from .models import Site
from .utils import current_site_id


class SitePermissionBackend(ModelBackend):
    """
    Checks for permission to the current site for the logging in user.
    """
    def authenticate(self, request, username=None, password=<PASSWORD>, **kwargs):
        user = super(SitePermissionBackend, self).authenticate(
            request, username, password, **kwargs)

        if user and user.is_active and hasattr(user, 'sitepermission'):
            site = Site.objects.get(id=current_site_id())
            if site in user.sitepermission.sites.all():
                return user
            else:
                '''TODO: Handle in UI with a more verbose message.'''
                raise PermissionDenied

    def get_user(self, user_id):
        return super(SitePermissionBackend, self).get_user(user_id)

{'fraction_non_alphanumeric': 0.08758169934640522, 'fraction_numerical': 0.0008714596949891067, 'mean_word_length': 2.898132427843803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2434735', 'n_tokens_mistral': 611, 'n_tokens_neox': 592, 'n_words': 191}