Files
octopus/myproject/templates/errors/access_denied.html
Andrey Smakotin 71ca681073 fix: Исправлена ошибка ForeignKey в products - замена PlatformAdmin на CustomUser
Устранена ошибка ValueError "Cannot query 'email': Must be 'PlatformAdmin' instance"
при доступе CustomUser к странице /products/.

Проблема:
- Модели products (TENANT_APPS) использовали get_user_model() для ForeignKey
- get_user_model() возвращал PlatformAdmin (AUTH_USER_MODEL)
- Но tenant модели должны ссылаться на CustomUser (tenant пользователей)
- Это создавало конфликт типов при запросах от CustomUser

Изменения:

1. products/models/base.py:
   - Убран get_user_model()
   - BaseProductEntity.archived_by теперь ForeignKey('accounts.CustomUser')

2. products/models/categories.py:
   - Убран get_user_model()
   - ProductCategory.deleted_by теперь ForeignKey('accounts.CustomUser')

3. products/models/import_job.py:
   - Убран get_user_model()
   - ProductImportJob.user теперь ForeignKey('accounts.CustomUser')

4. Создана миграция 0002 с data migration:
   - Очистка некорректных ссылок (установка NULL)
   - Изменение типа ForeignKey полей с PlatformAdmin на CustomUser

5. user_roles/auth_backend.py:
   - Добавлена функция _is_tenant_user() для проверки типа пользователя
   - Исправлена логика has_perm() и has_module_perms()
   - CustomUser теперь не проверяется через ModelBackend.has_perm()

6. admin_access_middleware.py:
   - Улучшены сообщения об ошибках доступа
   - Добавлен рендеринг через шаблон access_denied.html

7. templates/errors/access_denied.html:
   - Новый шаблон для красивого отображения ошибок доступа

Результат:
- CustomUser может без ошибок работать со страницей /products/
- Корректная архитектура: tenant модели ссылаются на tenant пользователей
- PlatformAdmin продолжает работать корректно
- Чистое решение без костылей

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 23:47:07 +03:00

36 lines
1.3 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% extends 'base.html' %}
{% block title %}Доступ запрещён{% endblock %}
{% block content %}
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow-sm">
<div class="card-header bg-warning">
<h4 class="mb-0">⚠️ Доступ запрещён</h4>
</div>
<div class="card-body">
<p class="lead">{{ message }}</p>
<hr>
<div class="d-flex gap-2">
<a href="/" class="btn btn-primary">
На главную
</a>
{% if user.is_authenticated %}
<a href="/accounts/logout/" class="btn btn-outline-secondary">
Выйти
</a>
{% else %}
<a href="/accounts/login/" class="btn btn-outline-secondary">
Войти
</a>
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}