예제 #1
0
    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
                

                if "Super Admin" in request.user.user_roles.all().distinct('group__name').values_list('group__name', flat = True):
                    request.__class__.group = Group.objects.get(pk=5)
                else:
                    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 = 'Super Admin' in request.user.user_roles.all().distinct('group__name').values_list('group__name', flat = True)
                #     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 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 = 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)
        else:
            request = clear_roles(request)
예제 #3
0
    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():
            roles = cache.get('roles_{}'.format(request.user.id))
            is_admin = cache.get('admin_{}'.format(request.user.id), False)
            if roles:
                request.roles = roles
                request.is_super_admin = is_admin

            if not roles:
                roles = Role.get_active_roles(request.user)
                if roles:
                    cache.set('roles_{}'.format(request.user.id), roles,
                              20 * 60)
                    if roles.filter(group__name="Super Admin").exists():
                        request.is_super_admin = True
                        cache.set('admin_{}'.format(request.user.id), True,
                                  20 * 60)
                    else:
                        request.is_super_admin = False
                        cache.set('admin_{}'.format(request.user.id), False,
                                  20 * 60)
                    request.roles = roles

            if not roles:
                print(" user have no roles")

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

        else:
            clear_roles(request)