Implement role cleanup on professor/student deletion to prevent orphaned user roles#575
Implement role cleanup on professor/student deletion to prevent orphaned user roles#575
Conversation
|
@leomurta 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs. I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review. |
Co-authored-by: leomurta <304759+leomurta@users.noreply.github.com>
… and professor model
|
Julguei que tinham testes desnecessários para validar a nova função. Além disso, na função handle_user_role_removal haviam validações para verificar se os roles de professor, aluno e desconhecido existiam, o que pra mim não fazia muito sentido, visto que são roles padrão do sistema (roles.rb). Por fim, ao final dessa função, ele limpava o atributo user de aluno/professor, sendo que de qualquer jeito a entidade vai ser destruída, além de que isso não é reproduzido em nenhuma função para :before_save. Obs. A função em student.rb e professor.rb são semelhantes, mudando somente de aluno para professor, portanto as mudanças foram as mesmas em ambos. Concordam com as alterações? Fariam mais alguma mudança? |
This PR addresses a critical data consistency issue where deleting a Professor or Student linked to a User would leave the User in an inconsistent state with orphaned roles.
Problem
Currently, when a Professor or Student is deleted, any linked User retains their Professor/Student role even though the corresponding entity no longer exists. This violates the system's constraint that users with Professor or Student roles must be linked to actual Professor/Student records.
For example, a user like this:

Would be left with the Professor role even after their associated Professor record is deleted, creating an inconsistent state.
Solution
This implementation adds
before_destroycallbacks to both Professor and Student models that automatically handle role cleanup when these entities are deleted:Key Features:
actual_roleupdated appropriatelyCode Changes:
Test Coverage:
Benefits
Fixes #574.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com/usr/lib/apt/methods/https(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.