1
0
forked from erp-dev/erp
Files
erpnew/diagnose_permissions.py
2025-11-19 14:08:05 +08:00

132 lines
4.0 KiB
Python
Raw 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.
#!/usr/bin/env python
"""
PlateOrder PATCH 权限诊断脚本
用于检查用户是否有正确的权限
"""
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'flower.settings')
django.setup()
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from printing.models import PlateOrder
User = get_user_model()
def diagnose_user_permissions(username):
"""诊断用户权限"""
print(f"\n{'='*60}")
print(f"诊断用户: {username}")
print(f"{'='*60}\n")
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
print(f"❌ 用户 '{username}' 不存在!")
return
print(f"✅ 用户存在: {user.username} ({user.email})")
print(f" - is_active: {user.is_active}")
print(f" - is_staff: {user.is_staff}")
print(f" - is_superuser: {user.is_superuser}")
if user.is_superuser:
print("\n✅ 用户是超级管理员,拥有所有权限!")
return
# 检查 PlateOrder 相关权限
required_perms = [
'printing.view_plateorder',
'printing.add_plateorder',
'printing.change_plateorder', # ⚠️ PATCH 需要这个
'printing.delete_plateorder',
]
print("\n📋 PlateOrder 权限检查:")
print("-" * 60)
for perm in required_perms:
has_perm = user.has_perm(perm)
icon = "" if has_perm else ""
action = perm.split('.')[1].replace('_', ' ').title()
print(f"{icon} {action:30} ({perm})")
if perm == 'printing.change_plateorder' and not has_perm:
print(" ⚠️ 缺少此权限会导致 PATCH/PUT 请求返回 401/403")
# 检查用户所属组
groups = user.groups.all()
if groups.exists():
print(f"\n👥 用户所属组: {', '.join(g.name for g in groups)}")
for group in groups:
print(f"\n'{group.name}' 的权限:")
group_perms = group.permissions.filter(
content_type__app_label='printing',
content_type__model='plateorder'
)
for perm in group_perms:
print(f" - {perm.codename}")
else:
print("\n👥 用户不属于任何组")
# 检查直接授予的权限
direct_perms = user.user_permissions.filter(
content_type__app_label='printing',
content_type__model='plateorder'
)
if direct_perms.exists():
print(f"\n🔑 直接授予的权限:")
for perm in direct_perms:
print(f" - {perm.codename}")
else:
print("\n🔑 没有直接授予的权限")
print("\n" + "="*60)
# 提供修复建议
if not user.has_perm('printing.change_plateorder'):
print("\n💡 修复方法:")
print("-" * 60)
print("在 Django shell 中运行以下代码:")
print(f"""
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
User = get_user_model()
user = User.objects.get(username='{username}')
change_perm = Permission.objects.get(codename='change_plateorder')
user.user_permissions.add(change_perm)
print("✅ 权限已添加!")
""")
def list_all_users():
"""列出所有用户"""
users = User.objects.all()
if not users.exists():
print("❌ 系统中没有用户!")
return
print(f"\n{'='*60}")
print("系统中的所有用户:")
print(f"{'='*60}\n")
for user in users:
has_change = user.has_perm('printing.change_plateorder')
icon = "" if has_change else ""
print(f"{icon} {user.username:20} (is_active: {user.is_active}, has_change_plateorder: {has_change})")
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
username = sys.argv[1]
diagnose_user_permissions(username)
else:
list_all_users()
print("\n💡 使用方法: python diagnose_permissions.py <username>")