Fixes to roles_controller update & delete action. Changes need to be reflected in SiteSetting DefaultRole setting. #5951
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First Commit - Extended return condition to render error if Users are associated with role.
I have tried to modify return condition for error status :method_not_allowed by adding additional OR condition which checks if any User is associated with the role because if we don't manually do it here we get a Internal Server Error from Postgres stating foreign key constraint violation which is correct at its place.
But at UI we get "The action cant be completed" in the toast message. This don't align with our requirements. So i have manually added the OR condition check.
This way I eliminate Internal Server Error stuff which is a good practice. Also the toast shows the proper message as already stated in useDeleteRole hook. The same message gets displayed when we try to delete the pre defined roles.
PFA Screenshot of the issue :
Suppose I have a user with Role as : test-role
Now when i try to delete test-role i get toast as :
and in my terminal i get :
After Fixing the issue :
Toast I get :
And on terminal i get :
Second Commit - Modified update and destroy action route for roles_controller. Added further error handler to useDeleteRole.jsx
In update action we chain the name update to SiteSetting DefaultRole. This ensures consistency.
We avoid the delete action for the role that is assigned as DefaultRole in SiteSetting. If we don't do this we don't have any side effects as this case is already handled by setting fallback to User role, in case the DefaultRole is not present. But I think this should be handled properly by giving a correct error message. We return status: :forbidden in that case and the same is handled in the useDeleteRole hook.
What I observe is that if the update and delete action is not aligned with SiteSetting Default Role then the drop-down appearing for the setting in the UI is left empty without any valid selection. This isn't good practice.
PFA Screenshot of the issue :
Suppose I have a Role as : test-role. This i set as default role in site setting
Now when i update the test-role to test-role-update, it gets successfully updated while in the UI the dropdown selection gets empty.
Same situation happens when we try to delete this test-role. Deletion happens successfully.
Note : We can delete only if no users are associated to that role. Here I am considering no users are associated and then when we delete
After Fixing the issue :
On Update :
On Delete the toast i get :
On terminal output shows as :