feat: add self-modification protection for user roles

Protect owners from accidentally locking themselves out by:
- Adding RoleService.can_modify_user_role() to centralize validation logic
- Blocking edit/delete operations on own role in views
- Hiding edit/delete buttons for own role in template

This prevents owners from:
- Changing their own role to a lower privilege level
- Deactivating themselves
- Deleting their own access

Standard admin pattern used by GitHub, WordPress, Django Admin.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-01 23:06:54 +03:00
parent ffc3b0c42d
commit f2c1f7e02d
3 changed files with 46 additions and 6 deletions

View File

@@ -98,3 +98,25 @@ class RoleService:
if not user_role:
return False
return user_role.code in role_codes
@staticmethod
def can_modify_user_role(modifier_user, target_user_role):
"""
Проверяет, может ли пользователь изменить указанную роль.
Args:
modifier_user: Пользователь, который хочет изменить роль
target_user_role: UserRole объект, который нужно изменить
Returns:
tuple: (bool, str) - (можно ли изменить, причина отказа)
"""
# Защита от самоблокировки
if target_user_role.user == modifier_user:
return False, "Вы не можете изменить свою собственную роль"
# Можно добавить другие проверки в будущем:
# - Запрет удаления последнего owner'а
# - Проверка иерархии ролей и т.д.
return True, ""