python-Django,如何将项目ID传递给特定用户?

问题描述 投票:0回答:1

大家 我创建了一个项目模型,使管理员能够创建单独的项目并管理它们,其中每个项目都有自己的流程,因为它们的结构相似但数据不同。因此,我使用Tenant并将其链接到一个Domain,以便每个项目及其数据都是独立的。现在我面临着如何分发这些项目的问题,因为每个项目都有一个用户,每个用户可以有自己的域。当以用户身份登录时,它会检查用户是否有域。现在问题出现了,因为他不认识项目和域之间的链接。

  • 解释流程(管理员创建项目和用户,然后在一个用户与一个项目之间关联,然后创建具有域的租户并将其与一个用户关联,现在我需要当用户尝试登录时检查他是否满足条件将他重定向到他的项目他可以通过项目/id 访问他的项目吗

  • 外键(可以通过项目/id访问其项目的项目) 域具有对租户的列租户引用 租户具有对用户的列用户引用 用户有对项目的列项目引用)

我尝试使用以下代码,因为它的第一部分是通过验证用户类型来验证的。如果他是管理员,他会被转到索引页,但是当他在这里以用户身份登录时,他不满足代码中描述的条件。

@login_required
def login_view(request):
    msg = None
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    if user.is_superuser:
                        auth_login(request, user)
                        return redirect('index')
                    elif user.is_staff:
                        # Check if the user is associated with a project
                        if hasattr(user, 'project'):
                            project = user.project

                            # Check if the project has a related tenant
                            if hasattr(tenant, 'user'):
                                tenant = tenant.user

                                # Check if the tenant has a related domain
                                if hasattr(domain, 'tenant'):
                                    domain = domain.tenant

                                    # Use schema_context to activate the tenant's schema and context
                                    with schema_context(domain.domain):
                                        # Construct Project-Based URL
                                        project_url = reverse('project', args=[str(project.id)])
                                        # Redirect the user to the constructed project URL
                                        return redirect(project_url)
                                else:
                                    return redirect('no_domain_page')  # Redirect to a page indicating no domain
                            else:
                                return redirect('no_tenant_page') # Redirect to a page indicating no tenant
                        else:
                            return redirect('no_project_page')  # Redirect to a page indicating no project
                    else:
                        msg = 'Unauthorized access.'
                else:
                    msg = 'This user is not active or does not exist.'
            else:
                msg = 'Invalid username or password.'
        else:
            msg = 'Error in the entered data!'
    else:
        form = LoginForm()

    return render(request, 'login.html', {'form': form, 'msg': msg})

django-views getelementbyid django-login member-access-enumeration
1个回答
0
投票

我找到了上述问题的解决方案:

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login as auth_login
from django_tenants.models import Domain
from django_tenants.utils import schema_context
from django.urls import reverse
from django.core.exceptions import ObjectDoesNotExist  # Import the exception
@login_required
def login_view(request):
    msg = None
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            try:
                # Check if the user exists
                user = User.objects.get(username=username)
                # Authenticate the user
                user = authenticate(username=username, password=password)
                if user is not None and user.is_active:
                    # Check if the user is a superuser
                    if user.is_superuser:
                        auth_login(request, user)
                        return redirect('index')
                    elif user.is_staff:
                         # Assuming each user is associated with exactly one Tenant
                        tenant = user.tenant
                        domains = Domain.objects.filter(tenant=tenant)
                        found_domain = None
                        for d in domains:
                            print(f"  - {d}")
                            found_domain = d
                        if found_domain and hasattr(found_domain, 'tenant'):
                            tenant = found_domain.tenant
                            with schema_context(found_domain.domain):
                                project_url = reverse('project', args=[str(user.project.uuid)])
                                redirect_url = f'http://{found_domain.domain}:8080{project_url}'
                                auth_login(request, user)
                                return redirect(redirect_url)
                        else:
                            msg = 'No link associated with the user was found!'
                else:
                        msg = 'Username or password is incorrect!'
            except User.DoesNotExist:
                msg = 'User not found'
            except ObjectDoesNotExist as e:
                # Handle the exception, e.g., log it or set a default value for tenant
                print(f"ObjectDoesNotExist: {e}")
                msg = 'An error occurred during authentication, no link associated with the user was found!!'
        else:
            msg = 'Error in entered data!'
    else:
        form = LoginForm()

    return render(request, 'login.html', {'form': form, 'msg': msg})
© www.soinside.com 2019 - 2024. All rights reserved.