<commit_before>from onadata.apps.userrole.models import UserRole as Role
from rest_framework.authtoken.models import Token
from django.contrib.auth import logout

from django.shortcuts import get_object_or_404, render, redirect



def clear_roles(request):
    request.__class__.role = None
    request.__class__.organization = None
    request.__class__.project = None
    request.__class__.site = None
    request.__class__.group = None
    request.__class__.roles = []
    request.__class__.is_super_admin = False
    # request.__class__.groups = []
    return request


class RoleMiddleware(object):
    def process_request(self, request):

        if request.META.get('HTTP_AUTHORIZATION'):
            token_key = request.META.get('HTTP_AUTHORIZATION').split(' ')[-1]
            try:
                request.user = Token.objects.get(key=token_key).user
            except:
                pass

        if not request.user.is_anonymous():

            role = None
            if request.session.get('role'):
                try:
                    role = Role.objects.select_related('group', 'organization').get(pk=request.session.get('role'),
                                                                                    user=request.user)
                except Role.DoesNotExist:
                    pass

            if not role:
                roles = Role.get_active_roles(request.user)
                # roles = Role.objects.filter(user=request.user).select_related('group', 'organization')
                if roles:
                    role = roles[0]
                    request.session['role'] = role.id
            if role:
                request.__class__.role = role
                request.__class__.organization = role.organization
                request.__class__.project = role.project
                request.__class__.site = role.site
                request.__class__.group = role.group
                # request.__class__.roles = Role.objects.filter(user=request.user, organization=role.organization)
                request.__class__.roles = Role.get_active_roles(request.user)
                request.__class__.is_super_admin = request.group.name in ('Super Admin')
                #     for role in request.roles:
                #         groups.append(role.group)
                #     request.__class__.groups = groups
            else:
                # request = clear_roles(request)
                logout(request)

                return render(request, 'fieldsight/permission_denied.html')

        else:
            request = clear_roles(request)

    def authenticate(self, request):
        pass
<commit_msg>user permission fixed.<commit_after>from onadata.apps.userrole.models import UserRole as Role
from rest_framework.authtoken.models import Token
from django.contrib.auth import logout

from django.shortcuts import get_object_or_404, render, redirect



def clear_roles(request):
    request.__class__.role = None
    request.__class__.organization = None
    request.__class__.project = None
    request.__class__.site = None
    request.__class__.group = None
    request.__class__.roles = []
    request.__class__.is_super_admin = False
    # request.__class__.groups = []
    return request


class RoleMiddleware(object):
    def process_request(self, request):

        if request.META.get('HTTP_AUTHORIZATION'):
            token_key = request.META.get('HTTP_AUTHORIZATION').split(' ')[-1]
            try:
                request.user = Token.objects.get(key=token_key).user
            except:
                pass

        if not request.user.is_anonymous():

            role = None
            if request.session.get('role'):
                try:
                    role = Role.objects.select_related('group', 'organization').get(pk=request.session.get('role'),
                                                                                    user=request.user)
                except Role.DoesNotExist:
                    pass

            if not role:
                roles = Role.get_active_roles(request.user)
                # roles = Role.objects.filter(user=request.user).select_related('group', 'organization')
                if roles:
                    role = roles[0]
                    request.session['role'] = role.id
            
            if role:
                request.__class__.role = role
                request.__class__.organization = role.organization
                request.__class__.project = role.project
                request.__class__.site = role.site
                request.__class__.group =role.group
                if "Super Admin" in request.user.groups.all():
                    request.__class__.group = Group.objects.get(pk=5)
                # request.__class__.roles = Role.objects.filter(user=request.user, organization=role.organization)
                request.__class__.roles = Role.get_active_roles(request.user)
                request.__class__.is_super_admin = request.group.name in ('Super Admin')
                #     for role in request.roles:
                #         groups.append(role.group)
                #     request.__class__.groups = groups
            else:
                # request = clear_roles(request)
                logout(request)

                return render(request, 'fieldsight/permission_denied.html')

        else:
            request = clear_roles(request)

    def authenticate(self, request):
        pass

{'fraction_non_alphanumeric': 0.09042747533796128, 'fraction_numerical': 0.002009499451954695, 'mean_word_length': 1.7498744349573079, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5086873', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1324, 'n_words': 259}